diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-01-14 15:06:50 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-01-19 17:53:49 +0300 |
commit | f5edd3f149474d5927fc40a9aea56f9f4879a0f7 (patch) | |
tree | 21cae4e29b4b2c57606e0b8b53fbd23a4175e4b0 | |
parent | 36bd68738c36dbef5d3aca44cfface1f73675bc1 (diff) |
git: Merge code paths to generate new commands
The logic to assemble arguments of a new git command is non-trivial and
it's easy to forget some argument. Still we got three different
implementations of this to generate commands with or without a
repository and with or without a directory. This makes the interface
inflexible as it is hard to extend or refactor.
Let's merge the three existing implementations to all call the same
`CommandFactory.newCommand()` function to unify their code paths.
-rw-r--r-- | internal/git/command_factory.go | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go index bc7d10227..72b2764af 100644 --- a/internal/git/command_factory.go +++ b/internal/git/command_factory.go @@ -36,59 +36,43 @@ func (cf *CommandFactory) gitPath() string { // unsafeCmd creates a git.unsafeCmd with the given args, environment, and Repository func (cf *CommandFactory) unsafeCmd(ctx context.Context, extraEnv []string, stream cmdStream, repo repository.GitRepo, args ...string) (*command.Command, error) { - args, env, err := cf.argsAndEnv(repo, args...) - if err != nil { - return nil, err - } - - env = append(env, extraEnv...) - - return cf.unsafeBareCmd(ctx, stream, env, args...) -} - -func (cf *CommandFactory) argsAndEnv(repo repository.GitRepo, args ...string) ([]string, []string, error) { - repoPath, err := cf.locator.GetRepoPath(repo) - if err != nil { - return nil, nil, err - } - - env := alternates.Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()) - args = append([]string{"--git-dir", repoPath}, args...) - - return args, env, nil + return cf.newCommand(ctx, repo, stream, "", extraEnv, args...) } // unsafeBareCmd creates a git.Command with the given args, stdin/stdout/stderr, and env func (cf *CommandFactory) unsafeBareCmd(ctx context.Context, stream cmdStream, env []string, args ...string) (*command.Command, error) { - env = append(env, command.GitEnv...) + return cf.newCommand(ctx, nil, stream, "", env, args...) +} - cmd, err := command.New(ctx, exec.Command(cf.gitPath(), args...), stream.In, stream.Out, stream.Err, env...) - if err != nil { - return nil, err - } +// unsafeBareCmdInDir call sunsafeBareCmd in dir. +func (cf *CommandFactory) unsafeBareCmdInDir(ctx context.Context, dir string, stream cmdStream, env []string, args ...string) (*command.Command, error) { + return cf.newCommand(ctx, nil, stream, dir, env, args...) +} - if err := cf.cgroupsManager.AddCommand(cmd); err != nil { - return nil, err - } +func (cf *CommandFactory) newCommand(ctx context.Context, repo repository.GitRepo, stream cmdStream, dir string, env []string, args ...string) (*command.Command, error) { + if repo != nil { + repoPath, err := cf.locator.GetRepoPath(repo) + if err != nil { + return nil, err + } - return cmd, err -} + env = append(alternates.Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()), env...) + args = append([]string{"--git-dir", repoPath}, args...) + } -// unsafeBareCmdInDir calls unsafeBareCmd in dir. -func (cf *CommandFactory) unsafeBareCmdInDir(ctx context.Context, dir string, stream cmdStream, env []string, args ...string) (*command.Command, error) { env = append(env, command.GitEnv...) - cmd1 := exec.Command(cf.gitPath(), args...) - cmd1.Dir = dir + execCommand := exec.Command(cf.gitPath(), args...) + execCommand.Dir = dir - cmd2, err := command.New(ctx, cmd1, stream.In, stream.Out, stream.Err, env...) + command, err := command.New(ctx, execCommand, stream.In, stream.Out, stream.Err, env...) if err != nil { return nil, err } - if err := cf.cgroupsManager.AddCommand(cmd2); err != nil { + if err := cf.cgroupsManager.AddCommand(command); err != nil { return nil, err } - return cmd2, nil + return command, nil } |