diff --git a/commands/pull_request.go b/commands/pull_request.go index e608ac2a46a42c83eef77143f05de5e593ba9638..d54c2237f5d3fe0e58aaed219c7594a552b2952c 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -126,30 +126,48 @@ func writePullRequestTitleAndBody(repo *github.Repo) (title, body string, err er } func writePullRequestChanges(repo *github.Repo, messageFile string) error { - message := ` -# Requesting a pull to %s from %s -# -# Write a message for this pull request. The first block -# of the text is the title and the rest is description.%s -` - startRegexp := regexp.MustCompilePOSIX("^") - endRegexp := regexp.MustCompilePOSIX(" +$") - - commitLogs, _ := git.Log(repo.Base, repo.Head) - var changesMsg string - if len(commitLogs) > 0 { - commitLogs = strings.TrimSpace(commitLogs) - commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") - commitLogs = endRegexp.ReplaceAllString(commitLogs, "") - changesMsg = ` + commits, err := git.RefList(repo.Base, repo.Head) + if err != nil { + return err + } + + var defaultMsg, commitSummary string + if len(commits) == 1 { + defaultMsg, err = git.Show(commits[0]) + if err != nil { + return err + } + defaultMsg = fmt.Sprintf("%s\n", defaultMsg) + } else if len(commits) > 1 { + commitLogs, err := git.Log(repo.Base, repo.Head) + if err != nil { + return err + } + + if len(commitLogs) > 0 { + startRegexp := regexp.MustCompilePOSIX("^") + endRegexp := regexp.MustCompilePOSIX(" +$") + + commitLogs = strings.TrimSpace(commitLogs) + commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") + commitLogs = endRegexp.ReplaceAllString(commitLogs, "") + commitSummary = ` # # Changes: # %s` - changesMsg = fmt.Sprintf(changesMsg, commitLogs) + commitSummary = fmt.Sprintf(commitSummary, commitLogs) + } } - message = fmt.Sprintf(message, repo.FullBase(), repo.FullHead(), changesMsg) + message := `%s +# Requesting a pull to %s from %s +# +# Write a message for this pull request. The first block +# of the text is the title and the rest is description.%s +` + + message = fmt.Sprintf(message, defaultMsg, repo.FullBase(), repo.FullHead(), commitSummary) return ioutil.WriteFile(messageFile, []byte(message), 0644) } diff --git a/git/git.go b/git/git.go index 821bfec7c60c1cda8c0aa81980f1a6c2032c11a5..f69f080d10d6c1470e2164d1c17ed80a9579b494 100644 --- a/git/git.go +++ b/git/git.go @@ -68,6 +68,25 @@ func Ref(ref string) (string, error) { return output[0], nil } +func RefList(a, b string) ([]string, error) { + ref := fmt.Sprintf("%s...%s", a, b) + output, err := execGitCmd("rev-list", "--cherry-pick", "--right-only", "--no-merges", ref) + if err != nil { + return nil, errors.New("Can't load rev-list for %s" + ref) + } + + return output, nil +} + +func Show(sha string) (string, error) { + output, err := execGitCmd("show", "-s", "--format=%w(78,0,0)%s%+b", sha) + if err != nil { + return "", errors.New("Can't show commit for %s" + sha) + } + + return output[0], nil +} + func Log(sha1, sha2 string) (string, error) { execCmd := cmd.New("git") execCmd.WithArg("log").WithArg("--no-color") @@ -111,7 +130,10 @@ func execGitCmd(input ...string) (outputs []string, err error) { out, err := cmd.ExecOutput() for _, line := range strings.Split(out, "\n") { - outputs = append(outputs, string(line)) + line = strings.TrimSpace(line) + if line != "" { + outputs = append(outputs, string(line)) + } } return outputs, err diff --git a/git/git_test.go b/git/git_test.go index 5dbea54a4ed48ab5b9db8ce0bd07b186b4c9575a..fc169f0b169ad0eb82d5676a18c1d14891ea880a 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -35,12 +35,27 @@ func TestGitHead(t *testing.T) { } func TestGitLog(t *testing.T) { - logs, _ := Log("master", "HEAD") - assert.T(t, len(logs) >= 0) + log, err := Log("e357a98a1a580b09d4f1d9bf613a6a51e131ef6e", "49e984e2fe86f68c386aeb133b390d39e4264ec1") + assert.Equal(t, nil, err) + assert.NotEqual(t, "", log) } func TestGitRef(t *testing.T) { - gitRef, err := Ref("master") + gitRef, err := Ref("1c1077c052d32a83aa13a8afaa4a9630d2f28ef6") + assert.Equal(t, nil, err) + assert.Equal(t, "1c1077c052d32a83aa13a8afaa4a9630d2f28ef6", gitRef) +} + +func TestGitRefList(t *testing.T) { + refList, err := RefList("e357a98a1a580b09d4f1d9bf613a6a51e131ef6e", "49e984e2fe86f68c386aeb133b390d39e4264ec1") + assert.Equal(t, nil, err) + assert.Equal(t, 1, len(refList)) + + assert.Equal(t, "49e984e2fe86f68c386aeb133b390d39e4264ec1", refList[0]) +} + +func TestGitShow(t *testing.T) { + output, err := Show("ce20e63ad00751bfed5d08072b11cf1b43af1995") assert.Equal(t, nil, err) - assert.NotEqual(t, "", gitRef) + assert.Equal(t, "Add Git.RefList", output) }