Skip to content

Commit de9f9c3

Browse files
committed
Add template commit arg to applier.Commit
This allows clients to pre-populate the commit with information like the message, author/committer, or signature.
1 parent 332e3f0 commit de9f9c3

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

applier.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,20 @@ func (a *Applier) CreateTree(ctx context.Context) (*github.Tree, error) {
215215
return tree, nil
216216
}
217217

218-
// Commit commits the latest tree, optionally using the details in header. If
219-
// there are pending tree entries, it calls CreateTree before creating the
220-
// commit. If header is nil or missing fields, Commit uses a default message,
221-
// the current time, and the authenticated user as needed for the commit
222-
// details. Commit returns an error if there are no pending trees or tree
223-
// entries.
224-
func (a *Applier) Commit(ctx context.Context, header *gitdiff.PatchHeader) (*github.Commit, error) {
218+
// Commit commits the latest tree, optionally using the details in tmpl and
219+
// header. If there are pending tree entries, it calls CreateTree before
220+
// creating the commit. It returns an error if there are no pending trees or
221+
// tree entries.
222+
//
223+
// If tmpl is not nil, Commit uses it as a template for the new commit,
224+
// overwriting fields as needed. If header is not nil, Commit uses it to set
225+
// the message, author, and committer for the new commit. Values in header
226+
// overwrite those in tmpl.
227+
//
228+
// If both tmpl and header are nil or missing fields, Commit uses a default
229+
// message, the current time, and the authenticated user as needed for the
230+
// commit details.
231+
func (a *Applier) Commit(ctx context.Context, tmpl *github.Commit, header *gitdiff.PatchHeader) (*github.Commit, error) {
225232
if !a.uncommitted && len(a.entries) == 0 {
226233
return nil, errors.New("no pending tree or tree entries")
227234
}
@@ -231,13 +238,16 @@ func (a *Applier) Commit(ctx context.Context, header *gitdiff.PatchHeader) (*git
231238
}
232239
}
233240

234-
c := &github.Commit{
235-
Tree: &github.Tree{
236-
SHA: github.String(a.tree),
237-
},
238-
Parents: []*github.Commit{
239-
a.commit,
240-
},
241+
var c github.Commit
242+
if tmpl != nil {
243+
c = *tmpl
244+
}
245+
246+
c.Tree = &github.Tree{
247+
SHA: github.String(a.tree),
248+
}
249+
c.Parents = []*github.Commit{
250+
a.commit,
241251
}
242252

243253
if header != nil {
@@ -249,7 +259,7 @@ func (a *Applier) Commit(ctx context.Context, header *gitdiff.PatchHeader) (*git
249259
c.Message = github.String("Apply patch with patch2pr")
250260
}
251261

252-
commit, _, err := a.client.Git.CreateCommit(ctx, a.owner, a.repo, c)
262+
commit, _, err := a.client.Git.CreateCommit(ctx, a.owner, a.repo, &c)
253263
if err != nil {
254264
return nil, err
255265
}

cmd/patch2pr/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func execute(ctx context.Context, client *github.Client, patchFile string, opts
188188
}
189189
}
190190

191-
newCommit, err := applier.Commit(ctx, fillHeader(header, patchFile, opts.Message))
191+
newCommit, err := applier.Commit(ctx, nil, fillHeader(header, patchFile, opts.Message))
192192
if err != nil {
193193
return nil, fmt.Errorf("commit failed: %w", err)
194194
}

0 commit comments

Comments
 (0)