mirror of
https://github.com/go-gitea/gitea.git
synced 2026-06-19 00:29:59 +02:00
Parse `Co-authored-by:` trailers from commit messages and surface contributors as an avatar stack across the commit page, commits list, PR commits tab, latest-commit row, blame, graph, and dashboard feed. - Up to 10 visible 20px avatars, GitHub-style overlap (6px first stride, 4px between subsequent), `+N` chip for the rest. - Label: 1 → name; 2 → `<a> and <b>`; 3+ → `<N> people` opens a Tippy popup with all participants. - Names and avatars link to the repo's commits-by-author search; fall back to profile or `mailto:`. - Trailer parsing uses `net/mail.ParseAddress`, scans only the trailing paragraph, filters out the commit's own author/committer. - Drops the non-standard `Co-committed-by:` emission on squash merge and web edits. Devtest: `/devtest/coauthor-avatars`. Fixes #25521 ---- <img width="353" height="277" alt="image" src="https://github.com/user-attachments/assets/72092ceb-97ca-4b09-9557-0b72d3c5458e" /> <img width="533" height="328" src="https://github.com/user-attachments/assets/11d0c8f8-8b3f-4f2e-9993-879f1c06bcc5" /> --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
82 lines
2.9 KiB
Go
82 lines
2.9 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/container"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/log"
|
|
"gitea.dev/modules/setting"
|
|
)
|
|
|
|
// doMergeStyleSquash gets a commit author signature for squash commits
|
|
func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
|
|
if err := ctx.pr.Issue.LoadPoster(ctx); err != nil {
|
|
log.Error("%-v Issue[%d].LoadPoster: %v", ctx.pr, ctx.pr.Issue.ID, err)
|
|
return nil, err
|
|
}
|
|
|
|
// Try to get an signature from the same user in one of the commits, as the
|
|
// poster email might be private or commits might have a different signature
|
|
// than the primary email address of the poster.
|
|
gitRepo, err := git.OpenRepository(ctx, ctx.tmpBasePath)
|
|
if err != nil {
|
|
log.Error("%-v Unable to open base repository: %v", ctx.pr, err)
|
|
return nil, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
commits, err := gitRepo.CommitsBetweenIDs(tmpRepoTrackingBranch, "HEAD")
|
|
if err != nil {
|
|
log.Error("%-v Unable to get commits between: %s %s: %v", ctx.pr, "HEAD", tmpRepoTrackingBranch, err)
|
|
return nil, err
|
|
}
|
|
|
|
uniqueEmails := make(container.Set[string])
|
|
for _, commit := range commits {
|
|
if commit.Author != nil && uniqueEmails.Add(commit.Author.Email) {
|
|
commitUser, _ := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
|
if commitUser != nil && commitUser.ID == ctx.pr.Issue.Poster.ID {
|
|
return commit.Author, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return ctx.pr.Issue.Poster.NewGitSig(), nil
|
|
}
|
|
|
|
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
|
|
func doMergeStyleSquash(ctx *mergeContext, message string) error {
|
|
sig, err := getAuthorSignatureSquash(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("getAuthorSignatureSquash: %w", err)
|
|
}
|
|
|
|
cmdMerge := gitcmd.NewCommand("merge", "--squash").AddDynamicArguments(tmpRepoTrackingBranch)
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleSquash, cmdMerge); err != nil {
|
|
log.Error("%-v Unable to merge --squash tracking into base: %v", ctx.pr, err)
|
|
return err
|
|
}
|
|
|
|
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
|
|
message = AddCommitMessageTailer(message, git.CoAuthoredByTrailer, sig.String())
|
|
}
|
|
cmdCommit := gitcmd.NewCommand("commit").
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
|
|
AddOptionFormat("--message=%s", message).
|
|
AddArguments("--allow-empty")
|
|
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())
|
|
}
|
|
ctx.outbuf.Reset()
|
|
return nil
|
|
}
|