From 8f0ef6432353f799acbcf89c0076a841a4530c20 Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 25 Apr 2023 13:28:50 +0800 Subject: [PATCH] :necktie: up: update the repo branch info list store type - from map to slice for store - add new method on repo --- info_branch.go | 52 +++++++++++++++++++++++++++++++------------------- info_test.go | 2 +- repo.go | 11 +++++++++++ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/info_branch.go b/info_branch.go index 600cd2f..7e7f83b 100644 --- a/info_branch.go +++ b/info_branch.go @@ -74,17 +74,17 @@ type BranchInfos struct { brLines []string current *BranchInfo - // local branches. key is short branch name, eg: dev - locales map[string]*BranchInfo - // remote branches. key is full branch name, eg: origin/dev - remotes map[string]*BranchInfo + // local branch list + locales []*BranchInfo + // all remote branch list + remotes []*BranchInfo } // EmptyBranchInfos instance func EmptyBranchInfos() *BranchInfos { return &BranchInfos{ - locales: make(map[string]*BranchInfo), - remotes: make(map[string]*BranchInfo), + // locales: make(map[string]*BranchInfo), + // remotes: make(map[string]*BranchInfo), } } @@ -92,8 +92,8 @@ func EmptyBranchInfos() *BranchInfos { func NewBranchInfos(gitOut string) *BranchInfos { return &BranchInfos{ brLines: strings.Split(strings.TrimSpace(gitOut), "\n"), - locales: make(map[string]*BranchInfo), - remotes: make(map[string]*BranchInfo), + // locales: make(map[string]*BranchInfo), + // remotes: make(map[string]*BranchInfo), } } @@ -124,10 +124,9 @@ func (bs *BranchInfos) Parse() *BranchInfos { // collect if info.IsRemoted() { - bs.remotes[info.Name] = info + bs.remotes = append(bs.remotes, info) } else { - bs.locales[info.Name] = info - + bs.locales = append(bs.locales, info) if info.Current { bs.current = info } @@ -187,7 +186,7 @@ const ( // // search on remotes // Search("fea", BrSearchRemote) // // search on remotes and remote name must be equals "origin" -// Search("origin fea", BrSearchRemote) +// Search("origin:fea", BrSearchRemote) func (bs *BranchInfos) Search(name string, flag int) []*BranchInfo { var list []*BranchInfo @@ -198,8 +197,8 @@ func (bs *BranchInfos) Search(name string, flag int) []*BranchInfo { var remote string // "remote name" - search on the remote - if strings.Contains(name, " ") { - remote, name = strutil.MustCut(name, " ") + if strings.Contains(name, ":") { + remote, name = strutil.MustCut(name, ":") } if remote == "" && flag&BrSearchLocal == BrSearchLocal { @@ -246,23 +245,36 @@ func (bs *BranchInfos) Current() *BranchInfo { } // Locales branches -func (bs *BranchInfos) Locales() map[string]*BranchInfo { +func (bs *BranchInfos) Locales() []*BranchInfo { return bs.locales } // Remotes branch infos get // // if remote="", will return all remote branches -func (bs *BranchInfos) Remotes(remote string) map[string]*BranchInfo { +func (bs *BranchInfos) Remotes(remote string) []*BranchInfo { if remote == "" { return bs.remotes } - rs := make(map[string]*BranchInfo) - for name, info := range bs.remotes { + ls := make([]*BranchInfo, 0) + for _, info := range bs.remotes { if info.Remote == remote { - rs[name] = info + ls = append(ls, info) } } - return rs + return ls +} + +// All branches list +func (bs *BranchInfos) All() []*BranchInfo { + ls := make([]*BranchInfo, 0, len(bs.locales)+len(bs.remotes)) + for _, info := range bs.locales { + ls = append(ls, info) + } + + for _, info := range bs.remotes { + ls = append(ls, info) + } + return ls } diff --git a/info_test.go b/info_test.go index 81f8005..a2f6551 100644 --- a/info_test.go +++ b/info_test.go @@ -161,7 +161,7 @@ func TestBranchInfo_parse_verbose(t *testing.T) { assert.Len(t, rets, 2) // search - rets = bis.Search("origin new", gitw.BrSearchRemote) + rets = bis.Search("origin:new", gitw.BrSearchRemote) assert.NotEmpty(t, rets) assert.Len(t, rets, 1) assert.True(t, rets[0].IsRemoted()) diff --git a/repo.go b/repo.go index ce3fdde..a6f2b1f 100644 --- a/repo.go +++ b/repo.go @@ -145,6 +145,11 @@ func (r *Repo) Info() *RepoInfo { return ri } +// FetchAll fetch all remote branches +func (r *Repo) FetchAll(args ...string) error { + return r.gw.Cmd("fetch", "--all").AddArgs(args).Run() +} + // ------------------------------------------------- // repo tags // ------------------------------------------------- @@ -394,6 +399,12 @@ func (r *Repo) BranchInfos() *BranchInfos { return r.loadBranchInfos().branchInfos } +// ReloadBranches reload branch infos of the repo +func (r *Repo) ReloadBranches() *BranchInfos { + r.branchInfos = nil + return r.loadBranchInfos().branchInfos +} + // CurBranchInfo get current branch info of the repo func (r *Repo) CurBranchInfo() *BranchInfo { return r.loadBranchInfos().branchInfos.Current()