зеркало из https://github.com/mislav/hub.git
Rewrite implementation of edit pull request message
This commit is contained in:
Родитель
1ee4daa7ea
Коммит
a50c94fa7d
|
@ -126,30 +126,48 @@ func writePullRequestTitleAndBody(repo *github.Repo) (title, body string, err er
|
|||
}
|
||||
|
||||
func writePullRequestChanges(repo *github.Repo, messageFile string) error {
|
||||
message := `
|
||||
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`
|
||||
commitSummary = fmt.Sprintf(commitSummary, commitLogs)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
`
|
||||
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 = `
|
||||
#
|
||||
# Changes:
|
||||
#
|
||||
%s`
|
||||
changesMsg = fmt.Sprintf(changesMsg, commitLogs)
|
||||
}
|
||||
|
||||
message = fmt.Sprintf(message, repo.FullBase(), repo.FullHead(), changesMsg)
|
||||
message = fmt.Sprintf(message, defaultMsg, repo.FullBase(), repo.FullHead(), commitSummary)
|
||||
|
||||
return ioutil.WriteFile(messageFile, []byte(message), 0644)
|
||||
}
|
||||
|
|
14
git/git.go
14
git/git.go
|
@ -78,6 +78,15 @@ func RefList(a, b string) ([]string, error) {
|
|||
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")
|
||||
|
@ -121,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
|
||||
|
|
|
@ -44,3 +44,17 @@ func TestGitRef(t *testing.T) {
|
|||
assert.Equal(t, nil, err)
|
||||
assert.NotEqual(t, "", 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.Equal(t, "Add Git.RefList", output)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче