提交 df2e473e 编写于 作者: M Mislav Marohnić

Refactor message editing and always respect `--edit`

Previously, passing `--edit` in combination with `-m` would skip opening
the editor. For compatibility with `git commit`, `--edit` is now always
respected.
上级 7b2311e0
......@@ -357,16 +357,20 @@ func createIssue(cmd *Command, args *Args) {
gh := github.NewClient(project.Host)
var title string
var body string
var editor *github.Editor
messageBuilder := &github.MessageBuilder{
Filename: "ISSUE_EDITMSG",
Title: "issue",
}
if cmd.FlagPassed("message") {
title, body = readMsg(flagIssueMessage)
messageBuilder.Message = flagIssueMessage
messageBuilder.Edit = flagIssueEdit
} else if cmd.FlagPassed("file") {
title, body, editor, err = readMsgFromFile(flagIssueFile, flagIssueEdit, "ISSUE_EDITMSG", "issue")
messageBuilder.Message, err = msgFromFile(flagIssueFile)
utils.Check(err)
messageBuilder.Edit = flagIssueEdit
} else {
messageBuilder.Edit = true
message := ""
helpMessage := fmt.Sprintf(`Creating an issue for %s
......@@ -382,17 +386,12 @@ text is the title and the rest is the description.`, project)
}
}
editor, err := github.NewEditor("ISSUE_EDITMSG", "issue", message)
utils.Check(err)
editor.AddCommentedSection(helpMessage)
title, body, err = editor.EditTitleAndBody()
utils.Check(err)
messageBuilder.Message = message
messageBuilder.AddCommentedSection(helpMessage)
}
if editor != nil {
defer editor.DeleteFile()
}
title, body, err := messageBuilder.Extract()
utils.Check(err)
if title == "" {
utils.Check(fmt.Errorf("Aborting creation due to empty issue title"))
......@@ -424,4 +423,6 @@ text is the title and the rest is the description.`, project)
printBrowseOrCopy(args, issue.HtmlUrl, flagIssueBrowse, flagIssueCopy)
}
messageBuilder.Cleanup()
}
......@@ -202,8 +202,10 @@ func pullRequest(cmd *Command, args *Args) {
}
}
var editor *github.Editor
var title, body string
messageBuilder := &github.MessageBuilder{
Filename: "PULLREQ_EDITMSG",
Title: "pull request",
}
baseTracking := base
headTracking := head
......@@ -224,11 +226,15 @@ func pullRequest(cmd *Command, args *Args) {
}
if cmd.FlagPassed("message") {
title, body = readMsg(flagPullRequestMessage)
messageBuilder.Message = flagPullRequestMessage
messageBuilder.Edit = flagPullRequestEdit
} else if cmd.FlagPassed("file") {
title, body, editor, err = readMsgFromFile(flagPullRequestFile, flagPullRequestEdit, "PULLREQ_EDITMSG", "pull request")
messageBuilder.Message, err = msgFromFile(flagPullRequestFile)
utils.Check(err)
messageBuilder.Edit = flagPullRequestEdit
} else if flagPullRequestIssue == "" {
messageBuilder.Edit = true
headForMessage := headTracking
if flagPullRequestPush {
headForMessage = head
......@@ -270,14 +276,13 @@ of text is the title and the rest is the description.`, fullBase, fullHead)
helpMessage = helpMessage + "\n\nChanges:\n\n" + strings.TrimSpace(commitLogs)
}
editor, err = github.NewEditor("PULLREQ_EDITMSG", "pull request", message)
utils.Check(err)
editor.AddCommentedSection(helpMessage)
title, body, err = editor.EditTitleAndBody()
utils.Check(err)
messageBuilder.Message = message
messageBuilder.AddCommentedSection(helpMessage)
}
title, body, err := messageBuilder.Extract()
utils.Check(err)
if title == "" && flagPullRequestIssue == "" {
utils.Check(fmt.Errorf("Aborting due to empty pull request title"))
}
......@@ -345,8 +350,8 @@ of text is the title and the rest is the description.`, fullBase, fullHead)
}
}
if err == nil && editor != nil {
defer editor.DeleteFile()
if err == nil {
defer messageBuilder.Cleanup()
}
utils.Check(err)
......
......@@ -296,30 +296,31 @@ func createRelease(cmd *Command, args *Args) {
gh := github.NewClient(project.Host)
var title string
var body string
var editor *github.Editor
messageBuilder := &github.MessageBuilder{
Filename: "RELEASE_EDITMSG",
Title: "release",
}
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
messageBuilder.Message = flagReleaseMessage
messageBuilder.Edit = flagReleaseEdit
} else if cmd.FlagPassed("file") {
title, body, editor, err = readMsgFromFile(flagReleaseFile, flagReleaseEdit, "RELEASE_EDITMSG", "release")
messageBuilder.Message, err = msgFromFile(flagReleaseFile)
utils.Check(err)
messageBuilder.Edit = flagReleaseEdit
} else {
message := ""
messageBuilder.Edit = true
helpMessage := fmt.Sprintf(`Creating release %s for %s
Write a message for this release. The first block of
text is the title and the rest is the description.`, tagName, project.String())
editor, err := github.NewEditor("RELEASE_EDITMSG", "release", message)
utils.Check(err)
editor.AddCommentedSection(helpMessage)
title, body, err = editor.EditTitleAndBody()
utils.Check(err)
messageBuilder.AddCommentedSection(helpMessage)
}
title, body, err := messageBuilder.Extract()
utils.Check(err)
if title == "" {
utils.Check(fmt.Errorf("Aborting release due to empty release title"))
}
......@@ -345,9 +346,7 @@ text is the title and the rest is the description.`, tagName, project.String())
printBrowseOrCopy(args, release.HtmlUrl, flagReleaseBrowse, flagReleaseCopy)
}
if editor != nil {
editor.DeleteFile()
}
messageBuilder.Cleanup()
uploadAssets(gh, release, flagReleaseAssets, args)
}
......@@ -384,36 +383,34 @@ func editRelease(cmd *Command, args *Args) {
params["prerelease"] = flagReleasePrerelease
}
var title string
var body string
var editor *github.Editor
messageBuilder := &github.MessageBuilder{
Filename: "RELEASE_EDITMSG",
Title: "release",
}
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
messageBuilder.Message = flagReleaseMessage
messageBuilder.Edit = flagReleaseEdit
} else if cmd.FlagPassed("file") {
title, body, editor, err = readMsgFromFile(flagReleaseFile, flagReleaseEdit, "RELEASE_EDITMSG", "release")
messageBuilder.Message, err = msgFromFile(flagReleaseFile)
utils.Check(err)
if title == "" {
utils.Check(fmt.Errorf("Aborting editing due to empty release title"))
}
messageBuilder.Edit = flagReleaseEdit
} else {
message := fmt.Sprintf("%s\n\n%s", release.Name, release.Body)
messageBuilder.Edit = true
messageBuilder.Message = fmt.Sprintf("%s\n\n%s", release.Name, release.Body)
helpMessage := fmt.Sprintf(`Editing release %s for %s
Write a message for this release. The first block of
text is the title and the rest is the description.`, tagName, project.String())
editor, err := github.NewEditor("RELEASE_EDITMSG", "release", message)
utils.Check(err)
editor.AddCommentedSection(helpMessage)
messageBuilder.AddCommentedSection(helpMessage)
}
title, body, err = editor.EditTitleAndBody()
utils.Check(err)
title, body, err := messageBuilder.Extract()
utils.Check(err)
if title == "" {
utils.Check(fmt.Errorf("Aborting editing due to empty release title"))
}
if title == "" && !cmd.FlagPassed("message") {
utils.Check(fmt.Errorf("Aborting editing due to empty release title"))
}
if title != "" {
......@@ -431,9 +428,7 @@ text is the title and the rest is the description.`, tagName, project.String())
utils.Check(err)
}
if editor != nil {
editor.DeleteFile()
}
messageBuilder.Cleanup()
}
uploadAssets(gh, release, flagReleaseAssets, args)
......
......@@ -85,15 +85,7 @@ const crashReportTmpl = "Crash report - %v\n\n" +
"Error (%s): `%v`\n\n" +
"Stack:\n\n```\n%s\n```\n\n" +
"Runtime:\n\n```\n%s\n```\n\n" +
"Version:\n\n```\n%s\n```\n" +
`
# Creating crash report:
#
# This information will be posted as a new issue under github/hub.
# We're NOT including any information about the command that you were executing,
# but knowing a little bit more about it would really help us to solve this problem.
# Feel free to modify the title and the description for this issue.
`
"Version:\n\n```\n%s\n```\n"
func reportTitleAndBody(reportedError error, stack string) (title, body string, err error) {
errType := reflect.TypeOf(reportedError).String()
......@@ -108,14 +100,26 @@ func reportTitleAndBody(reportedError error, stack string) (title, body string,
fullVersion,
)
editor, err := NewEditor("CRASH_REPORT", "crash report", message)
if err != nil {
return "", "", err
messageBuilder := &MessageBuilder{
Filename: "CRASH_REPORT",
Title: "crash report",
Message: message,
Edit: true,
}
messageBuilder.AddCommentedSection(`Creating crash report:
This information will be posted as a new issue under github/hub.
We're NOT including any information about the command that you were executing,
but knowing a little bit more about it would really help us to solve this problem.
Feel free to modify the title and the description for this issue.`)
defer editor.DeleteFile()
title, body, err = messageBuilder.Extract()
if err != nil {
return
}
defer messageBuilder.Cleanup()
return editor.EditTitleAndBody()
return
}
func runtimeInfo() string {
......
......@@ -82,6 +82,31 @@ func (e *Editor) EditTitleAndBody() (title, body string, err error) {
return
}
func (e *Editor) EditContent() (content string, err error) {
b, err := e.openAndEdit()
if err != nil {
return
}
b = bytes.TrimSpace(b)
reader := bytes.NewReader(b)
scanner := bufio.NewScanner(reader)
unquotedLines := []string{}
for scanner.Scan() {
line := scanner.Text()
if e.CS == "" || !strings.HasPrefix(line, e.CS) {
unquotedLines = append(unquotedLines, line)
}
}
if err = scanner.Err(); err != nil {
return
}
content = strings.Join(unquotedLines, "\n")
return
}
func (e *Editor) openAndEdit() (content []byte, err error) {
err = e.writeContent()
if err != nil {
......
package github
import (
"regexp"
"strings"
)
type MessageBuilder struct {
Title string
Filename string
Message string
Edit bool
commentedSections []string
editor *Editor
}
func (b *MessageBuilder) AddCommentedSection(section string) {
b.commentedSections = append(b.commentedSections, section)
}
func (b *MessageBuilder) Extract() (title, body string, err error) {
content := b.Message
if b.Edit {
b.editor, err = NewEditor(b.Filename, b.Title, content)
if err != nil {
return
}
for _, section := range b.commentedSections {
b.editor.AddCommentedSection(section)
}
content, err = b.editor.EditContent()
if err != nil {
return
}
} else {
nl := regexp.MustCompile(`\r?\n`)
content = nl.ReplaceAllString(content, "\n")
}
parts := strings.SplitN(content, "\n\n", 2)
if len(parts) >= 1 {
title = strings.TrimSpace(strings.Replace(parts[0], "\n", " ", -1))
}
if len(parts) >= 2 {
body = strings.TrimSpace(parts[1])
}
if title == "" {
defer b.Cleanup()
}
return
}
func (b *MessageBuilder) Cleanup() {
if b.editor != nil {
b.editor.DeleteFile()
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册