Skip to content

preparations to remove context from repo struct #33893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7dee037
add context to GetMergeBase
TheFox0x7 Mar 14, 2025
3631512
add context to GetDiffBinary
TheFox0x7 Mar 14, 2025
162899b
add context to GetPatch
TheFox0x7 Mar 14, 2025
0099c82
use function provided context in GetCompareInfo
TheFox0x7 Mar 14, 2025
34153fd
add context to GetCommitByPath
TheFox0x7 Mar 14, 2025
dfaff77
add context to CommitBetweenLimit
TheFox0x7 Mar 14, 2025
44a6fde
add context to commitsBefore
TheFox0x7 Mar 14, 2025
7fbf350
add context to commitsByRange
TheFox0x7 Mar 14, 2025
3ad5ee2
add contect to GetDiff and SearchCommits
TheFox0x7 Mar 14, 2025
30767c3
add context to FileCommitsCount and FilesCountBetween
TheFox0x7 Mar 14, 2025
1611740
add context to IsCommitInBranch and CommitsBetweenIDs
TheFox0x7 Mar 14, 2025
ea27930
add context to GetTreePathLatestCommit
TheFox0x7 Mar 14, 2025
d2c85c4
add context to CommitsByFileAndRange and FileChangedBetweenCommits
TheFox0x7 Mar 14, 2025
4f1709b
add context to LsTree
TheFox0x7 Mar 14, 2025
3a0af2c
add context to GetRepoRawDiffForFile
TheFox0x7 Mar 14, 2025
8fd4716
add context to GetDiffNumChangedFiles
TheFox0x7 Mar 14, 2025
a4e5604
add context to GetCodeActivityStats
TheFox0x7 Mar 14, 2025
e8f1fb3
add context to LineBlame
TheFox0x7 Mar 14, 2025
00bb0b7
add context to GetDefaultPublicKey
TheFox0x7 Mar 14, 2025
bd41dd4
add context to branch commands
TheFox0x7 Mar 14, 2025
a933423
add context to remaining branch commands
TheFox0x7 Mar 14, 2025
6087fa4
add context to ReadTreeToIndex
TheFox0x7 Mar 14, 2025
ad250db
add context to CommitTree
TheFox0x7 Mar 14, 2025
0d52636
add context to tag commands
TheFox0x7 Mar 14, 2025
6437369
add context to CheckAtrribute
TheFox0x7 Mar 14, 2025
4a60296
fix gogit build
TheFox0x7 Mar 15, 2025
3932669
add context to repo_index commands
TheFox0x7 Mar 15, 2025
97e767e
add context to Size
TheFox0x7 Mar 15, 2025
5122043
fix build
TheFox0x7 Mar 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions models/activities/repo_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion modules/git/blob_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package git

import (
"context"
"io"

"github.com/go-git/go-git/v5/plumbing"
Expand All @@ -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()
}
5 changes: 3 additions & 2 deletions modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package git
import (
"bufio"
"bytes"
"context"
"io"

"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -199,13 +199,13 @@ 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
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
Expand Down Expand Up @@ -249,17 +249,17 @@ 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
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/git/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions modules/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions modules/git/last_commit_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package git

import (
"context"
"crypto/sha256"
"fmt"

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions modules/git/repo_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {}
}
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions modules/git/repo_blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions modules/git/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -114,36 +114,36 @@ 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
}

// 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
}

Expand All @@ -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
}
Loading