提交 a5aa2c63 编写于 作者: J Jingwen Owen Ou

Add commit logs to PR message

上级 761d0eb4
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)}
}
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))
}
......@@ -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\\)")
......
......@@ -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))
}
......@@ -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)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册