From 7dee037c07c1331c03d79e351c38086168d7cd8a Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 17:20:40 +0100 Subject: [PATCH 01/29] add context to GetMergeBase --- modules/git/repo_compare.go | 6 +++--- routers/api/v1/repo/pull.go | 10 +++++----- routers/web/repo/compare.go | 2 +- routers/web/repo/pull.go | 6 +++--- services/asymkey/sign.go | 2 +- services/issue/pull.go | 6 +++--- services/migrations/gitea_uploader.go | 2 +- services/pull/pull.go | 6 +++--- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index ff44506e13c2d..fce2b79676cdf 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -31,7 +31,7 @@ type CompareInfo struct { } // GetMergeBase checks and returns merge base of two branches and the reference used as base. -func (repo *Repository) GetMergeBase(tmpRemote, base, head string) (string, string, error) { +func (repo *Repository) GetMergeBase(ctx context.Context, tmpRemote, base, head string) (string, string, error) { if tmpRemote == "" { tmpRemote = "origin" } @@ -39,13 +39,13 @@ func (repo *Repository) GetMergeBase(tmpRemote, base, head string) (string, stri if tmpRemote != "origin" { tmpBaseName := RemotePrefix + tmpRemote + "/tmp_" + base // Fetch commit into a temporary branch in order to be able to handle commits and tags - _, _, err := NewCommand("fetch", "--no-tags").AddDynamicArguments(tmpRemote).AddDashesAndList(base+":"+tmpBaseName).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + _, _, err := NewCommand("fetch", "--no-tags").AddDynamicArguments(tmpRemote).AddDashesAndList(base+":"+tmpBaseName).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err == nil { base = tmpBaseName } } - stdout, _, err := NewCommand("merge-base").AddDashesAndList(base, head).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err := NewCommand("merge-base").AddDashesAndList(base, head).RunStdString(ctx, &RunOpts{Dir: repo.Path}) return strings.TrimSpace(stdout), base, err } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index f5d0e37c650c4..027e7043e9e50 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1187,7 +1187,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) return nil, nil } - compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseRef.ShortName(), headRef.ShortName(), false, false) + compareInfo, err := headGitRepo.GetCompareInfo(ctx, repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseRef.ShortName(), headRef.ShortName(), false, false) if err != nil { ctx.APIErrorInternal(err) return nil, nil @@ -1447,9 +1447,9 @@ func GetPullRequestCommits(ctx *context.APIContext) { defer closer.Close() if pr.HasMerged { - prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), false, false) + prInfo, err = baseGitRepo.GetCompareInfo(ctx, pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), false, false) } else { - prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), false, false) + prInfo, err = baseGitRepo.GetCompareInfo(ctx, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), false, false) } if err != nil { ctx.APIErrorInternal(err) @@ -1570,9 +1570,9 @@ func GetPullRequestFiles(ctx *context.APIContext) { var prInfo *git.CompareInfo if pr.HasMerged { - prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) + prInfo, err = baseGitRepo.GetCompareInfo(ctx, pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) } else { - prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true, false) + prInfo, err = baseGitRepo.GetCompareInfo(ctx, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true, false) } if err != nil { ctx.APIErrorInternal(err) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 6cea95e387a5a..3d2fd51ca8ab0 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -550,7 +550,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { headBranchRef = git.TagPrefix + ci.HeadBranch } - ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranchRef, headBranchRef, ci.DirectComparison, fileOnly) + ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(ctx, baseRepo.RepoPath(), baseBranchRef, headBranchRef, ci.DirectComparison, fileOnly) if err != nil { ctx.ServerError("GetCompareInfo", err) return nil diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e12798f93d200..0c5ef6c1f6c0f 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -272,7 +272,7 @@ func prepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue) baseCommit := GetMergedBaseCommitID(ctx, issue) - compareInfo, err := ctx.Repo.GitRepo.GetCompareInfo(ctx.Repo.Repository.RepoPath(), + compareInfo, err := ctx.Repo.GitRepo.GetCompareInfo(ctx, ctx.Repo.Repository.RepoPath(), baseCommit, pull.GetGitRefName(), false, false) if err != nil { if strings.Contains(err.Error(), "fatal: Not a valid object name") || strings.Contains(err.Error(), "unknown revision or path not in the working tree") { @@ -372,7 +372,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses) } - compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(), + compareInfo, err := baseGitRepo.GetCompareInfo(ctx, pull.BaseRepo.RepoPath(), pull.MergeBase, pull.GetGitRefName(), false, false) if err != nil { if strings.Contains(err.Error(), "fatal: Not a valid object name") { @@ -520,7 +520,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C } } - compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(), + compareInfo, err := baseGitRepo.GetCompareInfo(ctx, pull.BaseRepo.RepoPath(), git.BranchPrefix+pull.BaseBranch, pull.GetGitRefName(), false, false) if err != nil { if strings.Contains(err.Error(), "fatal: Not a valid object name") { diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index da265dec27ab6..aada6954cdf63 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -384,7 +384,7 @@ Loop: return false, "", nil, &ErrWontSign{commitsSigned} } // need to work out merge-base - mergeBaseCommit, _, err := gitRepo.GetMergeBase("", baseCommit, headCommit) + mergeBaseCommit, _, err := gitRepo.GetMergeBase(ctx, "", baseCommit, headCommit) if err != nil { return false, "", nil, err } diff --git a/services/issue/pull.go b/services/issue/pull.go index bd19c254362b9..5c549e44b9442 100644 --- a/services/issue/pull.go +++ b/services/issue/pull.go @@ -18,7 +18,7 @@ import ( "code.gitea.io/gitea/modules/setting" ) -func getMergeBase(repo *git.Repository, pr *issues_model.PullRequest, baseBranch, headBranch string) (string, error) { +func getMergeBase(ctx context.Context, repo *git.Repository, pr *issues_model.PullRequest, baseBranch, headBranch string) (string, error) { // Add a temporary remote tmpRemote := fmt.Sprintf("mergebase-%d-%d", pr.ID, time.Now().UnixNano()) if err := repo.AddRemote(tmpRemote, repo.Path, false); err != nil { @@ -30,7 +30,7 @@ func getMergeBase(repo *git.Repository, pr *issues_model.PullRequest, baseBranch } }() - mergeBase, _, err := repo.GetMergeBase(tmpRemote, baseBranch, headBranch) + mergeBase, _, err := repo.GetMergeBase(ctx, tmpRemote, baseBranch, headBranch) return mergeBase, err } @@ -102,7 +102,7 @@ func PullRequestCodeOwnersReviewSpecialCommits(ctx context.Context, pr *issues_m if startCommitID == "" && endCommitID == "" { // get the mergebase - mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName()) + mergeBase, err := getMergeBase(ctx, repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName()) if err != nil { return nil, err } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index b17cc3ce4160e..9423ed726af16 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -735,7 +735,7 @@ func (g *GiteaLocalUploader) newPullRequest(ctx context.Context, pr *base.PullRe if pr.Base.Ref != "" && pr.Head.SHA != "" { // A PR against a tag base does not make sense - therefore pr.Base.Ref must be a branch // TODO: should we be checking for the refs/heads/ prefix on the pr.Base.Ref? (i.e. are these actually branches or refs) - pr.Base.SHA, _, err = g.gitRepo.GetMergeBase("", git.BranchPrefix+pr.Base.Ref, pr.Head.SHA) + pr.Base.SHA, _, err = g.gitRepo.GetMergeBase(ctx, "", git.BranchPrefix+pr.Base.Ref, pr.Head.SHA) if err != nil { log.Error("Cannot determine the merge base for PR #%d in %s/%s. Error: %v", pr.Number, g.repoOwner, g.repoName, err) } diff --git a/services/pull/pull.go b/services/pull/pull.go index 4641d4ac40491..762b50adb57e7 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -142,7 +142,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error { return err } - compareInfo, err := baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), + compareInfo, err := baseGitRepo.GetCompareInfo(ctx, pr.BaseRepo.RepoPath(), git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName(), false, false) if err != nil { return err @@ -524,7 +524,7 @@ func checkIfPRContentChanged(ctx context.Context, pr *issues_model.PullRequest, defer tmpRepo.Close() // Find the merge-base - _, base, err := tmpRepo.GetMergeBase("", "base", "tracking") + _, base, err := tmpRepo.GetMergeBase(ctx, "", "base", "tracking") if err != nil { return false, fmt.Errorf("GetMergeBase: %w", err) } @@ -1088,7 +1088,7 @@ func GetPullCommits(ctx *gitea_context.Context, issue *issues_model.Issue) ([]Co if pull.HasMerged { baseBranch = pull.MergeBase } - prInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(), baseBranch, pull.GetGitRefName(), true, false) + prInfo, err := baseGitRepo.GetCompareInfo(ctx, pull.BaseRepo.RepoPath(), baseBranch, pull.GetGitRefName(), true, false) if err != nil { return nil, "", err } From 3631512779265002cd6e3087462e530c356012f3 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 18:46:20 +0100 Subject: [PATCH 02/29] add context to GetDiffBinary --- modules/git/repo_compare.go | 10 +++++----- services/pull/patch.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index fce2b79676cdf..5c3c68123f478 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -50,7 +50,7 @@ func (repo *Repository) GetMergeBase(ctx context.Context, tmpRemote, base, head } // GetCompareInfo generates and returns compare information between base and head branches of repositories. -func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, directComparison, fileOnly bool) (_ *CompareInfo, err error) { +func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch, headBranch string, directComparison, fileOnly bool) (_ *CompareInfo, err error) { var ( remoteBranch string tmpRemote string @@ -77,9 +77,9 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, compareInfo.HeadCommitID = headBranch } - compareInfo.MergeBase, remoteBranch, err = repo.GetMergeBase(tmpRemote, baseBranch, headBranch) + compareInfo.MergeBase, remoteBranch, err = repo.GetMergeBase(ctx, tmpRemote, baseBranch, headBranch) if err == nil { - compareInfo.BaseCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch) + compareInfo.BaseCommitID, err = GetFullCommitID(ctx, repo.Path, remoteBranch) if err != nil { compareInfo.BaseCommitID = remoteBranch } @@ -237,8 +237,8 @@ func (repo *Repository) GetDiff(compareArg string, w io.Writer) error { } // GetDiffBinary generates and returns patch data between given revisions, including binary diffs. -func (repo *Repository) GetDiffBinary(compareArg string, w io.Writer) error { - return NewCommand("diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(repo.Ctx, &RunOpts{ +func (repo *Repository) GetDiffBinary(ctx context.Context, compareArg string, w io.Writer) error { + return NewCommand("diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: w, }) diff --git a/services/pull/patch.go b/services/pull/patch.go index 29f2f992abf92..ca890d11285d0 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -46,7 +46,7 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io case patch: err = gitRepo.GetPatch(compareArg, w) case binary: - err = gitRepo.GetDiffBinary(compareArg, w) + err = gitRepo.GetDiffBinary(ctx, compareArg, w) default: err = gitRepo.GetDiff(compareArg, w) } @@ -364,7 +364,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo * _ = util.Remove(tmpPatchFile.Name()) }() - if err := gitRepo.GetDiffBinary(pr.MergeBase+"...tracking", tmpPatchFile); err != nil { + if err := gitRepo.GetDiffBinary(ctx, pr.MergeBase+"...tracking", tmpPatchFile); err != nil { tmpPatchFile.Close() log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) From 162899bb2078a69a69dd0e1c8f848223ccb380d6 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 18:49:05 +0100 Subject: [PATCH 03/29] add context to GetPatch modernize test --- modules/git/repo_compare.go | 4 ++-- modules/git/repo_compare_test.go | 18 +++++------------- services/pull/patch.go | 2 +- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 5c3c68123f478..117b8086d3602 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -245,10 +245,10 @@ func (repo *Repository) GetDiffBinary(ctx context.Context, compareArg string, w } // GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply` -func (repo *Repository) GetPatch(compareArg string, w io.Writer) error { +func (repo *Repository) GetPatch(ctx context.Context, compareArg string, w io.Writer) error { stderr := new(bytes.Buffer) return NewCommand("format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg). - Run(repo.Ctx, &RunOpts{ + Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: w, Stderr: stderr, diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index 25ee4c5198568..c815ac6abc8d4 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetFormatPatch(t *testing.T) { @@ -21,24 +22,15 @@ func TestGetFormatPatch(t *testing.T) { } repo, err := openRepositoryWithDefaultContext(clonedPath) - if err != nil { - assert.NoError(t, err) - return - } + require.NoError(t, err) defer repo.Close() rd := &bytes.Buffer{} - err = repo.GetPatch("8d92fc95^...8d92fc95", rd) - if err != nil { - assert.NoError(t, err) - return - } + err = repo.GetPatch(t.Context(), "8d92fc95^...8d92fc95", rd) + require.NoError(t, err) patchb, err := io.ReadAll(rd) - if err != nil { - assert.NoError(t, err) - return - } + require.NoError(t, err) patch := string(patchb) assert.Regexp(t, "^From 8d92fc95", patch) diff --git a/services/pull/patch.go b/services/pull/patch.go index ca890d11285d0..53ff7e58171aa 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -44,7 +44,7 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io compareArg := pr.MergeBase + "..." + pr.GetGitRefName() switch { case patch: - err = gitRepo.GetPatch(compareArg, w) + err = gitRepo.GetPatch(ctx, compareArg, w) case binary: err = gitRepo.GetDiffBinary(ctx, compareArg, w) default: From 0099c8223ad60124bd2f6e9184ca6497edb28f41 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 18:52:46 +0100 Subject: [PATCH 04/29] use function provided context in GetCompareInfo --- modules/git/repo_compare.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 117b8086d3602..9fcabc985a1f5 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -72,7 +72,7 @@ func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch compareInfo := new(CompareInfo) - compareInfo.HeadCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, headBranch) + compareInfo.HeadCommitID, err = GetFullCommitID(ctx, repo.Path, headBranch) if err != nil { compareInfo.HeadCommitID = headBranch } @@ -96,7 +96,7 @@ func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch var logs []byte logs, _, err = NewCommand("log").AddArguments(prettyLogFormat). AddDynamicArguments(baseCommitID+separator+headBranch).AddArguments("--"). - RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } @@ -109,7 +109,7 @@ func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch } } else { compareInfo.Commits = []*Commit{} - compareInfo.MergeBase, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch) + compareInfo.MergeBase, err = GetFullCommitID(ctx, repo.Path, remoteBranch) if err != nil { compareInfo.MergeBase = remoteBranch } From 34153fdd36e5f7fd82be66b970575949eb76f594 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 19:27:55 +0100 Subject: [PATCH 05/29] add context to GetCommitByPath CommitsCountBetween is dead code --- modules/git/commit.go | 6 +++--- modules/git/last_commit_cache.go | 5 +++-- modules/git/repo_commit.go | 11 ++++++----- routers/web/repo/view_file.go | 2 +- services/repository/files/content.go | 2 +- tests/integration/api_repo_get_contents_list_test.go | 4 ++-- tests/integration/api_repo_get_contents_test.go | 4 ++-- tests/integration/repofiles_change_test.go | 6 +++--- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 3e790e89d92d1..051495b69a3b7 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -77,11 +77,11 @@ func (c *Commit) ParentCount() int { } // GetCommitByPath return the commit of relative path object. -func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) { +func (c *Commit) GetCommitByPath(ctx context.Context, relpath string) (*Commit, error) { if c.repo.LastCommitCache != nil { - return c.repo.LastCommitCache.GetCommitByPath(c.ID.String(), relpath) + return c.repo.LastCommitCache.GetCommitByPath(ctx, c.ID.String(), relpath) } - return c.repo.getCommitByPathWithID(c.ID, relpath) + return c.repo.getCommitByPathWithID(ctx, c.ID, relpath) } // AddChanges marks local changes to be ready for commit. diff --git a/modules/git/last_commit_cache.go b/modules/git/last_commit_cache.go index cf9c10d7b468e..f8d8bab81c339 100644 --- a/modules/git/last_commit_cache.go +++ b/modules/git/last_commit_cache.go @@ -4,6 +4,7 @@ package git import ( + "context" "crypto/sha256" "fmt" @@ -83,7 +84,7 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) { } // GetCommitByPath gets the last commit for the entry in the provided commit -func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, error) { +func (c *LastCommitCache) GetCommitByPath(ctx context.Context, commitID, entryPath string) (*Commit, error) { sha, err := NewIDFromString(commitID) if err != nil { return nil, err @@ -94,7 +95,7 @@ func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, return lastCommit, err } - lastCommit, err = c.repo.getCommitByPathWithID(sha, entryPath) + lastCommit, err = c.repo.getCommitByPathWithID(ctx, sha, entryPath) if err != nil { return nil, err } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 72f35711f0fd6..cd0566e858a8e 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -6,6 +6,7 @@ package git import ( "bytes" + "context" "io" "os" "strconv" @@ -53,13 +54,13 @@ func (repo *Repository) GetTagCommit(name string) (*Commit, error) { return repo.GetCommit(commitID) } -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 relpath[0] == ':' { relpath = `\` + relpath } - stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -375,8 +376,8 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error } // CommitsCountBetween return numbers of commits between two commits -func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { - count, err := CommitsCount(repo.Ctx, CommitsCountOptions{ +func (repo *Repository) CommitsCountBetween(ctx context.Context, start, end string) (int64, error) { + count, err := CommitsCount(ctx, CommitsCountOptions{ RepoPath: repo.Path, Revision: []string{start + ".." + end}, }) @@ -384,7 +385,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { if err != nil && strings.Contains(err.Error(), "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... - return CommitsCount(repo.Ctx, CommitsCountOptions{ + return CommitsCount(ctx, CommitsCountOptions{ RepoPath: repo.Path, Revision: []string{start, end}, }) diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 4ce7a8e3a4480..6ed21fbda2c8e 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -46,7 +46,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["FileName"] = blob.Name() ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) - commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath) + commit, err := ctx.Repo.Commit.GetCommitByPath(ctx, ctx.Repo.TreePath) if err != nil { ctx.ServerError("GetCommitByPath", err) return diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 0ab7422ce2ebf..b07bee8754775 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -170,7 +170,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref return nil, err } - lastCommit, err := commit.GetCommitByPath(treePath) + lastCommit, err := commit.GetCommitByPath(ctx, treePath) if err != nil { return nil, err } diff --git a/tests/integration/api_repo_get_contents_list_test.go b/tests/integration/api_repo_get_contents_list_test.go index 1ba74490a30da..1ebbf44eb1148 100644 --- a/tests/integration/api_repo_get_contents_list_test.go +++ b/tests/integration/api_repo_get_contents_list_test.go @@ -120,7 +120,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { assert.NotNil(t, contentsListResponse) branchCommit, err := gitRepo.GetBranchCommit(ref) assert.NoError(t, err) - lastCommit, err = branchCommit.GetCommitByPath("README.md") + lastCommit, err = branchCommit.GetCommitByPath(t.Context(), "README.md") assert.NoError(t, err) expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) @@ -134,7 +134,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { assert.NotNil(t, contentsListResponse) tagCommit, err := gitRepo.GetTagCommit(ref) assert.NoError(t, err) - lastCommit, err = tagCommit.GetCommitByPath("README.md") + lastCommit, err = tagCommit.GetCommitByPath(t.Context(), "README.md") assert.NoError(t, err) expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) diff --git a/tests/integration/api_repo_get_contents_test.go b/tests/integration/api_repo_get_contents_test.go index d0f61da0c0402..e8f49cd6e4bca 100644 --- a/tests/integration/api_repo_get_contents_test.go +++ b/tests/integration/api_repo_get_contents_test.go @@ -121,7 +121,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { DecodeJSON(t, resp, &contentsResponse) assert.NotNil(t, contentsResponse) branchCommit, _ := gitRepo.GetBranchCommit(ref) - lastCommit, _ = branchCommit.GetCommitByPath("README.md") + lastCommit, _ = branchCommit.GetCommitByPath(t.Context(), "README.md") expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, *expectedContentsResponse, contentsResponse) @@ -133,7 +133,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { DecodeJSON(t, resp, &contentsResponse) assert.NotNil(t, contentsResponse) tagCommit, _ := gitRepo.GetTagCommit(ref) - lastCommit, _ = tagCommit.GetCommitByPath("README.md") + lastCommit, _ = tagCommit.GetCommitByPath(t.Context(), "README.md") expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, *expectedContentsResponse, contentsResponse) diff --git a/tests/integration/repofiles_change_test.go b/tests/integration/repofiles_change_test.go index ef520a2e51ce1..950d605367a88 100644 --- a/tests/integration/repofiles_change_test.go +++ b/tests/integration/repofiles_change_test.go @@ -304,7 +304,7 @@ func TestChangeRepoFilesForUpdate(t *testing.T) { defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) + lastCommit, _ := commit.GetCommitByPath(t.Context(), opts.Files[0].TreePath) expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) @@ -340,7 +340,7 @@ func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) + lastCommit, _ := commit.GetCommitByPath(t.Context(), opts.Files[0].TreePath) expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) // assert that the old file no longer exists in the last commit of the branch fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath) @@ -391,7 +391,7 @@ func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch) - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) + lastCommit, _ := commit.GetCommitByPath(git.DefaultContext, opts.Files[0].TreePath) expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) }) From dfaff77574397f59eaff8097141470df792a5fc0 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 19:31:52 +0100 Subject: [PATCH 06/29] add context to CommitBetweenLimit --- modules/git/repo_commit.go | 12 ++++++------ routers/api/v1/repo/wiki.go | 4 ++-- routers/web/repo/wiki.go | 4 ++-- services/pull/pull.go | 4 ++-- tests/integration/api_repo_file_create_test.go | 4 ++-- tests/integration/api_repo_file_update_test.go | 2 +- tests/integration/api_repo_files_change_test.go | 4 ++-- tests/integration/api_repo_get_contents_list_test.go | 2 +- tests/integration/api_repo_get_contents_test.go | 2 +- tests/integration/editor_test.go | 4 ++-- tests/integration/repofiles_change_test.go | 2 +- 11 files changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index cd0566e858a8e..af3c8c9c02a8b 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -74,8 +74,8 @@ func (repo *Repository) getCommitByPathWithID(ctx context.Context, id ObjectID, } // GetCommitByPath returns the last commit of relative path. -func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { - stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDashesAndList(relpath).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) GetCommitByPath(ctx context.Context, relpath string) (*Commit, error) { + stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDashesAndList(relpath).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -310,26 +310,26 @@ func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) } // CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last) -func (repo *Repository) CommitsBetweenLimit(last, before *Commit, limit, skip int) ([]*Commit, error) { +func (repo *Repository) CommitsBetweenLimit(ctx context.Context, last, before *Commit, limit, skip int) ([]*Commit, error) { var stdout []byte var err error if before == nil { stdout, _, err = NewCommand("rev-list"). AddOptionValues("--max-count", strconv.Itoa(limit)). AddOptionValues("--skip", strconv.Itoa(skip)). - AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + AddDynamicArguments(last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } else { stdout, _, err = NewCommand("rev-list"). AddOptionValues("--max-count", strconv.Itoa(limit)). AddOptionValues("--skip", strconv.Itoa(skip)). - AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil && strings.Contains(err.Error(), "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 --max-count n before last so let's try that... stdout, _, err = NewCommand("rev-list"). AddOptionValues("--max-count", strconv.Itoa(limit)). AddOptionValues("--skip", strconv.Itoa(skip)). - AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } } if err != nil { diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 8d73383f76573..808df4fd5e94d 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -196,7 +196,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) // Get last change information. - lastCommit, err := wikiRepo.GetCommitByPath(pageFilename) + lastCommit, err := wikiRepo.GetCommitByPath(ctx, pageFilename) if err != nil { ctx.APIErrorInternal(err) return nil @@ -320,7 +320,7 @@ func ListWikiPages(ctx *context.APIContext) { if i < skip || i >= maxNum || !entry.IsRegular() { continue } - c, err := wikiRepo.GetCommitByPath(entry.Name()) + c, err := wikiRepo.GetCommitByPath(ctx, entry.Name()) if err != nil { ctx.APIErrorInternal(err) return diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 0f8e1223c60e8..d6c3fa5b5606e 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -584,7 +584,7 @@ func Wiki(ctx *context.Context) { ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext) } // Get last change information. - lastCommit, err := wikiRepo.GetCommitByPath(wikiPath) + lastCommit, err := wikiRepo.GetCommitByPath(ctx, wikiPath) if err != nil { ctx.ServerError("GetCommitByPath", err) return @@ -622,7 +622,7 @@ func WikiRevision(ctx *context.Context) { // Get last change information. wikiPath := entry.Name() - lastCommit, err := wikiRepo.GetCommitByPath(wikiPath) + lastCommit, err := wikiRepo.GetCommitByPath(ctx, wikiPath) if err != nil { ctx.ServerError("GetCommitByPath", err) return diff --git a/services/pull/pull.go b/services/pull/pull.go index 762b50adb57e7..1f8fc480f5a11 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -841,7 +841,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ limit := setting.Repository.PullRequest.DefaultMergeMessageCommitsLimit - commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0) + commits, err := gitRepo.CommitsBetweenLimit(ctx, headCommit, mergeBase, limit, 0) if err != nil { log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err) return "" @@ -911,7 +911,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ skip := limit limit = 30 for { - commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip) + commits, err := gitRepo.CommitsBetweenLimit(ctx, headCommit, mergeBase, limit, skip) if err != nil { log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err) return "" diff --git a/tests/integration/api_repo_file_create_test.go b/tests/integration/api_repo_file_create_test.go index 2bf4a81280ef5..c588ee55aef0d 100644 --- a/tests/integration/api_repo_file_create_test.go +++ b/tests/integration/api_repo_file_create_test.go @@ -168,7 +168,7 @@ func TestAPICreateFile(t *testing.T) { resp := MakeRequest(t, req, http.StatusCreated) gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) - latestCommit, _ := gitRepo.GetCommitByPath(treePath) + latestCommit, _ := gitRepo.GetCommitByPath(t.Context(), treePath) expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String()) var fileResponse api.FileResponse DecodeJSON(t, resp, &fileResponse) @@ -286,7 +286,7 @@ func TestAPICreateFile(t *testing.T) { emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo gitRepo, _ := gitrepo.OpenRepository(t.Context(), emptyRepo) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) - latestCommit, _ := gitRepo.GetCommitByPath(treePath) + latestCommit, _ := gitRepo.GetCommitByPath(t.Context(), treePath) expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String()) DecodeJSON(t, resp, &fileResponse) assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) diff --git a/tests/integration/api_repo_file_update_test.go b/tests/integration/api_repo_file_update_test.go index c8ce94a3f59a0..4df259b1e974e 100644 --- a/tests/integration/api_repo_file_update_test.go +++ b/tests/integration/api_repo_file_update_test.go @@ -136,7 +136,7 @@ func TestAPIUpdateFile(t *testing.T) { resp := MakeRequest(t, req, http.StatusOK) gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1) commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName) - lasCommit, _ := gitRepo.GetCommitByPath(treePath) + lasCommit, _ := gitRepo.GetCommitByPath(t.Context(), treePath) expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String()) var fileResponse api.FileResponse DecodeJSON(t, resp, &fileResponse) diff --git a/tests/integration/api_repo_files_change_test.go b/tests/integration/api_repo_files_change_test.go index aca58025d2474..2e84f91c0a740 100644 --- a/tests/integration/api_repo_files_change_test.go +++ b/tests/integration/api_repo_files_change_test.go @@ -97,8 +97,8 @@ func TestAPIChangeFiles(t *testing.T) { resp := MakeRequest(t, req, http.StatusCreated) gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1) commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName) - createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath) - updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath) + createLasCommit, _ := gitRepo.GetCommitByPath(t.Context(), createTreePath) + updateLastCommit, _ := gitRepo.GetCommitByPath(t.Context(), updateTreePath) expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String()) expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String()) var filesResponse api.FilesResponse diff --git a/tests/integration/api_repo_get_contents_list_test.go b/tests/integration/api_repo_get_contents_list_test.go index 1ebbf44eb1148..b8890bcf98157 100644 --- a/tests/integration/api_repo_get_contents_list_test.go +++ b/tests/integration/api_repo_get_contents_list_test.go @@ -96,7 +96,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { var contentsListResponse []*api.ContentsResponse DecodeJSON(t, resp, &contentsListResponse) assert.NotNil(t, contentsListResponse) - lastCommit, err := gitRepo.GetCommitByPath("README.md") + lastCommit, err := gitRepo.GetCommitByPath(t.Context(), "README.md") assert.NoError(t, err) expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) diff --git a/tests/integration/api_repo_get_contents_test.go b/tests/integration/api_repo_get_contents_test.go index e8f49cd6e4bca..293d656d61b9d 100644 --- a/tests/integration/api_repo_get_contents_test.go +++ b/tests/integration/api_repo_get_contents_test.go @@ -100,7 +100,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { var contentsResponse api.ContentsResponse DecodeJSON(t, resp, &contentsResponse) assert.NotNil(t, contentsResponse) - lastCommit, _ := gitRepo.GetCommitByPath("README.md") + lastCommit, _ := gitRepo.GetCommitByPath(git.DefaultContext, "README.md") expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String()) assert.EqualValues(t, *expectedContentsResponse, contentsResponse) diff --git a/tests/integration/editor_test.go b/tests/integration/editor_test.go index fa58b8df425ce..311825f4eb5ec 100644 --- a/tests/integration/editor_test.go +++ b/tests/integration/editor_test.go @@ -322,9 +322,9 @@ index 0000000000..bbbbbbbbbb }, ) - commit1, err := gitRepo.GetCommitByPath("patch-file-1.txt") + commit1, err := gitRepo.GetCommitByPath(t.Context(), "patch-file-1.txt") require.NoError(t, err) - commit2, err := gitRepo.GetCommitByPath("patch-file-2.txt") + commit2, err := gitRepo.GetCommitByPath(t.Context(), "patch-file-2.txt") require.NoError(t, err) resp1, _ := testWebGit(t, "/user2/repo1/_cherrypick/"+commit1.ID.String()+"/master", map[string]string{"revert": "true"}, diff --git a/tests/integration/repofiles_change_test.go b/tests/integration/repofiles_change_test.go index 950d605367a88..4eca90f59a1f6 100644 --- a/tests/integration/repofiles_change_test.go +++ b/tests/integration/repofiles_change_test.go @@ -267,7 +267,7 @@ func TestChangeRepoFilesForCreate(t *testing.T) { defer gitRepo.Close() commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch) - lastCommit, _ := gitRepo.GetCommitByPath("new/file.txt") + lastCommit, _ := gitRepo.GetCommitByPath(ctx, "new/file.txt") expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String()) assert.NotNil(t, expectedFileResponse) if expectedFileResponse != nil { From 44a6fdec8fc730e0fa5a315590757015e9c0dbe9 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 19:53:53 +0100 Subject: [PATCH 07/29] add context to commitsBefore --- modules/git/commit.go | 8 ++++---- modules/git/commit_test.go | 2 +- modules/git/diff.go | 6 +++--- modules/git/repo_commit.go | 26 +++++++++++++------------- modules/git/repo_commit_test.go | 2 +- routers/private/hook_pre_receive.go | 4 ++-- services/pull/patch.go | 10 +++++----- services/repository/push.go | 2 +- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 051495b69a3b7..b58bc1612ffe9 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -204,8 +204,8 @@ func (c *Commit) CommitsByRange(page, pageSize int, not string) ([]*Commit, erro } // CommitsBefore returns all the commits before current revision -func (c *Commit) CommitsBefore() ([]*Commit, error) { - return c.repo.getCommitsBefore(c.ID) +func (c *Commit) CommitsBefore(ctx context.Context) ([]*Commit, error) { + return c.repo.getCommitsBefore(ctx, c.ID) } // HasPreviousCommit returns true if a given commitHash is contained in commit's parents @@ -249,8 +249,8 @@ func (c *Commit) IsForcePush(oldCommitID string) (bool, error) { } // CommitsBeforeLimit returns num commits before current revision -func (c *Commit) CommitsBeforeLimit(num int) ([]*Commit, error) { - return c.repo.getCommitsBeforeLimit(c.ID, num) +func (c *Commit) CommitsBeforeLimit(ctx context.Context, num int) ([]*Commit, error) { + return c.repo.getCommitsBeforeLimit(ctx, c.ID, num) } // CommitsBeforeUntil returns the commits between commitID to current revision diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 5319e09bb74cd..9d48c600c6e2d 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -353,7 +353,7 @@ func Test_GetCommitBranchStart(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, "2839944139e0de9737a044f78b0e4b40d989a9e3", commit.ID.String()) - startCommitID, err := repo.GetCommitBranchStart(os.Environ(), "branch1", commit.ID.String()) + startCommitID, err := repo.GetCommitBranchStart(t.Context(), os.Environ(), "branch1", commit.ID.String()) assert.NoError(t, err) assert.NotEmpty(t, startCommitID) assert.EqualValues(t, "95bb4d39648ee7e325106df01a621c530863a653", startCommitID) diff --git a/modules/git/diff.go b/modules/git/diff.go index c4df6b80633e1..fe7ee4d4cbc89 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -277,9 +277,9 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi } // GetAffectedFiles returns the affected files between two commits -func GetAffectedFiles(repo *Repository, branchName, oldCommitID, newCommitID string, env []string) ([]string, error) { +func GetAffectedFiles(ctx context.Context, repo *Repository, branchName, oldCommitID, newCommitID string, env []string) ([]string, error) { if oldCommitID == emptySha1ObjectID.String() || oldCommitID == emptySha256ObjectID.String() { - startCommitID, err := repo.GetCommitBranchStart(env, branchName, newCommitID) + startCommitID, err := repo.GetCommitBranchStart(ctx, env, branchName, newCommitID) if err != nil { return nil, err } @@ -302,7 +302,7 @@ func GetAffectedFiles(repo *Repository, branchName, oldCommitID, newCommitID str // Run `git diff --name-only` to get the names of the changed files err = NewCommand("diff", "--name-only").AddDynamicArguments(oldCommitID, newCommitID). - Run(repo.Ctx, &RunOpts{ + Run(ctx, &RunOpts{ Env: env, Dir: repo.Path, Stdout: stdoutWriter, diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index af3c8c9c02a8b..c77726a8aafbe 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -395,14 +395,14 @@ func (repo *Repository) CommitsCountBetween(ctx context.Context, start, end stri } // 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 := NewCommand("log", prettyLogFormat) if limit > 0 { cmd.AddOptionFormat("-%d", limit) } cmd.AddDynamicArguments(id.String()) - stdout, _, runErr := cmd.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, runErr := cmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -414,7 +414,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error) commits := make([]*Commit, 0, len(formattedLog)) for _, commit := range formattedLog { - branches, err := repo.getBranches(os.Environ(), commit.ID.String(), 2) + branches, err := repo.getBranches(ctx, os.Environ(), commit.ID.String(), 2) if err != nil { return nil, err } @@ -429,20 +429,20 @@ 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) { if DefaultFeatures().CheckVersionAtLeast("2.7.0") { stdout, _, err := NewCommand("for-each-ref", "--format=%(refname:strip=2)"). AddOptionFormat("--count=%d", limit). AddOptionValues("--contains", commitID, BranchPrefix). - RunStdString(repo.Ctx, &RunOpts{ + RunStdString(ctx, &RunOpts{ Dir: repo.Path, Env: env, }) @@ -454,7 +454,7 @@ func (repo *Repository) getBranches(env []string, commitID string, limit int) ([ return branches, nil } - stdout, _, err := NewCommand("branch").AddOptionValues("--contains", commitID).RunStdString(repo.Ctx, &RunOpts{ + stdout, _, err := NewCommand("branch").AddOptionValues("--contains", commitID).RunStdString(ctx, &RunOpts{ Dir: repo.Path, Env: env, }) @@ -521,11 +521,11 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error } // 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 := NewCommand("log", prettyLogFormat) cmd.AddDynamicArguments(endCommitID) - stdout, _, runErr := cmd.RunStdBytes(repo.Ctx, &RunOpts{ + stdout, _, runErr := cmd.RunStdBytes(ctx, &RunOpts{ Dir: repo.Path, Env: env, }) @@ -538,7 +538,7 @@ func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID s // check the commits one by one until we find a commit contained by another branch // and we think this commit is the divergence point for _, commitID := range parts { - branches, err := repo.getBranches(env, string(commitID), 2) + branches, err := repo.getBranches(ctx, env, string(commitID), 2) if err != nil { return "", err } diff --git a/modules/git/repo_commit_test.go b/modules/git/repo_commit_test.go index e9f469accdf0c..a4491cad79446 100644 --- a/modules/git/repo_commit_test.go +++ b/modules/git/repo_commit_test.go @@ -36,7 +36,7 @@ func TestRepository_GetCommitBranches(t *testing.T) { for _, testCase := range testCases { commit, err := bareRepo1.GetCommit(testCase.CommitID) assert.NoError(t, err) - branches, err := bareRepo1.getBranches(os.Environ(), commit.ID.String(), 2) + branches, err := bareRepo1.getBranches(t.Context(), os.Environ(), commit.ID.String(), 2) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedBranches, branches) } diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index ae23abc54237e..8e0e51335df35 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -235,7 +235,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r globs := protectBranch.GetProtectedFilePatterns() if len(globs) > 0 { - _, err := pull_service.CheckFileProtection(gitRepo, branchName, oldCommitID, newCommitID, globs, 1, ctx.env) + _, err := pull_service.CheckFileProtection(ctx, gitRepo, branchName, oldCommitID, newCommitID, globs, 1, ctx.env) if err != nil { if !pull_service.IsErrFilePathProtected(err) { log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) @@ -293,7 +293,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r // Allow commits that only touch unprotected files globs := protectBranch.GetUnprotectedFilePatterns() if len(globs) > 0 { - unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env) + unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(ctx, gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env) if err != nil { log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/services/pull/patch.go b/services/pull/patch.go index 53ff7e58171aa..b30b062d4239b 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -535,11 +535,11 @@ func (err ErrFilePathProtected) Unwrap() error { } // CheckFileProtection check file Protection -func CheckFileProtection(repo *git.Repository, branchName, oldCommitID, newCommitID string, patterns []glob.Glob, limit int, env []string) ([]string, error) { +func CheckFileProtection(ctx context.Context, repo *git.Repository, branchName, oldCommitID, newCommitID string, patterns []glob.Glob, limit int, env []string) ([]string, error) { if len(patterns) == 0 { return nil, nil } - affectedFiles, err := git.GetAffectedFiles(repo, branchName, oldCommitID, newCommitID, env) + affectedFiles, err := git.GetAffectedFiles(ctx, repo, branchName, oldCommitID, newCommitID, env) if err != nil { return nil, err } @@ -565,11 +565,11 @@ func CheckFileProtection(repo *git.Repository, branchName, oldCommitID, newCommi } // CheckUnprotectedFiles check if the commit only touches unprotected files -func CheckUnprotectedFiles(repo *git.Repository, branchName, oldCommitID, newCommitID string, patterns []glob.Glob, env []string) (bool, error) { +func CheckUnprotectedFiles(ctx context.Context, repo *git.Repository, branchName, oldCommitID, newCommitID string, patterns []glob.Glob, env []string) (bool, error) { if len(patterns) == 0 { return false, nil } - affectedFiles, err := git.GetAffectedFiles(repo, branchName, oldCommitID, newCommitID, env) + affectedFiles, err := git.GetAffectedFiles(ctx, repo, branchName, oldCommitID, newCommitID, env) if err != nil { return false, err } @@ -606,7 +606,7 @@ func checkPullFilesProtection(ctx context.Context, pr *issues_model.PullRequest, return nil } - pr.ChangedProtectedFiles, err = CheckFileProtection(gitRepo, pr.HeadBranch, pr.MergeBase, "tracking", pb.GetProtectedFilePatterns(), 10, os.Environ()) + pr.ChangedProtectedFiles, err = CheckFileProtection(ctx, gitRepo, pr.HeadBranch, pr.MergeBase, "tracking", pb.GetProtectedFilePatterns(), 10, os.Environ()) if err != nil && !IsErrFilePathProtected(err) { return err } diff --git a/services/repository/push.go b/services/repository/push.go index c40333f0a8b76..a71f279770181 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -195,7 +195,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } - l, err = newCommit.CommitsBeforeLimit(10) + l, err = newCommit.CommitsBeforeLimit(ctx, 10) if err != nil { return fmt.Errorf("newCommit.CommitsBeforeLimit: %w", err) } From 7fbf350d57e3d56e8dff9ccd359f844117d2258d Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 19:59:20 +0100 Subject: [PATCH 08/29] add context to commitsByRange add context to commitsBetweenNotBase --- modules/git/commit.go | 4 ++-- modules/git/repo_commit.go | 12 ++++++------ routers/api/v1/repo/commits.go | 2 +- routers/web/feed/branch.go | 2 +- routers/web/repo/commit.go | 2 +- services/pull/comment.go | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index b58bc1612ffe9..aed9661438eeb 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -199,8 +199,8 @@ func (c *Commit) CommitsCount() (int64, error) { } // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize -func (c *Commit) CommitsByRange(page, pageSize int, not string) ([]*Commit, error) { - return c.repo.commitsByRange(c.ID, page, pageSize, not) +func (c *Commit) CommitsByRange(ctx context.Context, page, pageSize int, not string) ([]*Commit, error) { + return c.repo.commitsByRange(ctx, c.ID, page, pageSize, not) } // CommitsBefore returns all the commits before current revision diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index c77726a8aafbe..1e09b5683819d 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -90,7 +90,7 @@ func (repo *Repository) GetCommitByPath(ctx context.Context, relpath string) (*C return commits[0], nil } -func (repo *Repository) commitsByRange(id ObjectID, page, pageSize int, not string) ([]*Commit, error) { +func (repo *Repository) commitsByRange(ctx context.Context, id ObjectID, page, pageSize int, not string) ([]*Commit, error) { cmd := NewCommand("log"). AddOptionFormat("--skip=%d", (page-1)*pageSize). AddOptionFormat("--max-count=%d", pageSize). @@ -101,7 +101,7 @@ func (repo *Repository) commitsByRange(id ObjectID, page, pageSize int, not stri cmd.AddOptionValues("--not", not) } - stdout, _, err := cmd.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err := cmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } @@ -340,17 +340,17 @@ func (repo *Repository) CommitsBetweenLimit(ctx context.Context, last, before *C // CommitsBetweenNotBase returns a list that contains commits between [before, last), excluding commits in baseBranch. // If before is detached (removed by reset + push) it is not included. -func (repo *Repository) CommitsBetweenNotBase(last, before *Commit, baseBranch string) ([]*Commit, error) { +func (repo *Repository) CommitsBetweenNotBase(ctx context.Context, last, before *Commit, baseBranch string) ([]*Commit, error) { var stdout []byte var err error if before == nil { - stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } else { - stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil && strings.Contains(err.Error(), "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 = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } } if err != nil { diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 03489d777b0d8..3cefccfb24947 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -212,7 +212,7 @@ func GetAllCommits(ctx *context.APIContext) { } // Query commits - commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not) + commits, err = baseCommit.CommitsByRange(ctx, listOptions.Page, listOptions.PageSize, not) if err != nil { ctx.APIErrorInternal(err) return diff --git a/routers/web/feed/branch.go b/routers/web/feed/branch.go index d3dae9503e843..b774c30ad9d72 100644 --- a/routers/web/feed/branch.go +++ b/routers/web/feed/branch.go @@ -16,7 +16,7 @@ import ( // ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) { - commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "") + commits, err := ctx.Repo.Commit.CommitsByRange(ctx, 0, 10, "") if err != nil { ctx.ServerError("ShowBranchFeed", err) return diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index bbdcf9875eee8..aca2b7d752790 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -77,7 +77,7 @@ func Commits(ctx *context.Context) { } // Both `git log branchName` and `git log commitId` work. - commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize, "") + commits, err := ctx.Repo.Commit.CommitsByRange(ctx, page, pageSize, "") if err != nil { ctx.ServerError("CommitsByRange", err) return diff --git a/services/pull/comment.go b/services/pull/comment.go index 53587d4f542be..d0bbb30c397e0 100644 --- a/services/pull/comment.go +++ b/services/pull/comment.go @@ -47,7 +47,7 @@ func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldC } // Find commits between new and old commit excluding base branch commits - commits, err := gitRepo.CommitsBetweenNotBase(newCommit, oldCommit, baseBranch) + commits, err := gitRepo.CommitsBetweenNotBase(ctx, newCommit, oldCommit, baseBranch) if err != nil { return nil, false, err } From 3ad5ee2a03a19edf32794ebf5143ffc2fee3329d Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:03:43 +0100 Subject: [PATCH 09/29] add contect to GetDiff and SearchCommits --- modules/git/commit.go | 4 ++-- modules/git/repo_commit.go | 6 +++--- modules/git/repo_compare.go | 4 ++-- routers/web/repo/commit.go | 2 +- services/pull/patch.go | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index aed9661438eeb..3b4b92872aed1 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -302,8 +302,8 @@ func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommits } // SearchCommits returns the commits match the keyword before current revision -func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { - return c.repo.searchCommits(c.ID, opts) +func (c *Commit) SearchCommits(ctx context.Context, opts SearchCommitsOptions) ([]*Commit, error) { + return c.repo.searchCommits(ctx, c.ID, opts) } // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 1e09b5683819d..4264807eebd28 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -109,7 +109,7 @@ func (repo *Repository) commitsByRange(ctx context.Context, id ObjectID, page, p return repo.parsePrettyFormatLogToList(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 *Command) { // ignore case @@ -155,7 +155,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.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err := cmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } @@ -176,7 +176,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.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + hashMatching, _, err := hashCmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil || bytes.Contains(stdout, hashMatching) { continue } diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 9fcabc985a1f5..d0ba096509baf 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -226,10 +226,10 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, } // GetDiff generates and returns patch data between given revisions, optimized for human readability -func (repo *Repository) GetDiff(compareArg string, w io.Writer) error { +func (repo *Repository) GetDiff(ctx context.Context, compareArg string, w io.Writer) error { stderr := new(bytes.Buffer) return NewCommand("diff", "-p").AddDynamicArguments(compareArg). - Run(repo.Ctx, &RunOpts{ + Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: w, Stderr: stderr, diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index aca2b7d752790..8c67c638333a7 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -192,7 +192,7 @@ func SearchCommits(ctx *context.Context) { all := ctx.FormBool("all") opts := git.NewSearchCommitsOptions(query, all) - commits, err := ctx.Repo.Commit.SearchCommits(opts) + commits, err := ctx.Repo.Commit.SearchCommits(ctx, opts) if err != nil { ctx.ServerError("SearchCommits", err) return diff --git a/services/pull/patch.go b/services/pull/patch.go index b30b062d4239b..860cd341d5fcc 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -48,7 +48,7 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io case binary: err = gitRepo.GetDiffBinary(ctx, compareArg, w) default: - err = gitRepo.GetDiff(compareArg, w) + err = gitRepo.GetDiff(ctx, compareArg, w) } if err != nil { From 30767c315e1ba5036b8ff291acd1bcc146ee1cd2 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:13:15 +0100 Subject: [PATCH 10/29] add context to FileCommitsCount and FilesCountBetween --- modules/git/repo_commit.go | 10 +++++----- routers/api/v1/repo/wiki.go | 4 ++-- routers/web/repo/commit.go | 2 +- routers/web/repo/wiki.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 4264807eebd28..a0ee3a0286532 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -199,8 +199,8 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo } // FileCommitsCount return the number of files at a revision -func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) { - return CommitsCount(repo.Ctx, +func (repo *Repository) FileCommitsCount(ctx context.Context, revision, file string) (int64, error) { + return CommitsCount(ctx, CommitsCountOptions{ RepoPath: repo.Path, Revision: []string{revision}, @@ -275,12 +275,12 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) } // FilesCountBetween return the number of files changed between two commits -func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) { - stdout, _, err := NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID+"..."+endCommitID).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) FilesCountBetween(ctx context.Context, startCommitID, endCommitID string) (int, error) { + stdout, _, err := NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID+"..."+endCommitID).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil && strings.Contains(err.Error(), "no merge base") { // git >= 2.28 now returns an error if startCommitID and endCommitID have become unrelated. // previously it would return the results of git diff --name-only startCommitID endCommitID so let's try that... - stdout, _, err = NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID, endCommitID).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID, endCommitID).RunStdString(ctx, &RunOpts{Dir: repo.Path}) } if err != nil { return 0, err diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 808df4fd5e94d..27553a7cc70f7 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -193,7 +193,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi } // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx, "master", pageFilename) // Get last change information. lastCommit, err := wikiRepo.GetCommitByPath(ctx, pageFilename) @@ -432,7 +432,7 @@ func ListPageRevisions(ctx *context.APIContext) { } // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx, "master", pageFilename) page := ctx.FormInt("page") if page <= 1 { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 8c67c638333a7..41d5f116f75be 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -221,7 +221,7 @@ func FileHistory(ctx *context.Context) { return } - commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), fileName) // FIXME: legacy code used ShortName + commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx, ctx.Repo.RefFullName.ShortName(), fileName) // FIXME: legacy code used ShortName if err != nil { ctx.ServerError("FileCommitsCount", err) return diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index d6c3fa5b5606e..a3b2b8dd1c7a3 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -362,7 +362,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { } // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx, ctx.Repo.Repository.DefaultWikiBranch, pageFilename) ctx.Data["CommitCount"] = commitsCount return wikiRepo, entry @@ -414,7 +414,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) ctx.Data["footerContent"] = "" // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx, ctx.Repo.Repository.DefaultWikiBranch, pageFilename) ctx.Data["CommitCount"] = commitsCount // get page From 1611740b16d7d560bee28a47cdc5b2b362252965 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:17:17 +0100 Subject: [PATCH 11/29] add context to IsCommitInBranch and CommitsBetweenIDs --- modules/git/commit.go | 4 ++-- modules/git/repo_commit.go | 20 ++++++++++---------- modules/git/repo_commit_test.go | 6 +++--- services/asymkey/sign.go | 2 +- services/mirror/mirror_pull.go | 2 +- services/pull/merge.go | 2 +- services/pull/merge_squash.go | 2 +- services/repository/push.go | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 3b4b92872aed1..2ecde17a0ac30 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -254,12 +254,12 @@ func (c *Commit) CommitsBeforeLimit(ctx context.Context, num int) ([]*Commit, er } // CommitsBeforeUntil returns the commits between commitID to current revision -func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) { +func (c *Commit) CommitsBeforeUntil(ctx context.Context, commitID string) ([]*Commit, error) { endCommit, err := c.repo.GetCommit(commitID) if err != nil { return nil, err } - return c.repo.CommitsBetween(c, endCommit) + return c.repo.CommitsBetween(ctx, c, endCommit) } // SearchCommitsOptions specify the parameters for SearchCommits diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index a0ee3a0286532..2a47680ab8c09 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -290,17 +290,17 @@ func (repo *Repository) FilesCountBetween(ctx context.Context, startCommitID, en // CommitsBetween returns a list that contains commits between [before, last). // If before is detached (removed by reset + push) it is not included. -func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) { +func (repo *Repository) CommitsBetween(ctx context.Context, last, before *Commit) ([]*Commit, error) { var stdout []byte var err error if before == nil { - stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } else { - stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil && strings.Contains(err.Error(), "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 = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) } } if err != nil { @@ -359,20 +359,20 @@ func (repo *Repository) CommitsBetweenNotBase(ctx context.Context, last, before return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout)) } -// CommitsBetweenIDs return commits between twoe commits -func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) { +// CommitsBetweenIDs return commits between two commits +func (repo *Repository) CommitsBetweenIDs(ctx context.Context, last, before string) ([]*Commit, error) { lastCommit, err := repo.GetCommit(last) if err != nil { return nil, err } if before == "" { - return repo.CommitsBetween(lastCommit, nil) + return repo.CommitsBetween(ctx, lastCommit, nil) } beforeCommit, err := repo.GetCommit(before) if err != nil { return nil, err } - return repo.CommitsBetween(lastCommit, beforeCommit) + return repo.CommitsBetween(ctx, lastCommit, beforeCommit) } // CommitsCountBetween return numbers of commits between two commits @@ -495,8 +495,8 @@ 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) { - stdout, _, err := NewCommand("branch", "--contains").AddDynamicArguments(commitID, branch).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) IsCommitInBranch(ctx context.Context, commitID, branch string) (r bool, err error) { + stdout, _, err := NewCommand("branch", "--contains").AddDynamicArguments(commitID, branch).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return false, err } diff --git a/modules/git/repo_commit_test.go b/modules/git/repo_commit_test.go index a4491cad79446..bf9fa1c6fb044 100644 --- a/modules/git/repo_commit_test.go +++ b/modules/git/repo_commit_test.go @@ -75,11 +75,11 @@ func TestIsCommitInBranch(t *testing.T) { assert.NoError(t, err) defer bareRepo1.Close() - result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1") + result, err := bareRepo1.IsCommitInBranch(t.Context(), "2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1") assert.NoError(t, err) assert.True(t, result) - result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2") + result, err = bareRepo1.IsCommitInBranch(t.Context(), "2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2") assert.NoError(t, err) assert.False(t, result) } @@ -100,7 +100,7 @@ func TestRepository_CommitsBetweenIDs(t *testing.T) { {"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new } for i, c := range cases { - commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID) + commits, err := bareRepo1.CommitsBetweenIDs(t.Context(), c.NewID, c.OldID) assert.NoError(t, err) assert.Len(t, commits, c.ExpectedCommits, "case %d", i) } diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index aada6954cdf63..b5145d0b186b1 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -388,7 +388,7 @@ Loop: if err != nil { return false, "", nil, err } - commitList, err := commit.CommitsBeforeUntil(mergeBaseCommit) + commitList, err := commit.CommitsBeforeUntil(ctx, mergeBaseCommit) if err != nil { return false, "", nil, err } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 658747e7c83f1..5fdf754ee07fd 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -530,7 +530,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { log.Error("SyncMirrors [repo: %-v]: unable to get GetFullCommitID [%s]: %v", m.Repo, result.newCommitID, err) continue } - commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID) + commits, err := gitRepo.CommitsBetweenIDs(ctx, newCommitID, oldCommitID) if err != nil { log.Error("SyncMirrors [repo: %-v]: unable to get CommitsBetweenIDs [new_commit_id: %s, old_commit_id: %s]: %v", m.Repo, newCommitID, oldCommitID, err) continue diff --git a/services/pull/merge.go b/services/pull/merge.go index 1e1ca55bc12e9..5c4f711c6907f 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -633,7 +633,7 @@ func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *use } commitID = commit.ID.String() - ok, err := baseGitRepo.IsCommitInBranch(commitID, pr.BaseBranch) + ok, err := baseGitRepo.IsCommitInBranch(ctx, commitID, pr.BaseBranch) if err != nil { return err } diff --git a/services/pull/merge_squash.go b/services/pull/merge_squash.go index 076189fd7acb9..bbd60531640fb 100644 --- a/services/pull/merge_squash.go +++ b/services/pull/merge_squash.go @@ -32,7 +32,7 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) { } defer gitRepo.Close() - commits, err := gitRepo.CommitsBetweenIDs(trackingBranch, "HEAD") + commits, err := gitRepo.CommitsBetweenIDs(ctx, trackingBranch, "HEAD") if err != nil { log.Error("%-v Unable to get commits between: %s %s: %v", ctx.pr, "HEAD", trackingBranch, err) return nil, err diff --git a/services/repository/push.go b/services/repository/push.go index a71f279770181..b5ce3a7aed871 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -201,7 +201,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } notify_service.CreateRef(ctx, pusher, repo, opts.RefFullName, opts.NewCommitID) } else { - l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID) + l, err = newCommit.CommitsBeforeUntil(ctx, opts.OldCommitID) if err != nil { return fmt.Errorf("newCommit.CommitsBeforeUntil: %w", err) } From ea2793046b14b28859292d5a9e762a73e2ca7feb Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:30:13 +0100 Subject: [PATCH 12/29] add context to GetTreePathLatestCommit --- modules/git/tree.go | 5 +++-- modules/git/tree_test.go | 2 +- routers/api/v1/repo/file.go | 2 +- routers/web/repo/download.go | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index f6fdff97d0400..d548a68ea1b2d 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -6,6 +6,7 @@ package git import ( "bytes" + "context" "strings" ) @@ -64,10 +65,10 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error } // GetTreePathLatestCommit returns the latest commit of a tree path -func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) { +func (repo *Repository) GetTreePathLatestCommit(ctx context.Context, refName, treePath string) (*Commit, error) { stdout, _, err := NewCommand("rev-list", "-1"). AddDynamicArguments(refName).AddDashesAndList(treePath). - RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } diff --git a/modules/git/tree_test.go b/modules/git/tree_test.go index 5fee64b038755..50f473f463531 100644 --- a/modules/git/tree_test.go +++ b/modules/git/tree_test.go @@ -35,7 +35,7 @@ func Test_GetTreePathLatestCommit(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7", commitID) - commit, err := repo.GetTreePathLatestCommit("master", "blame.txt") + commit, err := repo.GetTreePathLatestCommit(t.Context(), "master", "blame.txt") assert.NoError(t, err) assert.NotNil(t, commit) assert.EqualValues(t, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", commit.ID.String()) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 1ba71aa8a3cff..09c7f124a7b66 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -241,7 +241,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn return nil, nil, nil } - latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.APIErrorInternal(err) return nil, nil, nil diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go index 020cebf19637b..44e80e4fe4b6e 100644 --- a/routers/web/repo/download.go +++ b/routers/web/repo/download.go @@ -97,7 +97,7 @@ func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) { return nil, nil } - latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.ServerError("GetTreePathLatestCommit", err) return nil, nil From d2c85c4e210e0a0dc3bf04c95ba9334f95a87855 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:36:28 +0100 Subject: [PATCH 13/29] add context to CommitsByFileAndRange and FileChangedBetweenCommits --- modules/git/commit.go | 4 ++-- modules/git/repo_commit.go | 8 ++++---- modules/git/repo_commit_test.go | 4 ++-- routers/api/v1/repo/commits.go | 2 +- routers/api/v1/repo/wiki.go | 2 +- routers/web/feed/file.go | 2 +- routers/web/repo/commit.go | 2 +- routers/web/repo/wiki.go | 2 +- services/repository/files/update.go | 6 +++--- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 2ecde17a0ac30..6a82f2ded3514 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -313,8 +313,8 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) // FileChangedSinceCommit Returns true if the file given has changed since the past commit // YOU MUST ENSURE THAT pastCommit is a valid commit ID. -func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) { - return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String()) +func (c *Commit) FileChangedSinceCommit(ctx context.Context, filename, pastCommit string) (bool, error) { + return c.repo.FileChangedBetweenCommits(ctx, filename, pastCommit, c.ID.String()) } // HasFile returns true if the file given exists on this commit diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 2a47680ab8c09..1f00e8ae6af86 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -190,8 +190,8 @@ func (repo *Repository) searchCommits(ctx context.Context, id ObjectID, opts Sea // 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) { - stdout, _, err := NewCommand("diff", "--name-only", "-z").AddDynamicArguments(id1, id2).AddDashesAndList(filename).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) FileChangedBetweenCommits(ctx context.Context, filename, id1, id2 string) (bool, error) { + stdout, _, err := NewCommand("diff", "--name-only", "-z").AddDynamicArguments(id1, id2).AddDashesAndList(filename).RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return false, err } @@ -216,7 +216,7 @@ type CommitsByFileAndRangeOptions struct { } // CommitsByFileAndRange return the commits according revision file and the page -func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) { +func (repo *Repository) CommitsByFileAndRange(ctx context.Context, opts CommitsByFileAndRangeOptions) ([]*Commit, error) { stdoutReader, stdoutWriter := io.Pipe() defer func() { _ = stdoutReader.Close() @@ -234,7 +234,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) } gitCmd.AddDashesAndList(opts.File) - err := gitCmd.Run(repo.Ctx, &RunOpts{ + err := gitCmd.Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: stdoutWriter, Stderr: &stderr, diff --git a/modules/git/repo_commit_test.go b/modules/git/repo_commit_test.go index bf9fa1c6fb044..d1498dea90304 100644 --- a/modules/git/repo_commit_test.go +++ b/modules/git/repo_commit_test.go @@ -140,11 +140,11 @@ func TestCommitsByFileAndRange(t *testing.T) { defer bareRepo1.Close() // "foo" has 3 commits in "master" branch - commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1}) + commits, err := bareRepo1.CommitsByFileAndRange(t.Context(), CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1}) require.NoError(t, err) assert.Len(t, commits, 2) - commits, err = bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2}) + commits, err = bareRepo1.CommitsByFileAndRange(t.Context(), CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2}) require.NoError(t, err) assert.Len(t, commits, 1) } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 3cefccfb24947..91134d4bb900b 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -238,7 +238,7 @@ func GetAllCommits(ctx *context.APIContext) { return } - commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange( + commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(ctx, git.CommitsByFileAndRangeOptions{ Revision: sha, File: path, diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 27553a7cc70f7..25f79aca34c2a 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -440,7 +440,7 @@ func ListPageRevisions(ctx *context.APIContext) { } // get Commit Count - commitsHistory, err := wikiRepo.CommitsByFileAndRange( + commitsHistory, err := wikiRepo.CommitsByFileAndRange(ctx, git.CommitsByFileAndRangeOptions{ Revision: "master", File: pageFilename, diff --git a/routers/web/feed/file.go b/routers/web/feed/file.go index 407e4fa2d5daf..03aad60972c4e 100644 --- a/routers/web/feed/file.go +++ b/routers/web/feed/file.go @@ -22,7 +22,7 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string if len(fileName) == 0 { return } - commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange( + commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx, git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName File: fileName, diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 41d5f116f75be..12c8c566a5ec1 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -235,7 +235,7 @@ func FileHistory(ctx *context.Context) { page = 1 } - commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange( + commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx, git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName File: fileName, diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index a3b2b8dd1c7a3..1e16464593e78 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -424,7 +424,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) } // get Commit Count - commitsHistory, err := wikiRepo.CommitsByFileAndRange( + commitsHistory, err := wikiRepo.CommitsByFileAndRange(ctx, git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.Repository.DefaultWikiBranch, File: pageFilename, diff --git a/services/repository/files/update.go b/services/repository/files/update.go index cade7ba2bf7db..60e10cb246c4f 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -230,7 +230,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use } for _, file := range opts.Files { - if err := handleCheckErrors(file, commit, opts); err != nil { + if err := handleCheckErrors(ctx, file, commit, opts); err != nil { return nil, err } } @@ -362,7 +362,7 @@ func (err ErrSHAOrCommitIDNotProvided) Error() string { } // handles the check for various issues for ChangeRepoFiles -func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions) error { +func handleCheckErrors(ctx context.Context, file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions) error { if file.Operation == "update" || file.Operation == "delete" { fromEntry, err := commit.GetTreeEntryByPath(file.Options.fromTreePath) if err != nil { @@ -381,7 +381,7 @@ func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRep // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw // an error, but only if we aren't creating a new branch. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch { - if changed, err := commit.FileChangedSinceCommit(file.Options.treePath, opts.LastCommitID); err != nil { + if changed, err := commit.FileChangedSinceCommit(ctx, file.Options.treePath, opts.LastCommitID); err != nil { return err } else if changed { return ErrCommitIDDoesNotMatch{ From 4f1709b26bd572d0cdf69fef66022fd42d797443 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:44:55 +0100 Subject: [PATCH 14/29] add context to LsTree --- modules/git/tree.go | 4 ++-- services/wiki/wiki.go | 10 +++++----- services/wiki/wiki_test.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index d548a68ea1b2d..8556c873a5bcf 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -48,11 +48,11 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) { } // LsTree checks if the given filenames are in the tree -func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) { +func (repo *Repository) LsTree(ctx context.Context, ref string, filenames ...string) ([]string, error) { cmd := NewCommand("ls-tree", "-z", "--name-only"). AddDashesAndList(append([]string{ref}, filenames...)...) - res, _, err := cmd.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + res, _, err := cmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 413d416b8d60d..b7ec3d41f5dd9 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -51,12 +51,12 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { // prepareGitPath try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error -func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath) (bool, string, error) { +func prepareGitPath(ctx context.Context, gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath) (bool, string, error) { unescaped := string(wikiPath) + ".md" gitPath := WebPathToGitPath(wikiPath) // Look for both files - filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, unescaped, gitPath) + filesInIndex, err := gitRepo.LsTree(ctx, defaultWikiBranch, unescaped, gitPath) if err != nil { if strings.Contains(err.Error(), "Not a valid object name") { return false, gitPath, nil // branch doesn't exist @@ -140,7 +140,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } } - isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName) + isWikiExist, newWikiPath, err := prepareGitPath(ctx, gitRepo, repo.DefaultWikiBranch, newWikiName) if err != nil { return err } @@ -156,7 +156,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model isOldWikiExist := true oldWikiPath := newWikiPath if oldWikiName != newWikiName { - isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName) + isOldWikiExist, oldWikiPath, err = prepareGitPath(ctx, gitRepo, repo.DefaultWikiBranch, oldWikiName) if err != nil { return err } @@ -295,7 +295,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err) } - found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName) + found, wikiPath, err := prepareGitPath(ctx, gitRepo, repo.DefaultWikiBranch, wikiName) if err != nil { return err } diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index e8b89f5e97b3f..ef3c6e9eae1af 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -278,7 +278,7 @@ func TestPrepareWikiFileName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { webPath := UserTitleToWebPath("", tt.arg) - existence, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, webPath) + existence, newWikiPath, err := prepareGitPath(git.DefaultContext, gitRepo, repo.DefaultWikiBranch, webPath) if (err != nil) != tt.wantErr { assert.NoError(t, err) return @@ -309,7 +309,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) { defer gitRepo.Close() - existence, newWikiPath, err := prepareGitPath(gitRepo, "master", "Home") + existence, newWikiPath, err := prepareGitPath(git.DefaultContext, gitRepo, "master", "Home") assert.False(t, existence) assert.NoError(t, err) assert.EqualValues(t, "Home.md", newWikiPath) From 3a0af2c49432a5411d165adb607cdd2005e68b47 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:49:24 +0100 Subject: [PATCH 15/29] add context to GetRepoRawDiffForFile --- modules/git/diff.go | 8 ++++---- services/migrations/gitea_uploader.go | 2 +- services/pull/review.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/git/diff.go b/modules/git/diff.go index fe7ee4d4cbc89..62f286efc6e07 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -27,8 +27,8 @@ const ( ) // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. -func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) error { - return GetRepoRawDiffForFile(repo, "", commitID, diffType, "", writer) +func GetRawDiff(ctx context.Context, repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) error { + return GetRepoRawDiffForFile(ctx, repo, "", commitID, diffType, "", writer) } // GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer. @@ -46,7 +46,7 @@ func GetReverseRawDiff(ctx context.Context, repoPath, commitID string, writer io } // GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository -func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { +func GetRepoRawDiffForFile(ctx context.Context, repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { commit, err := repo.GetCommit(endCommit) if err != nil { return err @@ -89,7 +89,7 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff } stderr := new(bytes.Buffer) - if err = cmd.Run(repo.Ctx, &RunOpts{ + if err = cmd.Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: writer, Stderr: stderr, diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 9423ed726af16..491ce9d41c4b5 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -904,7 +904,7 @@ func (g *GiteaLocalUploader) CreateReviews(ctx context.Context, reviews ...*base _ = writer.Close() }() go func(comment *base.ReviewComment) { - if err := git.GetRepoRawDiffForFile(g.gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, comment.TreePath, writer); err != nil { + if err := git.GetRepoRawDiffForFile(ctx, g.gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, comment.TreePath, writer); err != nil { // We should ignore the error since the commit maybe removed when force push to the pull request log.Warn("GetRepoRawDiffForFile failed when migrating [%s, %s, %s, %s]: %v", g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath, err) } diff --git a/services/pull/review.go b/services/pull/review.go index 78723a58ae305..da2fb81f49594 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -257,7 +257,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo _ = writer.Close() }() go func() { - if err := git.GetRepoRawDiffForFile(gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, treePath, writer); err != nil { + if err := git.GetRepoRawDiffForFile(ctx, gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, treePath, writer); err != nil { _ = writer.CloseWithError(fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %w", gitRepo.Path, pr.MergeBase, headCommitID, treePath, err)) return } From 8fd47167db0726623d9c09f4504d6ad13efd7ad1 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 20:51:01 +0100 Subject: [PATCH 16/29] add context to GetDiffNumChangedFiles --- modules/git/repo_compare.go | 8 ++++---- routers/api/v1/repo/commits.go | 2 +- routers/web/repo/cherry_pick.go | 2 +- routers/web/repo/commit.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index d0ba096509baf..82d24097f3f12 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -119,7 +119,7 @@ func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch // Count number of changed files. // This probably should be removed as we need to use shortstat elsewhere // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly - compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch, directComparison) + compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(ctx, remoteBranch, headBranch, directComparison) if err != nil { return nil, err } @@ -139,7 +139,7 @@ func (l *lineCountWriter) Write(p []byte) (n int, err error) { // GetDiffNumChangedFiles counts the number of changed files // This is substantially quicker than shortstat but... -func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparison bool) (int, error) { +func (repo *Repository) GetDiffNumChangedFiles(ctx context.Context, base, head string, directComparison bool) (int, error) { // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly w := &lineCountWriter{} stderr := new(bytes.Buffer) @@ -151,7 +151,7 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis // avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git [...] -- [...]' if err := NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base+separator+head).AddArguments("--"). - Run(repo.Ctx, &RunOpts{ + Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: w, Stderr: stderr, @@ -161,7 +161,7 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis // previously it would return the results of git diff -z --name-only base head so let's try that... w = &lineCountWriter{} stderr.Reset() - if err = NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base, head).AddArguments("--").Run(repo.Ctx, &RunOpts{ + if err = NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base, head).AddArguments("--").Run(ctx, &RunOpts{ Dir: repo.Path, Stdout: w, Stderr: stderr, diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 91134d4bb900b..9ff4da06a5303 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -315,7 +315,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { sha := ctx.PathParam("sha") diffType := git.RawDiffType(ctx.PathParam("diffType")) - if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, diffType, ctx.Resp); err != nil { + if err := git.GetRawDiff(ctx, ctx.Repo.GitRepo, sha, diffType, ctx.Resp); err != nil { if git.IsErrNotExist(err) { ctx.APIErrorNotFound(sha) return diff --git a/routers/web/repo/cherry_pick.go b/routers/web/repo/cherry_pick.go index ec50e1435ef36..a658742b7d2aa 100644 --- a/routers/web/repo/cherry_pick.go +++ b/routers/web/repo/cherry_pick.go @@ -155,7 +155,7 @@ func CherryPickPost(ctx *context.Context) { return } } else { - if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, git.RawDiffType("patch"), buf); err != nil { + if err := git.GetRawDiff(ctx, ctx.Repo.GitRepo, sha, git.RawDiffType("patch"), buf); err != nil { if git.IsErrNotExist(err) { ctx.NotFound(errors.New("commit " + ctx.PathParam("sha") + " does not exist.")) return diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 12c8c566a5ec1..4100a7f806ed2 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -431,7 +431,7 @@ func RawDiff(ctx *context.Context) { return } } - if err := git.GetRawDiff( + if err := git.GetRawDiff(ctx, gitRepo, ctx.PathParam("sha"), git.RawDiffType(ctx.PathParam("ext")), From a4e5604ea149566bebb23794666acad67890909e Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:01:44 +0100 Subject: [PATCH 17/29] add context to GetCodeActivityStats --- models/activities/repo_activity.go | 4 ++-- modules/git/repo_stats.go | 6 +++--- modules/git/repo_stats_test.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/models/activities/repo_activity.go b/models/activities/repo_activity.go index 3ccdbd47d3d7a..60460e67bcaaa 100644 --- a/models/activities/repo_activity.go +++ b/models/activities/repo_activity.go @@ -74,7 +74,7 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom } defer closer.Close() - code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch) + code, err := gitRepo.GetCodeActivityStats(ctx, timeFrom, repo.DefaultBranch) if err != nil { return nil, fmt.Errorf("FillFromGit: %w", err) } @@ -91,7 +91,7 @@ func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository } defer closer.Close() - code, err := gitRepo.GetCodeActivityStats(timeFrom, "") + code, err := gitRepo.GetCodeActivityStats(ctx, timeFrom, "") if err != nil { return nil, fmt.Errorf("FillFromGit: %w", err) } diff --git a/modules/git/repo_stats.go b/modules/git/repo_stats.go index 76fe92bb349f1..57fee22ff1260 100644 --- a/modules/git/repo_stats.go +++ b/modules/git/repo_stats.go @@ -35,12 +35,12 @@ type CodeActivityAuthor struct { } // GetCodeActivityStats returns code statistics for activity page -func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) (*CodeActivityStats, error) { +func (repo *Repository) GetCodeActivityStats(ctx context.Context, fromTime time.Time, branch string) (*CodeActivityStats, error) { stats := &CodeActivityStats{} since := fromTime.Format(time.RFC3339) - stdout, _, runErr := NewCommand("rev-list", "--count", "--no-merges", "--branches=*", "--date=iso").AddOptionFormat("--since='%s'", since).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, runErr := NewCommand("rev-list", "--count", "--no-merges", "--branches=*", "--date=iso").AddOptionFormat("--since='%s'", since).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -68,7 +68,7 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) } stderr := new(strings.Builder) - err = gitCmd.Run(repo.Ctx, &RunOpts{ + err = gitCmd.Run(ctx, &RunOpts{ Env: []string{}, Dir: repo.Path, Stdout: stdoutWriter, diff --git a/modules/git/repo_stats_test.go b/modules/git/repo_stats_test.go index 3d032385ee282..274a8cf45b39b 100644 --- a/modules/git/repo_stats_test.go +++ b/modules/git/repo_stats_test.go @@ -20,7 +20,7 @@ func TestRepository_GetCodeActivityStats(t *testing.T) { timeFrom, err := time.Parse(time.RFC3339, "2016-01-01T00:00:00+00:00") assert.NoError(t, err) - code, err := bareRepo1.GetCodeActivityStats(timeFrom, "") + code, err := bareRepo1.GetCodeActivityStats(t.Context(), timeFrom, "") assert.NoError(t, err) assert.NotNil(t, code) From e8f1fb31fd2ee610a497bbdd3ccfdde2c65664e2 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:05:19 +0100 Subject: [PATCH 18/29] add context to LineBlame --- modules/git/repo_blame.go | 5 +++-- services/pull/review.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_blame.go b/modules/git/repo_blame.go index 6941a76c42ded..e60eb109d6ae9 100644 --- a/modules/git/repo_blame.go +++ b/modules/git/repo_blame.go @@ -4,15 +4,16 @@ package git import ( + "context" "fmt" ) // LineBlame returns the latest commit at the given line -func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) { +func (repo *Repository) LineBlame(ctx context.Context, revision, path, file string, line uint) (*Commit, error) { res, _, err := NewCommand("blame"). AddOptionFormat("-L %d,%d", line, line). AddOptionValues("-p", revision). - AddDashesAndList(file).RunStdString(repo.Ctx, &RunOpts{Dir: path}) + AddDashesAndList(file).RunStdString(ctx, &RunOpts{Dir: path}) if err != nil { return nil, err } diff --git a/services/pull/review.go b/services/pull/review.go index da2fb81f49594..93e69fda8f870 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -51,7 +51,7 @@ var ErrSubmitReviewOnClosedPR = errors.New("can't submit review for a closed or // If the line got changed the comment is going to be invalidated. func checkInvalidation(ctx context.Context, c *issues_model.Comment, repo *git.Repository, branch string) error { // FIXME differentiate between previous and proposed line - commit, err := repo.LineBlame(branch, repo.Path, c.TreePath, uint(c.UnsignedLine())) + commit, err := repo.LineBlame(ctx, branch, repo.Path, c.TreePath, uint(c.UnsignedLine())) if err != nil && (strings.Contains(err.Error(), "fatal: no such path") || notEnoughLines.MatchString(err.Error())) { c.Invalidated = true return issues_model.UpdateCommentInvalidate(ctx, c) @@ -233,7 +233,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo // FIXME validate treePath // Get latest commit referencing the commented line // No need for get commit for base branch changes - commit, err := gitRepo.LineBlame(head, gitRepo.Path, treePath, uint(line)) + commit, err := gitRepo.LineBlame(ctx, head, gitRepo.Path, treePath, uint(line)) if err == nil { commitID = commit.ID.String() } else if !(strings.Contains(err.Error(), "exit status 128 - fatal: no such path") || notEnoughLines.MatchString(err.Error())) { From 00bb0b7893512f8fc197570ae974588490c8a379 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:07:03 +0100 Subject: [PATCH 19/29] add context to GetDefaultPublicKey --- modules/git/commit.go | 4 ++-- modules/git/repo_gpg.go | 11 ++++++----- services/asymkey/commit.go | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 6a82f2ded3514..24e609068b45a 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -468,11 +468,11 @@ func GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, err } // GetRepositoryDefaultPublicGPGKey returns the default public key for this commit -func (c *Commit) GetRepositoryDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) { +func (c *Commit) GetRepositoryDefaultPublicGPGKey(ctx context.Context, forceUpdate bool) (*GPGSettings, error) { if c.repo == nil { return nil, nil } - return c.repo.GetDefaultPublicGPGKey(forceUpdate) + return c.repo.GetDefaultPublicGPGKey(ctx, forceUpdate) } func IsStringLikelyCommitID(objFmt ObjectFormat, s string, minLength ...int) bool { diff --git a/modules/git/repo_gpg.go b/modules/git/repo_gpg.go index 8f91b4dce558b..5838ddacf44da 100644 --- a/modules/git/repo_gpg.go +++ b/modules/git/repo_gpg.go @@ -5,6 +5,7 @@ package git import ( + "context" "fmt" "strings" @@ -24,7 +25,7 @@ func (gpgSettings *GPGSettings) LoadPublicKeyContent() error { } // GetDefaultPublicGPGKey will return and cache the default public GPG settings for this repository -func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) { +func (repo *Repository) GetDefaultPublicGPGKey(ctx context.Context, forceUpdate bool) (*GPGSettings, error) { if repo.gpgSettings != nil && !forceUpdate { return repo.gpgSettings, nil } @@ -33,7 +34,7 @@ func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, Sign: true, } - value, _, _ := NewCommand("config", "--get", "commit.gpgsign").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + value, _, _ := NewCommand("config", "--get", "commit.gpgsign").RunStdString(ctx, &RunOpts{Dir: repo.Path}) sign, valid := ParseBool(strings.TrimSpace(value)) if !sign || !valid { gpgSettings.Sign = false @@ -41,13 +42,13 @@ func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, return gpgSettings, nil } - signingKey, _, _ := NewCommand("config", "--get", "user.signingkey").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + signingKey, _, _ := NewCommand("config", "--get", "user.signingkey").RunStdString(ctx, &RunOpts{Dir: repo.Path}) gpgSettings.KeyID = strings.TrimSpace(signingKey) - defaultEmail, _, _ := NewCommand("config", "--get", "user.email").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + defaultEmail, _, _ := NewCommand("config", "--get", "user.email").RunStdString(ctx, &RunOpts{Dir: repo.Path}) gpgSettings.Email = strings.TrimSpace(defaultEmail) - defaultName, _, _ := NewCommand("config", "--get", "user.name").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + defaultName, _, _ := NewCommand("config", "--get", "user.name").RunStdString(ctx, &RunOpts{Dir: repo.Path}) gpgSettings.Name = strings.TrimSpace(defaultName) if err := gpgSettings.LoadPublicKeyContent(); err != nil { diff --git a/services/asymkey/commit.go b/services/asymkey/commit.go index df29133972156..8455a96e5d1d3 100644 --- a/services/asymkey/commit.go +++ b/services/asymkey/commit.go @@ -169,7 +169,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi } } - defaultGPGSettings, err := c.GetRepositoryDefaultPublicGPGKey(false) + defaultGPGSettings, err := c.GetRepositoryDefaultPublicGPGKey(ctx, false) if err != nil { log.Error("Error getting default public gpg key: %v", err) } else if defaultGPGSettings == nil { From bd41dd43f41c5002476d9b28ee8e6aa5a85b9134 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:11:23 +0100 Subject: [PATCH 20/29] add context to branch commands --- modules/git/repo_branch.go | 12 ++++++------ routers/api/v1/repo/commits.go | 2 +- services/repository/branch.go | 2 +- services/repository/migrate.go | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 916391f1677b6..b24ca2a289ff1 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -34,11 +34,11 @@ type Branch struct { } // GetHEADBranch returns corresponding branch of HEAD. -func (repo *Repository) GetHEADBranch() (*Branch, error) { +func (repo *Repository) GetHEADBranch(ctx context.Context) (*Branch, error) { if repo == nil { return nil, fmt.Errorf("nil repo") } - stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err := NewCommand("symbolic-ref", "HEAD").RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } @@ -104,7 +104,7 @@ type DeleteBranchOptions struct { } // DeleteBranch delete a branch by name on repository. -func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error { +func (repo *Repository) DeleteBranch(ctx context.Context, name string, opts DeleteBranchOptions) error { cmd := NewCommand("branch") if opts.Force { @@ -114,17 +114,17 @@ func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) erro } cmd.AddDashesAndList(name) - _, _, err := cmd.RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + _, _, err := cmd.RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } // CreateBranch create a new branch -func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error { +func (repo *Repository) CreateBranch(ctx context.Context, branch, oldbranchOrCommit string) error { cmd := NewCommand("branch") cmd.AddDashesAndList(branch, oldbranchOrCommit) - _, _, err := cmd.RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + _, _, err := cmd.RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 9ff4da06a5303..daae4eb60adcb 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -180,7 +180,7 @@ func GetAllCommits(ctx *context.APIContext) { var baseCommit *git.Commit if len(sha) == 0 { // no sha supplied - use default branch - head, err := ctx.Repo.GitRepo.GetHEADBranch() + head, err := ctx.Repo.GitRepo.GetHEADBranch(ctx) if err != nil { ctx.APIErrorInternal(err) return diff --git a/services/repository/branch.go b/services/repository/branch.go index 8804778bd5ebb..13e2109eb18de 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -550,7 +550,7 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R } } - return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ + return gitRepo.DeleteBranch(ctx, branchName, git.DeleteBranchOptions{ Force: true, }) }); err != nil { diff --git a/services/repository/migrate.go b/services/repository/migrate.go index 50b434f003f3d..561ade6a89744 100644 --- a/services/repository/migrate.go +++ b/services/repository/migrate.go @@ -140,7 +140,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, if !repo.IsEmpty { if len(repo.DefaultBranch) == 0 { // Try to get HEAD branch and set it as default branch. - headBranch, err := gitRepo.GetHEADBranch() + headBranch, err := gitRepo.GetHEADBranch(ctx) if err != nil { return repo, fmt.Errorf("GetHEADBranch: %w", err) } From a933423f1dfdd17fd042e5cb0fb256c94918962e Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:13:11 +0100 Subject: [PATCH 21/29] add context to remaining branch commands --- modules/git/repo_branch.go | 12 ++++++------ modules/git/repo_compare.go | 4 ++-- services/issue/pull.go | 4 ++-- services/migrations/dump.go | 2 +- services/migrations/gitea_uploader.go | 2 +- services/repository/branch.go | 2 +- services/wiki/wiki.go | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index b24ca2a289ff1..dfcbd36ba9235 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -130,20 +130,20 @@ func (repo *Repository) CreateBranch(ctx context.Context, branch, oldbranchOrCom } // AddRemote adds a new remote to repository. -func (repo *Repository) AddRemote(name, url string, fetch bool) error { +func (repo *Repository) AddRemote(ctx context.Context, name, url string, fetch bool) error { cmd := NewCommand("remote", "add") if fetch { cmd.AddArguments("-f") } cmd.AddDynamicArguments(name, url) - _, _, err := cmd.RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + _, _, err := cmd.RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } // RemoveRemote removes a remote from repository. -func (repo *Repository) RemoveRemote(name string) error { - _, _, err := NewCommand("remote", "rm").AddDynamicArguments(name).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) RemoveRemote(ctx context.Context, name string) error { + _, _, err := NewCommand("remote", "rm").AddDynamicArguments(name).RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } @@ -153,7 +153,7 @@ func (branch *Branch) GetCommit() (*Commit, error) { } // RenameBranch rename a branch -func (repo *Repository) RenameBranch(from, to string) error { - _, _, err := NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) RenameBranch(ctx context.Context, from, to string) error { + _, _, err := NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 82d24097f3f12..795d8f4de0cca 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -60,11 +60,11 @@ func (repo *Repository) GetCompareInfo(ctx context.Context, basePath, baseBranch if repo.Path != basePath { // Add a temporary remote tmpRemote = strconv.FormatInt(time.Now().UnixNano(), 10) - if err = repo.AddRemote(tmpRemote, basePath, false); err != nil { + if err = repo.AddRemote(ctx, tmpRemote, basePath, false); err != nil { return nil, fmt.Errorf("AddRemote: %w", err) } defer func() { - if err := repo.RemoveRemote(tmpRemote); err != nil { + if err := repo.RemoveRemote(ctx, tmpRemote); err != nil { logger.Error("GetPullRequestInfo: RemoveRemote: %v", err) } }() diff --git a/services/issue/pull.go b/services/issue/pull.go index 5c549e44b9442..07a6fb9ce9004 100644 --- a/services/issue/pull.go +++ b/services/issue/pull.go @@ -21,11 +21,11 @@ import ( func getMergeBase(ctx context.Context, repo *git.Repository, pr *issues_model.PullRequest, baseBranch, headBranch string) (string, error) { // Add a temporary remote tmpRemote := fmt.Sprintf("mergebase-%d-%d", pr.ID, time.Now().UnixNano()) - if err := repo.AddRemote(tmpRemote, repo.Path, false); err != nil { + if err := repo.AddRemote(ctx, tmpRemote, repo.Path, false); err != nil { return "", fmt.Errorf("AddRemote: %w", err) } defer func() { - if err := repo.RemoveRemote(tmpRemote); err != nil { + if err := repo.RemoveRemote(ctx, tmpRemote); err != nil { log.Error("getMergeBase: RemoveRemote: %v", err) } }() diff --git a/services/migrations/dump.go b/services/migrations/dump.go index b4ca1e41e0e71..80b5c0005ed2b 100644 --- a/services/migrations/dump.go +++ b/services/migrations/dump.go @@ -507,7 +507,7 @@ func (g *RepositoryDumper) handlePullRequest(ctx context.Context, pr *base.PullR remote = "head-pr-" + strconv.FormatInt(pr.Number, 10) } // ... now add the remote - err := g.gitRepo.AddRemote(remote, pr.Head.CloneURL, true) + err := g.gitRepo.AddRemote(ctx, remote, pr.Head.CloneURL, true) if err != nil { log.Error("PR #%d in %s/%s AddRemote[%s] failed: %v", pr.Number, g.repoOwner, g.repoName, remote, err) } else { diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 491ce9d41c4b5..63f40569817a4 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -624,7 +624,7 @@ func (g *GiteaLocalUploader) updateGitForPullRequest(ctx context.Context, pr *ba remote = "head-pr-" + strconv.FormatInt(pr.Number, 10) } // ... now add the remote - err := g.gitRepo.AddRemote(remote, pr.Head.CloneURL, true) + err := g.gitRepo.AddRemote(ctx, remote, pr.Head.CloneURL, true) if err != nil { log.Error("PR #%d in %s/%s AddRemote[%s] failed: %v", pr.Number, g.repoOwner, g.repoName, remote, err) } else { diff --git a/services/repository/branch.go b/services/repository/branch.go index 13e2109eb18de..01d3be4317567 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -442,7 +442,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m } if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error { - err2 := gitRepo.RenameBranch(from, to) + err2 := gitRepo.RenameBranch(ctx, from, to) if err2 != nil { return err2 } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b7ec3d41f5dd9..7dbef3c3cbed9 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -397,7 +397,7 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n } defer gitRepo.Close() - err = gitRepo.RenameBranch(oldDefBranch, newBranch) + err = gitRepo.RenameBranch(ctx, oldDefBranch, newBranch) if err != nil { return fmt.Errorf("unable to rename default branch: %w", err) } From 6087fa4a3cacde8282abe203b9db831c172dca9f Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:23:40 +0100 Subject: [PATCH 22/29] add context to ReadTreeToIndex --- modules/git/repo_attribute.go | 6 +++--- modules/git/repo_index.go | 8 ++++---- modules/git/repo_language_stats_nogogit.go | 5 +++-- modules/git/repo_language_stats_test.go | 2 +- modules/indexer/stats/db.go | 2 +- routers/web/repo/blame.go | 2 +- routers/web/repo/setting/lfs.go | 2 +- routers/web/repo/view_file.go | 4 ++-- services/gitdiff/gitdiff.go | 2 +- services/markup/renderhelper_codepreview.go | 2 +- services/repository/files/content.go | 4 ++-- services/wiki/wiki.go | 4 ++-- 12 files changed, 22 insertions(+), 21 deletions(-) diff --git a/modules/git/repo_attribute.go b/modules/git/repo_attribute.go index 1b6be0a3fa5ef..526016b791337 100644 --- a/modules/git/repo_attribute.go +++ b/modules/git/repo_attribute.go @@ -282,8 +282,8 @@ func (wr *nulSeparatedAttributeWriter) Close() error { } // Create a check attribute reader for the current repository and provided commit ID -func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeReader, context.CancelFunc) { - indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID) +func (repo *Repository) CheckAttributeReader(ctx context.Context, commitID string) (*CheckAttributeReader, context.CancelFunc) { + indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(ctx, commitID) if err != nil { return nil, func() {} } @@ -301,7 +301,7 @@ func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeRe IndexFile: indexFilename, WorkTree: worktree, } - ctx, cancel := context.WithCancel(repo.Ctx) + ctx, cancel := context.WithCancel(ctx) if err := checker.Init(ctx); err != nil { log.Error("Unable to open checker for %s. Error: %v", commitID, err) } else { diff --git a/modules/git/repo_index.go b/modules/git/repo_index.go index 1c7fcc063e0ca..21c3b5ba1a7b7 100644 --- a/modules/git/repo_index.go +++ b/modules/git/repo_index.go @@ -15,14 +15,14 @@ import ( ) // ReadTreeToIndex reads a treeish to the index -func (repo *Repository) ReadTreeToIndex(treeish string, indexFilename ...string) error { +func (repo *Repository) ReadTreeToIndex(ctx context.Context, treeish string, indexFilename ...string) error { objectFormat, err := repo.GetObjectFormat() if err != nil { return err } if len(treeish) != objectFormat.FullLength() { - res, _, err := NewCommand("rev-parse", "--verify").AddDynamicArguments(treeish).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + res, _, err := NewCommand("rev-parse", "--verify").AddDynamicArguments(treeish).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return err } @@ -50,7 +50,7 @@ func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) er } // ReadTreeToTemporaryIndex reads a treeish to a temporary index file -func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (tmpIndexFilename, tmpDir string, cancel context.CancelFunc, err error) { +func (repo *Repository) ReadTreeToTemporaryIndex(ctx context.Context, treeish string) (tmpIndexFilename, tmpDir string, cancel context.CancelFunc, err error) { defer func() { // if error happens and there is a cancel function, do clean up if err != nil && cancel != nil { @@ -74,7 +74,7 @@ func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (tmpIndexFilena tmpIndexFilename = filepath.Join(tmpDir, ".tmp-index") cancel = removeDirFn(tmpDir) - err = repo.ReadTreeToIndex(treeish, tmpIndexFilename) + err = repo.ReadTreeToIndex(ctx, treeish, tmpIndexFilename) if err != nil { return "", "", cancel, err } diff --git a/modules/git/repo_language_stats_nogogit.go b/modules/git/repo_language_stats_nogogit.go index de7707bd6cd8b..858e5ae26357e 100644 --- a/modules/git/repo_language_stats_nogogit.go +++ b/modules/git/repo_language_stats_nogogit.go @@ -7,6 +7,7 @@ package git import ( "bytes" + "context" "io" "code.gitea.io/gitea/modules/analyze" @@ -17,7 +18,7 @@ import ( ) // GetLanguageStats calculates language stats for git repository at specified commit -func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) { +func (repo *Repository) GetLanguageStats(ctx context.Context, commitID string) (map[string]int64, error) { // We will feed the commit IDs in order into cat-file --batch, followed by blobs as necessary. // so let's create a batch stdin and stdout batchStdinWriter, batchReader, cancel, err := repo.CatFileBatch(repo.Ctx) @@ -62,7 +63,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err return nil, err } - checker, deferable := repo.CheckAttributeReader(commitID) + checker, deferable := repo.CheckAttributeReader(ctx, commitID) defer deferable() contentBuf := bytes.Buffer{} diff --git a/modules/git/repo_language_stats_test.go b/modules/git/repo_language_stats_test.go index 1ee5f4c3af27b..a9f5087313eb6 100644 --- a/modules/git/repo_language_stats_test.go +++ b/modules/git/repo_language_stats_test.go @@ -20,7 +20,7 @@ func TestRepository_GetLanguageStats(t *testing.T) { defer gitRepo.Close() - stats, err := gitRepo.GetLanguageStats("8fee858da5796dfb37704761701bb8e800ad9ef3") + stats, err := gitRepo.GetLanguageStats(t.Context(), "8fee858da5796dfb37704761701bb8e800ad9ef3") require.NoError(t, err) assert.EqualValues(t, map[string]int64{ diff --git a/modules/indexer/stats/db.go b/modules/indexer/stats/db.go index 067a6f609bdc8..851b6100fcf1d 100644 --- a/modules/indexer/stats/db.go +++ b/modules/indexer/stats/db.go @@ -62,7 +62,7 @@ func (db *DBIndexer) Index(id int64) error { } // Calculate and save language statistics to database - stats, err := gitRepo.GetLanguageStats(commitID) + stats, err := gitRepo.GetLanguageStats(ctx, commitID) if err != nil { if !setting.IsInTesting { log.Error("Unable to get language stats for ID %s for default branch %s in %s. Error: %v", commitID, repo.DefaultBranch, repo.FullName(), err) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index efd85b9452798..72d4c32e68d10 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -234,7 +234,7 @@ func processBlameParts(ctx *context.Context, blameParts []*git.BlamePart) map[st func renderBlame(ctx *context.Context, blameParts []*git.BlamePart, commitNames map[string]*user_model.UserCommit) { repoLink := ctx.Repo.RepoLink - language, err := files_service.TryGetContentLanguage(ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath) + language, err := files_service.TryGetContentLanguage(ctx, ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath) if err != nil { log.Error("Unable to get file language for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err) } diff --git a/routers/web/repo/setting/lfs.go b/routers/web/repo/setting/lfs.go index 655291d25c3a4..1cb84e4d2e938 100644 --- a/routers/web/repo/setting/lfs.go +++ b/routers/web/repo/setting/lfs.go @@ -144,7 +144,7 @@ func LFSLocks(ctx *context.Context) { filenames[i] = lock.Path } - if err := gitRepo.ReadTreeToIndex(ctx.Repo.Repository.DefaultBranch); err != nil { + if err := gitRepo.ReadTreeToIndex(ctx, ctx.Repo.Repository.DefaultBranch); err != nil { log.Error("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err) ctx.ServerError("LFSLocks", fmt.Errorf("unable to read the default branch to the index: %s (%w)", ctx.Repo.Repository.DefaultBranch, err)) return diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 6ed21fbda2c8e..6eab512ea3fc7 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -201,7 +201,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1 } - language, err := files_service.TryGetContentLanguage(ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath) + language, err := files_service.TryGetContentLanguage(ctx, ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath) if err != nil { log.Error("Unable to get file language for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err) } @@ -276,7 +276,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { } if ctx.Repo.GitRepo != nil { - checker, deferable := ctx.Repo.GitRepo.CheckAttributeReader(ctx.Repo.CommitID) + checker, deferable := ctx.Repo.GitRepo.CheckAttributeReader(ctx, ctx.Repo.CommitID) if checker != nil { defer deferable() attrs, err := checker.CheckPath(ctx.Repo.TreePath) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 7208e1fb9641e..261c9f2c3d5a9 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1237,7 +1237,7 @@ func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOp return nil, err } - checker, deferrable := gitRepo.CheckAttributeReader(opts.AfterCommitID) + checker, deferrable := gitRepo.CheckAttributeReader(ctx, opts.AfterCommitID) defer deferrable() for _, diffFile := range diff.Files { diff --git a/services/markup/renderhelper_codepreview.go b/services/markup/renderhelper_codepreview.go index d638af7ff06b8..c2170f01e815a 100644 --- a/services/markup/renderhelper_codepreview.go +++ b/services/markup/renderhelper_codepreview.go @@ -61,7 +61,7 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie return "", err } - language, _ := files.TryGetContentLanguage(gitRepo, opts.CommitID, opts.FilePath) + language, _ := files.TryGetContentLanguage(ctx, gitRepo, opts.CommitID, opts.FilePath) blob, err := commit.GetBlobByPath(opts.FilePath) if err != nil { return "", err diff --git a/services/repository/files/content.go b/services/repository/files/content.go index b07bee8754775..8043069065dd0 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -271,8 +271,8 @@ func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git } // TryGetContentLanguage tries to get the (linguist) language of the file content -func TryGetContentLanguage(gitRepo *git.Repository, commitID, treePath string) (string, error) { - indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(commitID) +func TryGetContentLanguage(ctx context.Context, gitRepo *git.Repository, commitID, treePath string) (string, error) { + indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(ctx, commitID) if err != nil { return "", err } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 7dbef3c3cbed9..b8fca28f3c27e 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -134,7 +134,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model defer gitRepo.Close() if hasDefaultBranch { - if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil { + if err := gitRepo.ReadTreeToIndex(ctx, "HEAD"); err != nil { log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err) return fmt.Errorf("fnable to read HEAD tree to index in: %s %w", basePath, err) } @@ -290,7 +290,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } defer gitRepo.Close() - if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil { + if err := gitRepo.ReadTreeToIndex(ctx, "HEAD"); err != nil { log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err) return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err) } From ad250db37ebb4e09ee76aa19467a31ffc59a6d7f Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:28:30 +0100 Subject: [PATCH 23/29] add context to CommitTree --- modules/git/repo_tree.go | 5 +++-- services/wiki/wiki.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_tree.go b/modules/git/repo_tree.go index 70e5aee02353f..48a22275082c5 100644 --- a/modules/git/repo_tree.go +++ b/modules/git/repo_tree.go @@ -6,6 +6,7 @@ package git import ( "bytes" + "context" "os" "strings" "time" @@ -21,7 +22,7 @@ type CommitTreeOpts struct { } // CommitTree creates a commit from a given tree id for the user with provided message -func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opts CommitTreeOpts) (ObjectID, error) { +func (repo *Repository) CommitTree(ctx context.Context, author, committer *Signature, tree *Tree, opts CommitTreeOpts) (ObjectID, error) { commitTimeStr := time.Now().Format(time.RFC3339) // Because this may call hooks we should pass in the environment @@ -53,7 +54,7 @@ func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opt stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) - err := cmd.Run(repo.Ctx, &RunOpts{ + err := cmd.Run(ctx, &RunOpts{ Env: env, Dir: repo.Path, Stdin: messageBytes, diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b8fca28f3c27e..b2f41a05fb1e8 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -209,7 +209,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model commitTreeOpts.Parents = []string{"HEAD"} } - commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts) + commitHash, err := gitRepo.CommitTree(ctx, doer.NewGitSig(), committer, tree, commitTreeOpts) if err != nil { log.Error("CommitTree failed: %v", err) return err @@ -332,7 +332,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model commitTreeOpts.NoGPGSign = true } - commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts) + commitHash, err := gitRepo.CommitTree(ctx, doer.NewGitSig(), committer, tree, commitTreeOpts) if err != nil { return err } From 0d52636be24e1fb54dff159d3504d306f0c85c45 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:35:20 +0100 Subject: [PATCH 24/29] add context to tag commands --- modules/git/repo_tag.go | 28 ++++++++-------- modules/git/repo_tag_test.go | 32 +++++++++---------- modules/repository/repo.go | 2 +- routers/api/v1/repo/tag.go | 8 ++--- services/release/release.go | 4 +-- services/repository/push.go | 2 +- tests/integration/api_releases_test.go | 4 +-- .../api_repo_get_contents_list_test.go | 2 +- .../integration/api_repo_get_contents_test.go | 2 +- tests/integration/api_repo_git_tags_test.go | 6 ++-- 10 files changed, 45 insertions(+), 45 deletions(-) diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go index c74618471a047..ca95fafe2c7f7 100644 --- a/modules/git/repo_tag.go +++ b/modules/git/repo_tag.go @@ -17,24 +17,24 @@ import ( const TagPrefix = "refs/tags/" // CreateTag create one tag in the repository -func (repo *Repository) CreateTag(name, revision string) error { - _, _, err := NewCommand("tag").AddDashesAndList(name, revision).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) CreateTag(ctx context.Context, name, revision string) error { + _, _, err := NewCommand("tag").AddDashesAndList(name, revision).RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } // CreateAnnotatedTag create one annotated tag in the repository -func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error { - _, _, err := NewCommand("tag", "-a", "-m").AddDynamicArguments(message).AddDashesAndList(name, revision).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) CreateAnnotatedTag(ctx context.Context, name, message, revision string) error { + _, _, err := NewCommand("tag", "-a", "-m").AddDynamicArguments(message).AddDashesAndList(name, revision).RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } // GetTagNameBySHA returns the name of a tag from its tag object SHA or commit SHA -func (repo *Repository) GetTagNameBySHA(sha string) (string, error) { +func (repo *Repository) GetTagNameBySHA(ctx context.Context, sha string) (string, error) { if len(sha) < 5 { return "", fmt.Errorf("SHA is too short: %s", sha) } - stdout, _, err := NewCommand("show-ref", "--tags", "-d").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, err := NewCommand("show-ref", "--tags", "-d").RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return "", err } @@ -56,8 +56,8 @@ func (repo *Repository) GetTagNameBySHA(sha string) (string, error) { } // GetTagID returns the object ID for a tag (annotated tags have both an object SHA AND a commit SHA) -func (repo *Repository) GetTagID(name string) (string, error) { - stdout, _, err := NewCommand("show-ref", "--tags").AddDashesAndList(name).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) GetTagID(ctx context.Context, name string) (string, error) { + stdout, _, err := NewCommand("show-ref", "--tags").AddDashesAndList(name).RunStdString(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return "", err } @@ -72,8 +72,8 @@ func (repo *Repository) GetTagID(name string) (string, error) { } // GetTag returns a Git tag by given name. -func (repo *Repository) GetTag(name string) (*Tag, error) { - idStr, err := repo.GetTagID(name) +func (repo *Repository) GetTag(ctx context.Context, name string) (*Tag, error) { + idStr, err := repo.GetTagID(ctx, name) if err != nil { return nil, err } @@ -105,7 +105,7 @@ func (repo *Repository) GetTagWithID(idStr, name string) (*Tag, error) { } // GetTagInfos returns all tag infos of the repository. -func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) { +func (repo *Repository) GetTagInfos(ctx context.Context, page, pageSize int) ([]*Tag, int, error) { // Generally, refname:short should be equal to refname:lstrip=2 except core.warnAmbiguousRefs is used to select the strict abbreviation mode. // https://git-scm.com/docs/git-for-each-ref#Documentation/git-for-each-ref.txt-refname forEachRefFmt := foreachref.NewFormat("objecttype", "refname:lstrip=2", "object", "objectname", "creator", "contents", "contents:signature") @@ -119,7 +119,7 @@ func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) { go func() { err := NewCommand("for-each-ref"). AddOptionFormat("--format=%s", forEachRefFmt.Flag()). - AddArguments("--sort", "-*creatordate", "refs/tags").Run(repo.Ctx, rc) + AddArguments("--sort", "-*creatordate", "refs/tags").Run(ctx, rc) if err != nil { _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderr.String())) } else { @@ -197,7 +197,7 @@ func parseTagRef(ref map[string]string) (tag *Tag, err error) { } // GetAnnotatedTag returns a Git tag by its SHA, must be an annotated tag -func (repo *Repository) GetAnnotatedTag(sha string) (*Tag, error) { +func (repo *Repository) GetAnnotatedTag(ctx context.Context, sha string) (*Tag, error) { id, err := NewIDFromString(sha) if err != nil { return nil, err @@ -212,7 +212,7 @@ func (repo *Repository) GetAnnotatedTag(sha string) (*Tag, error) { } // Get tag name - name, err := repo.GetTagNameBySHA(id.String()) + name, err := repo.GetTagNameBySHA(ctx, id.String()) if err != nil { return nil, err } diff --git a/modules/git/repo_tag_test.go b/modules/git/repo_tag_test.go index f1f081680a360..17f4d679194be 100644 --- a/modules/git/repo_tag_test.go +++ b/modules/git/repo_tag_test.go @@ -20,7 +20,7 @@ func TestRepository_GetTags(t *testing.T) { } defer bareRepo1.Close() - tags, total, err := bareRepo1.GetTagInfos(0, 0) + tags, total, err := bareRepo1.GetTagInfos(t.Context(), 0, 0) if err != nil { assert.NoError(t, err) return @@ -56,14 +56,14 @@ func TestRepository_GetTag(t *testing.T) { lTagName := "lightweightTag" // Create the lightweight tag - err = bareRepo1.CreateTag(lTagName, lTagCommitID) + err = bareRepo1.CreateTag(t.Context(), lTagName, lTagCommitID) if err != nil { assert.NoError(t, err, "Unable to create the lightweight tag: %s for ID: %s. Error: %v", lTagName, lTagCommitID, err) return } // and try to get the Tag for lightweight tag - lTag, err := bareRepo1.GetTag(lTagName) + lTag, err := bareRepo1.GetTag(t.Context(), lTagName) if err != nil { assert.NoError(t, err) return @@ -83,20 +83,20 @@ func TestRepository_GetTag(t *testing.T) { aTagMessage := "my annotated message \n - test two line" // Create the annotated tag - err = bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID) + err = bareRepo1.CreateAnnotatedTag(t.Context(), aTagName, aTagMessage, aTagCommitID) if err != nil { assert.NoError(t, err, "Unable to create the annotated tag: %s for ID: %s. Error: %v", aTagName, aTagCommitID, err) return } // Now try to get the tag for the annotated Tag - aTagID, err := bareRepo1.GetTagID(aTagName) + aTagID, err := bareRepo1.GetTagID(t.Context(), aTagName) if err != nil { assert.NoError(t, err) return } - aTag, err := bareRepo1.GetTag(aTagName) + aTag, err := bareRepo1.GetTag(t.Context(), aTagName) if err != nil { assert.NoError(t, err) return @@ -116,20 +116,20 @@ func TestRepository_GetTag(t *testing.T) { rTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0" rTagName := "release/" + lTagName - err = bareRepo1.CreateTag(rTagName, rTagCommitID) + err = bareRepo1.CreateTag(t.Context(), rTagName, rTagCommitID) if err != nil { assert.NoError(t, err, "Unable to create the tag: %s for ID: %s. Error: %v", rTagName, rTagCommitID, err) return } - rTagID, err := bareRepo1.GetTagID(rTagName) + rTagID, err := bareRepo1.GetTagID(t.Context(), rTagName) if err != nil { assert.NoError(t, err) return } assert.EqualValues(t, rTagCommitID, rTagID) - oTagID, err := bareRepo1.GetTagID(lTagName) + oTagID, err := bareRepo1.GetTagID(t.Context(), lTagName) if err != nil { assert.NoError(t, err) return @@ -155,16 +155,16 @@ func TestRepository_GetAnnotatedTag(t *testing.T) { lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1" lTagName := "lightweightTag" - bareRepo1.CreateTag(lTagName, lTagCommitID) + bareRepo1.CreateTag(t.Context(), lTagName, lTagCommitID) aTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0" aTagName := "annotatedTag" aTagMessage := "my annotated message" - bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID) - aTagID, _ := bareRepo1.GetTagID(aTagName) + bareRepo1.CreateAnnotatedTag(t.Context(), aTagName, aTagMessage, aTagCommitID) + aTagID, _ := bareRepo1.GetTagID(t.Context(), aTagName) // Try an annotated tag - tag, err := bareRepo1.GetAnnotatedTag(aTagID) + tag, err := bareRepo1.GetAnnotatedTag(t.Context(), aTagID) if err != nil { assert.NoError(t, err) return @@ -175,18 +175,18 @@ func TestRepository_GetAnnotatedTag(t *testing.T) { assert.EqualValues(t, "tag", tag.Type) // Annotated tag's Commit ID should fail - tag2, err := bareRepo1.GetAnnotatedTag(aTagCommitID) + tag2, err := bareRepo1.GetAnnotatedTag(t.Context(), aTagCommitID) assert.Error(t, err) assert.True(t, IsErrNotExist(err)) assert.Nil(t, tag2) // Annotated tag's name should fail - tag3, err := bareRepo1.GetAnnotatedTag(aTagName) + tag3, err := bareRepo1.GetAnnotatedTag(t.Context(), aTagName) assert.Errorf(t, err, "Length must be 40: %d", len(aTagName)) assert.Nil(t, tag3) // Lightweight Tag should fail - tag4, err := bareRepo1.GetAnnotatedTag(lTagCommitID) + tag4, err := bareRepo1.GetAnnotatedTag(t.Context(), lTagCommitID) assert.Error(t, err) assert.True(t, IsErrNotExist(err)) assert.Nil(t, tag4) diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 97b0343381327..1c8c10fd11bd5 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -294,7 +294,7 @@ func (shortRelease) TableName() string { // repositories like https://github.com/vim/vim (with over 13000 tags). func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository) error { log.Trace("pullMirrorReleaseSync: rebuilding releases for pull-mirror Repo[%d:%s/%s]", repo.ID, repo.OwnerName, repo.Name) - tags, numTags, err := gitRepo.GetTagInfos(0, 0) + tags, numTags, err := gitRepo.GetTagInfos(ctx, 0, 0) if err != nil { return fmt.Errorf("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err) } diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index 2e6c1c1023552..69e7f55561938 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -55,7 +55,7 @@ func ListTags(ctx *context.APIContext) { listOpts := utils.GetListOptions(ctx) - tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize) + tags, total, err := ctx.Repo.GitRepo.GetTagInfos(ctx, listOpts.Page, listOpts.PageSize) if err != nil { ctx.APIErrorInternal(err) return @@ -107,7 +107,7 @@ func GetAnnotatedTag(ctx *context.APIContext) { return } - if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil { + if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(ctx, sha); err != nil { ctx.APIError(http.StatusBadRequest, err) } else { commit, err := tag.Commit(ctx.Repo.GitRepo) @@ -148,7 +148,7 @@ func GetTag(ctx *context.APIContext) { // "$ref": "#/responses/notFound" tagName := ctx.PathParam("*") - tag, err := ctx.Repo.GitRepo.GetTag(tagName) + tag, err := ctx.Repo.GitRepo.GetTag(ctx, tagName) if err != nil { ctx.APIErrorNotFound(tagName) return @@ -218,7 +218,7 @@ func CreateTag(ctx *context.APIContext) { return } - tag, err := ctx.Repo.GitRepo.GetTag(form.TagName) + tag, err := ctx.Repo.GitRepo.GetTag(ctx, form.TagName) if err != nil { ctx.APIErrorInternal(err) return diff --git a/services/release/release.go b/services/release/release.go index 0b8a74252a08d..de64fe13e3789 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -106,7 +106,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel } if len(msg) > 0 { - if err = gitRepo.CreateAnnotatedTag(rel.TagName, msg, commit.ID.String()); err != nil { + if err = gitRepo.CreateAnnotatedTag(ctx, rel.TagName, msg, commit.ID.String()); err != nil { if strings.Contains(err.Error(), "is not a valid tag name") { return false, ErrInvalidTagName{ TagName: rel.TagName, @@ -114,7 +114,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel } return false, err } - } else if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil { + } else if err = gitRepo.CreateTag(ctx, rel.TagName, commit.ID.String()); err != nil { if strings.Contains(err.Error(), "is not a valid tag name") { return false, ErrInvalidTagName{ TagName: rel.TagName, diff --git a/services/repository/push.go b/services/repository/push.go index b5ce3a7aed871..c96b79de0669f 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -356,7 +356,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo emailToUser := make(map[string]*user_model.User) for i, lowerTag := range lowerTags { - tag, err := gitRepo.GetTag(tags[i]) + tag, err := gitRepo.GetTag(ctx, tags[i]) if err != nil { return fmt.Errorf("GetTag: %w", err) } diff --git a/tests/integration/api_releases_test.go b/tests/integration/api_releases_test.go index b3d4928b7ba2a..4ec6a98356921 100644 --- a/tests/integration/api_releases_test.go +++ b/tests/integration/api_releases_test.go @@ -114,7 +114,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) { assert.NoError(t, err) defer gitRepo.Close() - err = gitRepo.CreateTag("v0.0.1", "master") + err = gitRepo.CreateTag(t.Context(), "v0.0.1", "master") assert.NoError(t, err) target, err := gitRepo.GetTagCommitID("v0.0.1") @@ -202,7 +202,7 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) { assert.NoError(t, err) defer gitRepo.Close() - err = gitRepo.CreateTag("v0.0.1", "master") + err = gitRepo.CreateTag(t.Context(), "v0.0.1", "master") assert.NoError(t, err) createNewReleaseUsingAPI(t, token, owner, repo, "v0.0.1", "", "v0.0.1", "test") diff --git a/tests/integration/api_repo_get_contents_list_test.go b/tests/integration/api_repo_get_contents_list_test.go index b8890bcf98157..1094d7b4e20c0 100644 --- a/tests/integration/api_repo_get_contents_list_test.go +++ b/tests/integration/api_repo_get_contents_list_test.go @@ -84,7 +84,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch) // Make a new tag in repo1 newTag := "test_tag" - err = gitRepo.CreateTag(newTag, commitID) + err = gitRepo.CreateTag(t.Context(), newTag, commitID) assert.NoError(t, err) /*** END SETUP ***/ diff --git a/tests/integration/api_repo_get_contents_test.go b/tests/integration/api_repo_get_contents_test.go index 293d656d61b9d..b83bf50e22818 100644 --- a/tests/integration/api_repo_get_contents_test.go +++ b/tests/integration/api_repo_get_contents_test.go @@ -88,7 +88,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { assert.NoError(t, err) // Make a new tag in repo1 newTag := "test_tag" - err = gitRepo.CreateTag(newTag, commitID) + err = gitRepo.CreateTag(t.Context(), newTag, commitID) assert.NoError(t, err) /*** END SETUP ***/ diff --git a/tests/integration/api_repo_git_tags_test.go b/tests/integration/api_repo_git_tags_test.go index 5a6633758939d..3dac165ddddb4 100644 --- a/tests/integration/api_repo_git_tags_test.go +++ b/tests/integration/api_repo_git_tags_test.go @@ -38,12 +38,12 @@ func TestAPIGitTags(t *testing.T) { commit, _ := gitRepo.GetBranchCommit("master") lTagName := "lightweightTag" - gitRepo.CreateTag(lTagName, commit.ID.String()) + gitRepo.CreateTag(t.Context(), lTagName, commit.ID.String()) aTagName := "annotatedTag" aTagMessage := "my annotated message" - gitRepo.CreateAnnotatedTag(aTagName, aTagMessage, commit.ID.String()) - aTag, _ := gitRepo.GetTag(aTagName) + gitRepo.CreateAnnotatedTag(t.Context(), aTagName, aTagMessage, commit.ID.String()) + aTag, _ := gitRepo.GetTag(t.Context(), aTagName) // SHOULD work for annotated tags req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s", user.Name, repo.Name, aTag.ID.String()). From 64373697c18f6542a1f74bd53b89636173031663 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Fri, 14 Mar 2025 21:39:46 +0100 Subject: [PATCH 25/29] add context to CheckAtrribute --- modules/git/repo_attribute.go | 4 ++-- modules/git/repo_language_stats_nogogit.go | 6 +++--- routers/web/repo/setting/lfs.go | 2 +- services/repository/files/content.go | 2 +- services/repository/files/update.go | 2 +- services/repository/files/upload.go | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/git/repo_attribute.go b/modules/git/repo_attribute.go index 526016b791337..8c503c89679a1 100644 --- a/modules/git/repo_attribute.go +++ b/modules/git/repo_attribute.go @@ -24,7 +24,7 @@ type CheckAttributeOpts struct { } // CheckAttribute return the Blame object of file -func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) { +func (repo *Repository) CheckAttribute(ctx context.Context, opts CheckAttributeOpts) (map[string]map[string]string, error) { env := []string{} if len(opts.IndexFile) > 0 { @@ -59,7 +59,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[ cmd.AddDashesAndList(opts.Filenames...) - if err := cmd.Run(repo.Ctx, &RunOpts{ + if err := cmd.Run(ctx, &RunOpts{ Env: env, Dir: repo.Path, Stdout: stdOut, diff --git a/modules/git/repo_language_stats_nogogit.go b/modules/git/repo_language_stats_nogogit.go index 858e5ae26357e..d49fd9722904e 100644 --- a/modules/git/repo_language_stats_nogogit.go +++ b/modules/git/repo_language_stats_nogogit.go @@ -21,7 +21,7 @@ import ( func (repo *Repository) GetLanguageStats(ctx context.Context, commitID string) (map[string]int64, error) { // We will feed the commit IDs in order into cat-file --batch, followed by blobs as necessary. // so let's create a batch stdin and stdout - batchStdinWriter, batchReader, cancel, err := repo.CatFileBatch(repo.Ctx) + batchStdinWriter, batchReader, cancel, err := repo.CatFileBatch(ctx) if err != nil { return nil, err } @@ -80,8 +80,8 @@ func (repo *Repository) GetLanguageStats(ctx context.Context, commitID string) ( for _, f := range entries { select { - case <-repo.Ctx.Done(): - return sizes, repo.Ctx.Err() + case <-ctx.Done(): + return sizes, ctx.Err() default: } diff --git a/routers/web/repo/setting/lfs.go b/routers/web/repo/setting/lfs.go index 1cb84e4d2e938..09eece9e6a943 100644 --- a/routers/web/repo/setting/lfs.go +++ b/routers/web/repo/setting/lfs.go @@ -150,7 +150,7 @@ func LFSLocks(ctx *context.Context) { return } - name2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{ + name2attribute2info, err := gitRepo.CheckAttribute(ctx, git.CheckAttributeOpts{ Attributes: []string{"lockable"}, Filenames: filenames, CachedOnly: true, diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 8043069065dd0..42e024fd15511 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -279,7 +279,7 @@ func TryGetContentLanguage(ctx context.Context, gitRepo *git.Repository, commitI defer deleteTemporaryFile() - filename2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{ + filename2attribute2info, err := gitRepo.CheckAttribute(ctx, git.CheckAttributeOpts{ CachedOnly: true, Attributes: []string{git.AttributeLinguistLanguage, git.AttributeGitlabLanguage}, Filenames: []string{treePath}, diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 60e10cb246c4f..8ee6867bcba58 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -483,7 +483,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file var lfsMetaObject *git_model.LFSMetaObject if setting.LFS.StartServer && hasOldBranch { // Check there is no way this can return multiple infos - filename2attribute2info, err := t.gitRepo.CheckAttribute(git.CheckAttributeOpts{ + filename2attribute2info, err := t.gitRepo.CheckAttribute(ctx, git.CheckAttributeOpts{ Attributes: []string{"filter"}, Filenames: []string{file.Options.treePath}, CachedOnly: true, diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go index 2e4ed1744ef7c..0c19f6f9b8bf0 100644 --- a/services/repository/files/upload.go +++ b/services/repository/files/upload.go @@ -107,7 +107,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use var filename2attribute2info map[string]map[string]string if setting.LFS.StartServer { - filename2attribute2info, err = t.gitRepo.CheckAttribute(git.CheckAttributeOpts{ + filename2attribute2info, err = t.gitRepo.CheckAttribute(ctx, git.CheckAttributeOpts{ Attributes: []string{"filter"}, Filenames: names, CachedOnly: true, From 4a60296f2a1743503262c46b5d9e2f157a9d6022 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Sat, 15 Mar 2025 12:48:02 +0100 Subject: [PATCH 26/29] fix gogit build --- modules/git/repo_language_stats_gogit.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_language_stats_gogit.go b/modules/git/repo_language_stats_gogit.go index a34c03c781f55..4f15b2ecebed3 100644 --- a/modules/git/repo_language_stats_gogit.go +++ b/modules/git/repo_language_stats_gogit.go @@ -7,6 +7,7 @@ package git import ( "bytes" + "context" "io" "code.gitea.io/gitea/modules/analyze" @@ -19,7 +20,7 @@ import ( ) // GetLanguageStats calculates language stats for git repository at specified commit -func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) { +func (repo *Repository) GetLanguageStats(ctx context.Context, commitID string) (map[string]int64, error) { r, err := git.PlainOpen(repo.Path) if err != nil { return nil, err @@ -40,7 +41,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err return nil, err } - checker, deferable := repo.CheckAttributeReader(commitID) + checker, deferable := repo.CheckAttributeReader(ctx, commitID) defer deferable() // sizes contains the current calculated size of all files by language From 3932669f7b7d2b270321ed43e40d6b2fec59652a Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Sat, 15 Mar 2025 13:01:22 +0100 Subject: [PATCH 27/29] add context to repo_index commands --- modules/git/repo_index.go | 30 +++++++++++------------ routers/web/repo/setting/lfs.go | 2 +- services/pull/patch.go | 4 +-- services/repository/contributors_graph.go | 6 ++--- services/wiki/wiki.go | 10 ++++---- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/modules/git/repo_index.go b/modules/git/repo_index.go index 21c3b5ba1a7b7..ca5e511d38158 100644 --- a/modules/git/repo_index.go +++ b/modules/git/repo_index.go @@ -34,15 +34,15 @@ func (repo *Repository) ReadTreeToIndex(ctx context.Context, treeish string, ind if err != nil { return err } - return repo.readTreeToIndex(id, indexFilename...) + return repo.readTreeToIndex(ctx, id, indexFilename...) } -func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) error { +func (repo *Repository) readTreeToIndex(ctx context.Context, id ObjectID, indexFilename ...string) error { var env []string if len(indexFilename) > 0 { env = append(os.Environ(), "GIT_INDEX_FILE="+indexFilename[0]) } - _, _, err := NewCommand("read-tree").AddDynamicArguments(id.String()).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path, Env: env}) + _, _, err := NewCommand("read-tree").AddDynamicArguments(id.String()).RunStdString(ctx, &RunOpts{Dir: repo.Path, Env: env}) if err != nil { return err } @@ -82,15 +82,15 @@ func (repo *Repository) ReadTreeToTemporaryIndex(ctx context.Context, treeish st } // EmptyIndex empties the index -func (repo *Repository) EmptyIndex() error { - _, _, err := NewCommand("read-tree", "--empty").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) EmptyIndex(ctx context.Context) error { + _, _, err := NewCommand("read-tree", "--empty").RunStdString(ctx, &RunOpts{Dir: repo.Path}) return err } // LsFiles checks if the given filenames are in the index -func (repo *Repository) LsFiles(filenames ...string) ([]string, error) { +func (repo *Repository) LsFiles(ctx context.Context, filenames ...string) ([]string, error) { cmd := NewCommand("ls-files", "-z").AddDashesAndList(filenames...) - res, _, err := cmd.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + res, _, err := cmd.RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func (repo *Repository) LsFiles(filenames ...string) ([]string, error) { } // RemoveFilesFromIndex removes given filenames from the index - it does not check whether they are present. -func (repo *Repository) RemoveFilesFromIndex(filenames ...string) error { +func (repo *Repository) RemoveFilesFromIndex(ctx context.Context, filenames ...string) error { objectFormat, err := repo.GetObjectFormat() if err != nil { return err @@ -118,7 +118,7 @@ func (repo *Repository) RemoveFilesFromIndex(filenames ...string) error { buffer.WriteString("0 blob " + objectFormat.EmptyObjectID().String() + "\t" + file + "\000") } } - return cmd.Run(repo.Ctx, &RunOpts{ + return cmd.Run(ctx, &RunOpts{ Dir: repo.Path, Stdin: bytes.NewReader(buffer.Bytes()), Stdout: stdout, @@ -133,7 +133,7 @@ type IndexObjectInfo struct { } // AddObjectsToIndex adds the provided object hashes to the index at the provided filenames -func (repo *Repository) AddObjectsToIndex(objects ...IndexObjectInfo) error { +func (repo *Repository) AddObjectsToIndex(ctx context.Context, objects ...IndexObjectInfo) error { cmd := NewCommand("update-index", "--add", "--replace", "-z", "--index-info") stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) @@ -142,7 +142,7 @@ func (repo *Repository) AddObjectsToIndex(objects ...IndexObjectInfo) error { // using format: mode SP type SP sha1 TAB path buffer.WriteString(object.Mode + " blob " + object.Object.String() + "\t" + object.Filename + "\000") } - return cmd.Run(repo.Ctx, &RunOpts{ + return cmd.Run(ctx, &RunOpts{ Dir: repo.Path, Stdin: bytes.NewReader(buffer.Bytes()), Stdout: stdout, @@ -151,13 +151,13 @@ func (repo *Repository) AddObjectsToIndex(objects ...IndexObjectInfo) error { } // AddObjectToIndex adds the provided object hash to the index at the provided filename -func (repo *Repository) AddObjectToIndex(mode string, object ObjectID, filename string) error { - return repo.AddObjectsToIndex(IndexObjectInfo{Mode: mode, Object: object, Filename: filename}) +func (repo *Repository) AddObjectToIndex(ctx context.Context, mode string, object ObjectID, filename string) error { + return repo.AddObjectsToIndex(ctx, IndexObjectInfo{Mode: mode, Object: object, Filename: filename}) } // WriteTree writes the current index as a tree to the object db and returns its hash -func (repo *Repository) WriteTree() (*Tree, error) { - stdout, _, runErr := NewCommand("write-tree").RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) +func (repo *Repository) WriteTree(ctx context.Context) (*Tree, error) { + stdout, _, runErr := NewCommand("write-tree").RunStdString(ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } diff --git a/routers/web/repo/setting/lfs.go b/routers/web/repo/setting/lfs.go index 09eece9e6a943..8afaf446586fd 100644 --- a/routers/web/repo/setting/lfs.go +++ b/routers/web/repo/setting/lfs.go @@ -174,7 +174,7 @@ func LFSLocks(ctx *context.Context) { } ctx.Data["Lockables"] = lockables - filelist, err := gitRepo.LsFiles(filenames...) + filelist, err := gitRepo.LsFiles(ctx, filenames...) if err != nil { log.Error("Unable to lsfiles in %s (%v)", tmpBasePath, err) ctx.ServerError("LFSLocks", err) diff --git a/services/pull/patch.go b/services/pull/patch.go index 860cd341d5fcc..a3f3ecded45ed 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -298,10 +298,10 @@ func AttemptThreeWayMerge(ctx context.Context, gitPath string, gitRepo *git.Repo } // Add and remove files in one command, as this is slow with many files otherwise - if err := gitRepo.RemoveFilesFromIndex(filesToRemove...); err != nil { + if err := gitRepo.RemoveFilesFromIndex(ctx, filesToRemove...); err != nil { return false, nil, err } - if err := gitRepo.AddObjectsToIndex(filesToAdd...); err != nil { + if err := gitRepo.AddObjectsToIndex(ctx, filesToAdd...); err != nil { return false, nil, err } diff --git a/services/repository/contributors_graph.go b/services/repository/contributors_graph.go index a4ae505313959..5d39ef41080a1 100644 --- a/services/repository/contributors_graph.go +++ b/services/repository/contributors_graph.go @@ -111,7 +111,7 @@ func GetContributorStats(ctx context.Context, cache cache.StringCache, repo *rep } // getExtendedCommitStats return the list of *ExtendedCommitStats for the given revision -func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*ExtendedCommitStats, error) { +func getExtendedCommitStats(ctx context.Context, repo *git.Repository, revision string /*, limit int */) ([]*ExtendedCommitStats, error) { baseCommit, err := repo.GetCommit(revision) if err != nil { return nil, err @@ -131,7 +131,7 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int var extendedCommitStats []*ExtendedCommitStats stderr := new(strings.Builder) - err = gitCmd.Run(repo.Ctx, &git.RunOpts{ + err = gitCmd.Run(ctx, &git.RunOpts{ Dir: repo.Path, Stdout: stdoutWriter, Stderr: stderr, @@ -212,7 +212,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.StringCache, ca if len(revision) == 0 { revision = repo.DefaultBranch } - extendedCommitStats, err := getExtendedCommitStats(gitRepo, revision) + extendedCommitStats, err := getExtendedCommitStats(ctx, gitRepo, revision) if err != nil { _ = cache.PutJSON(cacheKey, fmt.Errorf("ExtendedCommitStats: %w", err), contributorStatsCacheTimeout) return diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b2f41a05fb1e8..79593e0afb1f4 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -163,7 +163,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } if isOldWikiExist { - err := gitRepo.RemoveFilesFromIndex(oldWikiPath) + err := gitRepo.RemoveFilesFromIndex(ctx, oldWikiPath) if err != nil { log.Error("RemoveFilesFromIndex failed: %v", err) return err @@ -179,12 +179,12 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return err } - if err := gitRepo.AddObjectToIndex("100644", objectHash, newWikiPath); err != nil { + if err := gitRepo.AddObjectToIndex(ctx, "100644", objectHash, newWikiPath); err != nil { log.Error("AddObjectToIndex failed: %v", err) return err } - tree, err := gitRepo.WriteTree() + tree, err := gitRepo.WriteTree(ctx) if err != nil { log.Error("WriteTree failed: %v", err) return err @@ -300,7 +300,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return err } if found { - err := gitRepo.RemoveFilesFromIndex(wikiPath) + err := gitRepo.RemoveFilesFromIndex(ctx, wikiPath) if err != nil { return err } @@ -310,7 +310,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model // FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here - tree, err := gitRepo.WriteTree() + tree, err := gitRepo.WriteTree(ctx) if err != nil { return err } From 97e767e95311762ac6b8075f00cb364e3098afc0 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Sat, 15 Mar 2025 13:19:03 +0100 Subject: [PATCH 28/29] add context to Size --- modules/git/blob_gogit.go | 3 ++- modules/git/blob_nogogit.go | 5 +++-- modules/issue/template/unmarshal.go | 17 +++++++++-------- routers/api/v1/repo/file.go | 8 ++++---- routers/api/v1/repo/repo.go | 2 +- routers/api/v1/repo/wiki.go | 2 +- routers/common/serve.go | 2 +- routers/web/repo/blame.go | 2 +- routers/web/repo/compare.go | 2 +- routers/web/repo/editor.go | 6 +++--- routers/web/repo/issue_list.go | 2 +- routers/web/repo/issue_new.go | 14 +++++++------- routers/web/repo/issue_view.go | 2 +- routers/web/repo/middlewares.go | 2 +- routers/web/repo/milestone.go | 2 +- routers/web/repo/pull.go | 2 +- routers/web/repo/view.go | 6 +++--- routers/web/repo/view_file.go | 2 +- services/context/repo.go | 4 ++-- services/issue/template.go | 9 +++++---- services/markup/renderhelper_codepreview.go | 2 +- services/repository/files/content.go | 4 ++-- 22 files changed, 52 insertions(+), 48 deletions(-) diff --git a/modules/git/blob_gogit.go b/modules/git/blob_gogit.go index 8c79c067c1749..1c7ae1d854abf 100644 --- a/modules/git/blob_gogit.go +++ b/modules/git/blob_gogit.go @@ -7,6 +7,7 @@ package git import ( + "context" "io" "github.com/go-git/go-git/v5/plumbing" @@ -27,6 +28,6 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) { } // Size returns the uncompressed size of the blob -func (b *Blob) Size() int64 { +func (b *Blob) Size(_ context.Context) int64 { return b.gogitEncodedObj.Size() } diff --git a/modules/git/blob_nogogit.go b/modules/git/blob_nogogit.go index af3ce376d6a46..46a51b9b683f0 100644 --- a/modules/git/blob_nogogit.go +++ b/modules/git/blob_nogogit.go @@ -8,6 +8,7 @@ package git import ( "bufio" "bytes" + "context" "io" "code.gitea.io/gitea/modules/log" @@ -62,12 +63,12 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) { } // Size returns the uncompressed size of the blob -func (b *Blob) Size() int64 { +func (b *Blob) Size(ctx context.Context) int64 { if b.gotSize { return b.size } - wr, rd, cancel, err := b.repo.CatFileBatchCheck(b.repo.Ctx) + wr, rd, cancel, err := b.repo.CatFileBatchCheck(ctx) if err != nil { log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err) return 0 diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index 1d8e9dd02d995..24ab1642c3980 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -4,6 +4,7 @@ package template import ( + "context" "fmt" "io" "path" @@ -42,31 +43,31 @@ func Unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { } // UnmarshalFromEntry parses out a valid template from the blob in entry -func UnmarshalFromEntry(entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) { - return unmarshalFromEntry(entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here +func UnmarshalFromEntry(ctx context.Context, entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) { + return unmarshalFromEntry(ctx, entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here } // UnmarshalFromCommit parses out a valid template from the commit -func UnmarshalFromCommit(commit *git.Commit, filename string) (*api.IssueTemplate, error) { +func UnmarshalFromCommit(ctx context.Context, commit *git.Commit, filename string) (*api.IssueTemplate, error) { entry, err := commit.GetTreeEntryByPath(filename) if err != nil { return nil, fmt.Errorf("get entry for %q: %w", filename, err) } - return unmarshalFromEntry(entry, filename) + return unmarshalFromEntry(ctx, entry, filename) } // UnmarshalFromRepo parses out a valid template from the head commit of the branch -func UnmarshalFromRepo(repo *git.Repository, branch, filename string) (*api.IssueTemplate, error) { +func UnmarshalFromRepo(ctx context.Context, repo *git.Repository, branch, filename string) (*api.IssueTemplate, error) { commit, err := repo.GetBranchCommit(branch) if err != nil { return nil, fmt.Errorf("get commit on branch %q: %w", branch, err) } - return UnmarshalFromCommit(commit, filename) + return UnmarshalFromCommit(ctx, commit, filename) } -func unmarshalFromEntry(entry *git.TreeEntry, filename string) (*api.IssueTemplate, error) { - if size := entry.Blob().Size(); size > setting.UI.MaxDisplayFileSize { +func unmarshalFromEntry(ctx context.Context, entry *git.TreeEntry, filename string) (*api.IssueTemplate, error) { + if size := entry.Blob().Size(ctx); size > setting.UI.MaxDisplayFileSize { return nil, fmt.Errorf("too large: %v > MaxDisplayFileSize", size) } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 09c7f124a7b66..641bf4763902d 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -137,7 +137,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) { ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) // LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file - if blob.Size() > 1024 { + if blob.Size(ctx) > 1024 { // First handle caching for the blob if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) { return @@ -180,7 +180,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) { } // OK not cached - serve! - common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf)) + common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(ctx), bytes.NewReader(buf)) return } @@ -194,7 +194,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) { return } - common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf)) + common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(ctx), bytes.NewReader(buf)) return } else if err != nil { ctx.APIErrorInternal(err) @@ -384,7 +384,7 @@ func GetEditorconfig(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) + ec, _, err := ctx.Repo.GetEditorconfig(ctx, ctx.Repo.Commit) if err != nil { if git.IsErrNotExist(err) { ctx.APIErrorNotFound(err) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 3d638cb05e029..a7ae4065b9748 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -1200,7 +1200,7 @@ func GetIssueTemplates(ctx *context.APIContext) { // "$ref": "#/responses/IssueTemplates" // "404": // "$ref": "#/responses/notFound" - ret := issue.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + ret := issue.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) if cnt := len(ret.TemplateErrors); cnt != 0 { ctx.Resp.Header().Add("X-Gitea-Warning", "error occurs when parsing issue template: count="+strconv.Itoa(cnt)) } diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 25f79aca34c2a..705e8845a1189 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -502,7 +502,7 @@ func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) // given tree entry, encoded with base64. Writes to ctx if an error occurs. func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string { blob := entry.Blob() - if blob.Size() > setting.API.DefaultMaxBlobSize { + if blob.Size(ctx) > setting.API.DefaultMaxBlobSize { return "" } content, err := blob.GetBlobContentBase64() diff --git a/routers/common/serve.go b/routers/common/serve.go index 862230b30f048..3d8ef5edc1fd4 100644 --- a/routers/common/serve.go +++ b/routers/common/serve.go @@ -35,7 +35,7 @@ func ServeBlob(ctx *context.Base, repo *repo_model.Repository, filePath string, }() _ = repo.LoadOwner(ctx) - httplib.ServeContentByReader(ctx.Req, ctx.Resp, blob.Size(), dataRc, &httplib.ServeHeaderOptions{ + httplib.ServeContentByReader(ctx.Req, ctx.Resp, blob.Size(ctx), dataRc, &httplib.ServeHeaderOptions{ Filename: path.Base(filePath), CacheIsPublic: !repo.IsPrivate && repo.Owner != nil && repo.Owner.Visibility == structs.VisibleTypePublic, CacheDuration: setting.StaticCacheTime, diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 72d4c32e68d10..f1b41add6ca84 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -67,7 +67,7 @@ func RefBlame(ctx *context.Context) { ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) blob := entry.Blob() - fileSize := blob.Size() + fileSize := blob.Size(ctx) ctx.Data["FileSize"] = fileSize ctx.Data["FileName"] = blob.Name() diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 3d2fd51ca8ab0..df34a964503fc 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -137,7 +137,7 @@ func setCsvCompareContext(ctx *context.Context) { return nil, nil, nil } - if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() { + if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size(ctx) { return nil, nil, errTooLarge } diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 113622f87293c..94845140d05ba 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -146,7 +146,7 @@ func editFile(ctx *context.Context, isNewFile bool) { } blob := entry.Blob() - if blob.Size() >= setting.UI.MaxDisplayFileSize { + if blob.Size(ctx) >= setting.UI.MaxDisplayFileSize { ctx.NotFound(err) return } @@ -159,7 +159,7 @@ func editFile(ctx *context.Context, isNewFile bool) { defer dataRc.Close() - ctx.Data["FileSize"] = blob.Size() + ctx.Data["FileSize"] = blob.Size(ctx) ctx.Data["FileName"] = blob.Name() buf := make([]byte, 1024) @@ -201,7 +201,7 @@ func editFile(ctx *context.Context, isNewFile bool) { // GetEditorConfig returns a editorconfig JSON string for given treePath or "null" func GetEditorConfig(ctx *context.Context, treePath string) string { - ec, _, err := ctx.Repo.GetEditorconfig() + ec, _, err := ctx.Repo.GetEditorconfig(ctx) if err == nil { def, err := ec.GetDefinitionForFilename(treePath) if err == nil { diff --git a/routers/web/repo/issue_list.go b/routers/web/repo/issue_list.go index a65ae77795584..2dac5b5fcf033 100644 --- a/routers/web/repo/issue_list.go +++ b/routers/web/repo/issue_list.go @@ -765,7 +765,7 @@ func Issues(ctx *context.Context) { } ctx.Data["Title"] = ctx.Tr("repo.issues") ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) } issues(ctx, ctx.FormInt64("milestone"), ctx.FormInt64("project"), optional.Some(isPullList)) diff --git a/routers/web/repo/issue_new.go b/routers/web/repo/issue_new.go index 9f52396414f8b..3faf49015f932 100644 --- a/routers/web/repo/issue_new.go +++ b/routers/web/repo/issue_new.go @@ -53,7 +53,7 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles if ok, _ := commit.HasFile(filename); !ok { continue } - template, err := issue_template.UnmarshalFromCommit(commit, filename) + template, err := issue_template.UnmarshalFromCommit(ctx, commit, filename) if err != nil { templateErrs[filename] = err continue @@ -98,7 +98,7 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles // NewIssue render creating issue page func NewIssue(ctx *context.Context) { issueConfig, _ := issue_service.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) - hasTemplates := issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) + hasTemplates := issue_service.HasTemplatesOrContactLinks(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true @@ -134,7 +134,7 @@ func NewIssue(ctx *context.Context) { } ctx.Data["Tags"] = tags - ret := issue_service.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + ret := issue_service.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) templateLoaded, errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates, pageMetaData) for k, v := range errs { ret.TemplateErrors[k] = v @@ -187,14 +187,14 @@ func NewIssueChooseTemplate(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true - ret := issue_service.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + ret := issue_service.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["IssueTemplates"] = ret.IssueTemplates if len(ret.TemplateErrors) > 0 { ctx.Flash.Warning(renderErrorOfTemplates(ctx, ret.TemplateErrors), true) } - if !issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) { + if !issue_service.HasTemplatesOrContactLinks(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) { // The "issues/new" and "issues/new/choose" share the same query parameters "project" and "milestone", if no template here, just redirect to the "issues/new" page with these parameters. ctx.Redirect(fmt.Sprintf("%s/issues/new?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther) return @@ -329,7 +329,7 @@ func NewIssuePost(ctx *context.Context) { form := web.GetForm(ctx).(*forms.CreateIssueForm) ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled upload.AddUploadContext(ctx, "comment") @@ -370,7 +370,7 @@ func NewIssuePost(ctx *context.Context) { content := form.Content if filename := ctx.Req.Form.Get("template-file"); filename != "" { - if template, err := issue_template.UnmarshalFromRepo(ctx.Repo.GitRepo, ctx.Repo.Repository.DefaultBranch, filename); err == nil { + if template, err := issue_template.UnmarshalFromRepo(ctx, ctx.Repo.GitRepo, ctx.Repo.Repository.DefaultBranch, filename); err == nil { content = issue_template.RenderToMarkdown(template, ctx.Req.Form) } } diff --git a/routers/web/repo/issue_view.go b/routers/web/repo/issue_view.go index b312f1260a13e..6915535e096f9 100644 --- a/routers/web/repo/issue_view.go +++ b/routers/web/repo/issue_view.go @@ -330,7 +330,7 @@ func ViewIssue(ctx *context.Context) { return } ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) } ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanRead(unit.TypeProjects) diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go index 7518e6feae916..7d2634686cd16 100644 --- a/routers/web/repo/middlewares.go +++ b/routers/web/repo/middlewares.go @@ -18,7 +18,7 @@ func SetEditorconfigIfExists(ctx *context.Context) { return } - ec, _, err := ctx.Repo.GetEditorconfig() + ec, _, err := ctx.Repo.GetEditorconfig(ctx) if err != nil { // it used to check `!git.IsErrNotExist(err)` and create a system notice, but it is quite annoying and useless // because network errors also happen frequently, so we just ignore it diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index f1d0a857eaa70..ef00b2f100ede 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -265,7 +265,7 @@ func MilestoneIssuesAndPulls(ctx *context.Context) { issues(ctx, milestoneID, projectID, optional.None[bool]()) - ret := issue.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + ret := issue.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["NewIssueChooseTemplate"] = len(ret.IssueTemplates) > 0 ctx.Data["CanWriteIssues"] = ctx.Repo.CanWriteIssuesOrPulls(false) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 0c5ef6c1f6c0f..233a3d4b48c7e 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1318,7 +1318,7 @@ func CompareAndPullRequestPost(ctx *context.Context) { content := form.Content if filename := ctx.Req.Form.Get("template-file"); filename != "" { - if template, err := issue_template.UnmarshalFromRepo(ctx.Repo.GitRepo, ctx.Repo.Repository.DefaultBranch, filename); err == nil { + if template, err := issue_template.UnmarshalFromRepo(ctx, ctx.Repo.GitRepo, ctx.Repo.Repository.DefaultBranch, filename); err == nil { content = issue_template.RenderToMarkdown(template, ctx.Req.Form) } } diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 6ed5801d10fa7..7cd1f1d66f343 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -80,18 +80,18 @@ func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) ([]byte, // FIXME: what happens when README file is an image? if !isTextFile || !setting.LFS.StartServer { - return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil + return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(ctx), nil, st}, nil } pointer, _ := lfs.ReadPointerFromBuffer(buf) if !pointer.IsValid() { // fallback to plain file - return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil + return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(ctx), nil, st}, nil } meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid) if err != nil { // fallback to plain file log.Warn("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err) - return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil + return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(ctx), nil, st}, nil } dataRc.Close() diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 6eab512ea3fc7..d5a2511a6ed0c 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -57,7 +57,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { } if ctx.Repo.TreePath == ".editorconfig" { - _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) + _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx, ctx.Repo.Commit) if editorconfigWarning != nil { ctx.Data["FileWarning"] = strings.TrimSpace(editorconfigWarning.Error()) } diff --git a/services/context/repo.go b/services/context/repo.go index 6eccd1312a971..738e7c405b91b 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -203,7 +203,7 @@ func (r *Repository) RefTypeNameSubURL() string { // GetEditorconfig returns the .editorconfig definition if found in the // HEAD of the default repo branch. -func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (cfg *editorconfig.Editorconfig, warning, err error) { +func (r *Repository) GetEditorconfig(ctx context.Context, optCommit ...*git.Commit) (cfg *editorconfig.Editorconfig, warning, err error) { if r.GitRepo == nil { return nil, nil, nil } @@ -222,7 +222,7 @@ func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (cfg *editorconfi if err != nil { return nil, nil, err } - if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize { + if treeEntry.Blob().Size(ctx) >= setting.UI.MaxDisplayFileSize { return nil, nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"} } reader, err := treeEntry.Blob().DataAsync() diff --git a/services/issue/template.go b/services/issue/template.go index 4b0f1aa987047..87af5b59ae423 100644 --- a/services/issue/template.go +++ b/services/issue/template.go @@ -4,6 +4,7 @@ package issue import ( + "context" "fmt" "io" "net/url" @@ -109,7 +110,7 @@ func IsTemplateConfig(path string) bool { // ParseTemplatesFromDefaultBranch parses the issue templates in the repo's default branch, // returns valid templates and the errors of invalid template files (the errors map is guaranteed to be non-nil). -func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct { +func ParseTemplatesFromDefaultBranch(ctx context.Context, repo *repo.Repository, gitRepo *git.Repository) (ret struct { IssueTemplates []*api.IssueTemplate TemplateErrors map[string]error }, @@ -140,7 +141,7 @@ func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Reposit continue } fullName := path.Join(dirName, entry.Name()) - if it, err := template.UnmarshalFromEntry(entry, dirName); err != nil { + if it, err := template.UnmarshalFromEntry(ctx, entry, dirName); err != nil { ret.TemplateErrors[fullName] = err } else { if !strings.HasPrefix(it.Ref, "refs/") { // Assume that the ref intended is always a branch - for tags users should use refs/tags/ @@ -178,8 +179,8 @@ func GetTemplateConfigFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repo return GetDefaultTemplateConfig(), nil } -func HasTemplatesOrContactLinks(repo *repo.Repository, gitRepo *git.Repository) bool { - ret := ParseTemplatesFromDefaultBranch(repo, gitRepo) +func HasTemplatesOrContactLinks(ctx context.Context, repo *repo.Repository, gitRepo *git.Repository) bool { + ret := ParseTemplatesFromDefaultBranch(ctx, repo, gitRepo) if len(ret.IssueTemplates) > 0 { return true } diff --git a/services/markup/renderhelper_codepreview.go b/services/markup/renderhelper_codepreview.go index c2170f01e815a..b927f1d1894d1 100644 --- a/services/markup/renderhelper_codepreview.go +++ b/services/markup/renderhelper_codepreview.go @@ -67,7 +67,7 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie return "", err } - if blob.Size() > setting.UI.MaxDisplayFileSize { + if blob.Size(ctx) > setting.UI.MaxDisplayFileSize { return "", fmt.Errorf("file is too large") } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 42e024fd15511..4c8e8cf2da3cb 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -255,7 +255,7 @@ func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git return nil, err } content := "" - if gitBlob.Size() <= setting.API.DefaultMaxBlobSize { + if gitBlob.Size(ctx) <= setting.API.DefaultMaxBlobSize { content, err = gitBlob.GetBlobContentBase64() if err != nil { return nil, err @@ -264,7 +264,7 @@ func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git return &api.GitBlobResponse{ SHA: gitBlob.ID.String(), URL: repo.APIURL() + "/git/blobs/" + url.PathEscape(gitBlob.ID.String()), - Size: gitBlob.Size(), + Size: gitBlob.Size(ctx), Encoding: "base64", Content: content, }, nil From 51220438eff12881dba2da31e78e2c93f4530f29 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Sun, 16 Mar 2025 14:29:26 +0100 Subject: [PATCH 29/29] fix build --- modules/git/repo_tag.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go index ca95fafe2c7f7..d36c27a601aac 100644 --- a/modules/git/repo_tag.go +++ b/modules/git/repo_tag.go @@ -5,6 +5,7 @@ package git import ( + "context" "fmt" "io" "strings"