refactor: remove Ctx field from git.Repository (#38500)

This commit is contained in:
wxiaoguang
2026-07-17 18:44:31 +08:00
committed by GitHub
parent 2c8e99bbf7
commit 5b078f72aa
235 changed files with 1234 additions and 1258 deletions

View File

@@ -6,6 +6,7 @@ package git
import (
"bytes"
"context"
"io"
"strconv"
"strings"
@@ -15,36 +16,36 @@ import (
)
// GetBranchCommitID returns last commit ID string of given branch.
func (repo *Repository) GetBranchCommitID(name string) (string, error) {
return repo.GetRefCommitID(BranchPrefix + name)
func (repo *Repository) GetBranchCommitID(ctx context.Context, name string) (string, error) {
return repo.GetRefCommitID(ctx, BranchPrefix+name)
}
// GetTagCommitID returns last commit ID string of given tag.
func (repo *Repository) GetTagCommitID(name string) (string, error) {
return repo.GetRefCommitID(TagPrefix + name)
func (repo *Repository) GetTagCommitID(ctx context.Context, name string) (string, error) {
return repo.GetRefCommitID(ctx, TagPrefix+name)
}
// GetCommit returns a commit object of by the git ref.
func (repo *Repository) GetCommit(ref string) (*Commit, error) {
id, err := repo.ConvertToGitID(ref)
func (repo *Repository) GetCommit(ctx context.Context, ref string) (*Commit, error) {
id, err := repo.ConvertToGitID(ctx, ref)
if err != nil {
return nil, err
}
return repo.getCommit(id)
return repo.getCommit(ctx, id)
}
// GetBranchCommit returns the last commit of given branch.
func (repo *Repository) GetBranchCommit(name string) (*Commit, error) {
return repo.GetCommit(RefNameFromBranch(name).String())
func (repo *Repository) GetBranchCommit(ctx context.Context, name string) (*Commit, error) {
return repo.GetCommit(ctx, RefNameFromBranch(name).String())
}
// GetTagCommit get the commit of the specific tag via name
func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
return repo.GetCommit(RefNameFromTag(name).String())
func (repo *Repository) GetTagCommit(ctx context.Context, name string) (*Commit, error) {
return repo.GetCommit(ctx, RefNameFromTag(name).String())
}
func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Commit, error) {
func (repo *Repository) getCommitByPathWithID(ctx context.Context, id ObjectID, relpath string) (*Commit, error) {
// File name starts with ':' must be escaped.
if strings.HasPrefix(relpath, ":") {
relpath = `\` + relpath
@@ -54,7 +55,7 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com
AddDynamicArguments(id.String()).
AddDashesAndList(relpath).
WithDir(repo.Path).
RunStdString(repo.Ctx)
RunStdString(ctx)
if runErr != nil {
return nil, runErr
}
@@ -64,20 +65,20 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com
return nil, err
}
return repo.getCommit(id)
return repo.getCommit(ctx, id)
}
// GetCommitByPath returns the last commit of relative path.
func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
func (repo *Repository) GetCommitByPath(ctx context.Context, relpath string) (*Commit, error) {
stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat).
AddDashesAndList(relpath).
WithDir(repo.Path).
RunStdBytes(repo.Ctx)
RunStdBytes(ctx)
if runErr != nil {
return nil, runErr
}
commits, err := repo.parsePrettyFormatLogToList(stdout)
commits, err := repo.parsePrettyFormatLogToList(ctx, stdout)
if err != nil {
return nil, err
}
@@ -88,7 +89,7 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
}
// commitsByRangeWithTime returns the specific page commits before current revision, with not, since, until support
func (repo *Repository) commitsByRangeWithTime(id ObjectID, page, pageSize int, not, since, until string) ([]*Commit, error) {
func (repo *Repository) commitsByRangeWithTime(ctx context.Context, id ObjectID, page, pageSize int, not, since, until string) ([]*Commit, error) {
cmd := gitcmd.NewCommand("log").
AddOptionFormat("--skip=%d", (page-1)*pageSize).
AddOptionFormat("--max-count=%d", pageSize).
@@ -105,15 +106,15 @@ func (repo *Repository) commitsByRangeWithTime(id ObjectID, page, pageSize int,
cmd.AddOptionFormat("--until=%s", until)
}
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx)
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(ctx)
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
return repo.parsePrettyFormatLogToList(ctx, stdout)
}
func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([]*Commit, error) {
func (repo *Repository) searchCommits(ctx context.Context, id ObjectID, opts SearchCommitsOptions) ([]*Commit, error) {
// add common arguments to git command
addCommonSearchArgs := func(c *gitcmd.Command) {
// ignore case
@@ -159,7 +160,7 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([
// search for commits matching given constraints and keywords in commit msg
addCommonSearchArgs(cmd)
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx)
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(ctx)
if err != nil {
return nil, err
}
@@ -180,7 +181,7 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([
hashCmd.AddDynamicArguments(v)
// search with given constraints for commit matching sha hash of v
hashMatching, _, err := hashCmd.WithDir(repo.Path).RunStdBytes(repo.Ctx)
hashMatching, _, err := hashCmd.WithDir(repo.Path).RunStdBytes(ctx)
if err != nil || bytes.Contains(stdout, hashMatching) {
continue
}
@@ -189,17 +190,17 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([
}
}
return repo.parsePrettyFormatLogToList(bytes.TrimSuffix(stdout, []byte{'\n'}))
return repo.parsePrettyFormatLogToList(ctx, bytes.TrimSuffix(stdout, []byte{'\n'}))
}
// FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2
// You must ensure that id1 and id2 are valid commit ids.
func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) {
func (repo *Repository) FileChangedBetweenCommits(ctx context.Context, filename, id1, id2 string) (bool, error) {
stdout, _, err := gitcmd.NewCommand("diff", "--name-only", "-z").
AddDynamicArguments(id1, id2).
AddDashesAndList(filename).
WithDir(repo.Path).
RunStdBytes(repo.Ctx)
RunStdBytes(ctx)
if err != nil {
return false, err
}
@@ -219,7 +220,7 @@ type CommitsByFileAndRangeOptions struct {
}
// CommitsByFileAndRange return the commits according revision file and the page
func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) (commits []*Commit, hasMore bool, _ error) {
func (repo *Repository) CommitsByFileAndRange(ctx context.Context, opts CommitsByFileAndRangeOptions) (commits []*Commit, hasMore bool, _ error) {
limit := setting.Git.CommitsRangeSize
gitCmd := gitcmd.NewCommand("--no-pager", "log").
AddArguments("--pretty=tformat:%H").
@@ -244,7 +245,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
defer stdoutReaderClose()
err := gitCmd.WithDir(repo.Path).
WithPipelineFunc(func(context gitcmd.Context) error {
objectFormat, err := repo.GetObjectFormat()
objectFormat, err := repo.GetObjectFormat(ctx)
if err != nil {
return err
}
@@ -263,14 +264,14 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
if err != nil {
return err
}
commit, err := repo.getCommit(objectID)
commit, err := repo.getCommit(ctx, objectID)
if err != nil {
return err
}
commits = append(commits, commit)
}
}).
RunWithStderr(repo.Ctx)
RunWithStderr(ctx)
hasMore = len(commits) > limit
if hasMore {
@@ -281,7 +282,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
// CommitsBetween returns a list that contains commits between [after, before). After is the first item in the slice.
// If "before" and "after" are not related, it returns the all commits for the "after" commit.
func (repo *Repository) CommitsBetween(afterRef, beforeRef RefName, limit int, optSkip ...int) ([]*Commit, error) {
func (repo *Repository) CommitsBetween(ctx context.Context, afterRef, beforeRef RefName, limit int, optSkip ...int) ([]*Commit, error) {
gitCmd := func() *gitcmd.Command {
cmd := gitcmd.NewCommand("rev-list").WithDir(repo.Path)
if limit >= 0 {
@@ -295,42 +296,42 @@ func (repo *Repository) CommitsBetween(afterRef, beforeRef RefName, limit int, o
var stdout []byte
var err error
if beforeRef == "" {
stdout, _, err = gitCmd().AddDynamicArguments(afterRef.String()).RunStdBytes(repo.Ctx)
stdout, _, err = gitCmd().AddDynamicArguments(afterRef.String()).RunStdBytes(ctx)
} else {
stdout, _, err = gitCmd().AddDynamicArguments(beforeRef.String() + ".." + afterRef.String()).RunStdBytes(repo.Ctx)
stdout, _, err = gitCmd().AddDynamicArguments(beforeRef.String() + ".." + afterRef.String()).RunStdBytes(ctx)
if gitcmd.IsStderr(err, gitcmd.StderrNoMergeBase) {
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
// if the beforeRef and afterRef are not related (no merge base), just get all commits pushed by afterRef
stdout, _, err = gitCmd().AddDynamicArguments(afterRef.String()).RunStdBytes(repo.Ctx)
stdout, _, err = gitCmd().AddDynamicArguments(afterRef.String()).RunStdBytes(ctx)
}
}
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
return repo.parsePrettyFormatLogToList(ctx, bytes.TrimSpace(stdout))
}
// commitsBefore the limit is depth, not total number of returned commits.
func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error) {
func (repo *Repository) commitsBefore(ctx context.Context, id ObjectID, limit int) ([]*Commit, error) {
cmd := gitcmd.NewCommand("log", prettyLogFormat)
if limit > 0 {
cmd.AddOptionFormat("-%d", limit)
}
cmd.AddDynamicArguments(id.String())
stdout, _, runErr := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx)
stdout, _, runErr := cmd.WithDir(repo.Path).RunStdBytes(ctx)
if runErr != nil {
return nil, runErr
}
formattedLog, err := repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
formattedLog, err := repo.parsePrettyFormatLogToList(ctx, bytes.TrimSpace(stdout))
if err != nil {
return nil, err
}
commits := make([]*Commit, 0, len(formattedLog))
for _, commit := range formattedLog {
branches, err := repo.getBranches(nil, commit.ID.String(), 2)
branches, err := repo.getBranches(ctx, nil, commit.ID.String(), 2)
if err != nil {
return nil, err
}
@@ -345,22 +346,22 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error)
return commits, nil
}
func (repo *Repository) getCommitsBefore(id ObjectID) ([]*Commit, error) {
return repo.commitsBefore(id, 0)
func (repo *Repository) getCommitsBefore(ctx context.Context, id ObjectID) ([]*Commit, error) {
return repo.commitsBefore(ctx, id, 0)
}
func (repo *Repository) getCommitsBeforeLimit(id ObjectID, num int) ([]*Commit, error) {
return repo.commitsBefore(id, num)
func (repo *Repository) getCommitsBeforeLimit(ctx context.Context, id ObjectID, num int) ([]*Commit, error) {
return repo.commitsBefore(ctx, id, num)
}
func (repo *Repository) getBranches(env []string, commitID string, limit int) ([]string, error) {
func (repo *Repository) getBranches(ctx context.Context, env []string, commitID string, limit int) ([]string, error) {
stdout, _, err := gitcmd.NewCommand("for-each-ref", "--format=%(refname:strip=2)").
AddOptionFormat("--count=%d", limit).
AddOptionValues("--contains", commitID).
AddArguments(BranchPrefix).
WithEnv(env).
WithDir(repo.Path).
RunStdString(repo.Ctx)
RunStdString(ctx)
if err != nil {
return nil, err
}
@@ -368,11 +369,11 @@ func (repo *Repository) getBranches(env []string, commitID string, limit int) ([
}
// GetCommitsFromIDs get commits from commit IDs
func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit {
func (repo *Repository) GetCommitsFromIDs(ctx context.Context, commitIDs []string) []*Commit {
commits := make([]*Commit, 0, len(commitIDs))
for _, commitID := range commitIDs {
commit, err := repo.GetCommit(commitID)
commit, err := repo.GetCommit(ctx, commitID)
if err == nil && commit != nil {
commits = append(commits, commit)
}
@@ -382,11 +383,11 @@ func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit {
}
// IsCommitInBranch check if the commit is on the branch
func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err error) {
func (repo *Repository) IsCommitInBranch(ctx context.Context, commitID, branch string) (r bool, err error) {
stdout, _, err := gitcmd.NewCommand("branch", "--contains").
AddDynamicArguments(commitID, branch).
WithDir(repo.Path).
RunStdString(repo.Ctx)
RunStdString(ctx)
if err != nil {
return false, err
}
@@ -394,13 +395,13 @@ func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err e
}
// GetCommitBranchStart returns the commit where the branch diverged
func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID string) (string, error) {
func (repo *Repository) GetCommitBranchStart(ctx context.Context, env []string, branch, endCommitID string) (string, error) {
cmd := gitcmd.NewCommand("log", prettyLogFormat)
cmd.AddDynamicArguments(endCommitID)
stdout, _, runErr := cmd.WithDir(repo.Path).
WithEnv(env).
RunStdBytes(repo.Ctx)
RunStdBytes(ctx)
if runErr != nil {
return "", runErr
}
@@ -411,7 +412,7 @@ func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID s
// and we think this commit is the divergence point
for part := range parts {
commitID := string(part)
branches, err := repo.getBranches(env, commitID, 2)
branches, err := repo.getBranches(ctx, env, commitID, 2)
if err != nil {
return "", err
}