diff options
-rw-r--r-- | internal/git/hooks.go | 17 | ||||
-rw-r--r-- | internal/git/safecmd.go | 5 |
2 files changed, 20 insertions, 2 deletions
diff --git a/internal/git/hooks.go b/internal/git/hooks.go index 97099b859..5d3004f6a 100644 --- a/internal/git/hooks.go +++ b/internal/git/hooks.go @@ -20,6 +20,10 @@ var jsonpbMarshaller = &jsonpb.Marshaler{} // repository changes that may possibly update references func WithRefTxHook(ctx context.Context, repo *gitalypb.Repository, cfg config.Cfg) CmdOpt { return func(cc *cmdCfg) error { + if cc.refHookOverriden { + return errors.New("ref tx hook provided, but already overriden") + } + if repo == nil { return fmt.Errorf("missing repo: %w", ErrInvalidArg) } @@ -36,6 +40,19 @@ func WithRefTxHook(ctx context.Context, repo *gitalypb.Repository, cfg config.Cf } } +// WithOverrideRefTxHook returns an option that indicates the command should not +// require a ref hook. This option should only be used in cases where the git +// command does not alter references. +func WithOverrideRefTxHook() CmdOpt { + return func(cc *cmdCfg) error { + if cc.refHookConfigured { + return errors.New("ref tx hook override provided, but already configured") + } + cc.refHookOverriden = true + return nil + } +} + // refHookEnv returns all env vars required by the reference transaction hook func refHookEnv(ctx context.Context, repo *gitalypb.Repository, cfg config.Cfg) ([]string, error) { repoJSON, err := jsonpbMarshaller.MarshalToString(repo) diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go index d7f011a51..bb7f34b81 100644 --- a/internal/git/safecmd.go +++ b/internal/git/safecmd.go @@ -229,6 +229,7 @@ type cmdCfg struct { stdout io.Writer stderr io.Writer refHookConfigured bool + refHookOverridden bool } // CmdOpt is an option for running a command @@ -274,10 +275,10 @@ func handleOpts(ctx context.Context, sc Cmd, cc *cmdCfg, opts []CmdOpt) error { } } - if !cc.refHookConfigured && mayUpdateRef(sc.Subcommand()) { + if !cc.refHookConfigured && !cc.refHookOverridden && mayUpdateRef(sc.Subcommand()) { return fmt.Errorf("subcommand %q: %w", sc.Subcommand(), ErrRefHookRequired) } - if cc.refHookConfigured && !mayUpdateRef(sc.Subcommand()) { + if cc.refHookConfigured && (!mayUpdateRef(sc.Subcommand()) || cc.refHookOverridden) { return fmt.Errorf("subcommand %q: %w", sc.Subcommand(), ErrRefHookNotRequired) } |