Skip to content

Commit

Permalink
Disable shared hook updates ⚓
Browse files Browse the repository at this point in the history
- Improved readme, typos.
  • Loading branch information
gabyx committed Mar 23, 2021
1 parent a4d570d commit 882e8cc
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 130 deletions.
213 changes: 114 additions & 99 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/cli/git_hooks_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ git hooks config
* [git hooks config clone-url](git_hooks_config_clone-url.md) - Changes the Githooks clone url used for any update.
* [git hooks config delete-detected-lfs-hooks](git_hooks_config_delete-detected-lfs-hooks.md) - Change the behavior for detected LFS hooks during install.
* [git hooks config disable](git_hooks_config_disable.md) - Disables Githooks in the current repository or globally.
* [git hooks config disable-shared-hooks-update](git_hooks_config_disable-shared-hooks-update.md) - Disable/enable automatic updates of shared hooks.
* [git hooks config list](git_hooks_config_list.md) - Lists settings of the Githooks configuration.
* [git hooks config non-interactive-runner](git_hooks_config_non-interactive-runner.md) - Enables/disables non-interactive execution of the runner.
* [git hooks config search-dir](git_hooks_config_search-dir.md) - Changes the search directory used during installation.
Expand Down
30 changes: 30 additions & 0 deletions docs/cli/git_hooks_config_disable-shared-hooks-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## git hooks config disable-shared-hooks-update

Disable/enable automatic updates of shared hooks.

### Synopsis

Disable/enable automatic updates of shared hook
repositories.

```
git hooks config disable-shared-hooks-update [flags]
```

### Options

```
--print Print the setting.
--set Disable automatic updates of shared hooks.
--unset Enabled automatic updates of shared hooks..
--reset Reset (enable) automatic updates of shared hooks.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for disable-shared-hooks-update
```

### SEE ALSO

* [git hooks config](git_hooks_config.md) - Manages various Githooks configuration.

###### Auto generated by spf13/cobra
14 changes: 7 additions & 7 deletions docs/cli/git_hooks_config_non-interactive-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ git hooks config non-interactive-runner [flags]
### Options

```
--print Print the setting.
--enable Enables non-interactive mode of the runner executable.
--disable Disables non-interactive mode of the runner executable.
--Reset non-interactive mode of the runner executable. Reset the setting.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for non-interactive-runner
--print Print the setting.
--enable Enables non-interactive mode of the runner executable.
--disable Disables non-interactive mode of the runner executable.
--reset Reset non-interactive mode of the runner executable.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for non-interactive-runner
```

### SEE ALSO
Expand Down
14 changes: 7 additions & 7 deletions docs/cli/git_hooks_config_skip-non-existing-shared-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ git hooks config skip-non-existing-shared-hooks [flags]
### Options

```
--print Print the setting.
--enable Enable skipping non-existing shared hooks.
--disable Disable skipping non-existing shared hooks.
--Reset skipping non-existing shared hooks. Reset the setting.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for skip-non-existing-shared-hooks
--print Print the setting.
--enable Enable skipping non-existing shared hooks.
--disable Disable skipping non-existing shared hooks.
--reset Reset skipping non-existing shared hooks.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for skip-non-existing-shared-hooks
```

### SEE ALSO
Expand Down
14 changes: 7 additions & 7 deletions docs/cli/git_hooks_config_skip-untrusted-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ git hooks config skip-untrusted-hooks [flags]
### Options

```
--print Print the setting.
--enable Enable skipping active, untrusted hooks.
--disable Disable skipping active, untrusted hooks.
--Reset skipping active, untrusted hooks. Reset the setting.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for skip-untrusted-hooks
--print Print the setting.
--enable Enable skipping active, untrusted hooks.
--disable Disable skipping active, untrusted hooks.
--reset Reset skipping active, untrusted hooks.
--local Use the local Git configuration (default, except for `--print`).
--global Use the global Git configuration.
-h, --help help for skip-untrusted-hooks
```

