diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..28aed3d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/cmd/gg/pr_editor_template.md eol=lf diff --git a/cmd/gg/pr_editor_template.md b/cmd/gg/pr_editor_template.md new file mode 100644 index 0000000..708104a --- /dev/null +++ b/cmd/gg/pr_editor_template.md @@ -0,0 +1,11 @@ +{{ .Title }} +{{- with .Body }} + +{{ . }} +{{- end }} + +[comment]: # (Please enter the pull request message.) +[comment]: # (Lines formatted like this will be ignored,) +[comment]: # (and an empty message aborts the pull request.) +[comment]: # (The first line will be used as the title and must not be empty.) +[comment]: # ({{ .BaseOwner }}/{{ .BaseRepo }}: merge into {{ .BaseOwner }}:{{ .BaseBranch }} from {{ .HeadOwner }}:{{ .Branch }}) diff --git a/cmd/gg/requestpull.go b/cmd/gg/requestpull.go index 16cb8fe..719f257 100644 --- a/cmd/gg/requestpull.go +++ b/cmd/gg/requestpull.go @@ -17,6 +17,7 @@ package main import ( "bytes" "context" + _ "embed" "encoding/json" "errors" "fmt" @@ -26,6 +27,7 @@ import ( "net/url" "os" "strings" + "text/template" "unicode" "gg-scm.io/pkg/git" @@ -34,6 +36,9 @@ import ( const requestPullSynopsis = "create a GitHub pull request" +//go:embed pr_editor_template.md +var requestPullEditorTemplate string + func requestPull(ctx context.Context, cc *cmdContext, args []string) error { f := flag.NewFlagSet(true, "gg requestpull [-n] [-e=0] [--title=MSG [--body=MSG]] [--draft] [-R user1[,user2]] [BRANCH]", requestPullSynopsis+` @@ -179,18 +184,23 @@ aliases: pr return nil } if *edit && *titleFlag == "" { + tmpl, err := template.New("pr_editor_template.md").Parse(requestPullEditorTemplate) + if err != nil { + return err + } editorInit := new(bytes.Buffer) - editorInit.WriteString(title) - if body != "" { - editorInit.WriteString("\n\n") - editorInit.WriteString(body) + err = tmpl.Execute(editorInit, map[string]any{ + "Title": title, + "Body": body, + "BaseOwner": baseOwner, + "BaseRepo": baseRepo, + "BaseBranch": baseBranch, + "HeadOwner": headOwner, + "Branch": branch, + }) + if err != nil { + return err } - editorInit.WriteString("\n" + prCommentPrefix + "Please enter the pull request message." + prCommentSuffix + "\n" + - prCommentPrefix + "Lines formatted like this will be ignored," + prCommentSuffix + "\n" + - prCommentPrefix + "and an empty message aborts the pull request." + prCommentSuffix + "\n" + - prCommentPrefix + "The first line will be used as the title and must not be empty." + prCommentSuffix + "\n") - fmt.Fprintf(editorInit, "%s%s/%s: merge into %s:%s from %s:%s%s\n", - prCommentPrefix, baseOwner, baseRepo, baseOwner, baseBranch, headOwner, branch, prCommentSuffix) newMsg, err := cc.editor.open(ctx, "PR_EDITMSG.md", editorInit.Bytes()) if err != nil { return err @@ -306,12 +316,10 @@ func readPullRequestTemplate(ctx context.Context, g *git.Git) string { return "" } -const ( - prCommentPrefix = "[comment]: # (" - prCommentSuffix = ")" -) - func parseEditedPullRequestMessage(b []byte) (title, body string, _ error) { + const prCommentPrefix = "[comment]: # (" + const prCommentSuffix = ")" + // Split into lines. lines := bytes.Split(b, []byte{'\n'}) // Strip comment lines.