refactor: make git package handle all git operations (#38543)

before: gitrepo vs git packages
after: git package fully handle all git operations

by the way, use `WithRepo(repo)` instead of `WithDir(repo.Path)` to hide
path details.

benefits:

1. remove all unnecessary wrappers, developers no need to struggle with
"which package should be used"
2. simplify code, RepositoryFacade can (will) be used everywhere, all
"path" details are (will be) hidden
This commit is contained in:
wxiaoguang
2026-07-21 00:07:38 +08:00
committed by GitHub
parent b8977eeb47
commit 9dc04289aa
214 changed files with 859 additions and 1301 deletions

View File

@@ -50,7 +50,7 @@ const prettyLogFormat = `--pretty=format:%H`
func (repo *Repository) ShowPrettyFormatLogToList(ctx context.Context, revisionRange string) ([]*Commit, error) {
// avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git <command> [<revision>...] -- [<file>...]'
logs, _, err := gitcmd.NewCommand("log").AddArguments(prettyLogFormat).
AddDynamicArguments(revisionRange).AddArguments("--").WithDir(repo.Path).
AddDynamicArguments(revisionRange).AddArguments("--").WithRepo(repo).
RunStdBytes(ctx)
if err != nil {
return nil, err
@@ -86,7 +86,7 @@ func (repo *Repository) getCommitByPathWithID(ctx context.Context, id ObjectID,
stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat).
AddDynamicArguments(id.String()).
AddDashesAndList(relpath).
WithDir(repo.Path).
WithRepo(repo).
RunStdString(ctx)
if runErr != nil {
return nil, runErr
@@ -104,7 +104,7 @@ func (repo *Repository) getCommitByPathWithID(ctx context.Context, id ObjectID,
func (repo *Repository) GetCommitByPath(ctx context.Context, relpath string) (*Commit, error) {
stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat).
AddDashesAndList(relpath).
WithDir(repo.Path).
WithRepo(repo).
RunStdBytes(ctx)
if runErr != nil {
return nil, runErr
@@ -138,7 +138,7 @@ func (repo *Repository) commitsByRangeWithTime(ctx context.Context, id ObjectID,
cmd.AddOptionFormat("--until=%s", until)
}
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(ctx)
stdout, _, err := cmd.WithRepo(repo).RunStdBytes(ctx)
if err != nil {
return nil, err
}
@@ -192,7 +192,7 @@ func (repo *Repository) searchCommits(ctx context.Context, id ObjectID, opts Sea
// search for commits matching given constraints and keywords in commit msg
addCommonSearchArgs(cmd)
stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(ctx)
stdout, _, err := cmd.WithRepo(repo).RunStdBytes(ctx)
if err != nil {
return nil, err
}
@@ -213,7 +213,7 @@ func (repo *Repository) searchCommits(ctx context.Context, id ObjectID, opts Sea
hashCmd.AddDynamicArguments(v)
// search with given constraints for commit matching sha hash of v
hashMatching, _, err := hashCmd.WithDir(repo.Path).RunStdBytes(ctx)
hashMatching, _, err := hashCmd.WithRepo(repo).RunStdBytes(ctx)
if err != nil || bytes.Contains(stdout, hashMatching) {
continue
}
@@ -231,7 +231,7 @@ func (repo *Repository) FileChangedBetweenCommits(ctx context.Context, filename,
stdout, _, err := gitcmd.NewCommand("diff", "--name-only", "-z").
AddDynamicArguments(id1, id2).
AddDashesAndList(filename).
WithDir(repo.Path).
WithRepo(repo).
RunStdBytes(ctx)
if err != nil {
return false, err
@@ -275,7 +275,7 @@ func (repo *Repository) CommitsByFileAndRange(ctx context.Context, opts CommitsB
stdoutReader, stdoutReaderClose := gitCmd.MakeStdoutPipe()
defer stdoutReaderClose()
err := gitCmd.WithDir(repo.Path).
err := gitCmd.WithRepo(repo).
WithPipelineFunc(func(context gitcmd.Context) error {
objectFormat, err := repo.GetObjectFormat(ctx)
if err != nil {
@@ -316,7 +316,7 @@ func (repo *Repository) CommitsByFileAndRange(ctx context.Context, opts CommitsB
// If "before" and "after" are not related, it returns the all commits for the "after" commit.
func (repo *Repository) CommitsBetween(ctx context.Context, afterRef, beforeRef RefName, limit int, optSkip ...int) ([]*Commit, error) {
gitCmd := func() *gitcmd.Command {
cmd := gitcmd.NewCommand("rev-list").WithDir(repo.Path)
cmd := gitcmd.NewCommand("rev-list").WithRepo(repo)
if limit >= 0 {
cmd.AddOptionValues("--max-count", strconv.Itoa(limit))
}
@@ -351,7 +351,7 @@ func (repo *Repository) commitsBefore(ctx context.Context, id ObjectID, limit in
}
cmd.AddDynamicArguments(id.String())
stdout, _, runErr := cmd.WithDir(repo.Path).RunStdBytes(ctx)
stdout, _, runErr := cmd.WithRepo(repo).RunStdBytes(ctx)
if runErr != nil {
return nil, runErr
}
@@ -392,7 +392,7 @@ func (repo *Repository) getBranches(ctx context.Context, env []string, commitID
AddOptionValues("--contains", commitID).
AddArguments(BranchPrefix).
WithEnv(env).
WithDir(repo.Path).
WithRepo(repo).
RunStdString(ctx)
if err != nil {
return nil, err
@@ -418,7 +418,7 @@ func (repo *Repository) GetCommitsFromIDs(ctx context.Context, commitIDs []strin
func (repo *Repository) IsCommitInBranch(ctx context.Context, commitID, branch string) (r bool, err error) {
stdout, _, err := gitcmd.NewCommand("branch", "--contains").
AddDynamicArguments(commitID, branch).
WithDir(repo.Path).
WithRepo(repo).
RunStdString(ctx)
if err != nil {
return false, err
@@ -431,7 +431,7 @@ func (repo *Repository) GetCommitBranchStart(ctx context.Context, env []string,
cmd := gitcmd.NewCommand("log", prettyLogFormat)
cmd.AddDynamicArguments(endCommitID)
stdout, _, runErr := cmd.WithDir(repo.Path).
stdout, _, runErr := cmd.WithRepo(repo).
WithEnv(env).
RunStdBytes(ctx)
if runErr != nil {