diff --git a/git.go b/git.go index 9c30097..c5f57d0 100644 --- a/git.go +++ b/git.go @@ -19,6 +19,7 @@ type GitInfo struct { unmerged int untracked int stashed int + ahead int } func (gi *GitInfo) Parse(r io.Reader) { @@ -36,7 +37,8 @@ func (gi GitInfo) IsClean() bool { gi.copied == 0 && gi.unmerged == 0 && gi.untracked == 0 && - gi.stashed == 0 + gi.stashed == 0 && + gi.ahead == 0 } func (gi GitInfo) Summary() string { @@ -55,6 +57,10 @@ func (gi GitInfo) Summary() string { s = append(s, fmt.Sprintf("%d stashes", gi.stashed)) } + if gi.ahead > 0 { + s = append(s, fmt.Sprintf("%d unpushed commits", gi.ahead)) + } + return strings.Join(s, ", ") } @@ -64,7 +70,7 @@ func (gi *GitInfo) parseLine(l string) { s.Scan() switch s.Text() { case "#": - gi.parseStashes(l) + gi.parseHeader(l) case "1", "2": s.Scan() gi.parseXY(s.Text()) @@ -93,11 +99,14 @@ func (gi *GitInfo) parseXY(xy string) { } } -func (gi *GitInfo) parseStashes(s string) { - // line: # stash - stashed := strings.Split(s, " ") - if stashed[1] == "stash" { - n, _ := strconv.Atoi(stashed[2]) +func (gi *GitInfo) parseHeader(s string) { + parts := strings.Split(s, " ") + switch parts[1] { + case "stash": // line: # stash 1 + n, _ := strconv.Atoi(parts[2]) gi.stashed = n + case "branch.ab": // line: # branch.ab +1 -0 + n, _ := strconv.Atoi(parts[2]) + gi.ahead = n } } diff --git a/git_test.go b/git_test.go index ba67b00..cd58ab6 100644 --- a/git_test.go +++ b/git_test.go @@ -101,6 +101,15 @@ func TestParseWithStashed(t *testing.T) { require.Equal(t, 1, gi.stashed) } +func TestParseWithAhead(t *testing.T) { + s := strings.NewReader("# branch.ab +1 -0\n") + + gi := GitInfo{} + gi.Parse(s) + + require.Equal(t, 1, gi.ahead) +} + func TestIsClean(t *testing.T) { var gi GitInfo @@ -121,6 +130,7 @@ func TestSummary(t *testing.T) { {"withManyModified", GitInfo{modified: 1, added: 1, deleted: 1, renamed: 1, copied: 1, untracked: 1}, "6 files to commit"}, {"withUnmerged", GitInfo{unmerged: 1}, "1 files to merge"}, {"withStashed", GitInfo{stashed: 1}, "1 stashes"}, + {"withUnpushedCommits", GitInfo{ahead: 1}, "1 unpushed commits"}, {"withManyDifferent", GitInfo{modified: 1, stashed: 1}, "1 files to commit, 1 stashes"}, } for _, tt := range tests { diff --git a/search.go b/search.go index 87a7c46..c8cb3a8 100644 --- a/search.go +++ b/search.go @@ -36,7 +36,7 @@ func getRepos(path string, sub chan repoMsg) tea.Cmd { repopath := filepath.Dir(path) var buf = new(bytes.Buffer) - cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash") + cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch") cmd.Stdout = buf cmd.Dir = repopath err = cmd.Run()