2018-09-16 14:50:53 +03:00
|
|
|
// Inspired by the git-appraise project
|
2018-07-12 13:44:46 +03:00
|
|
|
|
2018-09-16 14:50:53 +03:00
|
|
|
// Package input contains helpers to use a text editor as an input for
|
|
|
|
// various field of a bug
|
2018-07-12 13:44:46 +03:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-12-05 05:08:54 +03:00
|
|
|
"path/filepath"
|
2018-07-31 16:18:09 +03:00
|
|
|
"strings"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2020-12-05 05:08:54 +03:00
|
|
|
"github.com/go-git/go-billy/v5/util"
|
2018-08-13 19:32:11 +03:00
|
|
|
"github.com/pkg/errors"
|
2018-07-12 13:44:46 +03:00
|
|
|
)
|
|
|
|
|
2018-07-31 17:43:43 +03:00
|
|
|
const messageFilename = "BUG_MESSAGE_EDITMSG"
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ErrEmptyMessage is returned when the required message has not been entered
|
2018-07-31 16:18:09 +03:00
|
|
|
var ErrEmptyMessage = errors.New("empty message")
|
2018-08-13 16:28:16 +03:00
|
|
|
|
|
|
|
// ErrEmptyMessage is returned when the required title has not been entered
|
2018-07-31 16:18:09 +03:00
|
|
|
var ErrEmptyTitle = errors.New("empty title")
|
|
|
|
|
|
|
|
const bugTitleCommentTemplate = `%s%s
|
|
|
|
|
|
|
|
# Please enter the title and comment message. The first non-empty line will be
|
|
|
|
# used as the title. Lines starting with '#' will be ignored.
|
|
|
|
# An empty title aborts the operation.
|
|
|
|
`
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// BugCreateEditorInput will open the default editor in the terminal with a
|
|
|
|
// template for the user to fill. The file is then processed to extract title
|
|
|
|
// and message.
|
2020-12-05 05:08:54 +03:00
|
|
|
func BugCreateEditorInput(repo repository.RepoCommonStorage, preTitle string, preMessage string) (string, string, error) {
|
2018-07-31 16:18:09 +03:00
|
|
|
if preMessage != "" {
|
|
|
|
preMessage = "\n\n" + preMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
template := fmt.Sprintf(bugTitleCommentTemplate, preTitle, preMessage)
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
raw, err := launchEditorWithTemplate(repo, messageFilename, template)
|
2018-07-31 16:18:09 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:42:13 +03:00
|
|
|
return processCreate(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BugCreateFileInput read from either from a file or from the standard input
|
|
|
|
// and extract a title and a message
|
|
|
|
func BugCreateFileInput(fileName string) (string, string, error) {
|
|
|
|
raw, err := fromFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return processCreate(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processCreate(raw string) (string, string, error) {
|
2018-07-31 16:18:09 +03:00
|
|
|
lines := strings.Split(raw, "\n")
|
|
|
|
|
|
|
|
var title string
|
2018-08-01 14:11:52 +03:00
|
|
|
var buffer bytes.Buffer
|
2018-07-31 16:18:09 +03:00
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if title == "" {
|
|
|
|
trimmed := strings.TrimSpace(line)
|
|
|
|
if trimmed != "" {
|
|
|
|
title = trimmed
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:11:52 +03:00
|
|
|
buffer.WriteString(line)
|
|
|
|
buffer.WriteString("\n")
|
2018-07-31 16:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if title == "" {
|
|
|
|
return "", "", ErrEmptyTitle
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:11:52 +03:00
|
|
|
message := strings.TrimSpace(buffer.String())
|
2018-07-31 16:18:09 +03:00
|
|
|
|
|
|
|
return title, message, nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:29:19 +03:00
|
|
|
const bugCommentTemplate = `%s
|
2018-07-31 16:18:09 +03:00
|
|
|
|
|
|
|
# Please enter the comment message. Lines starting with '#' will be ignored,
|
|
|
|
# and an empty message aborts the operation.
|
|
|
|
`
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// BugCommentEditorInput will open the default editor in the terminal with a
|
|
|
|
// template for the user to fill. The file is then processed to extract a comment.
|
2020-12-05 05:08:54 +03:00
|
|
|
func BugCommentEditorInput(repo repository.RepoCommonStorage, preMessage string) (string, error) {
|
2018-10-04 19:29:19 +03:00
|
|
|
template := fmt.Sprintf(bugCommentTemplate, preMessage)
|
2018-07-31 16:18:09 +03:00
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
raw, err := launchEditorWithTemplate(repo, messageFilename, template)
|
2018-07-31 16:18:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:42:13 +03:00
|
|
|
return processComment(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BugCommentFileInput read from either from a file or from the standard input
|
|
|
|
// and extract a message
|
|
|
|
func BugCommentFileInput(fileName string) (string, error) {
|
|
|
|
raw, err := fromFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return processComment(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processComment(raw string) (string, error) {
|
2018-07-31 16:18:09 +03:00
|
|
|
lines := strings.Split(raw, "\n")
|
|
|
|
|
2018-08-01 14:11:52 +03:00
|
|
|
var buffer bytes.Buffer
|
2018-07-31 16:18:09 +03:00
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-01 14:11:52 +03:00
|
|
|
buffer.WriteString(line)
|
|
|
|
buffer.WriteString("\n")
|
2018-07-31 16:18:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 14:11:52 +03:00
|
|
|
message := strings.TrimSpace(buffer.String())
|
2018-07-31 16:18:09 +03:00
|
|
|
|
|
|
|
if message == "" {
|
|
|
|
return "", ErrEmptyMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 22:25:06 +03:00
|
|
|
const bugTitleTemplate = `%s
|
2018-08-02 17:35:13 +03:00
|
|
|
|
|
|
|
# Please enter the new title. Only one line will used.
|
|
|
|
# Lines starting with '#' will be ignored, and an empty title aborts the operation.
|
|
|
|
`
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// BugTitleEditorInput will open the default editor in the terminal with a
|
|
|
|
// template for the user to fill. The file is then processed to extract a title.
|
2020-12-05 05:08:54 +03:00
|
|
|
func BugTitleEditorInput(repo repository.RepoCommonStorage, preTitle string) (string, error) {
|
2018-08-08 22:25:06 +03:00
|
|
|
template := fmt.Sprintf(bugTitleTemplate, preTitle)
|
2018-08-02 17:35:13 +03:00
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
raw, err := launchEditorWithTemplate(repo, messageFilename, template)
|
2018-08-02 17:35:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(raw, "\n")
|
|
|
|
|
|
|
|
var title string
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
trimmed := strings.TrimSpace(line)
|
|
|
|
if trimmed == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
title = trimmed
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if title == "" {
|
|
|
|
return "", ErrEmptyTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
return title, nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 20:28:32 +03:00
|
|
|
const queryTemplate = `%s
|
|
|
|
|
|
|
|
# Please edit the bug query.
|
|
|
|
# Lines starting with '#' will be ignored, and an empty query aborts the operation.
|
2018-09-15 14:57:39 +03:00
|
|
|
#
|
|
|
|
# Example: status:open author:"rené descartes" sort:edit
|
|
|
|
#
|
|
|
|
# Valid filters are:
|
|
|
|
#
|
|
|
|
# - status:open, status:closed
|
|
|
|
# - author:<query>
|
2019-03-01 17:30:07 +03:00
|
|
|
# - title:<title>
|
2018-09-15 14:57:39 +03:00
|
|
|
# - label:<label>
|
|
|
|
# - no:label
|
|
|
|
#
|
|
|
|
# Sorting
|
|
|
|
#
|
|
|
|
# - sort:id, sort:id-desc, sort:id-asc
|
|
|
|
# - sort:creation, sort:creation-desc, sort:creation-asc
|
|
|
|
# - sort:edit, sort:edit-desc, sort:edit-asc
|
|
|
|
#
|
|
|
|
# Notes
|
|
|
|
#
|
|
|
|
# - queries are case insensitive.
|
|
|
|
# - you can combine as many qualifiers as you want.
|
|
|
|
# - you can use double quotes for multi-word search terms (ex: author:"René Descartes")
|
2018-09-11 20:28:32 +03:00
|
|
|
`
|
|
|
|
|
|
|
|
// QueryEditorInput will open the default editor in the terminal with a
|
|
|
|
// template for the user to fill. The file is then processed to extract a query.
|
2020-12-05 05:08:54 +03:00
|
|
|
func QueryEditorInput(repo repository.RepoCommonStorage, preQuery string) (string, error) {
|
2018-09-11 20:28:32 +03:00
|
|
|
template := fmt.Sprintf(queryTemplate, preQuery)
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
raw, err := launchEditorWithTemplate(repo, messageFilename, template)
|
2018-09-11 20:28:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(raw, "\n")
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
trimmed := strings.TrimSpace(line)
|
|
|
|
if trimmed == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return trimmed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// launchEditorWithTemplate will launch an editor as launchEditor do, but with a
|
|
|
|
// provided template.
|
2020-12-05 05:08:54 +03:00
|
|
|
func launchEditorWithTemplate(repo repository.RepoCommonStorage, fileName string, template string) (string, error) {
|
|
|
|
err := util.WriteFile(repo.LocalStorage(), fileName, []byte(template), 0644)
|
2018-07-31 16:18:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
return launchEditor(repo, fileName)
|
2018-07-31 16:18:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// launchEditor launches the default editor configured for the given repo. This
|
2018-07-12 13:44:46 +03:00
|
|
|
// method blocks until the editor command has returned.
|
|
|
|
//
|
|
|
|
// The specified filename should be a temporary file and provided as a relative path
|
2020-12-05 05:08:54 +03:00
|
|
|
// from the repo (e.g. "FILENAME" will be converted to "[<reporoot>/].git/git-bug/FILENAME"). This file
|
2018-07-12 13:44:46 +03:00
|
|
|
// will be deleted after the editor is closed and its contents have been read.
|
|
|
|
//
|
|
|
|
// This method returns the text that was read from the temporary file, or
|
|
|
|
// an error if any step in the process failed.
|
2020-12-05 05:08:54 +03:00
|
|
|
func launchEditor(repo repository.RepoCommonStorage, fileName string) (string, error) {
|
|
|
|
defer repo.LocalStorage().Remove(fileName)
|
2018-07-31 16:18:09 +03:00
|
|
|
|
2018-07-12 13:44:46 +03:00
|
|
|
editor, err := repo.GetCoreEditor()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to detect default git editor: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
repo.LocalStorage().Root()
|
|
|
|
|
|
|
|
// bypass the interface but that's ok: we need that because we are communicating
|
|
|
|
// the absolute path to an external program
|
|
|
|
path := filepath.Join(repo.LocalStorage().Root(), fileName)
|
|
|
|
|
2018-07-12 13:44:46 +03:00
|
|
|
cmd, err := startInlineCommand(editor, path)
|
|
|
|
if err != nil {
|
|
|
|
// Running the editor directly did not work. This might mean that
|
|
|
|
// the editor string is not a path to an executable, but rather
|
|
|
|
// a shell command (e.g. "emacsclient --tty"). As such, we'll try
|
|
|
|
// to run the command through bash, and if that fails, try with sh
|
|
|
|
args := []string{"-c", fmt.Sprintf("%s %q", editor, path)}
|
|
|
|
cmd, err = startInlineCommand("bash", args...)
|
|
|
|
if err != nil {
|
|
|
|
cmd, err = startInlineCommand("sh", args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to start editor: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
return "", fmt.Errorf("Editing finished with error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := ioutil.ReadFile(path)
|
2018-07-31 16:18:09 +03:00
|
|
|
|
2018-07-12 13:44:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error reading edited file: %v\n", err)
|
|
|
|
}
|
2018-07-31 16:18:09 +03:00
|
|
|
|
2018-07-12 13:44:46 +03:00
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:42:13 +03:00
|
|
|
// fromFile loads and returns the contents of a given file. If - is passed
|
2018-07-12 13:44:46 +03:00
|
|
|
// through, much like git, it will read from stdin. This can be piped data,
|
|
|
|
// unless there is a tty in which case the user will be prompted to enter a
|
|
|
|
// message.
|
2018-10-17 22:42:13 +03:00
|
|
|
func fromFile(fileName string) (string, error) {
|
2018-07-12 13:44:46 +03:00
|
|
|
if fileName == "-" {
|
|
|
|
stat, err := os.Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error reading from stdin: %v\n", err)
|
|
|
|
}
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
// There is no tty. This will allow us to read piped data instead.
|
|
|
|
output, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error reading from stdin: %v\n", err)
|
|
|
|
}
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("(reading comment from standard input)\n")
|
|
|
|
var output bytes.Buffer
|
|
|
|
s := bufio.NewScanner(os.Stdin)
|
|
|
|
for s.Scan() {
|
|
|
|
output.Write(s.Bytes())
|
|
|
|
output.WriteRune('\n')
|
|
|
|
}
|
|
|
|
return output.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error reading file: %v\n", err)
|
|
|
|
}
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func startInlineCommand(command string, args ...string) (*exec.Cmd, error) {
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Start()
|
|
|
|
return cmd, err
|
|
|
|
}
|