mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-21 22:05:17 +02:00
before: gitrepo vs git packages after: git package fully handle all git operations by the way, use `WithRepo(repo)` instead of `WithDir(repo.Path)` to hide path details. benefits: 1. remove all unnecessary wrappers, developers no need to struggle with "which package should be used" 2. simplify code, RepositoryFacade can (will) be used everywhere, all "path" details are (will be) hidden
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.dev/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 RepositoryFacade, baseBranch, targetBranch string) (*DivergeObject, error) {
|
|
cmd := gitcmd.NewCommand("rev-list", "--count", "--left-right").
|
|
AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--")
|
|
stdout, _, err1 := cmd.WithRepo(repo).RunStdString(ctx)
|
|
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 RepositoryFacade, 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 := genCmd(startRef + ".." + endRef).WithRepo(repo).RunStdString(ctx)
|
|
if gitcmd.IsStderr(err, gitcmd.StderrNoMergeBase) {
|
|
// if the start and end are not related (no merge base), just get all commits pushed by "end ref"
|
|
// previously it would return the results of git rev-list before last so let's try that...
|
|
stdout, _, err = genCmd(endRef).WithRepo(repo).RunStdString(ctx)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commitIDs := strings.Fields(strings.TrimSpace(stdout))
|
|
return commitIDs, nil
|
|
}
|