mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-05 14:48:07 +00:00
Fix #36905 The changes focus on force-push PR timeline handling and commit range calculation: - Reworked pull-request push comment creation to use a new `gitrepo.GetCommitIDsBetweenReverse` helper, with special handling for force pushes (merge-base based range, tolerate missing/invalid old commits, and keep force-push timeline entries). - Added `Comment.GetPushActionContent` to parse push comment payloads and used it to delete only non-force-push push comments during force pushes. - Removed the old `Repository.CommitsBetweenNotBase` helper from `modules/git/repo_commit.go` in favor of the new commit ID range helper. - Added tests for `GetCommitIDsBetweenReverse` (normal range, `notRef` filtering, fallback branch usage) and expanded pull comment tests to cover force-push edge cases. <img width="989" height="563" alt="image" src="https://github.com/user-attachments/assets/a01e1bc2-fa8a-4028-8a35-d484e601ff3b" /> --------- Signed-off-by: Lunny Xiao <xiaolunwen@gmail.com> Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package gitrepo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
|
)
|
|
|
|
// DivergeObject represents commit count diverging commits
|
|
type DivergeObject struct {
|
|
Ahead int
|
|
Behind int
|
|
}
|
|
|
|
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
|
|
func GetDivergingCommits(ctx context.Context, repo Repository, baseBranch, targetBranch string) (*DivergeObject, error) {
|
|
cmd := gitcmd.NewCommand("rev-list", "--count", "--left-right").
|
|
AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--")
|
|
stdout, _, err1 := RunCmdString(ctx, repo, cmd)
|
|
if err1 != nil {
|
|
return nil, err1
|
|
}
|
|
|
|
left, right, found := strings.Cut(strings.Trim(stdout, "\n"), "\t")
|
|
if !found {
|
|
return nil, fmt.Errorf("git rev-list output is missing a tab: %q", stdout)
|
|
}
|
|
|
|
behind, err := strconv.Atoi(left)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ahead, err := strconv.Atoi(right)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &DivergeObject{Ahead: ahead, Behind: behind}, nil
|
|
}
|
|
|
|
// GetCommitIDsBetweenReverse returns the last commit IDs between two commits in reverse order (from old to new) with limit.
|
|
// If the result exceeds the limit, the old commits IDs will be ignored
|
|
func GetCommitIDsBetweenReverse(ctx context.Context, repo Repository, startRef, endRef, notRef string, limit int) ([]string, error) {
|
|
genCmd := func(reversions ...string) *gitcmd.Command {
|
|
cmd := gitcmd.NewCommand("rev-list", "--reverse").
|
|
AddArguments("-n").AddDynamicArguments(strconv.Itoa(limit)).
|
|
AddDynamicArguments(reversions...)
|
|
if notRef != "" { // --not should be kept as the last parameter of git command, otherwise the result will be wrong
|
|
cmd.AddOptionValues("--not", notRef)
|
|
}
|
|
return cmd
|
|
}
|
|
stdout, _, err := RunCmdString(ctx, repo, genCmd(startRef+".."+endRef))
|
|
// example git error message: fatal: origin/main..HEAD: no merge base
|
|
if err != nil && strings.Contains(err.Stderr(), "no merge base") {
|
|
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
|
|
// previously it would return the results of git rev-list before last so let's try that...
|
|
stdout, _, err = RunCmdString(ctx, repo, genCmd(startRef, endRef))
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commitIDs := strings.Fields(strings.TrimSpace(stdout))
|
|
return commitIDs, nil
|
|
}
|