Skip to content

Commit

Permalink
refactor git/edit to prepare for issue notes
Browse files Browse the repository at this point in the history
extracts EditFile from Edit and removeComments from parseTitleBody
  • Loading branch information
claytonrcarter committed Nov 7, 2018
1 parent 3aebeb2 commit a6aa3c0
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions internal/git/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,59 @@ import (
"strings"
)

// Edit opens a file in the users editor and returns the title and body. It
// store a temporary file in your .git directory or /tmp if accessed outside of
// a git repo.
// Edit opens a file in the users editor and returns the title and body.
func Edit(filePrefix, message string) (string, string, error) {
contents, err := EditFile(filePrefix, message)
if err != nil {
return "", "", err
}

return parseTitleBody(strings.TrimSpace(string(contents)))
}

// EditFile opens a file in the users editor and returns the contents. It
// stores a temporary file in your .git directory or /tmp if accessed outside of
// a git repo.
func EditFile(filePrefix, message string) (string, error) {
var (
dir string
err error
)
if InsideGitRepo() {
dir, err = GitDir()
if err != nil {
return "", "", err
return "", err
}
} else {
dir = "/tmp"
}
filePath := filepath.Join(dir, fmt.Sprintf("%s_EDITMSG", filePrefix))
editorPath, err := editorPath()
if err != nil {
return "", "", err
return "", err
}
defer os.Remove(filePath)

// Write generated/template message to file
if _, err := os.Stat(filePath); os.IsNotExist(err) && message != "" {
err = ioutil.WriteFile(filePath, []byte(message), 0644)
if err != nil {
return "", "", err
return "", err
}
}

cmd := editorCMD(editorPath, filePath)
err = cmd.Run()
if err != nil {
return "", "", err
return "", err
}

contents, err := ioutil.ReadFile(filePath)
if err != nil {
return "", "", err
return "", err
}

return parseTitleBody(strings.TrimSpace(string(contents)))
return removeComments(string(contents))
}

func editorPath() (string, error) {
Expand Down Expand Up @@ -81,7 +91,7 @@ func editorCMD(editorPath, filePath string) *exec.Cmd {
return cmd
}

func parseTitleBody(message string) (string, string, error) {
func removeComments(message string) (string, error) {
// Grab all the lines that don't start with the comment char
cc := CommentChar()
r := regexp.MustCompile(`(?m:^)[^` + cc + `].*(?m:$)`)
Expand All @@ -93,14 +103,22 @@ func parseTitleBody(message string) (string, string, error) {
noComments = append(noComments, p)
}
}
msg := strings.Join(noComments, "\n")
if strings.TrimSpace(msg) == "" {
return strings.TrimSpace(strings.Join(noComments, "\n")), nil
}

func parseTitleBody(message string) (string, string, error) {
msg, err := removeComments(message)
if err != nil {
return "", "", err
}

if msg == "" {
return "", "", nil
}

r = regexp.MustCompile(`\n\s*\n`)
r := regexp.MustCompile(`\n\s*\n`)
msg = strings.Replace(msg, "\\#", "#", -1)
parts = r.Split(msg, 2)
parts := r.Split(msg, 2)

if strings.Contains(parts[0], "\n") {
return "\n", parts[0], nil
Expand Down

0 comments on commit a6aa3c0

Please sign in to comment.