Skip to content

Commit

Permalink
Pluralize nouns in Git summary
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Dec 1, 2024
1 parent 9556adc commit d11df72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
15 changes: 11 additions & 4 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ func (gi GitInfo) Summary() string {
gi.copied +
gi.untracked
if committable > 0 {
s = append(s, fmt.Sprintf("%d files to commit", committable))
s = append(s, fmt.Sprintf("%d %s to commit", committable, pluralize(committable, "file", "files")))
}

if gi.unmerged > 0 {
s = append(s, fmt.Sprintf("%d files to merge", gi.unmerged))
s = append(s, fmt.Sprintf("%d %s to merge", gi.unmerged, pluralize(gi.unmerged, "file", "files")))
}

if gi.stashed > 0 {
s = append(s, fmt.Sprintf("%d stashes", gi.stashed))
s = append(s, fmt.Sprintf("%d %s", gi.stashed, pluralize(gi.stashed, "stash", "stashes")))
}

if gi.ahead > 0 {
s = append(s, fmt.Sprintf("%d unpushed commits", gi.ahead))
s = append(s, fmt.Sprintf("%d unpushed %s", gi.ahead, pluralize(gi.ahead, "commit", "commits")))
}

return strings.Join(s, ", ")
Expand Down Expand Up @@ -120,3 +120,10 @@ func (gi *GitInfo) parseHeader(s string) {
gi.ahead = n
}
}

func pluralize(n int, singular string, plural string) string {
if n == 1 {
return singular
}
return plural
}
13 changes: 8 additions & 5 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ func TestSummary(t *testing.T) {
input GitInfo
want string
}{
{"withOneModified", GitInfo{modified: 1}, "1 files to commit"},
{"withOneModified", GitInfo{modified: 1}, "1 file to commit"},
{"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"},
{"withOneUnmerged", GitInfo{unmerged: 1}, "1 file to merge"},
{"withManyUnmerged", GitInfo{unmerged: 2}, "2 files to merge"},
{"withOneStashed", GitInfo{stashed: 1}, "1 stash"},
{"withManyStashed", GitInfo{stashed: 2}, "2 stashes"},
{"withOneUnpushedCommit", GitInfo{ahead: 1}, "1 unpushed commit"},
{"withManyUnpushedCommit", GitInfo{ahead: 2}, "2 unpushed commits"},
{"withDifferent", GitInfo{modified: 2, stashed: 2}, "2 files to commit, 2 stashes"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d11df72

Please sign in to comment.