Skip to content

Commit 56c85a3

Browse files
committed
Move Git executable to local command
1 parent 7e2466b commit 56c85a3

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

modules/git/batch_reader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type WriteCloserError interface {
3333
func EnsureValidGitRepository(ctx context.Context, repoPath string) error {
3434
stderr := strings.Builder{}
3535
err := NewCommand(ctx, "rev-parse").
36-
SetDescription(fmt.Sprintf("%s rev-parse [repo_path: %s]", GitExecutable, repoPath)).
36+
SetDescription(fmt.Sprintf("rev-parse [repo_path: %s]", repoPath)).
3737
Run(&RunOpts{
3838
Dir: repoPath,
3939
Stderr: &stderr,
@@ -69,7 +69,7 @@ func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
6969
go func() {
7070
stderr := strings.Builder{}
7171
err := NewCommand(ctx, "cat-file", "--batch-check").
72-
SetDescription(fmt.Sprintf("%s cat-file --batch-check [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
72+
SetDescription(fmt.Sprintf("cat-file --batch-check [repo_path: %s] (%s:%d)", repoPath, filename, line)).
7373
Run(&RunOpts{
7474
Dir: repoPath,
7575
Stdin: batchStdinReader,
@@ -119,7 +119,7 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
119119
go func() {
120120
stderr := strings.Builder{}
121121
err := NewCommand(ctx, "cat-file", "--batch").
122-
SetDescription(fmt.Sprintf("%s cat-file --batch [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
122+
SetDescription(fmt.Sprintf("cat-file --batch [repo_path: %s] (%s:%d)", repoPath, filename, line)).
123123
Run(&RunOpts{
124124
Dir: repoPath,
125125
Stdin: batchStdinReader,

modules/git/blame.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"regexp"
1515

1616
"code.gitea.io/gitea/modules/process"
17+
"code.gitea.io/gitea/modules/setting"
1718
)
1819

1920
// BlamePart represents block of blame - continuous lines with one sha
@@ -114,7 +115,7 @@ func (r *BlameReader) Close() error {
114115

115116
// CreateBlameReader creates reader for given repository, commit and file
116117
func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) {
117-
return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
118+
return createBlameReader(ctx, repoPath, setting.Git.Path, "blame", commitID, "--porcelain", "--", file)
118119
}
119120

120121
func createBlameReader(ctx context.Context, dir string, command ...string) (*BlameReader, error) {

modules/git/cmd/local.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (c *LocalCommand) Run(opts *RunOpts) error {
9090
}
9191
desc = fmt.Sprintf("%s %s [repo_path: %s]", c.args[0], strings.Join(args, " "), opts.Dir)
9292
}
93+
desc = fmt.Sprintf("[%s] %s", c.service.GitExecutable, desc)
9394

9495
ctx, cancel, finished := process.GetManager().AddContextTimeout(c.parentContext, opts.Timeout, desc)
9596
defer finished()
@@ -135,6 +136,10 @@ func (c *LocalCommand) Run(opts *RunOpts) error {
135136
return ctx.Err()
136137
}
137138

139+
// defaultGitExecutable is the command name of git
140+
// Could be updated to an absolute path while initialization
141+
const defaultGitExecutable = "git"
142+
138143
// LocalService represents a command service to create local git commands
139144
type LocalService struct {
140145
GitExecutable string // git binary location
@@ -146,8 +151,17 @@ var _ Service = &LocalService{}
146151

147152
// NewLocalService returns a local service
148153
func NewLocalService(gitExecutable, repoRootPath string, defaultTimeout time.Duration) *LocalService {
154+
// If path is empty, we use the default value of GitExecutable "git" to search for the location of git.
155+
if gitExecutable == "" {
156+
gitExecutable = defaultGitExecutable
157+
}
158+
absPath, err := exec.LookPath(gitExecutable)
159+
if err != nil {
160+
panic(fmt.Sprintf("Git not found: %v", err))
161+
}
162+
149163
return &LocalService{
150-
GitExecutable: gitExecutable,
164+
GitExecutable: absPath,
151165
RepoRootPath: repoRootPath,
152166
defaultTimeout: defaultTimeout,
153167
}

modules/git/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828

2929
func getCmdService() cmd.Service {
3030
cmdServiceOnce.Do(func() {
31-
cmdService = cmd.NewLocalService(GitExecutable, setting.RepoRootPath, time.Duration(setting.Git.Timeout.Default)*time.Second)
31+
cmdService = cmd.NewLocalService(setting.Git.Path, setting.RepoRootPath, time.Duration(setting.Git.Timeout.Default)*time.Second)
3232
})
3333
return cmdService
3434
}

modules/git/git.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ var (
2828
// If everything works fine, the code for git 1.x could be removed in a separate PR before 1.17 frozen.
2929
GitVersionRequired = "2.0.0"
3030

31-
// GitExecutable is the command name of git
32-
// Could be updated to an absolute path while initialization
33-
GitExecutable = "git"
34-
3531
// DefaultContext is the default context to run git commands in
3632
// will be overwritten by Init with HammerContext
3733
DefaultContext = context.Background()
@@ -82,19 +78,9 @@ func LoadGitVersion() error {
8278
return err
8379
}
8480

85-
// SetExecutablePath changes the path of git executable and checks the file permission and version.
86-
func SetExecutablePath(path string) error {
87-
// If path is empty, we use the default value of GitExecutable "git" to search for the location of git.
88-
if path != "" {
89-
GitExecutable = path
90-
}
91-
absPath, err := exec.LookPath(GitExecutable)
92-
if err != nil {
93-
return fmt.Errorf("git not found: %w", err)
94-
}
95-
GitExecutable = absPath
96-
97-
err = LoadGitVersion()
81+
// checkGitVersion checks the file permission and version.
82+
func checkGitVersion() error {
83+
err := LoadGitVersion()
9884
if err != nil {
9985
return fmt.Errorf("unable to load git version: %w", err)
10086
}
@@ -136,7 +122,7 @@ func VersionInfo() string {
136122
func Init(ctx context.Context) error {
137123
DefaultContext = ctx
138124

139-
if err := SetExecutablePath(setting.Git.Path); err != nil {
125+
if err := checkGitVersion(); err != nil {
140126
return err
141127
}
142128

routers/web/repo/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func serviceRPC(ctx gocontext.Context, h serviceHandler, service string) {
472472

473473
var stderr bytes.Buffer
474474
cmd := git.NewCommand(h.r.Context(), service, "--stateless-rpc", h.dir)
475-
cmd.SetDescription(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir))
475+
cmd.SetDescription(fmt.Sprintf("%s %s [repo_path: %s]", service, "--stateless-rpc", h.dir))
476476
if err := cmd.Run(&git.RunOpts{
477477
Dir: h.dir,
478478
Env: append(os.Environ(), h.environ...),

0 commit comments

Comments
 (0)