Skip to content

Commit d11df72

Browse files
committed
Pluralize nouns in Git summary
1 parent 9556adc commit d11df72

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

git.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ func (gi GitInfo) Summary() string {
5454
gi.copied +
5555
gi.untracked
5656
if committable > 0 {
57-
s = append(s, fmt.Sprintf("%d files to commit", committable))
57+
s = append(s, fmt.Sprintf("%d %s to commit", committable, pluralize(committable, "file", "files")))
5858
}
5959

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

6464
if gi.stashed > 0 {
65-
s = append(s, fmt.Sprintf("%d stashes", gi.stashed))
65+
s = append(s, fmt.Sprintf("%d %s", gi.stashed, pluralize(gi.stashed, "stash", "stashes")))
6666
}
6767

6868
if gi.ahead > 0 {
69-
s = append(s, fmt.Sprintf("%d unpushed commits", gi.ahead))
69+
s = append(s, fmt.Sprintf("%d unpushed %s", gi.ahead, pluralize(gi.ahead, "commit", "commits")))
7070
}
7171

7272
return strings.Join(s, ", ")
@@ -120,3 +120,10 @@ func (gi *GitInfo) parseHeader(s string) {
120120
gi.ahead = n
121121
}
122122
}
123+
124+
func pluralize(n int, singular string, plural string) string {
125+
if n == 1 {
126+
return singular
127+
}
128+
return plural
129+
}

git_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,15 @@ func TestSummary(t *testing.T) {
135135
input GitInfo
136136
want string
137137
}{
138-
{"withOneModified", GitInfo{modified: 1}, "1 files to commit"},
138+
{"withOneModified", GitInfo{modified: 1}, "1 file to commit"},
139139
{"withManyModified", GitInfo{modified: 1, added: 1, deleted: 1, renamed: 1, copied: 1, untracked: 1}, "6 files to commit"},
140-
{"withUnmerged", GitInfo{unmerged: 1}, "1 files to merge"},
141-
{"withStashed", GitInfo{stashed: 1}, "1 stashes"},
142-
{"withUnpushedCommits", GitInfo{ahead: 1}, "1 unpushed commits"},
143-
{"withManyDifferent", GitInfo{modified: 1, stashed: 1}, "1 files to commit, 1 stashes"},
140+
{"withOneUnmerged", GitInfo{unmerged: 1}, "1 file to merge"},
141+
{"withManyUnmerged", GitInfo{unmerged: 2}, "2 files to merge"},
142+
{"withOneStashed", GitInfo{stashed: 1}, "1 stash"},
143+
{"withManyStashed", GitInfo{stashed: 2}, "2 stashes"},
144+
{"withOneUnpushedCommit", GitInfo{ahead: 1}, "1 unpushed commit"},
145+
{"withManyUnpushedCommit", GitInfo{ahead: 2}, "2 unpushed commits"},
146+
{"withDifferent", GitInfo{modified: 2, stashed: 2}, "2 files to commit, 2 stashes"},
144147
}
145148
for _, tt := range tests {
146149
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)