Skip to content

Commit

Permalink
Show warning when skipping untrusted hooks ⚓ (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx authored Dec 10, 2021
1 parent aaac620 commit abf3a7c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
47 changes: 27 additions & 20 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func setupSettings(repoPath string) (HookSettings, UISettings) {
log.DebugIfF(err != nil, "Prompt setup failed -> using fallback.")

nonInteractive := hooks.IsRunnerNonInteractive(gitx, git.Traverse)
skipNonExistingSharedHooks, _ := hooks.SkipNonExistingSharedHooks(gitx, git.Traverse)
skipNonExistingSharedHooks := hooks.SkipNonExistingSharedHooks(gitx, git.Traverse)
skipUntrustedHooks, _ := hooks.SkipUntrustedHooks(gitx, git.Traverse)

isTrusted, hasTrustFile, trustAllSet := hooks.IsRepoTrusted(gitx, repoPath)
Expand Down Expand Up @@ -399,6 +399,22 @@ func executeLFSHooks(settings *HookSettings) {
}
}

func failOrWarnOnActiveUntrusted(skipUntrustedHooks bool, hook *hooks.Hook) {
if hook.Active && !hook.Trusted {
if skipUntrustedHooks {
log.WarnF(
"Hook '%s'\nis active and needs to be trusted first. Skipping.", hook.NamespacePath)
} else {
log.PanicF(
"Hook '%s' is active and needs to be trusted first.\n"+
"Either trust the hook or disable it, or skip active,\n"+
"untrusted hooks by running:\n"+
" $ git hooks config skip-untrusted-hooks --enable",
hook.NamespacePath)
}
}
}

func executeOldHook(
settings *HookSettings,
uiSettings *UISettings,
Expand Down Expand Up @@ -439,21 +455,16 @@ func executeOldHook(
return
}

hook := hooks[0]
hook := &hooks[0]

if hook.Active && !hook.Trusted {
if !settings.NonInteractive {
// Active hook, but not trusted:
// Show trust prompt to let user trust it or disable it.
showTrustPrompt(uiSettings, checksums, &hook)
showTrustPrompt(uiSettings, checksums, hook)
}

log.PanicIfF(!settings.SkipUntrustedHooks && hook.Active && !hook.Trusted,
"Hook '%s' is active and needs to be trusted first.\n"+
"Either trust the hook or disable it, or skip active,\n"+
"untrusted hooks by running:\n"+
" $ git hooks config skip-untrusted-hooks --enable",
hook.NamespacePath)
failOrWarnOnActiveUntrusted(settings.SkipNonExistingSharedHooks, hook)
}

if !hook.Active || !hook.Trusted {
Expand All @@ -464,7 +475,7 @@ func executeOldHook(
}

log.DebugF("Executing hook: '%s'.", hook.Path)
err = cm.RunExecutable(&settings.ExecX, &hook, cm.UseStdStreams(true, true, true))
err = cm.RunExecutable(&settings.ExecX, hook, cm.UseStdStreams(true, true, true))

log.AssertNoErrorPanicF(err, "Hook launch failed: '%q'.", hook)
}
Expand Down Expand Up @@ -637,24 +648,24 @@ func checkSharedHook(

if !exists {

mess := "Failed to execute shared hooks in:\n" +
mess := "Repository: '%s'\nneeds shared hooks in:\n" +
"'%s'\n"

if hook.IsCloned {
mess += "It is not available. To fix, run:\n" +
mess += "which are are not available. To fix, run:\n" +
"$ git hooks shared update\n" +
"or gracefully continue by setting:\n" +
"$ git hooks config skip-non-existing-shared-hooks --enable [--global]"
} else {
mess += "It does not exist."
mess += "which does not exist."
}

if settings.SkipNonExistingSharedHooks {
mess += "\nContinuing..."
}

log.ErrorOrPanicF(isFatal && !settings.SkipNonExistingSharedHooks,
err, mess, hook.OriginalURL)
err, mess, hook.RepositoryDir, hook.OriginalURL)

return false
}
Expand Down Expand Up @@ -769,12 +780,7 @@ func getHooksIn(
showTrustPrompt(uiSettings, checksums, hook)
}

log.PanicIfF(!settings.SkipUntrustedHooks && hook.Active && !hook.Trusted,
"Hook '%s' is active and needs to be trusted first.\n"+
"Either trust the hook or disable it, or skip active,\n"+
"untrusted hooks by running:\n"+
" $ git hooks config skip-untrusted-hooks --enable",
hook.NamespacePath)
failOrWarnOnActiveUntrusted(settings.SkipUntrustedHooks, hook)
}

if !hook.Active || !hook.Trusted {
Expand Down Expand Up @@ -861,6 +867,7 @@ func showTrustPrompt(
disableHook = true
default:
// Don't run hook ...
// Trusted == false
}
} else {
log.Info("-> Already accepted.")
Expand Down
2 changes: 1 addition & 1 deletion githooks/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func runSkipNonExistingSharedHooks(ctx *ccm.CmdContext, opts *SetOptions, gitOpt
localOrGlobal = ""
}

enabled, _ := hooks.SkipNonExistingSharedHooks(ctx.GitX, scope)
enabled := hooks.SkipNonExistingSharedHooks(ctx.GitX, scope)
if enabled {
ctx.Log.InfoF("Skipping %s is enabled%s.", text, localOrGlobal)
} else {
Expand Down
10 changes: 5 additions & 5 deletions githooks/hooks/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (s *SharedRepo) IsCloneValid() bool {
return false
}

// SetSkipNonExistingSharedHooks sets settings if the hook runner should fail on non existing hooks.
// SetSkipNonExistingSharedHooks sets settings if the hook runner should skip on non existing hooks.
func SetSkipNonExistingSharedHooks(gitx *git.Context, enable bool, reset bool, scope git.ConfigScope) error {
switch {
case reset:
Expand All @@ -574,14 +574,14 @@ func SetSkipNonExistingSharedHooks(gitx *git.Context, enable bool, reset bool, s
}
}

// SkipNonExistingSharedHooks gets the settings if the hook runner should fail on non existing hooks.
func SkipNonExistingSharedHooks(gitx *git.Context, scope git.ConfigScope) (enabled bool, isSet bool) {
// SkipNonExistingSharedHooks gets the settings if the hook runner should skip on non existing hooks.
func SkipNonExistingSharedHooks(gitx *git.Context, scope git.ConfigScope) bool {
conf := gitx.GetConfig(GitCKSkipNonExistingSharedHooks, scope)
switch {
case strs.IsEmpty(conf):
return
return false
default:
return conf == git.GitCVTrue, true
return conf == git.GitCVTrue
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/step-103.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ git add A || exit 1
OUTPUT=$(git commit -a -m "Test" 2>&1)

# shellcheck disable=SC2181
if [ $? -eq 0 ] || ! echo "$OUTPUT" | grep -q "Failed to execute shared hook"; then
if [ $? -eq 0 ] || ! echo "$OUTPUT" | grep -q "needs shared hooks in:"; then
echo "! Expected to fail on not availabe shared hooks. output:"
echo "$OUTPUT"
exit 1
Expand Down

0 comments on commit abf3a7c

Please sign in to comment.