Skip to content

Commit

Permalink
mr_create: Check for zero commits in a Merge Request
Browse files Browse the repository at this point in the history
It is possible to open up a Merge Request with zero commits by pushing a
branch with no changes and then executing 'lab mr create'.

lab should abort the creation of an MR in this case.

Check the number of commits in the remote branch, and if it is zero, abort
the creation of the Merge Request.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Mar 19, 2021
1 parent a897827 commit 4f8cc17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
10 changes: 8 additions & 2 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,17 @@ func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat boo
)
remoteBase := fmt.Sprintf("%s/%s", targetRemote, base)
commitMsg = ""
if git.NumberCommits(remoteBase, head) == 1 {

numCommits := git.NumberCommits(remoteBase, head)
if numCommits == 1 {
commitMsg, err = git.LastCommitMessage()
if err != nil {
return "", err
}
}
if numCommits == 0 {
return "", errors.New("Aborting: Resulting Merge Request would have had 0 commits.")
}

const tmpl = `{{if .InitMsg}}{{.InitMsg}}{{end}}
Expand All @@ -307,10 +312,11 @@ func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat boo

mrTmpl := lab.LoadGitLabTmpl(lab.TmplMR)

commitLogs, numCommits, err := git.Log(remoteBase, head)
commitLogs, err := git.Log(remoteBase, head)
if err != nil {
return "", err
}

commitLogs = strings.TrimSpace(commitLogs)
commentChar := git.CommentChar()

Expand Down
20 changes: 4 additions & 16 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -117,7 +116,7 @@ func LastCommitMessage() (string, error) {
}

// Log produces a formatted gitlog between 2 git shas
func Log(sha1, sha2 string) (string, int, error) {
func Log(sha1, sha2 string) (string, error) {
cmd := New("-c", "log.showSignature=false",
"log",
"--no-color",
Expand All @@ -127,28 +126,17 @@ func Log(sha1, sha2 string) (string, int, error) {
cmd.Stdout = nil
outputs, err := cmd.Output()
if err != nil {
return "", 0, errors.Errorf("Can't load git log %s..%s", sha1, sha2)
return "", errors.Errorf("Can't load git log %s..%s", sha1, sha2)
}

diffCmd := New("diff", "--stat", sha1)
diffCmd.Stdout = nil
diffOutput, err := diffCmd.Output()
if err != nil {
return "", 0, errors.Errorf("Can't load diffstat")
return "", errors.Errorf("Can't load diffstat")
}

numCommitsCmd := New("rev-list", "--count", fmt.Sprintf("%s...%s", sha1, sha2))
numCommitsCmd.Stdout = nil
numCommitsString, err := numCommitsCmd.Output()
if err != nil {
return "", 0, errors.Errorf("Can't determine number of commits")
}
numCommits, err := strconv.Atoi(strings.TrimSpace(string(numCommitsString)))
if err != nil {
return "", 0, errors.Errorf("Can't determine number of commits(%s)", numCommitsString)
}

return string(outputs) + string(diffOutput), numCommits, nil
return string(outputs) + string(diffOutput), nil
}

// CurrentBranch returns the currently checked out branch
Expand Down
3 changes: 2 additions & 1 deletion internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ func TestLastCommitMessage(t *testing.T) {
}

func TestLog(t *testing.T) {
log, count, err := Log("HEAD~1", "HEAD")
log, err := Log("HEAD~1", "HEAD")
if err != nil {
t.Fatal(err)
}
count := NumberCommits("HEAD~1", "HEAD")
expectedSHA := "09b519c"
expectedAuthor := "Zaq? Wiedmann"
expectedMessage := "(ci) jobs with interleaved sleeps and prints"
Expand Down

0 comments on commit 4f8cc17

Please sign in to comment.