Skip to content

Commit 9332ff2

Browse files
authored
Move git command to git/gitcmd (#35483)
The name cmd is already used in many places and may cause conflicts, so I chose `gitcmd` instead to minimize potential naming conflicts.
1 parent fe5afcb commit 9332ff2

File tree

107 files changed

+690
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+690
-558
lines changed

cmd/hook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"code.gitea.io/gitea/modules/git"
18+
"code.gitea.io/gitea/modules/git/gitcmd"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/private"
2021
repo_module "code.gitea.io/gitea/modules/repository"
@@ -312,7 +313,7 @@ func runHookPostReceive(ctx context.Context, c *cli.Command) error {
312313
setup(ctx, c.Bool("debug"))
313314

314315
// First of all run update-server-info no matter what
315-
if _, _, err := git.NewCommand("update-server-info").RunStdString(ctx, nil); err != nil {
316+
if _, _, err := gitcmd.NewCommand("update-server-info").RunStdString(ctx, nil); err != nil {
316317
return fmt.Errorf("failed to call 'git update-server-info': %w", err)
317318
}
318319

cmd/serv.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/models/perm"
2222
"code.gitea.io/gitea/models/repo"
2323
"code.gitea.io/gitea/modules/git"
24+
"code.gitea.io/gitea/modules/git/gitcmd"
2425
"code.gitea.io/gitea/modules/json"
2526
"code.gitea.io/gitea/modules/lfstransfer"
2627
"code.gitea.io/gitea/modules/log"
@@ -312,30 +313,30 @@ func runServ(ctx context.Context, c *cli.Command) error {
312313
return nil
313314
}
314315

315-
var gitcmd *exec.Cmd
316-
gitBinPath := filepath.Dir(git.GitExecutable) // e.g. /usr/bin
317-
gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
316+
var command *exec.Cmd
317+
gitBinPath := filepath.Dir(gitcmd.GitExecutable) // e.g. /usr/bin
318+
gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
318319
if _, err := os.Stat(gitBinVerb); err != nil {
319320
// if the command "git-upload-pack" doesn't exist, try to split "git-upload-pack" to use the sub-command with git
320321
// ps: Windows only has "git.exe" in the bin path, so Windows always uses this way
321322
verbFields := strings.SplitN(verb, "-", 2)
322323
if len(verbFields) == 2 {
323324
// use git binary with the sub-command part: "C:\...\bin\git.exe", "upload-pack", ...
324-
gitcmd = exec.CommandContext(ctx, git.GitExecutable, verbFields[1], repoPath)
325+
command = exec.CommandContext(ctx, gitcmd.GitExecutable, verbFields[1], repoPath)
325326
}
326327
}
327-
if gitcmd == nil {
328+
if command == nil {
328329
// by default, use the verb (it has been checked above by allowedCommands)
329-
gitcmd = exec.CommandContext(ctx, gitBinVerb, repoPath)
330+
command = exec.CommandContext(ctx, gitBinVerb, repoPath)
330331
}
331332

332-
process.SetSysProcAttribute(gitcmd)
333-
gitcmd.Dir = setting.RepoRootPath
334-
gitcmd.Stdout = os.Stdout
335-
gitcmd.Stdin = os.Stdin
336-
gitcmd.Stderr = os.Stderr
337-
gitcmd.Env = append(gitcmd.Env, os.Environ()...)
338-
gitcmd.Env = append(gitcmd.Env,
333+
process.SetSysProcAttribute(command)
334+
command.Dir = setting.RepoRootPath
335+
command.Stdout = os.Stdout
336+
command.Stdin = os.Stdin
337+
command.Stderr = os.Stderr
338+
command.Env = append(command.Env, os.Environ()...)
339+
command.Env = append(command.Env,
339340
repo_module.EnvRepoIsWiki+"="+strconv.FormatBool(results.IsWiki),
340341
repo_module.EnvRepoName+"="+results.RepoName,
341342
repo_module.EnvRepoUsername+"="+results.OwnerName,
@@ -350,9 +351,9 @@ func runServ(ctx context.Context, c *cli.Command) error {
350351
)
351352
// to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
352353
// it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
353-
gitcmd.Env = append(gitcmd.Env, git.CommonCmdServEnvs()...)
354+
command.Env = append(command.Env, gitcmd.CommonCmdServEnvs()...)
354355

355-
if err = gitcmd.Run(); err != nil {
356+
if err = command.Run(); err != nil {
356357
return fail(ctx, "Failed to execute git command", "Failed to execute git command: %v", err)
357358
}
358359

models/migrations/v1_12/v128.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"code.gitea.io/gitea/modules/git"
15+
"code.gitea.io/gitea/modules/git/gitcmd"
1516
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
1718

@@ -83,17 +84,17 @@ func FixMergeBase(ctx context.Context, x *xorm.Engine) error {
8384

8485
if !pr.HasMerged {
8586
var err error
86-
pr.MergeBase, _, err = git.NewCommand("merge-base").AddDashesAndList(pr.BaseBranch, gitRefName).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
87+
pr.MergeBase, _, err = gitcmd.NewCommand("merge-base").AddDashesAndList(pr.BaseBranch, gitRefName).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
8788
if err != nil {
8889
var err2 error
89-
pr.MergeBase, _, err2 = git.NewCommand("rev-parse").AddDynamicArguments(git.BranchPrefix+pr.BaseBranch).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
90+
pr.MergeBase, _, err2 = gitcmd.NewCommand("rev-parse").AddDynamicArguments(git.BranchPrefix+pr.BaseBranch).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
9091
if err2 != nil {
9192
log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err, err2)
9293
continue
9394
}
9495
}
9596
} else {
96-
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
97+
parentsString, _, err := gitcmd.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
9798
if err != nil {
9899
log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
99100
continue
@@ -105,9 +106,9 @@ func FixMergeBase(ctx context.Context, x *xorm.Engine) error {
105106

106107
refs := append([]string{}, parents[1:]...)
107108
refs = append(refs, gitRefName)
108-
cmd := git.NewCommand("merge-base").AddDashesAndList(refs...)
109+
cmd := gitcmd.NewCommand("merge-base").AddDashesAndList(refs...)
109110

110-
pr.MergeBase, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath})
111+
pr.MergeBase, _, err = cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
111112
if err != nil {
112113
log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
113114
continue

models/migrations/v1_12/v134.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14-
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/git/gitcmd"
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
1717

@@ -80,7 +80,7 @@ func RefixMergeBase(ctx context.Context, x *xorm.Engine) error {
8080

8181
gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
8282

83-
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
83+
parentsString, _, err := gitcmd.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
8484
if err != nil {
8585
log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
8686
continue
@@ -93,9 +93,9 @@ func RefixMergeBase(ctx context.Context, x *xorm.Engine) error {
9393
// we should recalculate
9494
refs := append([]string{}, parents[1:]...)
9595
refs = append(refs, gitRefName)
96-
cmd := git.NewCommand("merge-base").AddDashesAndList(refs...)
96+
cmd := gitcmd.NewCommand("merge-base").AddDashesAndList(refs...)
9797

98-
pr.MergeBase, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath})
98+
pr.MergeBase, _, err = cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
9999
if err != nil {
100100
log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
101101
continue

modules/git/attribute/batch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"code.gitea.io/gitea/modules/git"
15+
"code.gitea.io/gitea/modules/git/gitcmd"
1516
"code.gitea.io/gitea/modules/log"
1617
)
1718

@@ -23,7 +24,7 @@ type BatchChecker struct {
2324
stdOut *nulSeparatedAttributeWriter
2425
ctx context.Context
2526
cancel context.CancelFunc
26-
cmd *git.Command
27+
cmd *gitcmd.Command
2728
}
2829

2930
// NewBatchChecker creates a check attribute reader for the current repository and provided commit ID
@@ -76,7 +77,7 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes []string)
7677
_ = lw.Close()
7778
}()
7879
stdErr := new(bytes.Buffer)
79-
err := cmd.Run(ctx, &git.RunOpts{
80+
err := cmd.Run(ctx, &gitcmd.RunOpts{
8081
Env: envs,
8182
Dir: repo.Path,
8283
Stdin: stdinReader,

modules/git/attribute/checker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
"os"
1212

1313
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/git/gitcmd"
1415
)
1516

16-
func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attributes []string) (*git.Command, []string, func(), error) {
17+
func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attributes []string) (*gitcmd.Command, []string, func(), error) {
1718
cancel := func() {}
1819
envs := []string{"GIT_FLUSH=1"}
19-
cmd := git.NewCommand("check-attr", "-z")
20+
cmd := gitcmd.NewCommand("check-attr", "-z")
2021
if len(attributes) == 0 {
2122
cmd.AddArguments("--all")
2223
}
@@ -70,7 +71,7 @@ func CheckAttributes(ctx context.Context, gitRepo *git.Repository, treeish strin
7071
stdOut := new(bytes.Buffer)
7172
stdErr := new(bytes.Buffer)
7273

73-
if err := cmd.Run(ctx, &git.RunOpts{
74+
if err := cmd.Run(ctx, &gitcmd.RunOpts{
7475
Env: append(os.Environ(), envs...),
7576
Dir: gitRepo.Path,
7677
Stdout: stdOut,

modules/git/batch_reader.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
"code.gitea.io/gitea/modules/git/gitcmd"
1516
"code.gitea.io/gitea/modules/log"
1617

1718
"github.com/djherbis/buffer"
@@ -29,13 +30,13 @@ type WriteCloserError interface {
2930
// This is needed otherwise the git cat-file will hang for invalid repositories.
3031
func ensureValidGitRepository(ctx context.Context, repoPath string) error {
3132
stderr := strings.Builder{}
32-
err := NewCommand("rev-parse").
33-
Run(ctx, &RunOpts{
33+
err := gitcmd.NewCommand("rev-parse").
34+
Run(ctx, &gitcmd.RunOpts{
3435
Dir: repoPath,
3536
Stderr: &stderr,
3637
})
3738
if err != nil {
38-
return ConcatenateError(err, (&stderr).String())
39+
return gitcmd.ConcatenateError(err, (&stderr).String())
3940
}
4041
return nil
4142
}
@@ -61,8 +62,8 @@ func catFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
6162

6263
go func() {
6364
stderr := strings.Builder{}
64-
err := NewCommand("cat-file", "--batch-check").
65-
Run(ctx, &RunOpts{
65+
err := gitcmd.NewCommand("cat-file", "--batch-check").
66+
Run(ctx, &gitcmd.RunOpts{
6667
Dir: repoPath,
6768
Stdin: batchStdinReader,
6869
Stdout: batchStdoutWriter,
@@ -71,8 +72,8 @@ func catFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
7172
UseContextTimeout: true,
7273
})
7374
if err != nil {
74-
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
75-
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
75+
_ = batchStdoutWriter.CloseWithError(gitcmd.ConcatenateError(err, (&stderr).String()))
76+
_ = batchStdinReader.CloseWithError(gitcmd.ConcatenateError(err, (&stderr).String()))
7677
} else {
7778
_ = batchStdoutWriter.Close()
7879
_ = batchStdinReader.Close()
@@ -109,8 +110,8 @@ func catFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
109110

110111
go func() {
111112
stderr := strings.Builder{}
112-
err := NewCommand("cat-file", "--batch").
113-
Run(ctx, &RunOpts{
113+
err := gitcmd.NewCommand("cat-file", "--batch").
114+
Run(ctx, &gitcmd.RunOpts{
114115
Dir: repoPath,
115116
Stdin: batchStdinReader,
116117
Stdout: batchStdoutWriter,
@@ -119,8 +120,8 @@ func catFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
119120
UseContextTimeout: true,
120121
})
121122
if err != nil {
122-
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
123-
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
123+
_ = batchStdoutWriter.CloseWithError(gitcmd.ConcatenateError(err, (&stderr).String()))
124+
_ = batchStdinReader.CloseWithError(gitcmd.ConcatenateError(err, (&stderr).String()))
124125
} else {
125126
_ = batchStdoutWriter.Close()
126127
_ = batchStdinReader.Close()

modules/git/blame.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212

13+
"code.gitea.io/gitea/modules/git/gitcmd"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
1516
)
@@ -141,7 +142,7 @@ func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath
141142
}
142143
}()
143144

144-
cmd := NewCommand("blame", "--porcelain")
145+
cmd := gitcmd.NewCommand("blame", "--porcelain")
145146

146147
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
147148
ignoreRevsFileName, ignoreRevsFileCleanup, err = tryCreateBlameIgnoreRevsFile(commit)
@@ -165,7 +166,7 @@ func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath
165166
go func() {
166167
stderr := bytes.Buffer{}
167168
// TODO: it doesn't work for directories (the directories shouldn't be "blamed"), and the "err" should be returned by "Read" but not by "Close"
168-
err := cmd.Run(ctx, &RunOpts{
169+
err := cmd.Run(ctx, &gitcmd.RunOpts{
169170
UseContextTimeout: true,
170171
Dir: repoPath,
171172
Stdout: stdout,

0 commit comments

Comments
 (0)