Add gpg signing for merge rebase and update by rebase (#36701)

Fix #36685 

--- 

Generated by a coding agent with Codex 5.2 LLM.
This commit is contained in:
Lunny Xiao
2026-04-05 13:37:35 -07:00
committed by GitHub
parent ca51b4f875
commit e47c6135dd
5 changed files with 67 additions and 21 deletions
+12 -8
View File
@@ -436,20 +436,24 @@ func doMergeAndPush(ctx context.Context, pr *issues_model.PullRequest, doer *use
func commitAndSignNoAuthor(ctx *mergeContext, message string) error {
cmdCommit := gitcmd.NewCommand("commit").AddOptionFormat("--message=%s", message)
if ctx.signKey == nil {
cmdCommit.AddArguments("--no-gpg-sign")
} else {
if ctx.signKey.Format != "" {
cmdCommit.AddConfig("gpg.format", ctx.signKey.Format)
}
cmdCommit.AddOptionFormat("-S%s", ctx.signKey.KeyID)
}
addCommitSigningOptions(cmdCommit, ctx.signKey)
if err := ctx.PrepareGitCmd(cmdCommit).RunWithStderr(ctx); err != nil {
return fmt.Errorf("git commit %v: %w\n%s", ctx.pr, err, ctx.outbuf.String())
}
return nil
}
func addCommitSigningOptions(cmd *gitcmd.Command, signKey *git.SigningKey) {
if signKey == nil {
cmd.AddArguments("--no-gpg-sign")
return
}
if signKey.Format != "" {
cmd.AddConfig("gpg.format", signKey.Format)
}
cmd.AddOptionFormat("--gpg-sign=%s", signKey.KeyID)
}
// ErrMergeConflicts represents an error if merging fails with a conflict
type ErrMergeConflicts struct {
Style repo_model.MergeStyle
+3 -1
View File
@@ -260,7 +260,9 @@ func rebaseTrackingOnToBase(ctx *mergeContext, mergeStyle repo_model.MergeStyle)
ctx.outbuf.Reset()
// Rebase before merging
if err := ctx.PrepareGitCmd(gitcmd.NewCommand("rebase").AddDynamicArguments(tmpRepoBaseBranch)).
cmdRebase := gitcmd.NewCommand("rebase").AddDynamicArguments(tmpRepoBaseBranch)
addCommitSigningOptions(cmdRebase, ctx.signKey)
if err := ctx.PrepareGitCmd(cmdRebase).
RunWithStderr(ctx); err != nil {
// Rebase will leave a REBASE_HEAD file in .git if there is a conflict
if _, statErr := os.Stat(filepath.Join(ctx.tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil {
+4 -4
View File
@@ -74,10 +74,10 @@ func doMergeRebaseFastForward(ctx *mergeContext) error {
}
if newMessage != "" {
if err := gitcmd.NewCommand("commit", "--amend").
AddOptionFormat("--message=%s", newMessage).
WithDir(ctx.tmpBasePath).
Run(ctx); err != nil {
cmdCommit := gitcmd.NewCommand("commit", "--amend").
AddOptionFormat("--message=%s", newMessage)
addCommitSigningOptions(cmdCommit, ctx.signKey)
if err := cmdCommit.WithDir(ctx.tmpBasePath).Run(ctx); err != nil {
log.Error("Unable to amend commit message: %v", err)
return err
}
+1 -8
View File
@@ -73,14 +73,7 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
AddOptionFormat("--message=%s", message).
AddArguments("--allow-empty")
if ctx.signKey == nil {
cmdCommit.AddArguments("--no-gpg-sign")
} else {
if ctx.signKey.Format != "" {
cmdCommit.AddConfig("gpg.format", ctx.signKey.Format)
}
cmdCommit.AddOptionFormat("-S%s", ctx.signKey.KeyID)
}
addCommitSigningOptions(cmdCommit, ctx.signKey)
if err := ctx.PrepareGitCmd(cmdCommit).RunWithStderr(ctx); err != nil {
log.Error("git commit %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), err.Stderr())
return fmt.Errorf("git commit [%s:%s -> %s:%s]: %w\n%s\n%s", ctx.pr.HeadRepo.FullName(), ctx.pr.HeadBranch, ctx.pr.BaseRepo.FullName(), ctx.pr.BaseBranch, err, ctx.outbuf.String(), err.Stderr())