### SEE ALSO
Expand Down
13 changes: 8 additions & 5 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,15 @@ func collectHooks(

func updateSharedHooks(settings *HookSettings, sharedHooks []hooks.SharedRepo, sharedType hooks.SharedHookType) {

if settings.HookName != "post-merge" &&
!(settings.HookName == "post-checkout" &&
settings.Args[0] == git.NullRef) &&
!strs.Includes(settings.GitX.GetConfigAll(hooks.GitCKSharedUpdateTriggers, git.Traverse),
settings.HookName) {
disableUpdate, _ := hooks.IsSharedHooksUpdateDisabled(settings.GitX, git.Traverse)
updateTriggers := settings.GitX.GetConfigAll(hooks.GitCKSharedUpdateTriggers, git.Traverse)

triggered := settings.HookName == "post-merge" ||
(settings.HookName == "post-checkout" &&
settings.Args[0] == git.NullRef) ||
strs.Includes(updateTriggers, settings.HookName)

if disableUpdate || !triggered {
log.Debug("Shared hooks not updated.")

return
Expand Down
90 changes: 87 additions & 3 deletions githooks/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,51 @@ func runSkipNonExistingSharedHooks(ctx *ccm.CmdContext, opts *SetOptions, gitOpt
}
}

func runDisableSharedHooksUpdate(ctx *ccm.CmdContext, opts *SetOptions, gitOpts *GitOptions) {
scope := wrapToGitScope(ctx.Log, gitOpts)

localOrGlobal := "locally" //nolint: goconst
if gitOpts.Global {
localOrGlobal = "globally" //nolint: goconst
}

const text = "automatic updates of shared hooks"
switch {
case opts.Set:
err := hooks.SetDisableSharedHooksUpdate(ctx.GitX, true, false, scope)
ctx.Log.AssertNoErrorPanicF(err, "Could not disable %s %s.", text, localOrGlobal)
ctx.Log.InfoF("Disable %s %s.", text, localOrGlobal)

case opts.Unset:
err := hooks.SetDisableSharedHooksUpdate(ctx.GitX, false, false, scope)
ctx.Log.AssertNoErrorPanicF(err, "Could not enable %s %s.", text, localOrGlobal)
ctx.Log.InfoF("Enabled %s %s.", text, localOrGlobal)

case opts.Reset:
err := hooks.SetDisableSharedHooksUpdate(ctx.GitX, false, true, scope)
ctx.Log.AssertNoErrorPanicF(err, "Could not reset (enable) %s %s.", text, localOrGlobal)
ctx.Log.InfoF("Reset (enabled) %s %s.", text, localOrGlobal)

case opts.Print:
localOrGlobal = " " + localOrGlobal
if !gitOpts.Global && !gitOpts.Local {
scope = git.Traverse
localOrGlobal = ""
}

msg := "Automatic updates of shared hooks are "
disabled, _ := hooks.IsSharedHooksUpdateDisabled(ctx.GitX, scope)
if disabled {
ctx.Log.InfoF(msg+"disabled%s.", text, localOrGlobal)
} else {
ctx.Log.InfoF(msg+"enabled%s.", text, localOrGlobal)
}

default:
cm.Panic("Wrong arguments.")
}
}

func runSkipUntrustedHooks(ctx *ccm.CmdContext, opts *SetOptions, gitOpts *GitOptions) {
scope := wrapToGitScope(ctx.Log, gitOpts)

Expand Down Expand Up @@ -796,7 +841,45 @@ has not been called yet.`,
wrapToEnableDisable(&optsPSUR)
optsPSUR.SetDesc = "Enable skipping non-existing shared hooks."
optsPSUR.UnsetDesc = "Disable skipping non-existing shared hooks."
optsPSUR.Reset = "Reset skipping non-existing shared hooks."
optsPSUR.ResetDesc = "Reset skipping non-existing shared hooks."

configSetOptions(nonExistSharedCmd, setOpts, &optsPSUR, ctx.Log, 0, 0)

nonExistSharedCmd.Flags().BoolVar(&gitOpts.Local, "local", false,
"Use the local Git configuration (default, except for '--print').")
nonExistSharedCmd.Flags().BoolVar(&gitOpts.Global,
"global", false, "Use the global Git configuration.")

configCmd.AddCommand(ccm.SetCommandDefaults(ctx.Log, nonExistSharedCmd))
}

func configDisableSharedHooksUpdate(
ctx *ccm.CmdContext,
configCmd *cobra.Command,
setOpts *SetOptions,
gitOpts *GitOptions) {

nonExistSharedCmd := &cobra.Command{
Use: "disable-shared-hooks-update [flags]",
Short: "Disable/enable automatic updates of shared hooks.",
Long: `Disable/enable automatic updates of shared hook
repositories.`,
Run: func(cmd *cobra.Command, args []string) {
if !gitOpts.Local && !gitOpts.Global {
gitOpts.Local = true
}

if gitOpts.Local {
ccm.AssertRepoRoot(ctx)
}

runDisableSharedHooksUpdate(ctx, setOpts, gitOpts)
}}

optsPSUR := createOptionMap(true, true, true)
optsPSUR.SetDesc = "Disable automatic updates of shared hooks."
optsPSUR.UnsetDesc = "Enabled automatic updates of shared hooks.."
optsPSUR.ResetDesc = "Reset (enable) automatic updates of shared hooks."

configSetOptions(nonExistSharedCmd, setOpts, &optsPSUR, ctx.Log, 0, 0)

Expand Down Expand Up @@ -836,7 +919,7 @@ Mostly wanted if all hooks must be executed.`,
wrapToEnableDisable(&optsPSUR)
optsPSUR.SetDesc = "Enable skipping active, untrusted hooks."
optsPSUR.UnsetDesc = "Disable skipping active, untrusted hooks."
optsPSUR.Reset = "Reset skipping active, untrusted hooks."
optsPSUR.ResetDesc = "Reset skipping active, untrusted hooks."

configSetOptions(nonExistSharedCmd, setOpts, &optsPSUR, ctx.Log, 0, 0)

Expand Down Expand Up @@ -875,7 +958,7 @@ See 'git hooks config trust-all --help'.`,
wrapToEnableDisable(&optsPSUR)
optsPSUR.SetDesc = "Enables non-interactive mode of the runner executable."
optsPSUR.UnsetDesc = "Disables non-interactive mode of the runner executable."
optsPSUR.Reset = "Reset non-interactive mode of the runner executable."
optsPSUR.ResetDesc = "Reset non-interactive mode of the runner executable."

configSetOptions(nonInteracticeRunner, setOpts, &optsPSUR, ctx.Log, 0, 0)

Expand Down Expand Up @@ -935,6 +1018,7 @@ func NewCmd(ctx *ccm.CmdContext) *cobra.Command {
configCloneBranchCmd(ctx, configCmd, &setOpts)

configSharedCmd(ctx, configCmd, &setOpts, &gitOpts)
configDisableSharedHooksUpdate(ctx, configCmd, &setOpts, &gitOpts)

configSkipNonExistingSharedHooks(ctx, configCmd, &setOpts, &gitOpts)
configFailUntrustedHooks(ctx, configCmd, &setOpts, &gitOpts)
Expand Down
7 changes: 5 additions & 2 deletions githooks/hooks/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ const (

// Git config keys for local/global config.
const (
GitCKShared = "githooks.shared"
GitCKSharedUpdateTriggers = "githooks.sharedHooksUpdateTriggers"
GitCKShared = "githooks.shared"
GitCKSharedUpdateTriggers = "githooks.sharedHooksUpdateTriggers"
GitCKAutoUpdateSharedHooksDisabled = "githooks.autoUpdateSharedHooksDisabled"

GitCKSkipNonExistingSharedHooks = "githooks.skipNonExistingSharedHooks"
GitCKSkipUntrustedHooks = "githooks.skipUntrustedHooks"
Expand Down Expand Up @@ -88,6 +89,7 @@ func GetGlobalGitConfigKeys() []string {
// Local & global.
GitCKShared,
GitCKSharedUpdateTriggers,
GitCKAutoUpdateSharedHooksDisabled,

GitCKSkipNonExistingSharedHooks,
GitCKSkipUntrustedHooks,
Expand All @@ -103,6 +105,7 @@ func GetLocalGitConfigKeys() []string {

GitCKShared,
GitCKSharedUpdateTriggers,
GitCKAutoUpdateSharedHooksDisabled,

GitCKSkipNonExistingSharedHooks,
GitCKSkipUntrustedHooks,
Expand Down
22 changes: 22 additions & 0 deletions githooks/hooks/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,25 @@ func SkipNonExistingSharedHooks(gitx *git.Context, scope git.ConfigScope) (enabl
return conf == git.GitCVTrue, true
}
}

// SetDisableSharedHooksUpdate sets settings if the hook runner should
// disable automatic updates for shared hooks.
func SetDisableSharedHooksUpdate(gitx *git.Context, enable bool, reset bool, scope git.ConfigScope) error {
switch {
case reset:
return gitx.UnsetConfig(GitCKAutoUpdateSharedHooksDisabled, scope)
default:
return gitx.SetConfig(GitCKAutoUpdateSharedHooksDisabled, enable, scope)
}
}

// IsSharedHooksUpdateDisable checks if automatic updates for shared hooks is disabled.
func IsSharedHooksUpdateDisabled(gitx *git.Context, scope git.ConfigScope) (disabled bool, isSet bool) {
conf := gitx.GetConfig(GitCKAutoUpdateSharedHooksDisabled, scope)
switch {
case strs.IsEmpty(conf):
return
default:
return conf == git.GitCVTrue, true
}
}

0 comments on commit 882e8cc

Please sign in to comment.