Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavlo Strokov <pstrokov@gitlab.com>2020-11-26 18:39:51 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2020-11-29 19:28:43 +0300
commitbbff75dd8e27ef30c9a179ec41bb3cd5a7f46dfd (patch)
tree786645ab700088ce347db614b05fc5182b83cab4
parente2599b42db123fdf17cf141f39396fc12004222d (diff)
Make config.Cfg accessible to SafeCmd and others.
To break dependency between SafeCmd and similar the CommandFactory was introduced. In includes config.Cfg in it and provides a base factory methods used inside of SafeCmd and others. This is initial work in order to break dependency completely afterwards. Part of: https://gitlab.com/gitlab-org/gitaly/-/issues/2699
-rw-r--r--internal/git/command.go35
-rw-r--r--internal/git/command_test.go2
-rw-r--r--internal/git/safecmd.go10
-rw-r--r--internal/git/version.go2
4 files changed, 34 insertions, 15 deletions
diff --git a/internal/git/command.go b/internal/git/command.go
index 442c8389f..939521617 100644
--- a/internal/git/command.go
+++ b/internal/git/command.go
@@ -7,10 +7,29 @@ import (
"gitlab.com/gitlab-org/gitaly/internal/command"
"gitlab.com/gitlab-org/gitaly/internal/git/alternates"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
+ "gitlab.com/gitlab-org/gitaly/internal/storage"
)
+// CommandFactory knows how to properly construct different types of commands.
+type CommandFactory struct {
+ locator storage.Locator
+ cfg config.Cfg
+}
+
+// NewCommandFactory returns a new instance of initialized CommandFactory.
+// Current implementation relies on the global var 'config.Config' and a single type of 'Locator' we currently have.
+// This dependency will be removed on the next iterations in scope of: https://gitlab.com/gitlab-org/gitaly/-/issues/2699
+func NewCommandFactory() *CommandFactory {
+ return &CommandFactory{cfg: config.Config, locator: config.NewLocator(config.Config)}
+}
+
+func (cf *CommandFactory) gitPath() string {
+ return cf.cfg.Git.BinPath
+}
+
// unsafeCmdWithEnv creates a git.unsafeCmd with the given args, environment, and Repository
-func unsafeCmdWithEnv(ctx context.Context, extraEnv []string, stream CmdStream, repo repository.GitRepo, args ...string) (*command.Command, error) {
+func (cf *CommandFactory) unsafeCmdWithEnv(ctx context.Context, extraEnv []string, stream CmdStream, repo repository.GitRepo, args ...string) (*command.Command, error) {
args, env, err := argsAndEnv(repo, args...)
if err != nil {
return nil, err
@@ -18,12 +37,12 @@ func unsafeCmdWithEnv(ctx context.Context, extraEnv []string, stream CmdStream,
env = append(env, extraEnv...)
- return unsafeBareCmd(ctx, stream, env, args...)
+ return cf.unsafeBareCmd(ctx, stream, env, args...)
}
// unsafeStdinCmd creates a git.Command with the given args and Repository that is
// suitable for Write()ing to
-func unsafeStdinCmd(ctx context.Context, extraEnv []string, repo repository.GitRepo, args ...string) (*command.Command, error) {
+func (cf *CommandFactory) unsafeStdinCmd(ctx context.Context, extraEnv []string, repo repository.GitRepo, args ...string) (*command.Command, error) {
args, env, err := argsAndEnv(repo, args...)
if err != nil {
return nil, err
@@ -31,7 +50,7 @@ func unsafeStdinCmd(ctx context.Context, extraEnv []string, repo repository.GitR
env = append(env, extraEnv...)
- return unsafeBareCmd(ctx, CmdStream{In: command.SetupStdin}, env, args...)
+ return cf.unsafeBareCmd(ctx, CmdStream{In: command.SetupStdin}, env, args...)
}
func argsAndEnv(repo repository.GitRepo, args ...string) ([]string, []string, error) {
@@ -46,17 +65,17 @@ func argsAndEnv(repo repository.GitRepo, args ...string) ([]string, []string, er
}
// unsafeBareCmd creates a git.Command with the given args, stdin/stdout/stderr, and env
-func unsafeBareCmd(ctx context.Context, stream CmdStream, env []string, args ...string) (*command.Command, error) {
+func (cf *CommandFactory) unsafeBareCmd(ctx context.Context, stream CmdStream, env []string, args ...string) (*command.Command, error) {
env = append(env, command.GitEnv...)
- return command.New(ctx, exec.Command(command.GitPath(), args...), stream.In, stream.Out, stream.Err, env...)
+ return command.New(ctx, exec.Command(cf.gitPath(), args...), stream.In, stream.Out, stream.Err, env...)
}
// unsafeBareCmdInDir calls unsafeBareCmd in dir.
-func unsafeBareCmdInDir(ctx context.Context, dir string, stream CmdStream, env []string, args ...string) (*command.Command, error) {
+func (cf *CommandFactory) unsafeBareCmdInDir(ctx context.Context, dir string, stream CmdStream, env []string, args ...string) (*command.Command, error) {
env = append(env, command.GitEnv...)
- cmd := exec.Command(command.GitPath(), args...)
+ cmd := exec.Command(cf.gitPath(), args...)
cmd.Dir = dir
return command.New(ctx, cmd, stream.In, stream.Out, stream.Err, env...)
}
diff --git a/internal/git/command_test.go b/internal/git/command_test.go
index 2a236f9f6..1215fbbb1 100644
--- a/internal/git/command_test.go
+++ b/internal/git/command_test.go
@@ -29,7 +29,7 @@ func TestGitCommandProxy(t *testing.T) {
dir, cleanup := testhelper.TempDir(t)
defer cleanup()
- cmd, err := unsafeBareCmd(ctx, CmdStream{}, nil, "clone", "http://gitlab.com/bogus-repo", dir)
+ cmd, err := NewCommandFactory().unsafeBareCmd(ctx, CmdStream{}, nil, "clone", "http://gitlab.com/bogus-repo", dir)
require.NoError(t, err)
err = cmd.Wait()
diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go
index fdf201845..241509382 100644
--- a/internal/git/safecmd.go
+++ b/internal/git/safecmd.go
@@ -305,7 +305,7 @@ func SafeCmdWithEnv(ctx context.Context, env []string, repo repository.GitRepo,
return nil, err
}
- return unsafeCmdWithEnv(ctx, append(env, cc.env...), CmdStream{
+ return NewCommandFactory().unsafeCmdWithEnv(ctx, append(env, cc.env...), CmdStream{
In: cc.stdin,
Out: cc.stdout,
Err: cc.stderr,
@@ -326,7 +326,7 @@ func SafeBareCmd(ctx context.Context, stream CmdStream, env []string, globals []
return nil, err
}
- return unsafeBareCmd(ctx, stream, append(env, cc.env...), args...)
+ return NewCommandFactory().unsafeBareCmd(ctx, stream, append(env, cc.env...), args...)
}
// SafeBareCmdInDir runs SafeBareCmd in the dir.
@@ -346,7 +346,7 @@ func SafeBareCmdInDir(ctx context.Context, dir string, stream CmdStream, env []s
return nil, err
}
- return unsafeBareCmdInDir(ctx, dir, stream, append(env, cc.env...), args...)
+ return NewCommandFactory().unsafeBareCmdInDir(ctx, dir, stream, append(env, cc.env...), args...)
}
// SafeStdinCmd creates a git.Command with the given args and Repository that is
@@ -364,7 +364,7 @@ func SafeStdinCmd(ctx context.Context, repo repository.GitRepo, globals []Option
return nil, err
}
- return unsafeStdinCmd(ctx, cc.env, repo, args...)
+ return NewCommandFactory().unsafeStdinCmd(ctx, cc.env, repo, args...)
}
// SafeCmdWithoutRepo works like Command but without a git repository. It
@@ -381,7 +381,7 @@ func SafeCmdWithoutRepo(ctx context.Context, stream CmdStream, globals []Option,
return nil, err
}
- return unsafeBareCmd(ctx, stream, cc.env, args...)
+ return NewCommandFactory().unsafeBareCmd(ctx, stream, cc.env, args...)
}
func combineArgs(globals []Option, sc Cmd, cc *cmdCfg) (_ []string, err error) {
diff --git a/internal/git/version.go b/internal/git/version.go
index 78b4dc006..87cf01126 100644
--- a/internal/git/version.go
+++ b/internal/git/version.go
@@ -21,7 +21,7 @@ type version struct {
// Version returns the used git version.
func Version(ctx context.Context) (string, error) {
var buf bytes.Buffer
- cmd, err := unsafeBareCmd(ctx, CmdStream{Out: &buf}, nil, "version")
+ cmd, err := NewCommandFactory().unsafeBareCmd(ctx, CmdStream{Out: &buf}, nil, "version")
if err != nil {
return "", err
}