Skip to content

Commit

Permalink
👔 up: update the repo branch info list store type
Browse files Browse the repository at this point in the history
- from map to slice for store
- add new method on repo
  • Loading branch information
inhere committed Apr 25, 2023
1 parent 3039521 commit 8f0ef64
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
52 changes: 32 additions & 20 deletions info_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,26 @@ 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),
}
}

// NewBranchInfos create
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),
}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -------------------------------------------------
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8f0ef64

Please sign in to comment.