Skip to content

Commit ff2ed9b

Browse files
committed
Fix duplicate co-author in squashed merge commit messages
1 parent e69da2c commit ff2ed9b

File tree

1 file changed

+8
-45
lines changed

1 file changed

+8
-45
lines changed

services/pull/merge_squash.go

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,33 @@ package pull
55

66
import (
77
"fmt"
8+
"strings"
89

910
repo_model "code.gitea.io/gitea/models/repo"
10-
user_model "code.gitea.io/gitea/models/user"
11-
"code.gitea.io/gitea/modules/container"
1211
"code.gitea.io/gitea/modules/git"
1312
"code.gitea.io/gitea/modules/log"
1413
"code.gitea.io/gitea/modules/setting"
1514
)
1615

17-
// doMergeStyleSquash gets a commit author signature for squash commits
18-
func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
19-
if err := ctx.pr.Issue.LoadPoster(ctx); err != nil {
20-
log.Error("%-v Issue[%d].LoadPoster: %v", ctx.pr, ctx.pr.Issue.ID, err)
21-
return nil, err
22-
}
23-
24-
// Try to get an signature from the same user in one of the commits, as the
25-
// poster email might be private or commits might have a different signature
26-
// than the primary email address of the poster.
27-
gitRepo, err := git.OpenRepository(ctx, ctx.tmpBasePath)
28-
if err != nil {
29-
log.Error("%-v Unable to open base repository: %v", ctx.pr, err)
30-
return nil, err
31-
}
32-
defer gitRepo.Close()
33-
34-
commits, err := gitRepo.CommitsBetweenIDs(trackingBranch, "HEAD")
35-
if err != nil {
36-
log.Error("%-v Unable to get commits between: %s %s: %v", ctx.pr, "HEAD", trackingBranch, err)
37-
return nil, err
38-
}
39-
40-
uniqueEmails := make(container.Set[string])
41-
for _, commit := range commits {
42-
if commit.Author != nil && uniqueEmails.Add(commit.Author.Email) {
43-
commitUser, _ := user_model.GetUserByEmail(ctx, commit.Author.Email)
44-
if commitUser != nil && commitUser.ID == ctx.pr.Issue.Poster.ID {
45-
return commit.Author, nil
46-
}
47-
}
48-
}
49-
50-
return ctx.pr.Issue.Poster.NewGitSig(), nil
51-
}
52-
5316
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
5417
func doMergeStyleSquash(ctx *mergeContext, message string) error {
55-
sig, err := getAuthorSignatureSquash(ctx)
56-
if err != nil {
57-
return fmt.Errorf("getAuthorSignatureSquash: %w", err)
58-
}
18+
poster := ctx.pr.Issue.Poster.NewGitSig()
5919

6020
cmdMerge := git.NewCommand(ctx, "merge", "--squash").AddDynamicArguments(trackingBranch)
6121
if err := runMergeCommand(ctx, repo_model.MergeStyleSquash, cmdMerge); err != nil {
6222
log.Error("%-v Unable to merge --squash tracking into base: %v", ctx.pr, err)
6323
return err
6424
}
6525

66-
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
26+
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != poster.String() {
6727
// add trailer
68-
message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String())
28+
if !strings.Contains(message, fmt.Sprintf("Co-authored-by: %s", ctx.committer.String())) {
29+
message += fmt.Sprintf("\nCo-authored-by: %s", ctx.committer.String())
30+
}
31+
message += fmt.Sprintf("\nCo-committed-by: %s\n", ctx.committer.String())
6932
}
7033
cmdCommit := git.NewCommand(ctx, "commit").
71-
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
34+
AddOptionFormat("--author='%s <%s>'", poster.Name, poster.Email).
7235
AddOptionFormat("--message=%s", message)
7336
if ctx.signKeyID == "" {
7437
cmdCommit.AddArguments("--no-gpg-sign")

0 commit comments

Comments
 (0)