Skip to content

Commit

Permalink
feat: Make update from version < 3 break due to too many changes ⚓ (#…
Browse files Browse the repository at this point in the history
…161)

Signed-off-by: Gabriel Nützi <[email protected]>
  • Loading branch information
gabyx committed Apr 21, 2024
1 parent 12fbad6 commit 9d0c455
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .githooks/pre-commit/golint
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cd githooks || exit 1

if
CGO_ENABLED=0 golangci-lint run \
--skip-dirs /usr/local/go \
--exclude-dirs /usr/local/go \
-E godot \
-E gomnd \
-E goconst \
Expand Down
3 changes: 2 additions & 1 deletion docs/cli/git_hooks_installer.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ git hooks installer [flags]
You can list them separately or comma-separated in one argument.
--centralized If the install mode `centralized` should be used which
sets the global `core.hooksPath`.
--git-config-no-abs-path Make certain Githooks Git config valuesnot use abs. paths. Useful to have the Git config not change.
--git-config-no-abs-path Make certain Githooks Git config values
not use abs. paths. Useful to have the Git config not change.
This means you need to have the Githooks binaries in your path.
--clone-url string The clone url from which Githooks should clone
and install/update itself. Githooks tries to
Expand Down
85 changes: 10 additions & 75 deletions githooks/cmd/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
strs "github.com/gabyx/githooks/githooks/strings"
"github.com/gabyx/githooks/githooks/updates"
"github.com/gabyx/githooks/githooks/updates/download"
"github.com/hashicorp/go-version"

"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -131,7 +132,7 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) {
if !cm.PackageManagerEnabled {
cmd.PersistentFlags().Bool(
"git-config-no-abs-path", false,
"Make certain Githooks Git config values"+
"Make certain Githooks Git config values\n"+
"not use abs. paths. Useful to have the Git config not change.\n"+
"This means you need to have the Githooks binaries in your path.")
}
Expand Down Expand Up @@ -1221,7 +1222,7 @@ func runInstaller(
log.InfoF("Running install from '%s' -> '%s' ...", args.InternalUpdateFromVersion, build.BuildVersion)
}

transformLegacyGitConfigSettings(log, gitx)
transformLegacyGitConfigSettings(log, args.InternalUpdateFromVersion)

err := settings.RegisteredGitDirs.Load(settings.InstallDir, true, true)
log.AssertNoErrorPanicF(err, "Could not load register file in '%s'.", settings.InstallDir)
Expand Down Expand Up @@ -1445,79 +1446,13 @@ func runInstall(cmd *cobra.Command, ctx *ccm.CmdContext, vi *viper.Viper) error
return nil
}

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)

// Version 2.X to 3
// ================

// 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)

// Transform old install modes from v2 to v3
// =========================================
hasInstallBeforeV3 := gitx.GetConfig(hooks.GitCKInstallMode, git.GlobalScope) == "" &&
gitx.GetConfig(hooks.GitCKRunner, git.GlobalScope) != ""

