зеркало из https://github.com/mislav/hub.git
Don't delete file after editing
This commit is contained in:
Родитель
e4a6c9ce5e
Коммит
4daf6ce1d8
|
@ -149,9 +149,16 @@ func pullRequest(cmd *Command, args *Args) {
|
|||
}
|
||||
}
|
||||
|
||||
var editor *github.Editor
|
||||
if title == "" && flagPullRequestIssue == "" {
|
||||
commits, _ := git.RefList(base, head)
|
||||
title, body, err = writePullRequestTitleAndBody(base, head, fullBase, fullHead, commits)
|
||||
message, err := pullRequestChangesMessage(base, head, fullBase, fullHead, commits)
|
||||
utils.Check(err)
|
||||
|
||||
editor, err = github.NewEditor("PULLREQ", "pull request", message)
|
||||
utils.Check(err)
|
||||
|
||||
title, body, err = editor.EditTitleAndBody()
|
||||
utils.Check(err)
|
||||
}
|
||||
|
||||
|
@ -175,6 +182,10 @@ func pullRequest(cmd *Command, args *Args) {
|
|||
pr, err = client.CreatePullRequestForIssue(baseProject, base, fullHead, flagPullRequestIssue)
|
||||
}
|
||||
|
||||
if err == nil && editor != nil {
|
||||
defer editor.DeleteFile()
|
||||
}
|
||||
|
||||
utils.Check(err)
|
||||
pullRequestURL = pr.HTMLURL
|
||||
}
|
||||
|
@ -185,20 +196,6 @@ func pullRequest(cmd *Command, args *Args) {
|
|||
}
|
||||
}
|
||||
|
||||
func writePullRequestTitleAndBody(base, head, fullBase, fullHead string, commits []string) (title, body string, err error) {
|
||||
message, err := pullRequestChangesMessage(base, head, fullBase, fullHead, commits)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
editor, err := github.NewEditor("PULLREQ", "pull request", message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return editor.EditTitleAndBody()
|
||||
}
|
||||
|
||||
func pullRequestChangesMessage(base, head, fullBase, fullHead string, commits []string) (string, error) {
|
||||
var defaultMsg, commitSummary string
|
||||
if len(commits) == 1 {
|
||||
|
|
|
@ -31,7 +31,7 @@ func NewEditor(filePrefix, topic, message string) (editor *Editor, err error) {
|
|||
Topic: topic,
|
||||
File: messageFile,
|
||||
Message: message,
|
||||
openEditor: doTextEditorEdit,
|
||||
openEditor: openTextEditor,
|
||||
}
|
||||
|
||||
return
|
||||
|
@ -45,45 +45,8 @@ type Editor struct {
|
|||
openEditor func(program, file string) error
|
||||
}
|
||||
|
||||
func (e *Editor) isFileExist() bool {
|
||||
_, err := os.Stat(e.File)
|
||||
return err == nil || !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (e *Editor) WriteContent() (err error) {
|
||||
// only write message if file doesn't exist
|
||||
if !e.isFileExist() && e.Message != "" {
|
||||
err = ioutil.WriteFile(e.File, []byte(e.Message), 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Editor) readContent() (content []byte, err error) {
|
||||
return ioutil.ReadFile(e.File)
|
||||
}
|
||||
|
||||
func (e *Editor) openAndEdit() (content []byte, err error) {
|
||||
err = e.WriteContent()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// always remove file after editing is done
|
||||
defer os.Remove(e.File)
|
||||
|
||||
err = e.openEditor(e.Program, e.File)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error using text editor for %s message", e.Topic)
|
||||
return
|
||||
}
|
||||
|
||||
content, err = e.readContent()
|
||||
|
||||
return
|
||||
func (e *Editor) DeleteFile() error {
|
||||
return os.Remove(e.File)
|
||||
}
|
||||
|
||||
func (e *Editor) EditTitleAndBody() (title, body string, err error) {
|
||||
|
@ -99,7 +62,45 @@ func (e *Editor) EditTitleAndBody() (title, body string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func doTextEditorEdit(program, file string) error {
|
||||
func (e *Editor) openAndEdit() (content []byte, err error) {
|
||||
err = e.writeContent()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = e.openEditor(e.Program, e.File)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error using text editor for %s message", e.Topic)
|
||||
return
|
||||
}
|
||||
|
||||
content, err = e.readContent()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Editor) writeContent() (err error) {
|
||||
// only write message if file doesn't exist
|
||||
if !e.isFileExist() && e.Message != "" {
|
||||
err = ioutil.WriteFile(e.File, []byte(e.Message), 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Editor) isFileExist() bool {
|
||||
_, err := os.Stat(e.File)
|
||||
return err == nil || !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (e *Editor) readContent() (content []byte, err error) {
|
||||
return ioutil.ReadFile(e.File)
|
||||
}
|
||||
|
||||
func openTextEditor(program, file string) error {
|
||||
editCmd := cmd.New(program)
|
||||
r := regexp.MustCompile("[mg]?vi[m]$")
|
||||
if r.MatchString(program) {
|
||||
|
|
|
@ -3,7 +3,6 @@ package github
|
|||
import (
|
||||
"bufio"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -28,10 +27,6 @@ func TestEditor_openAndEdit_fileExist(t *testing.T) {
|
|||
content, err := editor.openAndEdit()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "hello", string(content))
|
||||
|
||||
// file is removed after edit
|
||||
_, err = os.Stat(tempFile.Name())
|
||||
assert.T(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestEditor_openAndEdit_fileNotExist(t *testing.T) {
|
||||
|
@ -51,10 +46,6 @@ func TestEditor_openAndEdit_fileNotExist(t *testing.T) {
|
|||
content, err := editor.openAndEdit()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "hello", string(content))
|
||||
|
||||
// file is removed after edit
|
||||
_, err = os.Stat(tempFile)
|
||||
assert.T(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestEditor_EditTitleAndBody(t *testing.T) {
|
||||
|
@ -82,10 +73,6 @@ A body continues
|
|||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "A title A title continues", title)
|
||||
assert.Equal(t, "A body\nA body continues", body)
|
||||
|
||||
// file is removed after edit
|
||||
_, err = os.Stat(tempFile)
|
||||
assert.T(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestReadTitleAndBody(t *testing.T) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче