зеркало из https://github.com/mislav/hub.git
Add commit logs to PR message
This commit is contained in:
Родитель
761d0eb4c5
Коммит
a5aa2c63e8
|
@ -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)}
|
||||
}
|
|
@ -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))
|
||||
}
|
17
git.go
17
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\\)")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче