diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-04 10:00:49 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-11 15:32:45 +0300 |
commit | a43337d00bb94afbbecc3df646d1f1730cf0960f (patch) | |
tree | a8e7be9d8556971e3f2e8f7b989aacc5f5d65c7c | |
parent | bdc17b8c023a5b8b71fbb36b2c0658cfb80ded39 (diff) |
localrepo: Expose functions to execute commands
When initially implementing the localrepo abstractions, the plan had
been to migrate ad-hoc executions of git commands into more abstract
functions in order to not have to distribute the knowledge about how to
execute certain git commands throughout our codebase. That's also why the
localrepo does not expose any way to execute git commands on that repo
diretcly.
The migration isn't really happening though, or only with a very slow
pace. So let's expose two ways to run git commands in the context of a
localrepo with `Exec()` and `ExecAndWait()`. These take away the need to
instantiate git command factories everywhere, and especially the latter
takes away the pain point that one always needs to do two error checks
when executing a git command.
The previous `command()` function will be removed in a subsequent commit
when all its users have been migrated.
-rw-r--r-- | internal/git/localrepo/repo.go | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/internal/git/localrepo/repo.go b/internal/git/localrepo/repo.go index 9959116ce..5b72ed6fd 100644 --- a/internal/git/localrepo/repo.go +++ b/internal/git/localrepo/repo.go @@ -26,13 +26,27 @@ func New(repo repository.GitRepo, cfg config.Cfg) *Repo { } } -// command creates a Git Command with the given args and Repository, executed -// in the Repository. It validates the arguments in the command before -// executing. func (repo *Repo) command(ctx context.Context, globals []git.GlobalOption, cmd git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { return repo.commandFactory.New(ctx, repo, globals, cmd, opts...) } +// Exec creates a git command with the given args and Repo, executed in the +// Repo. It validates the arguments in the command before executing. +func (repo *Repo) Exec(ctx context.Context, globals []git.GlobalOption, cmd git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { + return repo.commandFactory.New(ctx, repo, globals, cmd, opts...) +} + +// ExecAndWait is similar to Exec, but waits for the command to exit before +// returning. +func (repo *Repo) ExecAndWait(ctx context.Context, globals []git.GlobalOption, cmd git.Cmd, opts ...git.CmdOpt) error { + command, err := repo.Exec(ctx, globals, cmd, opts...) + if err != nil { + return err + } + + return command.Wait() +} + // Config returns executor of the 'config' sub-command. func (repo *Repo) Config() Config { return Config{repo: repo} |