Skip to content

Commit f551b39

Browse files
authored
Merge pull request #595 from prarit/numCommits
mr_create: Add number of commits to create metadata
2 parents f3c8fb2 + b0089e1 commit f551b39

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

cmd/mr_create.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat boo
282282
const tmpl = `{{if .InitMsg}}{{.InitMsg}}{{end}}
283283
284284
{{if .Tmpl}}{{.Tmpl}}{{end}}
285-
{{.CommentChar}} Requesting a merge into {{.Base}} from {{.Head}}
285+
{{.CommentChar}} Requesting a merge into {{.Base}} from {{.Head}} ({{.NumCommits}} commits)
286286
{{.CommentChar}}
287287
{{.CommentChar}} Write a message for this merge request. The first block
288288
{{.CommentChar}} of text is the title and the rest is the description.{{if .CommitLogs}}
@@ -293,7 +293,7 @@ func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat boo
293293

294294
mrTmpl := lab.LoadGitLabTmpl(lab.TmplMR)
295295

296-
commitLogs, err := git.Log(remoteBase, head)
296+
commitLogs, numCommits, err := git.Log(remoteBase, head)
297297
if err != nil {
298298
return "", err
299299
}
@@ -319,13 +319,15 @@ func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat boo
319319
Base string
320320
Head string
321321
CommitLogs string
322+
NumCommits int
322323
}{
323324
InitMsg: commitMsg,
324325
Tmpl: mrTmpl,
325326
CommentChar: commentChar,
326327
Base: targetRemote + ":" + base,
327328
Head: sourceRemote + ":" + head,
328329
CommitLogs: commitLogs,
330+
NumCommits: numCommits,
329331
}
330332

331333
var b bytes.Buffer

cmd/mr_create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_mrText(t *testing.T) {
1717
require.Contains(t, text, `
1818
1919
I am the default merge request template for lab
20-
# Requesting a merge into origin:master from lab-testing:mrtest
20+
# Requesting a merge into origin:master from lab-testing:mrtest (12 commits)
2121
#
2222
# Write a message for this merge request. The first block
2323
# of text is the title and the rest is the description.
@@ -37,7 +37,7 @@ func Test_mrText_CoverLetter(t *testing.T) {
3737
require.Contains(t, coverLetter, `
3838
3939
I am the default merge request template for lab
40-
# Requesting a merge into origin:master from lab-testing:mrtest
40+
# Requesting a merge into origin:master from lab-testing:mrtest (12 commits)
4141
#
4242
# Write a message for this merge request. The first block
4343
# of text is the title and the rest is the description.

internal/git/git.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"regexp"
11+
"strconv"
1112
"strings"
1213
"time"
1314

@@ -129,7 +130,7 @@ func LastCommitMessage() (string, error) {
129130
}
130131

131132
// Log produces a formatted gitlog between 2 git shas
132-
func Log(sha1, sha2 string) (string, error) {
133+
func Log(sha1, sha2 string) (string, int, error) {
133134
cmd := New("-c", "log.showSignature=false",
134135
"log",
135136
"--no-color",
@@ -139,16 +140,28 @@ func Log(sha1, sha2 string) (string, error) {
139140
cmd.Stdout = nil
140141
outputs, err := cmd.Output()
141142
if err != nil {
142-
return "", errors.Errorf("Can't load git log %s..%s", sha1, sha2)
143+
return "", 0, errors.Errorf("Can't load git log %s..%s", sha1, sha2)
143144
}
144145

145146
diffCmd := New("diff", "--stat", sha1)
146147
diffCmd.Stdout = nil
147148
diffOutput, err := diffCmd.Output()
148149
if err != nil {
149-
return "", errors.Errorf("Can't load diffstat")
150+
return "", 0, errors.Errorf("Can't load diffstat")
150151
}
151-
return string(outputs) + string(diffOutput), nil
152+
153+
numCommitsCmd := New("rev-list", "--count", fmt.Sprintf("%s...%s", sha1, sha2))
154+
numCommitsCmd.Stdout = nil
155+
numCommitsString, err := numCommitsCmd.Output()
156+
if err != nil {
157+
return "", 0, errors.Errorf("Can't determine number of commits")
158+
}
159+
numCommits, err := strconv.Atoi(strings.TrimSpace(string(numCommitsString)))
160+
if err != nil {
161+
return "", 0, errors.Errorf("Can't determine number of commits(%s)", numCommitsString)
162+
}
163+
164+
return string(outputs) + string(diffOutput), numCommits, nil
152165
}
153166

154167
// CurrentBranch returns the currently checked out branch

internal/git/git_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestLastCommitMessage(t *testing.T) {
7373
}
7474

7575
func TestLog(t *testing.T) {
76-
log, err := Log("HEAD~1", "HEAD")
76+
log, count, err := Log("HEAD~1", "HEAD")
7777
if err != nil {
7878
t.Fatal(err)
7979
}
@@ -83,6 +83,7 @@ func TestLog(t *testing.T) {
8383
assert.Contains(t, log, expectedSHA)
8484
assert.Contains(t, log, expectedAuthor)
8585
assert.Contains(t, log, expectedMessage)
86+
assert.Equal(t, 1, count)
8687
}
8788

8889
func TestCurrentBranch(t *testing.T) {

0 commit comments

Comments
 (0)