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
+17 -17
View File
@@ -38,13 +38,13 @@ import (
)
// CreateNewBranch creates a new repository branch
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) {
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, oldBranchName, branchName string) (err error) {
branch, err := git_model.GetBranch(ctx, repo.ID, oldBranchName)
if err != nil {
return err
}
return CreateNewBranchFromCommit(ctx, doer, repo, branch.CommitID, branchName)
return CreateNewBranchFromCommit(ctx, doer, repo, gitRepo, branch.CommitID, branchName)
}
// Branch contains the branch information
@@ -226,14 +226,14 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
if pr.HasMerged {
baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
if !ok {
baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo)
baseGitRepo, err = gitrepo.OpenRepository(pr.BaseRepo)
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer baseGitRepo.Close()
repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
}
pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitHeadRefName())
pullCommit, err := baseGitRepo.GetRefCommitID(ctx, pr.GetGitHeadRefName())
if err != nil && !git.IsErrNotExist(err) {
return nil, fmt.Errorf("GetBranchCommitID: %v", err)
}
@@ -257,8 +257,8 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
}
// checkBranchName validates branch name with existing repository branches
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
_, err := gitrepo.WalkReferences(ctx, repo, func(_, refName string) error {
func checkBranchName(ctx context.Context, gitRepo *git.Repository, name string) error {
_, err := gitRepo.WalkReferences(ctx, "", 0, 0, func(_, refName string) error {
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
switch {
case branchRefName == name:
@@ -289,7 +289,7 @@ func checkBranchName(ctx context.Context, repo *repo_model.Repository, name stri
// It will check whether the branches of the repository have never been synced before.
// If so, it will sync all branches of the repository.
// Otherwise, it will sync the branches that need to be updated.
func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, commitIDs []string, getCommit func(commitID string) (*git.Commit, error)) error {
func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, gitRepo *git.Repository, branchNames, commitIDs []string) error {
// Some designs that make the code look strange but are made for performance optimization purposes:
// 1. Sync branches in a batch to reduce the number of DB queries.
// 2. Lazy load commit information since it may be not necessary.
@@ -343,7 +343,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
continue
}
commit, err := getCommit(commitID)
commit, err := gitRepo.GetCommit(ctx, commitID)
if err != nil {
return fmt.Errorf("get commit of %s failed: %v", branchName, err)
}
@@ -374,14 +374,14 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
}
// CreateNewBranchFromCommit creates a new repository branch
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commitID, branchName string) (err error) {
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, commitID, branchName string) (err error) {
err = repo.MustNotBeArchived()
if err != nil {
return err
}
// Check if branch name can be used
if err := checkBranchName(ctx, repo, branchName); err != nil {
if err := checkBranchName(ctx, gitRepo, branchName); err != nil {
return err
}
@@ -504,7 +504,7 @@ func UpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git
}
if expectedOldCommitID != "" {
expectedID, err := gitRepo.ConvertToGitID(expectedOldCommitID)
expectedID, err := gitRepo.ConvertToGitID(ctx, expectedOldCommitID)
if err != nil {
return fmt.Errorf("ConvertToGitID(old): %w", err)
}
@@ -513,11 +513,11 @@ func UpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git
}
}
newID, err := gitRepo.ConvertToGitID(newCommitID)
newID, err := gitRepo.ConvertToGitID(ctx, newCommitID)
if err != nil {
return fmt.Errorf("ConvertToGitID(new): %w", err)
}
newCommit, err := gitRepo.GetCommit(newID.String())
newCommit, err := gitRepo.GetCommit(ctx, newID.String())
if err != nil {
return err
}
@@ -615,7 +615,7 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
return err
}
branchCommit, err := gitRepo.GetBranchCommit(branchName)
branchCommit, err := gitRepo.GetBranchCommit(ctx, branchName)
// branchCommit can be nil if the branch doesn't exist in git
if err != nil && !errors.Is(err, util.ErrNotExist) {
return err
@@ -803,7 +803,7 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
if err != nil {
return nil, err
}
headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID)
headCommit, err := headGitRepo.GetCommit(ctx, headGitBranch.CommitID)
if err != nil {
return nil, err
}
@@ -886,12 +886,12 @@ func DeleteBranchAfterMerge(ctx context.Context, doer *user_model.User, prID int
defer gitHeadCloser.Close()
// Check if branch has no new commits
headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitHeadRefName())
headCommitID, err := gitBaseRepo.GetRefCommitID(ctx, pr.GetGitHeadRefName())
if err != nil {
log.Error("GetRefCommitID: %v", err)
return errFailedToDelete(err)
}
branchCommitID, err := gitHeadRepo.GetBranchCommitID(pr.HeadBranch)
branchCommitID, err := gitHeadRepo.GetBranchCommitID(ctx, pr.HeadBranch)
if err != nil {
log.Error("GetBranchCommitID: %v", err)
return errFailedToDelete(err)