if hasInstallBeforeV3 {
useCoreHooksPath := gitx.GetConfig("githooks.useCoreHooksPath", git.GlobalScope)
useManual := gitx.GetConfig("githooks.useManual", git.GlobalScope)

switch {
case useCoreHooksPath == git.GitCVTrue:
log.Warn("Transform old `--use-core-hookspath` install to new `--centralized` install.")
err := gitx.SetConfig(
hooks.GitCKInstallMode,
install.InstallModeTypeV.Centralized.Name(),
git.GlobalScope)
log.AssertNoError(err, "Could not set install mode.")

case useManual == git.GitCVTrue:
log.Warn("Transform old manual install to new normal install.")
err := gitx.SetConfig(
hooks.GitCKInstallMode,
install.InstallModeTypeV.Manual.Name(),
git.GlobalScope)
log.AssertNoError(err, "Could not set install mode.")

manualTemplateDir := gitx.GetConfig("githooks.manualTemplateDir", git.GlobalScope)
err = gitx.SetConfig(
hooks.GitCKPathForUseCoreHooksPath,
path.Join(manualTemplateDir, "hooks"),
git.GlobalScope)
log.AssertNoError(err, "Could not set path to use for core hooks path.")

case useManual != git.GitCVTrue && useCoreHooksPath != git.GitCVTrue:
log.Warn("Transform old normal (`init.templateDir`) install to new `--manual` install.")
// That was template dir mode.
// It gets transformed to `manual` mode.
err := gitx.SetConfig(hooks.GitCKInstallMode, "manual", git.GlobalScope)
log.AssertNoError(err, "Could not set install mode.")

templateDir := gitx.GetConfig(git.GitCKInitTemplateDir, git.GlobalScope)
if strs.IsNotEmpty(templateDir) {
err = gitx.SetConfig(hooks.GitCKPathForUseCoreHooksPath, path.Join(templateDir, "hooks"), git.GlobalScope)
log.AssertNoError(err, "Could not set path to use for core hooks path.")
}
func transformLegacyGitConfigSettings(log cm.ILogContext, internalUpdateFromVersion string) {
if strs.IsNotEmpty(internalUpdateFromVersion) {
if v, _ := version.NewSemver(internalUpdateFromVersion); v.Segments()[0] < 3 { // nolint: gomnd
log.PanicF("Cannot install new version '%s' over current version '%s' < 3.\n"+
"Too much changed. Please uninstall this version fully\n"+
"(also in registered repositories) and then install the new version.",
build.BuildVersion, internalUpdateFromVersion)
}
}

// Delete all
_ = git.NewCtx().UnsetConfig("githooks.useCoreHooksPath", git.GlobalScope)
_ = git.NewCtx().UnsetConfig("githooks.useManual", git.GlobalScope)
_ = git.NewCtx().UnsetConfig("githooks.manualTemplateDir", git.GlobalScope)
// =========================

}
12 changes: 9 additions & 3 deletions githooks/prompt/show-impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func showEntryLoopTerminal(
text string,
defaultAnswer string,
emptyCausesDefault bool,
answerToLowerCase bool,
validator AnswerValidator) (string, bool, error) {

var err error // all errors
Expand Down Expand Up @@ -189,7 +190,10 @@ func showEntryLoopTerminal(
}

// Trim everything.
ans = strings.ToLower(strings.TrimSpace(ans))
ans = strings.TrimSpace(ans)
if answerToLowerCase {
ans = strings.ToLower(ans)
}

// Validate the answer if possible.
if validator == nil {
Expand Down Expand Up @@ -223,13 +227,14 @@ func showEntryLoopTerminal(
func showMessageTerminal(
p *Context,
message string,
asError bool) (bool, error) {
_asError bool) (bool, error) {

_, isPromptDisplayed, err := showEntryLoopTerminal(
p,
message,
"",
true,
false,
nil)

return isPromptDisplayed, err
Expand All @@ -247,6 +252,7 @@ func showOptionsTerminal(
question,
defaultAnswer,
emptyCausesDefault,
true,
validator)
}

Expand Down Expand Up @@ -300,7 +306,7 @@ func showEntryTerminal(
text string,
defaultAnswer string,
validator AnswerValidator) (string, bool, error) {
return showEntryLoopTerminal(p, text, defaultAnswer, true, validator)
return showEntryLoopTerminal(p, text, defaultAnswer, true, false, validator)
}

func showEntryMulti(
Expand Down
46 changes: 30 additions & 16 deletions tests/general.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ function die() {
exit 1
}

function check_paths_are_equal() {
local a
a=$(echo "$1" | wrap_windows_paths)
local b
b=$(echo "$1" | wrap_windows_paths)
shift 2

[ "$a" = "$b" ] || {
echo "! Paths '$a' != '$b' :" "$@"
exit 1
}
}

function wrap_windows_paths() {
# On Windows we need to wrap paths sometimes.
# to make them equivalent to the Git bash thing.
cat | sed -E "s@[Cc]:/@/c/@g"
}

function accept_all_trust_prompts() {
export ACCEPT_CHANGES=Y
return 0
Expand Down Expand Up @@ -223,7 +242,6 @@ function install_hooks_if_not_centralized() {
}

function check_normal_install {
local expected="${1:-}"
check_install "$@"

if [ -n "$(git config --global core.hooksPath)" ]; then
Expand All @@ -232,21 +250,20 @@ function check_normal_install {
exit 1
fi
}

function check_centralized_install() {
local path_to_use
local expected="${1:-}"
local expected
expected="${2:-}"

path_to_use=$(git config --global githooks.pathForUseCoreHooksPath)
[ -d "$path_to_use" ] || {
echo "! Path '$path_to_use' does not exist."
exit 1
}

if [ "$path_to_use" != "$(git config --global core.hooksPath)" ]; then
echo "! Global config 'core.hooksPath' does not point to the same directory:" \
"'$(git config --global core.hooksPath)' != '$path_to_use'"
exit 1
fi
check_paths_are_equal "$path_to_use" "$(git config --global core.hooksPath)" \
"! Global config 'core.hooksPath' does not point to the same directory."

if [ -n "$expected" ] && [ "$path_to_use" != "$expected" ]; then
echo "! Path '$path_to_use' is not '$expected'"
Expand Down Expand Up @@ -308,11 +325,8 @@ function check_local_install() {
exit 1
}

if [ "$path_to_use" != "$(git -C "$dir" config --local core.hooksPath)" ]; then
echo "! Local config 'core.hooksPath' in '$repo' does not point" \
"to the same directory: '$(git -C "$dir" config --local core.hooksPath)' != '$path_to_use'"
exit 1
fi
check_paths_are_equal "$path_to_use" "$(git -C "$dir" config --local core.hooksPath)" \
"Local config 'core.hooksPath' in '$repo' does not point to same directory."

if [ -n "$expected" ] && [ "$path_to_use" != "$expected" ]; then
echo "! Path '$path_to_use' is not '$expected'"
Expand Down Expand Up @@ -381,7 +395,8 @@ function check_no_install() {
}

function check_install() {
local expected="${1:-}"
local expected
expected="${1:-}"

local path_to_use
path_to_use=$(git config --global githooks.pathForUseCoreHooksPath)
Expand All @@ -395,9 +410,8 @@ function check_install() {
exit 1
fi

if [ -n "$expected" ] && [ "$path_to_use" != "$expected" ]; then
echo "! Path '$path_to_use' is not '$expected'"
exit 1
if [ -n "$expected" ]; then
check_paths_are_equal "$path_to_use" "$expected"
fi

if [ ! -f "$path_to_use/githooks-contains-run-wrappers" ]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/test-windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ EOF
docker run --rm \
-a stdout \
-a stderr "githooks:windows-lfs" \
"C:/Program Files/Git/bin/sh.exe" ./exec-steps.sh --skip-docker-check --seq 064
"C:/Program Files/Git/bin/sh.exe" ./exec-steps.sh --skip-docker-check "$@"

RESULT=$?

Expand Down

0 comments on commit 9d0c455

Please sign in to comment.