refactor: decouple git.Repository(ctx) from git.Commit & git.Tree (#38464)

1. Storing "ctx" in a long-living object is wrong
2. Make the commit & tree cacheable (for the future performance
optimization)
3. Also fix some bad designs like `// FIXME: bad design, this field can
be nil if the commit is from "last commit cache"`

ref:
* #33893
This commit is contained in:
wxiaoguang
2026-07-16 01:30:01 +08:00
committed by GitHub
parent ed678b9d45
commit 82edc3da01
115 changed files with 732 additions and 864 deletions
+7 -7
View File
@@ -168,9 +168,9 @@ func pushQueueHandleUpdates(optsList []*repo_module.PushUpdateOptions) error {
// Push new branch.
var l []*git.Commit
if opts.IsNewRef() {
l, err = pushNewBranch(ctx, repo, pusher, opts, newCommit)
l, err = pushNewBranch(ctx, repo, gitRepo, pusher, opts, newCommit)
} else {
l, err = pushUpdateBranch(ctx, repo, pusher, opts, newCommit)
l, err = pushUpdateBranch(ctx, repo, gitRepo, pusher, opts, newCommit)
}
if err != nil {
return err
@@ -264,7 +264,7 @@ func getCompareURL(repo *repo_model.Repository, gitRepo *git.Repository, objectF
return ""
}
func pushNewBranch(ctx context.Context, repo *repo_model.Repository, pusher *user_model.User, opts *repo_module.PushUpdateOptions, newCommit *git.Commit) ([]*git.Commit, error) {
func pushNewBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, pusher *user_model.User, opts *repo_module.PushUpdateOptions, newCommit *git.Commit) ([]*git.Commit, error) {
if repo.IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
repo.DefaultBranch = opts.RefName()
repo.IsEmpty = false
@@ -279,7 +279,7 @@ func pushNewBranch(ctx context.Context, repo *repo_model.Repository, pusher *use
}
}
l, err := newCommit.CommitsBeforeLimit(10)
l, err := newCommit.CommitsBeforeLimit(gitRepo, 10)
if err != nil {
return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %w", err)
}
@@ -287,15 +287,15 @@ func pushNewBranch(ctx context.Context, repo *repo_model.Repository, pusher *use
return l, nil
}
func pushUpdateBranch(_ context.Context, repo *repo_model.Repository, pusher *user_model.User, opts *repo_module.PushUpdateOptions, newCommit *git.Commit) ([]*git.Commit, error) {
l, err := newCommit.CommitsBeforeUntil(git.RefNameFromCommit(opts.OldCommitID))
func pushUpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, pusher *user_model.User, opts *repo_module.PushUpdateOptions, newCommit *git.Commit) ([]*git.Commit, error) {
l, err := newCommit.CommitsBeforeUntil(gitRepo, git.RefNameFromCommit(opts.OldCommitID))
if err != nil {
return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %w", err)
}
branch := opts.RefFullName.BranchName()
isForcePush, err := newCommit.IsForcePush(opts.OldCommitID)
isForcePush, err := newCommit.IsForcePush(ctx, gitRepo, opts.OldCommitID)
if err != nil {
log.Error("IsForcePush %s:%s failed: %v", repo.FullName(), branch, err)
}