mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +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:
		| @@ -27,13 +27,20 @@ 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 := gitcmd.NewCommand("fetch", "--no-tags").AddDynamicArguments(tmpRemote).AddDashesAndList(base+":"+tmpBaseName).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 		_, _, err := gitcmd.NewCommand("fetch", "--no-tags"). | ||||
| 			AddDynamicArguments(tmpRemote). | ||||
| 			AddDashesAndList(base + ":" + tmpBaseName). | ||||
| 			WithDir(repo.Path). | ||||
| 			RunStdString(repo.Ctx) | ||||
| 		if err == nil { | ||||
| 			base = tmpBaseName | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	stdout, _, err := gitcmd.NewCommand("merge-base").AddDashesAndList(base, head).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := gitcmd.NewCommand("merge-base"). | ||||
| 		AddDashesAndList(base, head). | ||||
| 		WithDir(repo.Path). | ||||
| 		RunStdString(repo.Ctx) | ||||
| 	return strings.TrimSpace(stdout), base, err | ||||
| } | ||||
|  | ||||
| @@ -61,22 +68,25 @@ 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 <command> [<revision>...] -- [<file>...]' | ||||
| 	if err := gitcmd.NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base+separator+head).AddArguments("--"). | ||||
| 		Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 			Dir:    repo.Path, | ||||
| 			Stdout: w, | ||||
| 			Stderr: stderr, | ||||
| 		}); err != nil { | ||||
| 	if err := gitcmd.NewCommand("diff", "-z", "--name-only"). | ||||
| 		AddDynamicArguments(base + separator + head). | ||||
| 		AddArguments("--"). | ||||
| 		WithDir(repo.Path). | ||||
| 		WithStdout(w). | ||||
| 		WithStderr(stderr). | ||||
| 		Run(repo.Ctx); err != nil { | ||||
| 		if strings.Contains(stderr.String(), "no merge base") { | ||||
| 			// git >= 2.28 now returns an error if base and head have become unrelated. | ||||
| 			// 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 = gitcmd.NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base, head).AddArguments("--").Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 				Dir:    repo.Path, | ||||
| 				Stdout: w, | ||||
| 				Stderr: stderr, | ||||
| 			}); err == nil { | ||||
| 			if err = gitcmd.NewCommand("diff", "-z", "--name-only"). | ||||
| 				AddDynamicArguments(base, head). | ||||
| 				AddArguments("--"). | ||||
| 				WithDir(repo.Path). | ||||
| 				WithStdout(w). | ||||
| 				WithStderr(stderr). | ||||
| 				Run(repo.Ctx); err == nil { | ||||
| 				return w.numLines, nil | ||||
| 			} | ||||
| 		} | ||||
| @@ -91,30 +101,29 @@ var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`) | ||||
| func (repo *Repository) GetDiff(compareArg string, w io.Writer) error { | ||||
| 	stderr := new(bytes.Buffer) | ||||
| 	return gitcmd.NewCommand("diff", "-p").AddDynamicArguments(compareArg). | ||||
| 		Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 			Dir:    repo.Path, | ||||
| 			Stdout: w, | ||||
| 			Stderr: stderr, | ||||
| 		}) | ||||
| 		WithDir(repo.Path). | ||||
| 		WithStdout(w). | ||||
| 		WithStderr(stderr). | ||||
| 		Run(repo.Ctx) | ||||
| } | ||||
|  | ||||
| // GetDiffBinary generates and returns patch data between given revisions, including binary diffs. | ||||
| func (repo *Repository) GetDiffBinary(compareArg string, w io.Writer) error { | ||||
| 	return gitcmd.NewCommand("diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 		Dir:    repo.Path, | ||||
| 		Stdout: w, | ||||
| 	}) | ||||
| 	return gitcmd.NewCommand("diff", "-p", "--binary", "--histogram"). | ||||
| 		AddDynamicArguments(compareArg). | ||||
| 		WithDir(repo.Path). | ||||
| 		WithStdout(w). | ||||
| 		Run(repo.Ctx) | ||||
| } | ||||
|  | ||||
| // 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 { | ||||
| 	stderr := new(bytes.Buffer) | ||||
| 	return gitcmd.NewCommand("format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg). | ||||
| 		Run(repo.Ctx, &gitcmd.RunOpts{ | ||||
| 			Dir:    repo.Path, | ||||
| 			Stdout: w, | ||||
| 			Stderr: stderr, | ||||
| 		}) | ||||
| 		WithDir(repo.Path). | ||||
| 		WithStdout(w). | ||||
| 		WithStderr(stderr). | ||||
| 		Run(repo.Ctx) | ||||
| } | ||||
|  | ||||
| // GetFilesChangedBetween returns a list of all files that have been changed between the given commits | ||||
| @@ -131,7 +140,7 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err | ||||
| 	} else { | ||||
| 		cmd.AddDynamicArguments(base, head) | ||||
| 	} | ||||
| 	stdout, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||
| 	stdout, _, err := cmd.WithDir(repo.Path).RunStdString(repo.Ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user