Skip to content

Commit 8749130

Browse files
committed
cmd/gg: add a blank line between PR editor template and comments
The blank line ensures that preview works correctly. To make this easier to work with, I turned the template into an embedded Markdown data file that uses the `text/template` package.
1 parent bbf2530 commit 8749130

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/cmd/gg/pr_editor_template.md eol=lf

cmd/gg/pr_editor_template.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{ .Title }}
2+
{{- with .Body }}
3+
4+
{{ . }}
5+
{{- end }}
6+
7+
[comment]: # (Please enter the pull request message.)
8+
[comment]: # (Lines formatted like this will be ignored,)
9+
[comment]: # (and an empty message aborts the pull request.)
10+
[comment]: # (The first line will be used as the title and must not be empty.)
11+
[comment]: # ({{ .BaseOwner }}/{{ .BaseRepo }}: merge into {{ .BaseOwner }}:{{ .BaseBranch }} from {{ .HeadOwner }}:{{ .Branch }})

cmd/gg/requestpull.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717
import (
1818
"bytes"
1919
"context"
20+
_ "embed"
2021
"encoding/json"
2122
"errors"
2223
"fmt"
@@ -26,6 +27,7 @@ import (
2627
"net/url"
2728
"os"
2829
"strings"
30+
"text/template"
2931
"unicode"
3032

3133
"gg-scm.io/pkg/git"
@@ -34,6 +36,9 @@ import (
3436

3537
const requestPullSynopsis = "create a GitHub pull request"
3638

39+
//go:embed pr_editor_template.md
40+
var requestPullEditorTemplate string
41+
3742
func requestPull(ctx context.Context, cc *cmdContext, args []string) error {
3843
f := flag.NewFlagSet(true, "gg requestpull [-n] [-e=0] [--title=MSG [--body=MSG]] [--draft] [-R user1[,user2]] [BRANCH]", requestPullSynopsis+`
3944
@@ -179,18 +184,23 @@ aliases: pr
179184
return nil
180185
}
181186
if *edit && *titleFlag == "" {
187+
tmpl, err := template.New("pr_editor_template.md").Parse(requestPullEditorTemplate)
188+
if err != nil {
189+
return err
190+
}
182191
editorInit := new(bytes.Buffer)
183-
editorInit.WriteString(title)
184-
if body != "" {
185-
editorInit.WriteString("\n\n")
186-
editorInit.WriteString(body)
192+
err = tmpl.Execute(editorInit, map[string]any{
193+
"Title": title,
194+
"Body": body,
195+
"BaseOwner": baseOwner,
196+
"BaseRepo": baseRepo,
197+
"BaseBranch": baseBranch,
198+
"HeadOwner": headOwner,
199+
"Branch": branch,
200+
})
201+
if err != nil {
202+
return err
187203
}
188-
editorInit.WriteString("\n" + prCommentPrefix + "Please enter the pull request message." + prCommentSuffix + "\n" +
189-
prCommentPrefix + "Lines formatted like this will be ignored," + prCommentSuffix + "\n" +
190-
prCommentPrefix + "and an empty message aborts the pull request." + prCommentSuffix + "\n" +
191-
prCommentPrefix + "The first line will be used as the title and must not be empty." + prCommentSuffix + "\n")
192-
fmt.Fprintf(editorInit, "%s%s/%s: merge into %s:%s from %s:%s%s\n",
193-
prCommentPrefix, baseOwner, baseRepo, baseOwner, baseBranch, headOwner, branch, prCommentSuffix)
194204
newMsg, err := cc.editor.open(ctx, "PR_EDITMSG.md", editorInit.Bytes())
195205
if err != nil {
196206
return err
@@ -306,12 +316,10 @@ func readPullRequestTemplate(ctx context.Context, g *git.Git) string {
306316
return ""
307317
}
308318

309-
const (
310-
prCommentPrefix = "[comment]: # ("
311-
prCommentSuffix = ")"
312-
)
313-
314319
func parseEditedPullRequestMessage(b []byte) (title, body string, _ error) {
320+
const prCommentPrefix = "[comment]: # ("
321+
const prCommentSuffix = ")"
322+
315323
// Split into lines.
316324
lines := bytes.Split(b, []byte{'\n'})
317325
// Strip comment lines.

0 commit comments

Comments
 (0)