From d11df7257d28828bd6cb597efd451701035dbd36 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Sun, 1 Dec 2024 08:30:48 +0100 Subject: [PATCH] Pluralize nouns in Git summary --- git.go | 15 +++++++++++---- git_test.go | 13 ++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/git.go b/git.go index ed88a11..1524ade 100644 --- a/git.go +++ b/git.go @@ -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, ", ") @@ -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 +} diff --git a/git_test.go b/git_test.go index 33ea536..c8c6fba 100644 --- a/git_test.go +++ b/git_test.go @@ -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) {