mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 20:43:50 +00:00 
			
		
		
		
	Move some functions to gitrepo package (#35543)
Refactor Git command functions to use WithXXX methods instead of exposing RunOpts. This change simplifies reuse across gitrepo and improves consistency, encapsulation, and maintainability of command options. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -60,7 +60,11 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com | ||||
| 		relpath = `\` + relpath | ||||
| 	} | ||||
|  | ||||
| 	stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat). | ||||
| 		AddDynamicArguments(id.String()). | ||||
| 		AddDashesAndList(relpath). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdString(repo.Ctx) | ||||
| 	if runErr != nil { | ||||
| 		return nil, runErr | ||||
| 	} | ||||
| @@ -75,7 +79,10 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com | ||||
|  | ||||
| // GetCommitByPath returns the last commit of relative path. | ||||
| func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { | ||||
| 	stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat).AddDashesAndList(relpath).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, runErr := gitcmd.NewCommand("log", "-1", prettyLogFormat). | ||||
| 		AddDashesAndList(relpath). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdBytes(repo.Ctx) | ||||
| 	if runErr != nil { | ||||
| 		return nil, runErr | ||||
| 	} | ||||
| @@ -108,7 +115,7 @@ func (repo *Repository) commitsByRangeWithTime(id ObjectID, page, pageSize int, | ||||
| 		cmd.AddOptionFormat("--until=%s", until) | ||||
| 	} | ||||
|  | ||||
| 	stdout, _, err := cmd.RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -162,7 +169,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, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -183,7 +190,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, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 			hashMatching, _, err := hashCmd.WithDir(repo.Path).RunStdBytes(repo.Ctx) | ||||
| 			if err != nil || bytes.Contains(stdout, hashMatching) { | ||||
| 				continue | ||||
| 			} | ||||
| @@ -198,7 +205,11 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([ | ||||
| // 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 := gitcmd.NewCommand("diff", "--name-only", "-z").AddDynamicArguments(id1, id2).AddDashesAndList(filename).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := gitcmd.NewCommand("diff", "--name-only", "-z"). | ||||
| 		AddDynamicArguments(id1, id2). | ||||
| 		AddDashesAndList(filename). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdBytes(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| @@ -249,11 +260,10 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) | ||||
| 		} | ||||
|  | ||||
| 		gitCmd.AddDashesAndList(opts.File) | ||||
| 		err := gitCmd.Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 			Dir:    repo.Path, | ||||
| 			Stdout: stdoutWriter, | ||||
| 			Stderr: &stderr, | ||||
| 		}) | ||||
| 		err := gitCmd.WithDir(repo.Path). | ||||
| 			WithStdout(stdoutWriter). | ||||
| 			WithStderr(&stderr). | ||||
| 			Run(repo.Ctx) | ||||
| 		if err != nil { | ||||
| 			_ = stdoutWriter.CloseWithError(gitcmd.ConcatenateError(err, (&stderr).String())) | ||||
| 		} else { | ||||
| @@ -291,11 +301,17 @@ 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 := gitcmd.NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID+"..."+endCommitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := gitcmd.NewCommand("diff", "--name-only"). | ||||
| 		AddDynamicArguments(startCommitID + "..." + endCommitID). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdString(repo.Ctx) | ||||
| 	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 = gitcmd.NewCommand("diff", "--name-only").AddDynamicArguments(startCommitID, endCommitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		stdout, _, err = gitcmd.NewCommand("diff", "--name-only"). | ||||
| 			AddDynamicArguments(startCommitID, endCommitID). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdString(repo.Ctx) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| @@ -309,13 +325,22 @@ func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) | ||||
| 	var stdout []byte | ||||
| 	var err error | ||||
| 	if before == nil { | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddDynamicArguments(last.ID.String()). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 	} else { | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddDynamicArguments(before.ID.String() + ".." + last.ID.String()). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 		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 = gitcmd.NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 			stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 				AddDynamicArguments(before.ID.String(), last.ID.String()). | ||||
| 				WithDir(repo.Path). | ||||
| 				RunStdBytes(repo.Ctx) | ||||
| 		} | ||||
| 	} | ||||
| 	if err != nil { | ||||
| @@ -332,19 +357,25 @@ func (repo *Repository) CommitsBetweenLimit(last, before *Commit, limit, skip in | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddOptionValues("--max-count", strconv.Itoa(limit)). | ||||
| 			AddOptionValues("--skip", strconv.Itoa(skip)). | ||||
| 			AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 			AddDynamicArguments(last.ID.String()). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 	} else { | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddOptionValues("--max-count", strconv.Itoa(limit)). | ||||
| 			AddOptionValues("--skip", strconv.Itoa(skip)). | ||||
| 			AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 			AddDynamicArguments(before.ID.String() + ".." + last.ID.String()). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 		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 = gitcmd.NewCommand("rev-list"). | ||||
| 				AddOptionValues("--max-count", strconv.Itoa(limit)). | ||||
| 				AddOptionValues("--skip", strconv.Itoa(skip)). | ||||
| 				AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 				AddDynamicArguments(before.ID.String(), last.ID.String()). | ||||
| 				WithDir(repo.Path). | ||||
| 				RunStdBytes(repo.Ctx) | ||||
| 		} | ||||
| 	} | ||||
| 	if err != nil { | ||||
| @@ -359,13 +390,25 @@ func (repo *Repository) CommitsBetweenNotBase(last, before *Commit, baseBranch s | ||||
| 	var stdout []byte | ||||
| 	var err error | ||||
| 	if before == nil { | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list").AddDynamicArguments(last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddDynamicArguments(last.ID.String()). | ||||
| 			AddOptionValues("--not", baseBranch). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 	} else { | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 			AddDynamicArguments(before.ID.String()+".."+last.ID.String()). | ||||
| 			AddOptionValues("--not", baseBranch). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdBytes(repo.Ctx) | ||||
| 		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 = gitcmd.NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).AddOptionValues("--not", baseBranch).RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 			stdout, _, err = gitcmd.NewCommand("rev-list"). | ||||
| 				AddDynamicArguments(before.ID.String(), last.ID.String()). | ||||
| 				AddOptionValues("--not", baseBranch). | ||||
| 				WithDir(repo.Path). | ||||
| 				RunStdBytes(repo.Ctx) | ||||
| 		} | ||||
| 	} | ||||
| 	if err != nil { | ||||
| @@ -417,7 +460,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error) | ||||
| 	} | ||||
| 	cmd.AddDynamicArguments(id.String()) | ||||
|  | ||||
| 	stdout, _, runErr := cmd.RunStdBytes(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, runErr := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx) | ||||
| 	if runErr != nil { | ||||
| 		return nil, runErr | ||||
| 	} | ||||
| @@ -457,10 +500,9 @@ func (repo *Repository) getBranches(env []string, commitID string, limit int) ([ | ||||
| 		stdout, _, err := gitcmd.NewCommand("for-each-ref", "--format=%(refname:strip=2)"). | ||||
| 			AddOptionFormat("--count=%d", limit). | ||||
| 			AddOptionValues("--contains", commitID, BranchPrefix). | ||||
| 			RunStdString(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 				Dir: repo.Path, | ||||
| 				Env: env, | ||||
| 			}) | ||||
| 			WithDir(repo.Path). | ||||
| 			WithEnv(env). | ||||
| 			RunStdString(repo.Ctx) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @@ -469,10 +511,11 @@ func (repo *Repository) getBranches(env []string, commitID string, limit int) ([ | ||||
| 		return branches, nil | ||||
| 	} | ||||
|  | ||||
| 	stdout, _, err := gitcmd.NewCommand("branch").AddOptionValues("--contains", commitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 		Dir: repo.Path, | ||||
| 		Env: env, | ||||
| 	}) | ||||
| 	stdout, _, err := gitcmd.NewCommand("branch"). | ||||
| 		AddOptionValues("--contains", commitID). | ||||
| 		WithDir(repo.Path). | ||||
| 		WithEnv(env). | ||||
| 		RunStdString(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -511,7 +554,10 @@ 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 := gitcmd.NewCommand("branch", "--contains").AddDynamicArguments(commitID, branch).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := gitcmd.NewCommand("branch", "--contains"). | ||||
| 		AddDynamicArguments(commitID, branch). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdString(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| @@ -540,10 +586,9 @@ func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID s | ||||
| 	cmd := gitcmd.NewCommand("log", prettyLogFormat) | ||||
| 	cmd.AddDynamicArguments(endCommitID) | ||||
|  | ||||
| 	stdout, _, runErr := cmd.RunStdBytes(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 		Dir: repo.Path, | ||||
| 		Env: env, | ||||
| 	}) | ||||
| 	stdout, _, runErr := cmd.WithDir(repo.Path). | ||||
| 		WithEnv(env). | ||||
| 		RunStdBytes(repo.Ctx) | ||||
| 	if runErr != nil { | ||||
| 		return "", runErr | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user