diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-08 15:14:06 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-08 15:58:32 +0300 |
commit | 55dc1422fe58525f9607c8c08e2f105be4717442 (patch) | |
tree | 70c0ff6fe13f11f346358dbd13847123a617f638 | |
parent | 6b32a64bbd4567122643505bdb93a748f2417da4 (diff) |
operations: Stop using `NewWithDir()` for worktrees
Same as with the preceding commit, we put the Git commands that operate
on worktrees into a different cgroup than Git commands that work on a
bare repository. There is only a single such RPC left though, which is
`UserApplyPatch`.
Convert this RPC to use a new Git option `WithWorktree()`. If set, this
option causes us to execute Git commands with the `-C` switch instead of
with `--git-dir` so that the command is executed as if it was executed
in the given worktree. Like this, we spawn those Git commands in the
same cgroup as would get picked in most of the other RPCs.
Changelog: fixed
-rw-r--r-- | internal/git/command_factory.go | 9 | ||||
-rw-r--r-- | internal/git/command_options.go | 9 | ||||
-rw-r--r-- | internal/gitaly/service/operations/apply_patch.go | 20 |
3 files changed, 23 insertions, 15 deletions
diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go index 116bcaee3..551921074 100644 --- a/internal/git/command_factory.go +++ b/internal/git/command_factory.go @@ -400,13 +400,20 @@ func (cf *ExecCommandFactory) newCommand(ctx context.Context, repo repository.Gi env := config.env + var repoPath string if repo != nil { - repoPath, err := cf.locator.GetRepoPath(repo) + var err error + repoPath, err = cf.locator.GetRepoPath(repo) if err != nil { return nil, err } env = append(alternates.Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()), env...) + } + + if config.worktreePath != "" { + args = append([]string{"-C", config.worktreePath}, args...) + } else if repoPath != "" { args = append([]string{"--git-dir", repoPath}, args...) } diff --git a/internal/git/command_options.go b/internal/git/command_options.go index c74b6c1e3..3c882bd12 100644 --- a/internal/git/command_options.go +++ b/internal/git/command_options.go @@ -170,6 +170,7 @@ type cmdCfg struct { globals []GlobalOption commandOpts []command.Option hooksConfigured bool + worktreePath string } // CmdOpt is an option for running a command @@ -320,3 +321,11 @@ func WithFinalizer(finalizer func(*command.Command)) CmdOpt { return nil } } + +// WithWorktree sets up the Git command to run in the given worktree path by using the `-C` switch. +func WithWorktree(worktreePath string) CmdOpt { + return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error { + c.worktreePath = worktreePath + return nil + } +} diff --git a/internal/gitaly/service/operations/apply_patch.go b/internal/gitaly/service/operations/apply_patch.go index 0c053280f..e2419ab43 100644 --- a/internal/gitaly/service/operations/apply_patch.go +++ b/internal/gitaly/service/operations/apply_patch.go @@ -111,7 +111,7 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyP }() var stdout, stderr bytes.Buffer - cmd, err := s.gitCmdFactory.NewWithDir(ctx, worktreePath, + if err := repo.ExecAndWait(ctx, git.SubCmd{ Name: "am", Flags: []git.Option{ @@ -131,12 +131,8 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyP git.WithStdout(&stdout), git.WithStderr(&stderr), git.WithRefTxHook(header.Repository), - ) - if err != nil { - return fmt.Errorf("create git am: %w", err) - } - - if err := cmd.Wait(); err != nil { + git.WithWorktree(worktreePath), + ); err != nil { // The Ruby implementation doesn't include stderr in errors, which makes // it difficult to determine the cause of an error. This special cases the // user facing patching error which is returned usually to maintain test @@ -150,7 +146,7 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyP } var revParseStdout, revParseStderr bytes.Buffer - revParseCmd, err := s.gitCmdFactory.NewWithDir(ctx, worktreePath, + if err := repo.ExecAndWait(ctx, git.SubCmd{ Name: "rev-parse", Flags: []git.Option{ @@ -161,12 +157,8 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyP }, git.WithStdout(&revParseStdout), git.WithStderr(&revParseStderr), - ) - if err != nil { - return fmt.Errorf("create git rev-parse: %w", gitError{ErrMsg: revParseStderr.String(), Err: err}) - } - - if err := revParseCmd.Wait(); err != nil { + git.WithWorktree(worktreePath), + ); err != nil { return fmt.Errorf("get patched commit: %w", gitError{ErrMsg: revParseStderr.String(), Err: err}) } |