diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 17:49:25 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 17:50:25 +1100 |
commit | dde30fa104347ab9c01f82b7886864b473e6f51c (patch) | |
tree | 560b545c54e7051d16cef4056f8485557dccee91 /pkg/commands | |
parent | 13a9bbb984601d19f1aabe7abfbf58fbe91cf621 (diff) |
add gone branches status
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/loaders/branches.go | 78 | ||||
-rw-r--r-- | pkg/commands/loaders/branches_test.go | 52 | ||||
-rw-r--r-- | pkg/commands/models/branch.go | 11 |
3 files changed, 102 insertions, 39 deletions
diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index c54e65ee9..1f78908a8 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -106,6 +106,49 @@ outer: return branches, nil } +// Obtain branch information from parsed line output of getRawBranches() +// split contains the '|' separated tokens in the line of output +func obtainBranch(split []string) *models.Branch { + name := strings.TrimPrefix(split[1], "heads/") + branch := &models.Branch{ + Name: name, + Pullables: "?", + Pushables: "?", + Head: split[0] == "*", + } + + upstreamName := split[2] + if upstreamName == "" { + // if we're here then it means we do not have a local version of the remote. + // The branch might still be tracking a remote though, we just don't know + // how many commits ahead/behind it is + return branch + } + + track := split[3] + if track == "[gone]" { + branch.UpstreamGone = true + } else { + re := regexp.MustCompile(`ahead (\d+)`) + match := re.FindStringSubmatch(track) + if len(match) > 1 { + branch.Pushables = match[1] + } else { + branch.Pushables = "0" + } + + re = regexp.MustCompile(`behind (\d+)`) + match = re.FindStringSubmatch(track) + if len(match) > 1 { + branch.Pullables = match[1] + } else { + branch.Pullables = "0" + } + } + + return branch +} + func (self *BranchLoader) obtainBranches() []*models.Branch { output, err := self.getRawBranches() if err != nil { @@ -128,40 +171,7 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { continue } - name := strings.TrimPrefix(split[1], "heads/") - branch := &models.Branch{ - Name: name, - Pullables: "?", - Pushables: "?", - Head: split[0] == "*", - } - - upstreamName := split[2] - if upstreamName == "" { - // if we're here then it means we do not have a local version of the remote. - // The branch might still be tracking a remote though, we just don't know - // how many commits ahead/behind it is - branches = append(branches, branch) - continue - } - - track := split[3] - re := regexp.MustCompile(`ahead (\d+)`) - match := re.FindStringSubmatch(track) - if len(match) > 1 { - branch.Pushables = match[1] - } else { - branch.Pushables = "0" - } - - re = regexp.MustCompile(`behind (\d+)`) - match = re.FindStringSubmatch(track) - if len(match) > 1 { - branch.Pullables = match[1] - } else { - branch.Pullables = "0" - } - + branch := obtainBranch(split) branches = append(branches, branch) } diff --git a/pkg/commands/loaders/branches_test.go b/pkg/commands/loaders/branches_test.go new file mode 100644 index 000000000..70f02dcf7 --- /dev/null +++ b/pkg/commands/loaders/branches_test.go @@ -0,0 +1,52 @@ +package loaders + +// "*|feat/detect-purge|origin/feat/detect-purge|[ahead 1]" +import ( + "testing" + + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/stretchr/testify/assert" +) + +func TestObtainBanch(t *testing.T) { + type scenario struct { + testName string + input []string + expectedBranch *models.Branch + } + + scenarios := []scenario{ + { + testName: "TrimHeads", + input: []string{"", "heads/a_branch", "", ""}, + expectedBranch: &models.Branch{Name: "a_branch", Pushables: "?", Pullables: "?", Head: false}, + }, + { + testName: "NoUpstream", + input: []string{"", "a_branch", "", ""}, + expectedBranch: &models.Branch{Name: "a_branch", Pushables: "?", Pullables: "?", Head: false}, + }, + { + testName: "IsHead", + input: []string{"*", "a_branch", "", ""}, + expectedBranch: &models.Branch{Name: "a_branch", Pushables: "?", Pullables: "?", Head: true}, + }, + { + testName: "IsBehindAndAhead", + input: []string{"", "a_branch", "a_remote/a_branch", "[behind 2, ahead 3]"}, + expectedBranch: &models.Branch{Name: "a_branch", Pushables: "3", Pullables: "2", Head: false}, + }, + { + testName: "RemoteBranchIsGone", + input: []string{"", "a_branch", "a_remote/a_branch", "[gone]"}, + expectedBranch: &models.Branch{Name: "a_branch", UpstreamGone: true, Pushables: "?", Pullables: "?", Head: false}, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + branch := obtainBranch(s.input) + assert.EqualValues(t, s.expectedBranch, branch) + }) + } +} diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index 3cdf5ad6d..dae934fdf 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -5,11 +5,12 @@ package models type Branch struct { Name string // the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name would be '123asdf' - DisplayName string - Recency string - Pushables string - Pullables string - Head bool + DisplayName string + Recency string + Pushables string + Pullables string + UpstreamGone bool + Head bool // if we have a named remote locally this will be the name of that remote e.g. // 'origin' or 'tiwood'. If we don't have the remote locally it'll look like // 'git@github.com:tiwood/lazygit.git' |