Skip to content

Commit 8f0ef64

Browse files
committed
👔 up: update the repo branch info list store type
- from map to slice for store - add new method on repo
1 parent 3039521 commit 8f0ef64

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

info_branch.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,26 @@ type BranchInfos struct {
7474
brLines []string
7575

7676
current *BranchInfo
77-
// local branches. key is short branch name, eg: dev
78-
locales map[string]*BranchInfo
79-
// remote branches. key is full branch name, eg: origin/dev
80-
remotes map[string]*BranchInfo
77+
// local branch list
78+
locales []*BranchInfo
79+
// all remote branch list
80+
remotes []*BranchInfo
8181
}
8282

8383
// EmptyBranchInfos instance
8484
func EmptyBranchInfos() *BranchInfos {
8585
return &BranchInfos{
86-
locales: make(map[string]*BranchInfo),
87-
remotes: make(map[string]*BranchInfo),
86+
// locales: make(map[string]*BranchInfo),
87+
// remotes: make(map[string]*BranchInfo),
8888
}
8989
}
9090

9191
// NewBranchInfos create
9292
func NewBranchInfos(gitOut string) *BranchInfos {
9393
return &BranchInfos{
9494
brLines: strings.Split(strings.TrimSpace(gitOut), "\n"),
95-
locales: make(map[string]*BranchInfo),
96-
remotes: make(map[string]*BranchInfo),
95+
// locales: make(map[string]*BranchInfo),
96+
// remotes: make(map[string]*BranchInfo),
9797
}
9898
}
9999

@@ -124,10 +124,9 @@ func (bs *BranchInfos) Parse() *BranchInfos {
124124

125125
// collect
126126
if info.IsRemoted() {
127-
bs.remotes[info.Name] = info
127+
bs.remotes = append(bs.remotes, info)
128128
} else {
129-
bs.locales[info.Name] = info
130-
129+
bs.locales = append(bs.locales, info)
131130
if info.Current {
132131
bs.current = info
133132
}
@@ -187,7 +186,7 @@ const (
187186
// // search on remotes
188187
// Search("fea", BrSearchRemote)
189188
// // search on remotes and remote name must be equals "origin"
190-
// Search("origin fea", BrSearchRemote)
189+
// Search("origin:fea", BrSearchRemote)
191190
func (bs *BranchInfos) Search(name string, flag int) []*BranchInfo {
192191
var list []*BranchInfo
193192

@@ -198,8 +197,8 @@ func (bs *BranchInfos) Search(name string, flag int) []*BranchInfo {
198197

199198
var remote string
200199
// "remote name" - search on the remote
201-
if strings.Contains(name, " ") {
202-
remote, name = strutil.MustCut(name, " ")
200+
if strings.Contains(name, ":") {
201+
remote, name = strutil.MustCut(name, ":")
203202
}
204203

205204
if remote == "" && flag&BrSearchLocal == BrSearchLocal {
@@ -246,23 +245,36 @@ func (bs *BranchInfos) Current() *BranchInfo {
246245
}
247246

248247
// Locales branches
249-
func (bs *BranchInfos) Locales() map[string]*BranchInfo {
248+
func (bs *BranchInfos) Locales() []*BranchInfo {
250249
return bs.locales
251250
}
252251

253252
// Remotes branch infos get
254253
//
255254
// if remote="", will return all remote branches
256-
func (bs *BranchInfos) Remotes(remote string) map[string]*BranchInfo {
255+
func (bs *BranchInfos) Remotes(remote string) []*BranchInfo {
257256
if remote == "" {
258257
return bs.remotes
259258
}
260259

261-
rs := make(map[string]*BranchInfo)
262-
for name, info := range bs.remotes {
260+
ls := make([]*BranchInfo, 0)
261+
for _, info := range bs.remotes {
263262
if info.Remote == remote {
264-
rs[name] = info
263+
ls = append(ls, info)
265264
}
266265
}
267-
return rs
266+
return ls
267+
}
268+
269+
// All branches list
270+
func (bs *BranchInfos) All() []*BranchInfo {
271+
ls := make([]*BranchInfo, 0, len(bs.locales)+len(bs.remotes))
272+
for _, info := range bs.locales {
273+
ls = append(ls, info)
274+
}
275+
276+
for _, info := range bs.remotes {
277+
ls = append(ls, info)
278+
}
279+
return ls
268280
}

info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestBranchInfo_parse_verbose(t *testing.T) {
161161
assert.Len(t, rets, 2)
162162

163163
// search
164-
rets = bis.Search("origin new", gitw.BrSearchRemote)
164+
rets = bis.Search("origin:new", gitw.BrSearchRemote)
165165
assert.NotEmpty(t, rets)
166166
assert.Len(t, rets, 1)
167167
assert.True(t, rets[0].IsRemoted())

repo.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ func (r *Repo) Info() *RepoInfo {
145145
return ri
146146
}
147147

148+
// FetchAll fetch all remote branches
149+
func (r *Repo) FetchAll(args ...string) error {
150+
return r.gw.Cmd("fetch", "--all").AddArgs(args).Run()
151+
}
152+
148153
// -------------------------------------------------
149154
// repo tags
150155
// -------------------------------------------------
@@ -394,6 +399,12 @@ func (r *Repo) BranchInfos() *BranchInfos {
394399
return r.loadBranchInfos().branchInfos
395400
}
396401

402+
// ReloadBranches reload branch infos of the repo
403+
func (r *Repo) ReloadBranches() *BranchInfos {
404+
r.branchInfos = nil
405+
return r.loadBranchInfos().branchInfos
406+
}
407+
397408
// CurBranchInfo get current branch info of the repo
398409
func (r *Repo) CurBranchInfo() *BranchInfo {
399410
return r.loadBranchInfos().branchInfos.Current()

0 commit comments

Comments
 (0)