Skip to content

Commit

Permalink
Detect unpushed commits
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Nov 26, 2024
1 parent c8449a7 commit afe6be4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
23 changes: 16 additions & 7 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type GitInfo struct {
unmerged int
untracked int
stashed int
ahead int
}

func (gi *GitInfo) Parse(r io.Reader) {
Expand All @@ -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 {
Expand All @@ -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, ", ")
}

Expand All @@ -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())
Expand Down Expand Up @@ -93,11 +99,14 @@ func (gi *GitInfo) parseXY(xy string) {
}
}

func (gi *GitInfo) parseStashes(s string) {
// line: # stash <N>
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
}
}
10 changes: 10 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit afe6be4

Please sign in to comment.