Skip to content

Commit

Permalink
feat: Disable autoupdate (gitconfig) (#155)
Browse files Browse the repository at this point in the history
- feat: Disable auto-update dispatch and build disable feature ⚓
- fix: Format cli docs ⚓
- feat!: Regex replace all gitconfig ⚓
- feat!: Refactor to proper nameing for `update-check` ⚓
- fix: Download during benchmark ⚓
- fix: Do not uninstall binaries when using package manager ⚓
- fix: Documentation ⚓
  • Loading branch information
gabyx committed Apr 21, 2024
1 parent cbdb42b commit 9002369
Show file tree
Hide file tree
Showing 37 changed files with 117 additions and 76 deletions.
2 changes: 0 additions & 2 deletions .githooks/pre-commit/cli-docs-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ for file in $STAGED_FILES; do
exit 1
}

git add "$repoDir/docs/cli/"*

echo "* Docs regenerated." >&2
else
echo "! You need to regenerate the CLI docs (its too old)!" >&2
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ below.

```shell
# enable with:
$ git hooks update --enable # `Config: githooks.autoUpdateEnabled`
$ git hooks update --enable # `Config: githooks.updateCheckEnabled`

# disable with:
$ git hooks update --disable
Expand All @@ -1480,7 +1480,7 @@ install time) which is checked out in `<installDir>/release`. Normally, if there
are new tags (versions) available, the newest tag (version) is installed.
However, [prerelease version](https://semver.org) tags (e.g. `v1.0.0-rc1`) are
generally skipped. You can disable this behavior by setting the global Git
config value `githooks.autoUpdateUsePrerelease = true`. Major version updates
config value `githooks.updateCheckUsePrerelease = true`. Major version updates
are **never** automatically installed an need the consent of the user.

If the annotated version tag or the commit message it points to
Expand Down
12 changes: 12 additions & 0 deletions docs/changes/Braking-Changes-v3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# API Breaking Changes v3.x.x to v2

## Githooks Updates

Githooks does not run updates automatically anymore. Update checks are however
still executed if `git hooks config enable-update-checks` is enabled. The
following settings are changed:

`githooks.autoUpdateEnabled` -> `githooks.updateCheckEnabled`
`githooks.autoUpdateUsePrerelease` -> `githooks.updateCheckUsePrerelease`

Any upgrade to `v3` will change this setting automatically.
2 changes: 1 addition & 1 deletion githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func shouldRunUpdateCheck(settings *HookSettings) bool {
return false
}

enabled, _ := updates.GetAutomaticUpdateCheckSettings(settings.GitX)
enabled, _ := updates.GetUpdateCheckSettings(settings.GitX)
if !enabled {
return false
}
Expand Down
22 changes: 11 additions & 11 deletions githooks/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,28 +429,28 @@ func runTrustAllHooks(ctx *ccm.CmdContext, opts *SetOptions) {

}

// RunUpdate enables/disables automatic Githooks update.
func RunUpdate(ctx *ccm.CmdContext, opts *SetOptions) {
const text = "Automatic Githooks update"
// RunUpdateCheck enables/disables Githooks update checks.
func RunUpdateCheck(ctx *ccm.CmdContext, opts *SetOptions) {
const text = "Githooks update"

switch {
case opts.Set:
err := updates.SetAutomaticUpdateCheckSettings(true, false)
ctx.Log.AssertNoErrorPanicF(err, "Could not enable automatic update settings.")
err := updates.SetUpdateCheckSettings(true, false)
ctx.Log.AssertNoErrorPanicF(err, "Could not enable update settings.")
ctx.Log.InfoF("%s checks are now enabled.", text)

case opts.Unset:
err := updates.SetAutomaticUpdateCheckSettings(false, false)
ctx.Log.AssertNoErrorPanicF(err, "Could not disable automatic update settings.")
err := updates.SetUpdateCheckSettings(false, false)
ctx.Log.AssertNoErrorPanicF(err, "Could not disable update settings.")
ctx.Log.InfoF("%s checks are now disabled.", text)

case opts.Reset:
err := updates.SetAutomaticUpdateCheckSettings(false, true)
ctx.Log.AssertNoErrorPanicF(err, "Could not reset automatic update settings.")
err := updates.SetUpdateCheckSettings(false, true)
ctx.Log.AssertNoErrorPanicF(err, "Could not reset update settings.")
ctx.Log.InfoF("%s setting is now unset.", text)

case opts.Print:
enabled, _ := updates.GetAutomaticUpdateCheckSettings(ctx.GitX)
enabled, _ := updates.GetUpdateCheckSettings(ctx.GitX)
switch {
case enabled:
ctx.Log.InfoF("%s checks are enabled.", text)
Expand Down Expand Up @@ -861,7 +861,7 @@ func configUpdateCmd(ctx *ccm.CmdContext, configCmd *cobra.Command, setOpts *Set
Short: "Change Githooks update settings.",
Long: `Enable or disable automatic Githooks updates.`,
Run: func(cmd *cobra.Command, args []string) {
RunUpdate(ctx, setOpts)
RunUpdateCheck(ctx, setOpts)
}}

optsPSUR := createOptionMap(true, true, true)
Expand Down
9 changes: 5 additions & 4 deletions githooks/cmd/installer/download-binaries-mock.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//go:build mock
//go:build mock || download_mock

package installer

import (
"os"
"path"
"runtime"

cm "github.com/gabyx/githooks/githooks/common"
strs "github.com/gabyx/githooks/githooks/strings"
"github.com/gabyx/githooks/githooks/updates"
"github.com/gabyx/githooks/githooks/updates/download"
"os"
"path"
"runtime"
)

// IsRunningCoverage returns if we are running coverage.
Expand Down
2 changes: 1 addition & 1 deletion githooks/cmd/installer/download-binaries.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !mock
//go:build !mock && !download_mock

package installer

Expand Down
14 changes: 12 additions & 2 deletions githooks/cmd/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func setupAutomaticUpdate(
dryRun bool,
promptx prompt.IContext) {

enabled, isSet := updates.GetAutomaticUpdateCheckSettings(gitx)
enabled, isSet := updates.GetUpdateCheckSettings(gitx)
promptMsg := ""

switch {
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func setupAutomaticUpdate(
log.Info("[dry run] Would enable automatic update checks.")
} else {

err := updates.SetAutomaticUpdateCheckSettings(true, false)
err := updates.SetUpdateCheckSettings(true, false)
if log.AssertNoErrorF(err, "Failed to enable automatic update checks.") {
log.Info("Automatic update checks are now enabled.")
}
Expand Down Expand Up @@ -1525,11 +1525,21 @@ func runInstall(cmd *cobra.Command, ctx *ccm.CmdContext, vi *viper.Viper) error
}

func transformLegacyGitConfigSettings(log cm.ILogContext, gitx *git.Context) {
// Server hooks.
useOnlyServerHooks := gitx.GetConfig("githooks.maintainOnlyServerHooks", git.GlobalScope)
if useOnlyServerHooks == git.GitCVTrue {
err := hooks.SetMaintainedHooks(gitx, []string{"server"}, git.GlobalScope)
log.AssertNoError(err, "Could not set maintained hooks to 'server'.")
}

_ = git.NewCtx().UnsetConfig("githooks.maintainOnlyServerHooks", git.GlobalScope)

// AutoUpdate to UpdateCheck
autoUpdateEnabled := gitx.GetConfig("githooks.autoUpdateEnabled", git.GlobalScope)
if strs.IsNotEmpty(autoUpdateEnabled) {
err := updates.SetUpdateCheckSettings(autoUpdateEnabled == git.GitCVTrue, false)
log.AssertNoError(err, "Could not set automatic update check.")
}
_ = git.NewCtx().UnsetConfig("githooks.autoUpdateEnabled", git.GlobalScope)

}
10 changes: 10 additions & 0 deletions githooks/cmd/uninstaller/uninstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ func cleanBinaries(
installDir string,
tempDir string) {

if cm.PackageManagerEnabled {
// Cannot uninstall binaries because this is done
// through the package manager
log.Warn(
"Not installing Githook binaries.",
"This must be done through your package manager.")

return
}

binDir := hooks.GetBinaryDir(installDir)

if cm.IsDirectory(binDir) {
Expand Down
2 changes: 1 addition & 1 deletion githooks/cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func runUpdate(

switch {
case setOpts.Set || setOpts.Unset:
config.RunUpdate(ctx, setOpts)
config.RunUpdateCheck(ctx, setOpts)

default:

Expand Down
10 changes: 10 additions & 0 deletions githooks/common/package-manager-disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build package_manager_enabled

package common

const (
// We are not using a package manager and
// updating is allwed and Githooks is
// not externally managed.
PackageManagerEnabled = true
)
10 changes: 10 additions & 0 deletions githooks/common/package-manager-enabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !package_manager_enabled

package common

const (
// We are using a package manager and
// updating is forbidden and Githooks is
// externally managed.
PackageManagerEnabled = false
)
4 changes: 2 additions & 2 deletions githooks/hooks/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const (

GitCKDisable = "githooks.disable"

GitCKUpdateCheckEnabled = "githooks.autoUpdateEnabled"
GitCKUpdateCheckUsePrerelease = "githooks.autoUpdateUsePrerelease"
GitCKUpdateCheckEnabled = "githooks.updateCheckEnabled"
GitCKUpdateCheckUsePrerelease = "githooks.updateCheckUsePrerelease"

GitCKBugReportInfo = "githooks.bugReportInfo"

Expand Down
3 changes: 2 additions & 1 deletion githooks/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ parse_args() {
elif [ "$p" = "--coverage" ]; then
BUILD_COVERAGE="true"
elif [ "$p" = "--benchmark" ]; then
DEBUG_TAG="${DEBUG_TAG}${DEBUG_TAG+,}benchmark"
DEBUG_TAG="${DEBUG_TAG}${DEBUG_TAG+,}benchmark,download_mock"
BUILD
elif [ "$p" = "--prod" ]; then
DEBUG_TAG=""
LD_FLAGS+=("-ldflags" "-s -w") # strip debug information
Expand Down
2 changes: 1 addition & 1 deletion githooks/scripts/clean-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ git config --global --unset githooks.runner
git config --global --unset-all githooks.shared
git config --global --unset githooks.failOnNonExistingSharedHooks
git config --global --unset githooks.maintainedHooks
git config --global --unset githooks.autoUpdateEnabled
git config --global --unset githooks.updateCheckEnabled
git config --global --unset githooks.cloneUrl
git config --global --unset githooks.cloneBranch
git config --global --unset githooks.previousSearchDir
Expand Down
8 changes: 4 additions & 4 deletions githooks/updates/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/gabyx/githooks/githooks/hooks"
)

// SetAutomaticUpdateCheckSettings set the automatic update settings.
func SetAutomaticUpdateCheckSettings(enable bool, reset bool) error {
// SetUpdateCheckSettings set the automatic update settings.
func SetUpdateCheckSettings(enable bool, reset bool) error {
opt := hooks.GitCKUpdateCheckEnabled
gitx := git.NewCtx()

Expand All @@ -20,8 +20,8 @@ func SetAutomaticUpdateCheckSettings(enable bool, reset bool) error {
}
}

// GetAutomaticUpdateCheckSettings gets the automatic update settings.
func GetAutomaticUpdateCheckSettings(gitx *git.Context) (enabled bool, isSet bool) {
// GetUpdateCheckSettings gets the automatic update settings.
func GetUpdateCheckSettings(gitx *git.Context) (enabled bool, isSet bool) {
conf := gitx.GetConfig(hooks.GitCKUpdateCheckEnabled, git.GlobalScope)
switch {
case conf == git.GitCVTrue:
Expand Down
7 changes: 0 additions & 7 deletions githooks/updates/build-update-disable.go

This file was deleted.

7 changes: 0 additions & 7 deletions githooks/updates/build-update-enable.go

This file was deleted.

8 changes: 8 additions & 0 deletions githooks/updates/update-enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package updates

import "github.com/gabyx/githooks/githooks/common"

const (
// Only allow update if not using a package manager.
Enabled = !common.PackageManagerEnabled
)
8 changes: 0 additions & 8 deletions githooks/updates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,6 @@ func RunUpdate(
usePreRelease bool,
run func() error) (updateAvailable bool, accepted bool, err error) {

err = RecordUpdateCheckTimestamp(installDir)

if err != nil {
err = cm.Error("Could not record update check timestamp.")

return
}

cloneDir := hooks.GetReleaseCloneDir(installDir)
status, err := FetchUpdates(cloneDir, "", "", build.BuildTag, true, ErrorOnWrongRemote, usePreRelease)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions tests/exec-steps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ function main() {
fi

unset_environment || {
echo -e "! Unset env. failed: uninstall output was:\n $uninstall_out" >&2
failed=$((failed + 1))
break
}
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/step-029.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mkdir -p "$GH_TEST_TMP/test29" &&
cd "$GH_TEST_TMP/test29" &&
git init || exit 1

git config --global githooks.autoUpdateEnabled true || exit 1
git config --global githooks.updateCheckEnabled true || exit 1

OUTPUT=$(
ACCEPT_CHANGES=A \
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/step-030.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mkdir -p "$GH_TEST_TMP/test30" &&
cd "$GH_TEST_TMP/test30" &&
git init || exit 1

git config --global githooks.autoUpdateEnabled false || exit 1
git config --global githooks.updateCheckEnabled false || exit 1

ACCEPT_CHANGES=A "$GH_TEST_BIN/runner" "$(pwd)"/.git/hooks/post-commit

Expand Down
2 changes: 1 addition & 1 deletion tests/steps/step-031.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mkdir -p "$GH_TEST_TMP/test31" &&
cd "$GH_TEST_TMP/test31" &&
git init || exit 1

git config --global githooks.autoUpdateEnabled true || exit 1
git config --global githooks.updateCheckEnabled true || exit 1

ACCEPT_CHANGES=A "$GH_TEST_BIN/runner" "$(pwd)"/.git/hooks/post-commit

Expand Down
2 changes: 1 addition & 1 deletion tests/steps/step-036.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ accept_all_trust_prompts || exit 1
echo 'y
' | "$GH_TEST_BIN/cli" installer --stdin || exit 1

if [ "$(git config --global --get githooks.autoUpdateEnabled)" != "true" ]; then
if [ "$(git config --global --get githooks.updateCheckEnabled)" != "true" ]; then
echo "! Automatic update checks are not enabled"
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions tests/steps/step-037.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ TEST_DIR=$(cd "$(dirname "$0")/.." && pwd)

accept_all_trust_prompts || exit 1

git config --global githooks.autoUpdateEnabled false || exit 1
git config --global githooks.updateCheckEnabled false || exit 1
echo 'y
' | "$GH_TEST_BIN/cli" installer --stdin || exit 1

if [ "$(git config --global --get githooks.autoUpdateEnabled)" != "true" ]; then
if [ "$(git config --global --get githooks.updateCheckEnabled)" != "true" ]; then
echo "! Automatic update checks are not enabled"
exit 1
fi
2 changes: 1 addition & 1 deletion tests/steps/step-042.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if ! "$GH_TEST_BIN/cli" install; then
exit 1
fi

ARE_UPDATES_CHECKS_ENABLED=$(git config --global --get githooks.autoUpdateEnabled)
ARE_UPDATES_CHECKS_ENABLED=$(git config --global --get githooks.updateCheckEnabled)
if [ "$ARE_UPDATES_CHECKS_ENABLED" != "true" ]; then
echo "! Update checks were expected to be enabled"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/step-045.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ git -C "$GH_TEST_REPO" reset --hard v9.9.1 >/dev/null || {

CURRENT="$(git -C ~/.githooks/release rev-parse HEAD)"
cd "$GH_TEST_TMP/test045/001" &&
git config --global githooks.autoUpdateEnabled true &&
git config --global githooks.updateCheckEnabled true &&
set_update_check_timestamp $MOCK_LAST_RUN &&
OUT=$(git commit --allow-empty -m 'Second commit' 2>&1) || exit 1

Expand Down
4 changes: 2 additions & 2 deletions tests/steps/step-049.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ TEST_DIR=$(cd "$(dirname "$0")/.." && pwd)

accept_all_trust_prompts || exit 1

git config --global githooks.autoUpdateEnabled false || exit 1
git config --global githooks.updateCheckEnabled false || exit 1
"$GH_TEST_BIN/cli" installer --non-interactive || exit 1

if [ "$(git config --global --get githooks.autoUpdateEnabled)" != "false" ]; then
if [ "$(git config --global --get githooks.updateCheckEnabled)" != "false" ]; then
echo "! Automatic update checks were unexpectedly enabled"
exit 1
fi
Loading

0 comments on commit 9002369

Please sign in to comment.