diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-11 12:12:18 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-11 16:04:45 +0300 |
commit | da5e64af177501b8cb55be539e491ad3c514d7e0 (patch) | |
tree | b4052394a2b1bdee0dca592355be8a771de54cfc | |
parent | 6756d7113c68e47a596fafaded78a73e29156f0c (diff) |
git: Allow setup of reference-transaction hook with repo interface
In order to set up the reference-trannsaction hook, one needs to pass in
a `gitalypb.Repository`. This is needlessly restrictive: the hook
doesn't use any information which wouldn't be available via the
`repository.GitRepo` interface. As a result, callers which do not have
the proto available simply use the `helper.ProtoRepoFromRepo()` function
to convert the interface to a proto.
This function is kind of dangerous though if used in different contexts:
the resulting proto is lacking project-specific information like the
project path and project name. So if one for example has the idea to
execute a hook different than the reference-transaction hook with a
proto constructed from that function, then we would probably start
failing in unintelligible ways later down the road.
Let's fix the interface by instead accepting a `repository.GitRepo` as
argument to `WithRefTxHook()`, constructing the protobuf right at the
callsite. This ensures we're always passing a consistently-populated
protobuf down to the hook, and thus there is no way that we accidentally
start using info in it which may sometimes be missing.
-rw-r--r-- | internal/git/hooks_options.go | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/internal/git/hooks_options.go b/internal/git/hooks_options.go index 67a23a986..bfd7780d1 100644 --- a/internal/git/hooks_options.go +++ b/internal/git/hooks_options.go @@ -7,6 +7,7 @@ import ( "path/filepath" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" + "gitlab.com/gitlab-org/gitaly/internal/git/repository" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/log" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" @@ -25,13 +26,22 @@ func WithDisabledHooks() CmdOpt { // WithRefTxHook returns an option that populates the safe command with the // environment variables necessary to properly execute a reference hook for // repository changes that may possibly update references -func WithRefTxHook(ctx context.Context, repo *gitalypb.Repository, cfg config.Cfg) CmdOpt { +func WithRefTxHook(ctx context.Context, repo repository.GitRepo, cfg config.Cfg) CmdOpt { return func(cc *cmdCfg) error { if repo == nil { return fmt.Errorf("missing repo: %w", ErrInvalidArg) } - if err := cc.configureHooks(ctx, repo, cfg, nil); err != nil { + // The reference-transaction hook does not need any project-specific information + // about the repository. So in order to make the hook usable by sites which do not + // have a project repository available (e.g. object pools), this function accepts a + // `repository.GitRepo` and just creates an ad-hoc proto repo. + if err := cc.configureHooks(ctx, &gitalypb.Repository{ + StorageName: repo.GetStorageName(), + GitAlternateObjectDirectories: repo.GetGitAlternateObjectDirectories(), + GitObjectDirectory: repo.GetGitObjectDirectory(), + RelativePath: repo.GetRelativePath(), + }, cfg, nil); err != nil { return fmt.Errorf("ref hook env var: %w", err) } |