From a5aa2c63e86f75ad98faf093589905191e76e898 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Sun, 28 Apr 2013 13:41:45 -0700 Subject: [PATCH] Add commit logs to PR message --- exec_cmd.go | 29 +++++++++++++++++++++++++++++ exec_cmd_test.go | 13 +++++++++++++ git.go | 17 +++++++++++++++++ git_test.go | 2 ++ pull_request.go | 26 ++++++++++++++++++++++---- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 exec_cmd.go create mode 100644 exec_cmd_test.go diff --git a/exec_cmd.go b/exec_cmd.go new file mode 100644 index 00000000..0329b877 --- /dev/null +++ b/exec_cmd.go @@ -0,0 +1,29 @@ +package main + +import ( + "os/exec" +) + +type ExecCmd struct { + Name string + Args []string +} + +func (cmd *ExecCmd) WithArg(arg string) *ExecCmd { + cmd.Args = append(cmd.Args, arg) + + return cmd +} + +func (cmd *ExecCmd) Exec() (out string, err error) { + output, err := exec.Command(cmd.Name, cmd.Args...).Output() + if err != nil { + return "", err + } + + return string(output), nil +} + +func NewExecCmd(name string) *ExecCmd { + return &ExecCmd{name, make([]string, 0)} +} diff --git a/exec_cmd_test.go b/exec_cmd_test.go new file mode 100644 index 00000000..d7506f55 --- /dev/null +++ b/exec_cmd_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/bmizerany/assert" + "testing" +) + +func TestWithArg(t *testing.T) { + execCmd := NewExecCmd("git") + execCmd.WithArg("log").WithArg("--no-color") + assert.Equal(t, "git", execCmd.Name) + assert.Equal(t, 2, len(execCmd.Args)) +} diff --git a/git.go b/git.go index 3c35f30a..d01ff3eb 100644 --- a/git.go +++ b/git.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "log" "os/exec" "path/filepath" @@ -41,6 +42,22 @@ func (g *Git) CurrentBranch() string { return execGitCmd("symbolic-ref -q --short HEAD")[0] } +func (g *Git) CommitLogs(sha1, sha2 string) string { + execCmd := NewExecCmd("git") + execCmd.WithArg("log").WithArg("--no-color") + execCmd.WithArg("--format=%h (%aN, %ar)%n%w(78,3,3)%s%n%+b") + execCmd.WithArg("--cherry") + shaRange := fmt.Sprintf("%s...%s", sha1, sha2) + execCmd.WithArg(shaRange) + + outputs, err := execCmd.Exec() + if err != nil { + log.Fatal(err) + } + + return outputs +} + // FIXME: only care about origin push remote now func (g *Git) Remote() string { r := regexp.MustCompile("origin\t(.+) \\(push\\)") diff --git a/git_test.go b/git_test.go index d9d9daff..13b323f3 100644 --- a/git_test.go +++ b/git_test.go @@ -13,4 +13,6 @@ func TestGitMethods(t *testing.T) { assert.Equal(t, "jingweno", git.Owner()) assert.Equal(t, "gh", git.Repo()) assert.Equal(t, "pull_request", git.CurrentBranch()) + logs := git.CommitLogs("master", "HEAD") + assert.Equal(t, 19, len(logs)) } diff --git a/pull_request.go b/pull_request.go index 2f002402..4f449f47 100644 --- a/pull_request.go +++ b/pull_request.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "fmt" "io/ioutil" "log" "os" @@ -43,7 +44,7 @@ func init() { func pullRequest(cmd *Command, args []string) { messageFile := filepath.Join(git.Dir(), "PULLREQ_EDITMSG") - writePullRequestChanges(messageFile) + writePullRequestChanges(messageFile, flagPullRequestBase, flagPullRequestHead) editCmd := buildEditCommand(messageFile) err := execCmd(editCmd) @@ -66,9 +67,26 @@ func pullRequest(cmd *Command, args []string) { } } -func writePullRequestChanges(messageFile string) { - message := []byte("\n#\n# Changes:\n#") - err := ioutil.WriteFile(messageFile, message, 0644) +func writePullRequestChanges(messageFile, base, head string) { + message := ` +# Requesting a pull to %s from %s +# +# Write a message for this pull reuqest. The first block +# of the text is the title and the rest is description. +# +# Changes: +# +%s +` + startRegexp := regexp.MustCompilePOSIX("^") + endRegexp := regexp.MustCompilePOSIX(" +$") + + commitLogs := git.CommitLogs("master", "pull_request") + commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") + commitLogs = endRegexp.ReplaceAllString(commitLogs, "") + + message = fmt.Sprintf(message, base, head, commitLogs) + err := ioutil.WriteFile(messageFile, []byte(message), 0644) if err != nil { log.Fatal(err) }