diff options
author | zeripath <art27@cantab.net> | 2022-01-19 23:26:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-19 23:26:57 +0000 |
commit | 5cb0c9aa0d7eed087055b1efca79628957207d36 (patch) | |
tree | d117a514e1f17e5f6bfcda1be273f6a971112663 /modules/git | |
parent | 4563148a61ba892e8f2bb66342f00a950bcd5315 (diff) | |
download | gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip |
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git
commands run within the request context.
This now means that the if there is a git repo already open in the context it will be used instead of reopening it.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/git')
-rw-r--r-- | modules/git/blame.go | 6 | ||||
-rw-r--r-- | modules/git/commit.go | 31 | ||||
-rw-r--r-- | modules/git/commit_info_test.go | 2 | ||||
-rw-r--r-- | modules/git/commit_test.go | 6 | ||||
-rw-r--r-- | modules/git/diff.go | 14 | ||||
-rw-r--r-- | modules/git/pipeline/catfile.go | 13 | ||||
-rw-r--r-- | modules/git/pipeline/lfs.go | 2 | ||||
-rw-r--r-- | modules/git/pipeline/lfs_nogogit.go | 4 | ||||
-rw-r--r-- | modules/git/pipeline/namerev.go | 5 | ||||
-rw-r--r-- | modules/git/pipeline/revlist.go | 9 | ||||
-rw-r--r-- | modules/git/repo.go | 169 | ||||
-rw-r--r-- | modules/git/repo_base.go | 49 | ||||
-rw-r--r-- | modules/git/repo_base_gogit.go | 3 | ||||
-rw-r--r-- | modules/git/repo_base_nogogit.go | 3 | ||||
-rw-r--r-- | modules/git/repo_branch.go | 4 | ||||
-rw-r--r-- | modules/git/repo_branch_gogit.go | 12 | ||||
-rw-r--r-- | modules/git/repo_commit.go | 6 | ||||
-rw-r--r-- | modules/git/repo_compare.go | 15 | ||||
-rw-r--r-- | modules/git/repo_test.go | 2 | ||||
-rw-r--r-- | modules/git/tree.go | 2 | ||||
-rw-r--r-- | modules/git/tree_entry_nogogit.go | 16 | ||||
-rw-r--r-- | modules/git/tree_nogogit.go | 4 |
22 files changed, 147 insertions, 230 deletions
diff --git a/modules/git/blame.go b/modules/git/blame.go index b30124594d..40e3d4e885 100644 --- a/modules/git/blame.go +++ b/modules/git/blame.go @@ -114,12 +114,6 @@ func (r *BlameReader) Close() error { // CreateBlameReader creates reader for given repository, commit and file func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) { - gitRepo, err := OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return nil, err - } - gitRepo.Close() - return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file) } diff --git a/modules/git/commit.go b/modules/git/commit.go index 3e3641b096..8a78653553 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -8,6 +8,7 @@ package git import ( "bufio" "bytes" + "context" "errors" "fmt" "io" @@ -138,12 +139,12 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt } // AllCommitsCount returns count of all commits in repository -func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64, error) { +func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, files ...string) (int64, error) { args := []string{"--all", "--count"} if hidePRRefs { args = append([]string{"--exclude=" + PullPrefix + "*"}, args...) } - cmd := NewCommand("rev-list") + cmd := NewCommandContext(ctx, "rev-list") cmd.AddArguments(args...) if len(files) > 0 { cmd.AddArguments("--") @@ -159,8 +160,8 @@ func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64, } // CommitsCountFiles returns number of total commits of until given revision. -func CommitsCountFiles(repoPath string, revision, relpath []string) (int64, error) { - cmd := NewCommand("rev-list", "--count") +func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath []string) (int64, error) { + cmd := NewCommandContext(ctx, "rev-list", "--count") cmd.AddArguments(revision...) if len(relpath) > 0 { cmd.AddArguments("--") @@ -176,13 +177,13 @@ func CommitsCountFiles(repoPath string, revision, relpath []string) (int64, erro } // CommitsCount returns number of total commits of until given revision. -func CommitsCount(repoPath string, revision ...string) (int64, error) { - return CommitsCountFiles(repoPath, revision, []string{}) +func CommitsCount(ctx context.Context, repoPath string, revision ...string) (int64, error) { + return CommitsCountFiles(ctx, repoPath, revision, []string{}) } // CommitsCount returns number of total commits of until current revision. func (c *Commit) CommitsCount() (int64, error) { - return CommitsCount(c.repo.Path, c.ID.String()) + return CommitsCount(c.repo.Ctx, c.repo.Path, c.ID.String()) } // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize @@ -205,7 +206,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) { } if err := CheckGitVersionAtLeast("1.8"); err == nil { - _, err := NewCommand("merge-base", "--is-ancestor", that, this).RunInDir(c.repo.Path) + _, err := NewCommandContext(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunInDir(c.repo.Path) if err == nil { return true, nil } @@ -218,7 +219,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) { return false, err } - result, err := NewCommand("rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunInDir(c.repo.Path) + result, err := NewCommandContext(c.repo.Ctx, "rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunInDir(c.repo.Path) if err != nil { return false, err } @@ -380,7 +381,7 @@ func (c *Commit) GetBranchName() (string, error) { } args = append(args, "--name-only", "--no-undefined", c.ID.String()) - data, err := NewCommand(args...).RunInDir(c.repo.Path) + data, err := NewCommandContext(c.repo.Ctx, args...).RunInDir(c.repo.Path) if err != nil { // handle special case where git can not describe commit if strings.Contains(err.Error(), "cannot describe") { @@ -406,7 +407,7 @@ func (c *Commit) LoadBranchName() (err error) { // GetTagName gets the current tag name for given commit func (c *Commit) GetTagName() (string, error) { - data, err := NewCommand("describe", "--exact-match", "--tags", "--always", c.ID.String()).RunInDir(c.repo.Path) + data, err := NewCommandContext(c.repo.Ctx, "describe", "--exact-match", "--tags", "--always", c.ID.String()).RunInDir(c.repo.Path) if err != nil { // handle special case where there is no tag for this commit if strings.Contains(err.Error(), "no tag exactly matches") { @@ -473,7 +474,7 @@ func parseCommitFileStatus(fileStatus *CommitFileStatus, stdout io.Reader) { } // GetCommitFileStatus returns file status of commit in given repository. -func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) { +func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*CommitFileStatus, error) { stdout, w := io.Pipe() done := make(chan struct{}) fileStatus := NewCommitFileStatus() @@ -485,7 +486,7 @@ func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) { stderr := new(bytes.Buffer) args := []string{"log", "--name-status", "-c", "--pretty=format:", "--parents", "--no-renames", "-z", "-1", commitID} - err := NewCommand(args...).RunInDirPipeline(repoPath, w, stderr) + err := NewCommandContext(ctx, args...).RunInDirPipeline(repoPath, w, stderr) w.Close() // Close writer to exit parsing goroutine if err != nil { return nil, ConcatenateError(err, stderr.String()) @@ -496,8 +497,8 @@ func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) { } // GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository. -func GetFullCommitID(repoPath, shortID string) (string, error) { - commitID, err := NewCommand("rev-parse", shortID).RunInDir(repoPath) +func GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error) { + commitID, err := NewCommandContext(ctx, "rev-parse", shortID).RunInDir(repoPath) if err != nil { if strings.Contains(err.Error(), "exit status 128") { return "", ErrNotExist{shortID, ""} diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index 8467bdaa90..a4c0483004 100644 --- a/modules/git/commit_info_test.go +++ b/modules/git/commit_info_test.go @@ -24,7 +24,7 @@ func cloneRepo(url, dir, name string) (string, error) { if _, err := os.Stat(repoDir); err == nil { return repoDir, nil } - return repoDir, Clone(url, repoDir, CloneRepoOptions{ + return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{ Mirror: false, Bare: false, Quiet: true, diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 57132c00dc..2b3365342d 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -15,7 +15,7 @@ import ( func TestCommitsCount(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - commitsCount, err := CommitsCount(bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") + commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") assert.NoError(t, err) assert.Equal(t, int64(3), commitsCount) } @@ -23,7 +23,7 @@ func TestCommitsCount(t *testing.T) { func TestGetFullCommitID(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - id, err := GetFullCommitID(bareRepo1Path, "8006ff9a") + id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "8006ff9a") assert.NoError(t, err) assert.Equal(t, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", id) } @@ -31,7 +31,7 @@ func TestGetFullCommitID(t *testing.T) { func TestGetFullCommitIDError(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - id, err := GetFullCommitID(bareRepo1Path, "unknown") + id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "unknown") assert.Empty(t, id) if assert.Error(t, err) { assert.EqualError(t, err, "object does not exist [id: unknown, rel_path: ]") diff --git a/modules/git/diff.go b/modules/git/diff.go index f90f911be0..02ed2c60c4 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -30,17 +30,17 @@ const ( ) // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. -func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { - return GetRawDiffForFile(repoPath, "", commitID, diffType, "", writer) +func GetRawDiff(ctx context.Context, repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { + return GetRawDiffForFile(ctx, repoPath, "", commitID, diffType, "", writer) } // GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer. -func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { - repo, err := OpenRepository(repoPath) +func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { + repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } - defer repo.Close() + defer closer.Close() return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer) } @@ -276,7 +276,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi } // GetAffectedFiles returns the affected files between two commits -func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repository) ([]string, error) { +func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []string) ([]string, error) { stdoutReader, stdoutWriter, err := os.Pipe() if err != nil { log.Error("Unable to create os.Pipe for %s", repo.Path) @@ -290,7 +290,7 @@ func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repos affectedFiles := make([]string, 0, 32) // Run `git diff --name-only` to get the names of the changed files - err = NewCommand("diff", "--name-only", oldCommitID, newCommitID). + err = NewCommandContext(repo.Ctx, "diff", "--name-only", oldCommitID, newCommitID). RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path, stdoutWriter, nil, nil, func(ctx context.Context, cancel context.CancelFunc) error { diff --git a/modules/git/pipeline/catfile.go b/modules/git/pipeline/catfile.go index 7293cf9d7f..0ce5b5e06e 100644 --- a/modules/git/pipeline/catfile.go +++ b/modules/git/pipeline/catfile.go @@ -7,6 +7,7 @@ package pipeline import ( "bufio" "bytes" + "context" "fmt" "io" "strconv" @@ -18,27 +19,27 @@ import ( ) // CatFileBatchCheck runs cat-file with --batch-check -func CatFileBatchCheck(shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { +func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { defer wg.Done() defer shasToCheckReader.Close() defer catFileCheckWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - cmd := git.NewCommand("cat-file", "--batch-check") + cmd := git.NewCommandContext(ctx, "cat-file", "--batch-check") if err := cmd.RunInDirFullPipeline(tmpBasePath, catFileCheckWriter, stderr, shasToCheckReader); err != nil { _ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %v - %s", tmpBasePath, err, errbuf.String())) } } // CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all -func CatFileBatchCheckAllObjects(catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) { +func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) { defer wg.Done() defer catFileCheckWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - cmd := git.NewCommand("cat-file", "--batch-check", "--batch-all-objects") + cmd := git.NewCommandContext(ctx, "cat-file", "--batch-check", "--batch-all-objects") if err := cmd.RunInDirPipeline(tmpBasePath, catFileCheckWriter, stderr); err != nil { log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String()) err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String()) @@ -48,14 +49,14 @@ func CatFileBatchCheckAllObjects(catFileCheckWriter *io.PipeWriter, wg *sync.Wai } // CatFileBatch runs cat-file --batch -func CatFileBatch(shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { +func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { defer wg.Done() defer shasToBatchReader.Close() defer catFileBatchWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - if err := git.NewCommand("cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil { + if err := git.NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil { _ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())) } } diff --git a/modules/git/pipeline/lfs.go b/modules/git/pipeline/lfs.go index 4551ccd3f4..1b64b672e4 100644 --- a/modules/git/pipeline/lfs.go +++ b/modules/git/pipeline/lfs.go @@ -120,7 +120,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) { i++ } }() - go NameRevStdin(shasToNameReader, nameRevStdinWriter, &wg, basePath) + go NameRevStdin(repo.Ctx, shasToNameReader, nameRevStdinWriter, &wg, basePath) go func() { defer wg.Done() defer shasToNameWriter.Close() diff --git a/modules/git/pipeline/lfs_nogogit.go b/modules/git/pipeline/lfs_nogogit.go index 1352aa7662..3b993606ac 100644 --- a/modules/git/pipeline/lfs_nogogit.go +++ b/modules/git/pipeline/lfs_nogogit.go @@ -53,7 +53,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) { go func() { stderr := strings.Builder{} - err := git.NewCommand("rev-list", "--all").RunInDirPipeline(repo.Path, revListWriter, &stderr) + err := git.NewCommandContext(repo.Ctx, "rev-list", "--all").RunInDirPipeline(repo.Path, revListWriter, &stderr) if err != nil { _ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String())) } else { @@ -212,7 +212,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) { i++ } }() - go NameRevStdin(shasToNameReader, nameRevStdinWriter, &wg, basePath) + go NameRevStdin(repo.Ctx, shasToNameReader, nameRevStdinWriter, &wg, basePath) go func() { defer wg.Done() defer shasToNameWriter.Close() diff --git a/modules/git/pipeline/namerev.go b/modules/git/pipeline/namerev.go index eebb53b0ca..43f93f8f7e 100644 --- a/modules/git/pipeline/namerev.go +++ b/modules/git/pipeline/namerev.go @@ -6,6 +6,7 @@ package pipeline import ( "bytes" + "context" "fmt" "io" "strings" @@ -15,14 +16,14 @@ import ( ) // NameRevStdin runs name-rev --stdin -func NameRevStdin(shasToNameReader *io.PipeReader, nameRevStdinWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { +func NameRevStdin(ctx context.Context, shasToNameReader *io.PipeReader, nameRevStdinWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) { defer wg.Done() defer shasToNameReader.Close() defer nameRevStdinWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - if err := git.NewCommand("name-rev", "--stdin", "--name-only", "--always").RunInDirFullPipeline(tmpBasePath, nameRevStdinWriter, stderr, shasToNameReader); err != nil { + if err := git.NewCommandContext(ctx, "name-rev", "--stdin", "--name-only", "--always").RunInDirFullPipeline(tmpBasePath, nameRevStdinWriter, stderr, shasToNameReader); err != nil { _ = shasToNameReader.CloseWithError(fmt.Errorf("git name-rev [%s]: %v - %s", tmpBasePath, err, errbuf.String())) } } diff --git a/modules/git/pipeline/revlist.go b/modules/git/pipeline/revlist.go index 4e13e19444..373ceea1fb 100644 --- a/modules/git/pipeline/revlist.go +++ b/modules/git/pipeline/revlist.go @@ -7,6 +7,7 @@ package pipeline import ( "bufio" "bytes" + "context" "fmt" "io" "strings" @@ -17,13 +18,13 @@ import ( ) // RevListAllObjects runs rev-list --objects --all and writes to a pipewriter -func RevListAllObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) { +func RevListAllObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) { defer wg.Done() defer revListWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - cmd := git.NewCommand("rev-list", "--objects", "--all") + cmd := git.NewCommandContext(ctx, "rev-list", "--objects", "--all") if err := cmd.RunInDirPipeline(basePath, revListWriter, stderr); err != nil { log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String()) err = fmt.Errorf("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String()) @@ -33,12 +34,12 @@ func RevListAllObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePat } // RevListObjects run rev-list --objects from headSHA to baseSHA -func RevListObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) { +func RevListObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) { defer wg.Done() defer revListWriter.Close() stderr := new(bytes.Buffer) var errbuf strings.Builder - cmd := git.NewCommand("rev-list", "--objects", headSHA, "--not", baseSHA) + cmd := git.NewCommandContext(ctx, "rev-list", "--objects", headSHA, "--not", baseSHA) if err := cmd.RunInDirPipeline(tmpBasePath, revListWriter, stderr); err != nil { log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()) errChan <- fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()) diff --git a/modules/git/repo.go b/modules/git/repo.go index b19e80cd44..591ef7362b 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -34,7 +34,7 @@ const prettyLogFormat = `--pretty=format:%H` // GetAllCommitsCount returns count of all commits in repository func (repo *Repository) GetAllCommitsCount() (int64, error) { - return AllCommitsCount(repo.Path, false) + return AllCommitsCount(repo.Ctx, repo.Path, false) } func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) { @@ -57,19 +57,19 @@ func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, erro } // IsRepoURLAccessible checks if given repository URL is accessible. -func IsRepoURLAccessible(url string) bool { - _, err := NewCommand("ls-remote", "-q", "-h", url, "HEAD").Run() +func IsRepoURLAccessible(ctx context.Context, url string) bool { + _, err := NewCommandContext(ctx, "ls-remote", "-q", "-h", url, "HEAD").Run() return err == nil } // InitRepository initializes a new Git repository. -func InitRepository(repoPath string, bare bool) error { +func InitRepository(ctx context.Context, repoPath string, bare bool) error { err := os.MkdirAll(repoPath, os.ModePerm) if err != nil { return err } - cmd := NewCommand("init") + cmd := NewCommandContext(ctx, "init") if bare { cmd.AddArguments("--bare") } @@ -80,7 +80,7 @@ func InitRepository(repoPath string, bare bool) error { // IsEmpty Check if repository is empty. func (repo *Repository) IsEmpty() (bool, error) { var errbuf strings.Builder - if err := NewCommand("log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil { + if err := NewCommandContext(repo.Ctx, "log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil { if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") || strings.Contains(errbuf.String(), "fatal: your current branch 'master' does not have any commits yet") { return true, nil @@ -104,12 +104,7 @@ type CloneRepoOptions struct { } // Clone clones original repository to target path. -func Clone(from, to string, opts CloneRepoOptions) error { - return CloneWithContext(DefaultContext, from, to, opts) -} - -// CloneWithContext clones original repository to target path. -func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) error { +func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error { cargs := make([]string, len(GlobalCommandArgs)) copy(cargs, GlobalCommandArgs) return CloneWithArgs(ctx, from, to, cargs, opts) @@ -171,35 +166,6 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo return nil } -// PullRemoteOptions options when pull from remote -type PullRemoteOptions struct { - Timeout time.Duration - All bool - Rebase bool - Remote string - Branch string -} - -// Pull pulls changes from remotes. -func Pull(repoPath string, opts PullRemoteOptions) error { - cmd := NewCommand("pull") - if opts.Rebase { - cmd.AddArguments("--rebase") - } - if opts.All { - cmd.AddArguments("--all") - } else { - cmd.AddArguments("--", opts.Remote, opts.Branch) - } - - if opts.Timeout <= 0 { - opts.Timeout = -1 - } - - _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath) - return err -} - // PushOptions options when push to remote type PushOptions struct { Remote string @@ -262,116 +228,9 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error { return err } -// CheckoutOptions options when heck out some branch -type CheckoutOptions struct { - Timeout time.Duration - Branch string - OldBranch string -} - -// Checkout checkouts a branch -func Checkout(repoPath string, opts CheckoutOptions) error { - cmd := NewCommand("checkout") - if len(opts.OldBranch) > 0 { - cmd.AddArguments("-b") - } - - if opts.Timeout <= 0 { - opts.Timeout = -1 - } - - cmd.AddArguments(opts.Branch) - - if len(opts.OldBranch) > 0 { - cmd.AddArguments(opts.OldBranch) - } - - _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath) - return err -} - -// ResetHEAD resets HEAD to given revision or head of branch. -func ResetHEAD(repoPath string, hard bool, revision string) error { - cmd := NewCommand("reset") - if hard { - cmd.AddArguments("--hard") - } - _, err := cmd.AddArguments(revision).RunInDir(repoPath) - return err -} - -// MoveFile moves a file to another file or directory. -func MoveFile(repoPath, oldTreeName, newTreeName string) error { - _, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath) - return err -} - -// CountObject represents repository count objects report -type CountObject struct { - Count int64 - Size int64 - InPack int64 - Packs int64 - SizePack int64 - PrunePack int64 - Garbage int64 - SizeGarbage int64 -} - -const ( - statCount = "count: " - statSize = "size: " - statInpack = "in-pack: " - statPacks = "packs: " - statSizePack = "size-pack: " - statPrunePackage = "prune-package: " - statGarbage = "garbage: " - statSizeGarbage = "size-garbage: " -) - -// CountObjects returns the results of git count-objects on the repoPath -func CountObjects(repoPath string) (*CountObject, error) { - cmd := NewCommand("count-objects", "-v") - stdout, err := cmd.RunInDir(repoPath) - if err != nil { - return nil, err - } - - return parseSize(stdout), nil -} - -// parseSize parses the output from count-objects and return a CountObject -func parseSize(objects string) *CountObject { - repoSize := new(CountObject) - for _, line := range strings.Split(objects, "\n") { - switch { - case strings.HasPrefix(line, statCount): - repoSize.Count, _ = strconv.ParseInt(line[7:], 10, 64) - case strings.HasPrefix(line, statSize): - repoSize.Size, _ = strconv.ParseInt(line[6:], 10, 64) - repoSize.Size *= 1024 - case strings.HasPrefix(line, statInpack): - repoSize.InPack, _ = strconv.ParseInt(line[9:], 10, 64) - case strings.HasPrefix(line, statPacks): - repoSize.Packs, _ = strconv.ParseInt(line[7:], 10, 64) - case strings.HasPrefix(line, statSizePack): - repoSize.Count, _ = strconv.ParseInt(line[11:], 10, 64) - repoSize.Count *= 1024 - case strings.HasPrefix(line, statPrunePackage): - repoSize.PrunePack, _ = strconv.ParseInt(line[16:], 10, 64) - case strings.HasPrefix(line, statGarbage): - repoSize.Garbage, _ = strconv.ParseInt(line[9:], 10, 64) - case strings.HasPrefix(line, statSizeGarbage): - repoSize.SizeGarbage, _ = strconv.ParseInt(line[14:], 10, 64) - repoSize.SizeGarbage *= 1024 - } - } - return repoSize -} - // GetLatestCommitTime returns time for latest commit in repository (across all branches) -func GetLatestCommitTime(repoPath string) (time.Time, error) { - cmd := NewCommand("for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)") +func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error) { + cmd := NewCommandContext(ctx, "for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)") stdout, err := cmd.RunInDir(repoPath) if err != nil { return time.Time{}, err @@ -386,9 +245,9 @@ type DivergeObject struct { Behind int } -func checkDivergence(repoPath, baseBranch, targetBranch string) (int, error) { +func checkDivergence(ctx context.Context, repoPath, baseBranch, targetBranch string) (int, error) { branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch) - cmd := NewCommand("rev-list", "--count", branches) + cmd := NewCommandContext(ctx, "rev-list", "--count", branches) stdout, err := cmd.RunInDir(repoPath) if err != nil { return -1, err @@ -401,15 +260,15 @@ func checkDivergence(repoPath, baseBranch, targetBranch string) (int, error) { } // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch -func GetDivergingCommits(repoPath, baseBranch, targetBranch string) (DivergeObject, error) { +func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (DivergeObject, error) { // $(git rev-list --count master..feature) commits ahead of master - ahead, errorAhead := checkDivergence(repoPath, baseBranch, targetBranch) + ahead, errorAhead := checkDivergence(ctx, repoPath, baseBranch, targetBranch) if errorAhead != nil { return DivergeObject{}, errorAhead } // $(git rev-list --count feature..master) commits behind master - behind, errorBehind := checkDivergence(repoPath, targetBranch, baseBranch) + behind, errorBehind := checkDivergence(ctx, repoPath, targetBranch, baseBranch) if errorBehind != nil { return DivergeObject{}, errorBehind } diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go new file mode 100644 index 0000000000..8c2f19f6d7 --- /dev/null +++ b/modules/git/repo_base.go @@ -0,0 +1,49 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "context" + "io" +) + +// contextKey is a value for use with context.WithValue. +type contextKey struct { + name string +} + +// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context +var RepositoryContextKey = &contextKey{"repository"} + +// RepositoryFromContext attempts to get the repository from the context +func RepositoryFromContext(ctx context.Context, path string) *Repository { + value := ctx.Value(RepositoryContextKey) + if value == nil { + return nil + } + + if repo, ok := value.(*Repository); ok && repo != nil { + if repo.Path == path { + return repo + } + } + + return nil +} + +type nopCloser func() + +func (nopCloser) Close() error { return nil } + +// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it +func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository, io.Closer, error) { + gitRepo := RepositoryFromContext(ctx, path) + if gitRepo != nil { + return gitRepo, nopCloser(nil), nil + } + + gitRepo, err := OpenRepositoryCtx(ctx, path) + return gitRepo, gitRepo, err +} diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index 7299526562..23009d80f7 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -73,13 +73,14 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error } // Close this repository, in particular close the underlying gogitStorage if this is not nil -func (repo *Repository) Close() { +func (repo *Repository) Close() (err error) { if repo == nil || repo.gogitStorage == nil { return } if err := repo.gogitStorage.Close(); err != nil { gitealog.Error("Error closing storage: %v", err) } + return } // GoGitRepo gets the go-git repo representation diff --git a/modules/git/repo_base_nogogit.go b/modules/git/repo_base_nogogit.go index e264fd4a14..ad4a00be1f 100644 --- a/modules/git/repo_base_nogogit.go +++ b/modules/git/repo_base_nogogit.go @@ -86,7 +86,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError } // Close this repository, in particular close the underlying gogitStorage if this is not nil -func (repo *Repository) Close() { +func (repo *Repository) Close() (err error) { if repo == nil { return } @@ -102,4 +102,5 @@ func (repo *Repository) Close() { repo.checkReader = nil repo.checkWriter = nil } + return } diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 71a81935d6..38c0a180cd 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -88,8 +88,8 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) { // GetBranchesByPath returns a branch by it's path // if limit = 0 it will not limit -func GetBranchesByPath(path string, skip, limit int) ([]*Branch, int, error) { - gitRepo, err := OpenRepository(path) +func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) { + gitRepo, err := OpenRepositoryCtx(ctx, path) if err != nil { return nil, 0, err } diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index d159aafd6f..57952bcc64 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -83,11 +83,15 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { // WalkReferences walks all the references from the repository func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) error) (int, error) { - repo, err := OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return 0, err + repo := RepositoryFromContext(ctx, repoPath) + if repo == nil { + var err error + repo, err = OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return 0, err + } + defer repo.Close() } - defer repo.Close() i := 0 iter, err := repo.gogitRepo.References() diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 896ad34518..ed04ee2f67 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -195,7 +195,7 @@ 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 CommitsCountFiles(repo.Path, []string{revision}, []string{file}) + return CommitsCountFiles(repo.Ctx, repo.Path, []string{revision}, []string{file}) } // CommitsByFileAndRange return the commits according revision file and the page @@ -321,11 +321,11 @@ 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 := CommitsCountFiles(repo.Path, []string{start + ".." + end}, []string{}) + count, err := CommitsCountFiles(repo.Ctx, repo.Path, []string{start + ".." + end}, []string{}) 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 CommitsCountFiles(repo.Path, []string{start, end}, []string{}) + return CommitsCountFiles(repo.Ctx, repo.Path, []string{start, end}, []string{}) } return count, err diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index fb0ba91265..ffede62ede 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -8,6 +8,7 @@ package git import ( "bufio" "bytes" + "context" "errors" "fmt" "io" @@ -72,14 +73,14 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, compareInfo := new(CompareInfo) - compareInfo.HeadCommitID, err = GetFullCommitID(repo.Path, headBranch) + compareInfo.HeadCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, headBranch) if err != nil { compareInfo.HeadCommitID = headBranch } compareInfo.MergeBase, remoteBranch, err = repo.GetMergeBase(tmpRemote, baseBranch, headBranch) if err == nil { - compareInfo.BaseCommitID, err = GetFullCommitID(repo.Path, remoteBranch) + compareInfo.BaseCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch) if err != nil { compareInfo.BaseCommitID = remoteBranch } @@ -105,7 +106,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, } } else { compareInfo.Commits = []*Commit{} - compareInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch) + compareInfo.MergeBase, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch) if err != nil { compareInfo.MergeBase = remoteBranch } @@ -163,15 +164,15 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis // GetDiffShortStat counts number of changed files, number of additions and deletions func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) { - numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Path, base+"..."+head) + numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Ctx, repo.Path, base+"..."+head) if err != nil && strings.Contains(err.Error(), "no merge base") { - return GetDiffShortStat(repo.Path, base, head) + return GetDiffShortStat(repo.Ctx, repo.Path, base, head) } return } // GetDiffShortStat counts number of changed files, number of additions and deletions -func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) { +func GetDiffShortStat(ctx context.Context, repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) { // Now if we call: // $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 // we get: @@ -181,7 +182,7 @@ func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions "--shortstat", }, args...) - stdout, err := NewCommand(args...).RunInDir(repoPath) + stdout, err := NewCommandContext(ctx, args...).RunInDir(repoPath) if err != nil { return 0, 0, 0, err } diff --git a/modules/git/repo_test.go b/modules/git/repo_test.go index 9517783e6b..e143c1b39d 100644 --- a/modules/git/repo_test.go +++ b/modules/git/repo_test.go @@ -13,7 +13,7 @@ import ( func TestGetLatestCommitTime(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - lct, err := GetLatestCommitTime(bareRepo1Path) + lct, err := GetLatestCommitTime(DefaultContext, bareRepo1Path) assert.NoError(t, err) // Time is Sun Jul 21 22:43:13 2019 +0200 // which is the time of commit diff --git a/modules/git/tree.go b/modules/git/tree.go index 3671f421e9..dd73afadcd 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -49,7 +49,7 @@ 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) { - cmd := NewCommand("ls-tree", "-z", "--name-only", "--", ref) + cmd := NewCommandContext(repo.Ctx, "ls-tree", "-z", "--name-only", "--", ref) for _, arg := range filenames { if arg != "" { cmd.AddArguments(arg) diff --git a/modules/git/tree_entry_nogogit.go b/modules/git/tree_entry_nogogit.go index 288ec4db6e..076062e1d6 100644 --- a/modules/git/tree_entry_nogogit.go +++ b/modules/git/tree_entry_nogogit.go @@ -7,10 +7,7 @@ package git -import ( - "strconv" - "strings" -) +import "code.gitea.io/gitea/modules/log" // TreeEntry the leaf in the git tree type TreeEntry struct { @@ -47,13 +44,20 @@ func (te *TreeEntry) Size() int64 { return te.size } - stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path) + wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx) + defer cancel() + _, err := wr.Write([]byte(te.ID.String() + "\n")) if err != nil { + log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err) + return 0 + } + _, _, te.size, err = ReadBatchLine(rd) + if err != nil { + log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err) return 0 } te.sized = true - te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) return te.size } diff --git a/modules/git/tree_nogogit.go b/modules/git/tree_nogogit.go index cfa2291e8f..f71e02f742 100644 --- a/modules/git/tree_nogogit.go +++ b/modules/git/tree_nogogit.go @@ -81,7 +81,7 @@ func (t *Tree) ListEntries() (Entries, error) { } } - stdout, err := NewCommand("ls-tree", "-l", t.ID.String()).RunInDirBytes(t.repo.Path) + stdout, err := NewCommandContext(t.repo.Ctx, "ls-tree", "-l", t.ID.String()).RunInDirBytes(t.repo.Path) if err != nil { if strings.Contains(err.Error(), "fatal: Not a valid object name") || strings.Contains(err.Error(), "fatal: not a tree object") { return nil, ErrNotExist{ @@ -104,7 +104,7 @@ func (t *Tree) ListEntriesRecursive() (Entries, error) { if t.entriesRecursiveParsed { return t.entriesRecursive, nil } - stdout, err := NewCommand("ls-tree", "-t", "-l", "-r", t.ID.String()).RunInDirBytes(t.repo.Path) + stdout, err := NewCommandContext(t.repo.Ctx, "ls-tree", "-t", "-l", "-r", t.ID.String()).RunInDirBytes(t.repo.Path) if err != nil { return nil, err } |