Skip to content

Commit

Permalink
feat: Disable automatically running update (#154)
Browse files Browse the repository at this point in the history
- Disable auto-update dispatch and build disable feature ⚓
  Githooks does not run an update anymore in the runner and only `git hooks update` works 
  when its enabled in the build (for pkg managers this is important).
  • Loading branch information
gabyx committed Apr 21, 2024
1 parent d594235 commit cbdb42b
Show file tree
Hide file tree
Showing 22 changed files with 237 additions and 223 deletions.
14 changes: 7 additions & 7 deletions .githooks/pre-commit/.export-staged
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env bash

assertStaged() {
# Export if run without githooks...
if [ -z "${STAGED_FILES:-}" ]; then
CHANGED_FILES=$(git diff --cached --diff-filter=ACMR --name-only)
# Export if run without githooks...
if [ -z "${STAGED_FILES:-}" ]; then
CHANGED_FILES=$(git diff --cached --diff-filter=ACMR --name-only)

# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
STAGED_FILES="$CHANGED_FILES"
fi
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
STAGED_FILES="$CHANGED_FILES"
fi
fi
}
20 changes: 10 additions & 10 deletions .githooks/pre-commit/gofmt
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ set -e
set -u

if ! command -v gofmt >/dev/null 2>&1; then
echo "! Skipping gofmt - not installed" >&2
exit 0
echo "! Skipping gofmt - not installed" >&2
exit 0
fi

SUCCESS=0

for PKG_DIR in githooks/*; do
if ! [ -d "$PKG_DIR" ] || echo "$PKG_DIR" | grep -q "vendor"; then
continue
fi
if ! [ -d "$PKG_DIR" ] || echo "$PKG_DIR" | grep -q "vendor"; then
continue
fi

if ! gofmt -l "$PKG_DIR"; then
SUCCESS=1
fi
if ! gofmt -l "$PKG_DIR"; then
SUCCESS=1
fi
done

if [ "$SUCCESS" = "0" ]; then
echo "* gofmt is OK." >&2
echo "* gofmt is OK." >&2
else
exit "$SUCCESS"
exit "$SUCCESS"
fi
28 changes: 14 additions & 14 deletions .githooks/pre-commit/golint
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ set -e
set -u

if ! command -v golangci-lint >/dev/null 2>&1; then
echo "! golangci-lint - not installed" >&2
exit 1
echo "! golangci-lint - not installed" >&2
exit 1
fi

cd githooks || exit 1

if CGO_ENABLED=0 golangci-lint run -E godot \
-E gomnd \
-E goconst \
-E gochecknoinits \
-E nlreturn \
-E misspell \
-E lll \
-E exportloopref \
-E gocritic \
--max-same-issues 0 ./...; then
echo "* golangci-lint OK" >&2
-E gomnd \
-E goconst \
-E gochecknoinits \
-E nlreturn \
-E misspell \
-E lll \
-E exportloopref \
-E gocritic \
--max-same-issues 0 ./...; then
echo "* golangci-lint OK" >&2
else
echo "! golangci-lint problems detected" >&2
exit 1
echo "! golangci-lint problems detected" >&2
exit 1
fi
22 changes: 11 additions & 11 deletions .githooks/pre-commit/no-tabs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ assertStaged
success=0

checkTab() {
local lines
lines=$(grep -rnH "$(printf '\t')" "$1") || true
local lines
lines=$(grep -rnH "$(printf '\t')" "$1") || true

if [ -n "$lines" ]; then
echo "! Tab character found in: $lines" >&2
success=1
fi
if [ -n "$lines" ]; then
echo "! Tab character found in: $lines" >&2
success=1
fi
}

for file in $STAGED_FILES; do
if echo "$file" | grep -qvE "(githooks|docs)/.*$"; then
checkTab "$file"
fi
if echo "$file" | grep -qvE "(githooks|docs)/.*$"; then
checkTab "$file"
fi
done

if [ "$success" = "0" ]; then
echo "* No tab characters detected" >&2
echo "* No tab characters detected" >&2
else
exit "$success"
exit "$success"
fi
22 changes: 11 additions & 11 deletions .githooks/pre-commit/no-todo-or-fixme
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ assertStaged
success=0

checkTodo() {
local lines
lines=$(grep -rnHE "(TODO|FIXME)" "$1") || true
local lines
lines=$(grep -rnHE "(TODO|FIXME)" "$1") || true

if [ -n "$lines" ]; then
echo "! TODO or FIXME found in: $lines" >&2
success=1
fi
if [ -n "$lines" ]; then
echo "! TODO or FIXME found in: $lines" >&2
success=1
fi
}

for file in $STAGED_FILES; do
if echo "$file" | grep -qvE '(cover/|pre-commit/no-todo-or-fixme|\.md|\.devcontainer/\.p10k)'; then
checkTodo "$file"
fi
if echo "$file" | grep -qvE '(cover/|pre-commit/no-todo-or-fixme|\.md|\.devcontainer/\.p10k)'; then
checkTodo "$file"
fi
done

if [ "$success" = "0" ]; then
echo "* No TODO or FIXME detected" >&2
echo "* No TODO or FIXME detected" >&2
else
exit "$success"
exit "$success"
fi
50 changes: 25 additions & 25 deletions .githooks/pre-commit/update-templates
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ set -e
set -u

if command -v gsed &>/dev/null; then
SED="gsed"
SED="gsed"
else
SED="sed"
SED="sed"
fi

if ! $SED --version 2>&1 | grep -q "GNU"; then
echo "! Install GNU sed for updating the templates." >&2
exit 1
echo "! Install GNU sed for updating the templates." >&2
exit 1
fi

# Patch the hooks
# shellcheck disable=SC2016
hook_names=$($SED -E '1,/ManagedHookNames.*\{/d;/\}/,$d ; s/\s+"(.*)",/\1/' githooks/hooks/githooks.go) ||
{
echo "update-templates:: Replacement command failed!" >&2
exit 1
}
{
echo "update-templates:: Replacement command failed!" >&2
exit 1
}

for hook_name in $hook_names; do
# shellcheck disable=SC2015
$SED -e "s|GITHOOKS_RUNNER=.*|GITHOOKS_RUNNER=\"\$(cd \"\$(dirname \"\$0\")/../\" \&\& pwd)/githooks/bin/runner\"|" \
-e "s|\(various Git triggers.*\)|\1\n#\n# This file is auto-generated, do not edit!|" \
<githooks/build/embedded/run-wrapper.sh >"hooks/$hook_name" &&
chmod u+x "hooks/$hook_name" &&
git add hooks/* ||
{
echo "Failed to update \`hooks/$hook_name\`" >&2
exit 2
}
# shellcheck disable=SC2015
$SED -e "s|GITHOOKS_RUNNER=.*|GITHOOKS_RUNNER=\"\$(cd \"\$(dirname \"\$0\")/../\" \&\& pwd)/githooks/bin/runner\"|" \
-e "s|\(various Git triggers.*\)|\1\n#\n# This file is auto-generated, do not edit!|" \
<githooks/build/embedded/run-wrapper.sh >"hooks/$hook_name" &&
chmod u+x "hooks/$hook_name" &&
git add hooks/* ||
{
echo "Failed to update \`hooks/$hook_name\`" >&2
exit 2
}
done

# Delete all files not in `$hook_name`
for hook_file in hooks/*; do
name=$(basename "$hook_file")
if ! echo "$hook_names" | grep -q "$name"; then
git rm "$hook_file" >/dev/null 2>&1 || {
echo "Failed to delete obsolete hook \`$hook_file\`" >&2
exit 3
}
fi
name=$(basename "$hook_file")
if ! echo "$hook_names" | grep -q "$name"; then
git rm "$hook_file" >/dev/null 2>&1 || {
echo "Failed to delete obsolete hook \`$hook_file\`" >&2
exit 3
}
fi
done

echo "* Updated all hooks in 'hooks/*'" >&2
Expand Down
42 changes: 9 additions & 33 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,38 +359,24 @@ func exportStagedFiles(settings *HookSettings) (cleanUp func()) {
}

func updateGithooks(settings *HookSettings, uiSettings *UISettings) {

if !shouldRunUpdateCheck(settings) {
return
}

opts := []string{"--internal-auto-update"}
if settings.NonInteractive {
opts = append(opts, "--non-interactive")
}
err := updates.RecordUpdateCheckTimestamp(settings.InstallDir)
log.AssertNoError(err, "Could not record update check time.")

var usePreRelease bool
if settings.GitX.GetConfig(hooks.GitCKAutoUpdateUsePrerelease, git.GlobalScope) == git.GitCVTrue {
if settings.GitX.GetConfig(hooks.GitCKUpdateCheckUsePrerelease, git.GlobalScope) == git.GitCVTrue {
usePreRelease = true
opts = append(opts, "--use-pre-release")
}

updateAvailable, accepted, err := updates.RunUpdate(
settings.InstallDir,
updates.DefaultAcceptUpdateCallback(log, uiSettings.PromptCtx, updates.AcceptNonInteractiveNone),
usePreRelease,
func() error {
return updates.RunUpdateOverExecutable(settings.InstallDir,
&settings.ExecX,
cm.UseStreams(nil, os.Stderr, os.Stderr), // Must not use stdout, because Git hooks.
opts...)
})
cloneDir := hooks.GetReleaseCloneDir(settings.InstallDir)
status, err := updates.FetchUpdates(cloneDir, "", "", build.BuildTag, true, updates.ErrorOnWrongRemote, usePreRelease)

if err != nil {
m := strs.Fmt(
"Running update failed. See latest log '%s' !",
path.Join(os.TempDir(),
"githooks-installer-*.log"))
"Running update check failed.")

log.AssertNoError(err, m)
err = uiSettings.PromptCtx.ShowMessage(m, true)
Expand All @@ -399,20 +385,10 @@ func updateGithooks(settings *HookSettings, uiSettings *UISettings) {
return
}

switch {
case updateAvailable:
if accepted {
log.Info("Update successfully dispatched.")
} else {
log.Info("Update declined.")
}
default:
log.InfoF("Githooks is at the latest version '%s'",
build.GetBuildVersion().String())
}

versionText, _ := updates.FormatUpdateText(&status, true)
log.Info(versionText)
log.Info(
"If you would like to disable auto-updates, run:",
"If you would like to disable update checks, run:",
" $ git hooks update disable")
}

Expand Down
19 changes: 14 additions & 5 deletions githooks/cmd/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,12 @@ func runInstallDispatched(
cm.PanicIfF(!status.IsUpdateAvailable,
"An autoupdate should only be triggered when and update is found.")

cm.PanicIfF(!updates.Enabled,
"An autoupdate should only be triggered when Githooks\n"+
"is built with updates enabled.")

} else {
log.Info("Fetching update in Githooks clone...")
log.Info("Fetching Githooks clone...")

status, err = updates.FetchUpdates(
settings.CloneDir,
Expand All @@ -441,7 +445,12 @@ func runInstallDispatched(
log.InfoF("Githooks installer existing: '%v'", haveInstaller)

// We download/build the binaries always.
doUpdate := status.IsUpdateAvailable && (args.Update || args.InternalAutoUpdate)
// Only do an update if enabled and we either have
// given the update flag or its an auto-update
// call.
doUpdate := updates.Enabled &&
status.IsUpdateAvailable && (args.Update || args.InternalAutoUpdate)

tag := ""
commit := ""

Expand Down Expand Up @@ -1306,7 +1315,7 @@ func runInstaller(
args *Arguments) {

if strs.IsEmpty(args.InternalUpdateFromVersion) {
log.InfoF("Running install to version '%s' ...", build.BuildVersion)
log.InfoF("Running install at current version '%s' ...", build.BuildVersion)
} else {
log.InfoF("Running install from '%s' -> '%s' ...", args.InternalUpdateFromVersion, build.BuildVersion)
}
Expand Down Expand Up @@ -1351,7 +1360,7 @@ func runInstaller(
args.DryRun,
uiSettings)

if !args.InternalAutoUpdate {
if !args.InternalAutoUpdate && updates.Enabled {
setupAutomaticUpdate(log, gitx, args.NonInteractive, args.DryRun, uiSettings.PromptCtx)
}

Expand Down Expand Up @@ -1483,7 +1492,7 @@ func runInstall(cmd *cobra.Command, ctx *ccm.CmdContext, vi *viper.Viper) error
setInstallDir(log, ctx.GitX, settings.InstallDir)
}

if !args.InternalPostDispatch {
if updates.Enabled && !args.InternalPostDispatch {
assertOneInstallerRunning(log, ctx.CleanupX)

// Dispatch from an old installer to a new one.
Expand Down
3 changes: 3 additions & 0 deletions githooks/cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func runUpdate(
promptx = ctx.PromptCtx
}

err := updates.RecordUpdateCheckTimestamp(ctx.InstallDir)
ctx.Log.AssertNoError(err, "Could not record update check time.")

updateAvailable, accepted, err := updates.RunUpdate(
ctx.InstallDir,
updates.DefaultAcceptUpdateCallback(ctx.Log, promptx, nonInteractiveAccept),
Expand Down
8 changes: 4 additions & 4 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"

GitCKAutoUpdateEnabled = "githooks.autoUpdateEnabled"
GitCKAutoUpdateUsePrerelease = "githooks.autoUpdateUsePrerelease"
GitCKUpdateCheckEnabled = "githooks.autoUpdateEnabled"
GitCKUpdateCheckUsePrerelease = "githooks.autoUpdateUsePrerelease"

GitCKBugReportInfo = "githooks.bugReportInfo"

Expand Down Expand Up @@ -72,8 +72,8 @@ func GetGlobalGitConfigKeys() []string {
GitCKMaintainedHooks,
GitCKPreviousSearchDir,

GitCKAutoUpdateEnabled,
GitCKAutoUpdateUsePrerelease,
GitCKUpdateCheckEnabled,
GitCKUpdateCheckUsePrerelease,

GitCKBugReportInfo,

Expand Down
Loading

0 comments on commit cbdb42b

Please sign in to comment.