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

Unify `-F <filename> -e` functionality between commands

上级 b5e63871
......@@ -97,6 +97,9 @@ With no arguments, show a list of open issues.
-F, --file <FILE>
Read the issue title and description from <FILE>.
-e, --edit
Further edit the contents of <FILE> in a text editor before submitting.
-o, --browse
Open the new issue in a web browser.
......@@ -121,6 +124,7 @@ With no arguments, show a list of open issues.
flagIssueMessage,
flagIssueFile string
flagIssueEdit,
flagIssueBrowse bool
flagIssueMilestone uint64
......@@ -136,6 +140,7 @@ func init() {
cmdCreateIssue.Flag.VarP(&flagIssueLabels, "label", "l", "LABEL")
cmdCreateIssue.Flag.VarP(&flagIssueAssignees, "assign", "a", "ASSIGNEE")
cmdCreateIssue.Flag.BoolVarP(&flagIssueBrowse, "browse", "o", false, "BROWSE")
cmdCreateIssue.Flag.BoolVarP(&flagIssueEdit, "edit", "e", false, "EDIT")
cmdIssue.Flag.StringVarP(&flagIssueAssignee, "assignee", "a", "", "ASSIGNEE")
cmdIssue.Flag.StringVarP(&flagIssueState, "state", "s", "", "STATE")
......@@ -296,7 +301,7 @@ func createIssue(cmd *Command, args *Args) {
if cmd.FlagPassed("message") {
title, body = readMsg(flagIssueMessage)
} else if cmd.FlagPassed("file") {
title, body, err = readMsgFromFile(flagIssueFile)
title, body, editor, err = readMsgFromFile(flagIssueFile, flagIssueEdit, "ISSUE", "issue")
utils.Check(err)
} else {
cs := git.CommentChar()
......
......@@ -16,7 +16,7 @@ var cmdPullRequest = &Command{
Usage: `
pull-request [-fo] [-b <BASE>] [-h <HEAD>] [-a <USERS>] [-M <MILESTONE>] [-l <LABELS>]
pull-request -m <MESSAGE>
pull-request -F <FILE>
pull-request -F <FILE> [--edit]
pull-request -i <ISSUE>
`,
Long: `Create a GitHub pull request.
......@@ -32,6 +32,9 @@ pull-request -i <ISSUE>
-F, --file <FILE>
Read the pull request title and description from <FILE>.
-e, --edit
Further edit the contents of <FILE> in a text editor before submitting.
-i, --issue <ISSUE>, <ISSUE-URL>
(Deprecated) Convert <ISSUE> to a pull request.
......@@ -159,9 +162,6 @@ func pullRequest(cmd *Command, args *Args) {
}
}
title, body, err := getTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
utils.Check(err)
if headRepo, err := client.Repository(headProject); err == nil {
headProject.Owner = headRepo.Owner.Login
headProject.Name = headRepo.Name
......@@ -180,7 +180,14 @@ func pullRequest(cmd *Command, args *Args) {
}
var editor *github.Editor
if title == "" && flagPullRequestIssue == "" {
var title, body string
if cmd.FlagPassed("message") {
title, body = readMsg(flagPullRequestMessage)
} else if cmd.FlagPassed("file") {
title, body, editor, err = readMsgFromFile(flagPullRequestFile, flagPullRequestEdit, "PULLREQ", "pull request")
utils.Check(err)
} else if flagPullRequestIssue == "" {
baseTracking := base
headTracking := head
......@@ -205,14 +212,6 @@ func pullRequest(cmd *Command, args *Args) {
utils.Check(err)
}
if flagPullRequestFile != "" && flagPullRequestEdit {
editor, err = github.NewEditor("PULLREQ", "pull request", title + "\n\n" + body)
utils.Check(err)
title, body, err = editor.EditTitleAndBody()
utils.Check(err)
}
if title == "" && flagPullRequestIssue == "" {
utils.Check(fmt.Errorf("Aborting due to empty pull request title"))
}
......
......@@ -69,6 +69,9 @@ With '--include-drafts', include draft releases in the listing.
-F, --file <FILE>
Read the release title and description from <FILE>.
-e, --edit
Further edit the contents of <FILE> in a text editor before submitting.
-c, --commitish <TARGET>
A SHA, tag, or branch name to attach the release to (default: current branch).
......@@ -104,6 +107,7 @@ hub(1), git-tag(1)
flagReleaseIncludeDrafts,
flagReleaseShowDownloads,
flagReleaseDraft,
flagReleaseEdit,
flagReleasePrerelease bool
flagReleaseMessage,
......@@ -118,6 +122,7 @@ func init() {
cmdShowRelease.Flag.BoolVarP(&flagReleaseShowDownloads, "show-downloads", "d", false, "DRAFTS")
cmdCreateRelease.Flag.BoolVarP(&flagReleaseEdit, "edit", "e", false, "EDIT")
cmdCreateRelease.Flag.BoolVarP(&flagReleaseDraft, "draft", "d", false, "DRAFT")
cmdCreateRelease.Flag.BoolVarP(&flagReleasePrerelease, "prerelease", "p", false, "PRERELEASE")
cmdCreateRelease.Flag.VarP(&flagReleaseAssets, "attach", "a", "ATTACH_ASSETS")
......@@ -125,6 +130,7 @@ func init() {
cmdCreateRelease.Flag.StringVarP(&flagReleaseFile, "file", "F", "", "FILE")
cmdCreateRelease.Flag.StringVarP(&flagReleaseCommitish, "commitish", "c", "", "COMMITISH")
cmdEditRelease.Flag.BoolVarP(&flagReleaseEdit, "edit", "e", false, "EDIT")
cmdEditRelease.Flag.BoolVarP(&flagReleaseDraft, "draft", "d", false, "DRAFT")
cmdEditRelease.Flag.BoolVarP(&flagReleasePrerelease, "prerelease", "p", false, "PRERELEASE")
cmdEditRelease.Flag.VarP(&flagReleaseAssets, "attach", "a", "ATTACH_ASSETS")
......@@ -280,7 +286,7 @@ func createRelease(cmd *Command, args *Args) {
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
} else if cmd.FlagPassed("file") {
title, body, err = readMsgFromFile(flagReleaseFile)
title, body, editor, err = readMsgFromFile(flagReleaseFile, flagReleaseEdit, "RELEASE", "release")
utils.Check(err)
} else {
cs := git.CommentChar()
......@@ -367,7 +373,7 @@ func editRelease(cmd *Command, args *Args) {
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
} else if cmd.FlagPassed("file") {
title, body, err = readMsgFromFile(flagReleaseFile)
title, body, editor, err = readMsgFromFile(flagReleaseFile, flagReleaseEdit, "RELEASE", "release")
utils.Check(err)
if title == "" {
......
......@@ -76,30 +76,39 @@ func isEmptyDir(path string) bool {
return match == nil
}
func getTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string, err error) {
if messageFlag != "" {
title, body = readMsg(messageFlag)
} else if fileFlag != "" {
title, body, err = readMsgFromFile(fileFlag)
func readMsgFromFile(filename string, edit bool, editorPrefix, editorTopic string) (title, body string, editor *github.Editor, err error) {
message, err := msgFromFile(filename)
if err != nil {
return
}
return
if edit {
editor, err = github.NewEditor(editorPrefix, editorTopic, message)
if err != nil {
return
}
title, body, err = editor.EditTitleAndBody()
return
} else {
title, body = readMsg(message)
return
}
}
func readMsgFromFile(filename string) (title, body string, err error) {
func msgFromFile(filename string) (string, error) {
var content []byte
var err error
if filename == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(filename)
}
if err != nil {
return
return "", err
}
text := strings.Replace(string(content), "\r\n", "\n", -1)
title, body = readMsg(text)
return
return strings.Replace(string(content), "\r\n", "\n", -1), nil
}
func readMsg(message string) (title, body string) {
......
......@@ -329,8 +329,9 @@ BODY
Given the GitHub API server:
"""
post('/repos/mislav/coral/pulls') {
assert :title => 'Edit title from file',
:body => "Edit body from file as well.\n\nMultiline, even!"
assert :title => 'Hello from editor',
:body => "Title from file\n\nBody from file as well."
status 201
json :html_url => "https://github.com/mislav/coral/pull/12"
}
"""
......@@ -339,20 +340,13 @@ BODY
Title from file
Body from file as well.
Multiline, even!
"""
When I run `hub pull-request -F pullreq-msg --edit` interactively
And I pass in:
And the text editor adds:
"""
Edit title from file
Edit body from file as well.
Multiline, even!
Hello from editor
"""
Then the exit status should be 0
And the file ".git/PULLREQ_EDITMSG" should not exist
When I successfully run `hub pull-request -F pullreq-msg --edit`
Then the file ".git/PULLREQ_EDITMSG" should not exist
Scenario: Title and body from stdin
Given the GitHub API server:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册