Skip to content

Commit

Permalink
Fix install pre-release versions (#66)
Browse files Browse the repository at this point in the history
- Pre-release options in `git hooks update` command.
- Pre-release is only discovered when `githooks.autoUpdateUsePrerelease = true`
  is used during runner execution.
- Tests.
  • Loading branch information
gabyx authored Dec 6, 2021
1 parent 16a440b commit 680cdc5
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/cli/git_hooks_installer.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ git hooks installer [flags]
--build-from-source If the binaries are built from source instead of
downloaded from the deploy url.
--build-tags stringArray Build tags for building from source (get extended with defaults).
--use-pre-release When fetching the latest installer, also consider pre-release versions.
-h, --help help for installer
```

Expand Down
14 changes: 7 additions & 7 deletions docs/cli/git_hooks_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Performs an update check.

### Synopsis


Executes an update check for a newer Githooks version.

If it finds one and the user accepts the prompt (or `--yes` is used)
Expand All @@ -21,12 +20,13 @@ git hooks update
### Options

```
--yes Always accepts a new update (non-interactive, only non-major versions).
--no Always deny an update and only check for it.
--yes-all Always accepts a new update (non-interactive, all versions).
--enable Enable daily Githooks update checks.
--disable Disable daily Githooks update checks.
-h, --help help for update
--yes Always accepts a new update (non-interactive, only non-major versions).
--no Always deny an update and only check for it.
--yes-all Always accepts a new update (non-interactive, all versions).
--use-pre-release Also discover pre-release versions when updating.
--enable Enable daily Githooks update checks.
--disable Disable daily Githooks update checks.
-h, --help help for update
```

### SEE ALSO
Expand Down
7 changes: 7 additions & 0 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,16 @@ func updateGithooks(settings *HookSettings, uiSettings *UISettings) {
opts = append(opts, "--non-interactive")
}

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

updateAvailable, accepted, err := updates.RunUpdate(
settings.InstallDir,
updates.DefaultAcceptUpdateCallback(log, uiSettings.PromptCtx, updates.AcceptNonInteractiveNone),
usePreRelease,
func() error {
return updates.RunUpdateOverExecutable(settings.InstallDir,
&settings.ExecX,
Expand Down
2 changes: 2 additions & 0 deletions githooks/cmd/installer/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ type Arguments struct {
BuildFromSource bool // If we build the install/update from source.
BuildTags []string // Go build tags.

UsePreRelease bool // If also pre-release versions should be considered.

UseStdin bool
}
12 changes: 8 additions & 4 deletions githooks/cmd/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) {
"build-tags", nil,
"Build tags for building from source (get extended with defaults).")

cmd.PersistentFlags().Bool(
"use-pre-release", false,
"When fetching the latest installer, also consider pre-release versions.")

cm.AssertNoErrorPanic(
vi.BindPFlag("config", cmd.PersistentFlags().Lookup("config")))
cm.AssertNoErrorPanic(
Expand Down Expand Up @@ -161,6 +165,8 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) {
vi.BindPFlag("buildFromSource", cmd.PersistentFlags().Lookup("build-from-source")))
cm.AssertNoErrorPanic(
vi.BindPFlag("buildTags", cmd.PersistentFlags().Lookup("build-tags")))
cm.AssertNoErrorPanic(
vi.BindPFlag("usePreRelease", cmd.PersistentFlags().Lookup("use-pre-release")))
cm.AssertNoErrorPanic(
vi.BindPFlag("installPrefix", cmd.PersistentFlags().Lookup("prefix")))
cm.AssertNoErrorPanic(
Expand Down Expand Up @@ -358,15 +364,13 @@ func prepareDispatch(
args *Arguments,
cleanUpX *cm.InterruptContext) (bool, error) {

skipPrerelease := !(gitx.GetConfig(hooks.GitCKAutoUpdateUsePrerelease, git.GlobalScope) == git.GitCVTrue)

var status updates.ReleaseStatus
var err error

if args.InternalAutoUpdate {
log.Info("Executing auto update...")

status, err = updates.GetStatus(settings.CloneDir, true, skipPrerelease)
status, err = updates.GetStatus(settings.CloneDir, true, args.UsePreRelease)
log.AssertNoErrorPanic(err,
"Could not get status of release clone '%s'",
settings.CloneDir)
Expand All @@ -380,7 +384,7 @@ func prepareDispatch(
build.BuildTag,
true,
updates.RecloneOnWrongRemote,
skipPrerelease)
args.UsePreRelease)

log.AssertNoErrorPanicF(err,
"Could not assert release clone '%s' existing",
Expand Down
20 changes: 14 additions & 6 deletions githooks/cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func runUpdate(
ctx *ccm.CmdContext,
setOpts *config.SetOptions,
nonInteractive bool,
nonInteractiveAccept updates.AcceptNonInteractiveMode) {
nonInteractiveAccept updates.AcceptNonInteractiveMode,
usePreRelease bool) {

switch {
case setOpts.Set || setOpts.Unset:
Expand All @@ -31,9 +32,15 @@ func runUpdate(
updateAvailable, accepted, err := updates.RunUpdate(
ctx.InstallDir,
updates.DefaultAcceptUpdateCallback(ctx.Log, promptx, nonInteractiveAccept),
usePreRelease,
func() error {

installer := installer.NewCmd(ctx)
installer.SetArgs([]string{})
args := []string{} // should not be empty, because of SetArgs
if usePreRelease {
args = append(args, "--use-pre-release")
}
installer.SetArgs(args)

return installer.Execute()
})
Expand All @@ -60,14 +67,14 @@ func NewCmd(ctx *ccm.CmdContext) *cobra.Command {
yes := false
no := false
yesMajor := false
usePreRelease := false

setOpts := config.SetOptions{}

updateCmd := &cobra.Command{
Use: "update",
Short: "Performs an update check.",
Long: `
Executes an update check for a newer Githooks version.
Long: `Executes an update check for a newer Githooks version.
If it finds one and the user accepts the prompt (or '--yes' is used)
the installer is executed to update to the latest version.
Expand All @@ -94,7 +101,7 @@ after a successful commit event.`,
nonInteractiveAccept = updates.AcceptNonInteractiveAll
}

runUpdate(ctx, &setOpts, nonInteractive, nonInteractiveAccept)
runUpdate(ctx, &setOpts, nonInteractive, nonInteractiveAccept, usePreRelease)
},
}

Expand All @@ -104,7 +111,8 @@ after a successful commit event.`,
"Always deny an update and only check for it.")
updateCmd.Flags().BoolVar(&yesMajor, "yes-all", false,
"Always accepts a new update (non-interactive, all versions).")

updateCmd.Flags().BoolVar(&usePreRelease, "use-pre-release", false,
"Also discover pre-release versions when updating.")
updateCmd.Flags().BoolVar(&setOpts.Set, "enable", false, "Enable daily Githooks update checks.")
updateCmd.Flags().BoolVar(&setOpts.Unset, "disable", false, "Disable daily Githooks update checks.")

Expand Down
16 changes: 8 additions & 8 deletions githooks/updates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func getNewUpdateCommit(
gitx *git.Context,
firstSHA string,
lastSHA string,
skipPrerelease bool) (commitF string, tagF string, versionF *version.Version, infoF []string, err error) {
usePreRelease bool) (commitF string, tagF string, versionF *version.Version, infoF []string, err error) {

// Get all commits in (firstSHA, lastSHA]
commits, err := gitx.GetCommits(firstSHA, lastSHA)
Expand All @@ -98,7 +98,7 @@ func getNewUpdateCommit(
return
case version == nil || strs.IsEmpty(tag):
continue // no version tag on this commit
case skipPrerelease && strs.IsNotEmpty(version.Prerelease()):
case !usePreRelease && strs.IsNotEmpty(version.Prerelease()):
// Skipping prerelease version
continue
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func FetchUpdates(
tag string,
checkRemote bool,
checkRemoteAction RemoteCheckAction,
skipPrerelease bool) (status ReleaseStatus, err error) {
usePreRelease bool) (status ReleaseStatus, err error) {

cm.AssertOrPanic(strs.IsNotEmpty(cloneDir))

Expand Down Expand Up @@ -276,7 +276,7 @@ func FetchUpdates(

resetRemoteTo := ""
remoteBranch := DefaultRemote + "/" + branch
status, err = getStatus(gitx, url, DefaultRemote, branch, remoteBranch, skipPrerelease)
status, err = getStatus(gitx, url, DefaultRemote, branch, remoteBranch, usePreRelease)

status.IsNewClone = isNewClone
if status.IsUpdateAvailable {
Expand Down Expand Up @@ -343,7 +343,7 @@ func getStatus(
remoteName string,
branch string,
remoteBranch string,
skipPrerelease bool) (status ReleaseStatus, err error) {
usePreRelease bool) (status ReleaseStatus, err error) {

localSHA, err := gitx.Get("rev-parse", branch)
if err != nil {
Expand All @@ -370,7 +370,7 @@ func getStatus(
// - Skip prerelease versions
// - also never skip annotated (Git trailers) "non-skip" versions.
updateCommit, updateTag, updateVersion, updateInfo, err =
getNewUpdateCommit(gitx, localSHA, remoteSHA, skipPrerelease)
getNewUpdateCommit(gitx, localSHA, remoteSHA, usePreRelease)

if err != nil {
return
Expand Down Expand Up @@ -471,6 +471,7 @@ type AcceptUpdateCallback func(status *ReleaseStatus) bool
func RunUpdate(
installDir string,
acceptUpdate AcceptUpdateCallback,
usePreRelease bool,
run func() error) (updateAvailable bool, accepted bool, err error) {

err = RecordUpdateCheckTimestamp()
Expand All @@ -482,8 +483,7 @@ func RunUpdate(
}

cloneDir := hooks.GetReleaseCloneDir(installDir)
skipPrerelease := true
status, err := FetchUpdates(cloneDir, "", "", build.BuildTag, true, ErrorOnWrongRemote, skipPrerelease)
status, err := FetchUpdates(cloneDir, "", "", build.BuildTag, true, ErrorOnWrongRemote, usePreRelease)
if err != nil {
err = cm.CombineErrors(cm.Error("Could not fetch updates."), err)

Expand Down
8 changes: 8 additions & 0 deletions tests/setup-githooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,13 @@ echo "Commit build v10.1.1 to repo ..." &&
-m "Update-Info: Breaking changes, read the change log." >/dev/null 2>&1 &&
git tag -f "v10.1.1" || exit 1

# Commit for to v10.1.2-rc1 (build not used, only for pre-release).
#################################
echo "Commit build v10.1.1 to repo ..." &&
cd "$GH_TEST_REPO" &&
git commit -a --allow-empty -m "Version v10.1.2-rc1" \
-m "Update-Info: Release candidate." >/dev/null 2>&1 &&
git tag -f "v10.1.2-rc1" || exit 1

echo "Reset main to 9.9.0 ..." &&
git reset --hard v9.9.0 || exit 1
43 changes: 41 additions & 2 deletions tests/step-063.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if ! echo "$out" | grep -q -E "Would you like to install.*[y/N]"; then
exit 1
fi

# Update to version 10.1.1
# Update to version 10.1.1 (its a major update which should be declined)
echo "Try update to 10.1.1 (2)"
CURRENT="$AFTER"
out=$(EXECUTE_UPDATE="" "$GH_INSTALL_BIN_DIR/cli" update --yes 2>&1) || {
Expand All @@ -81,7 +81,7 @@ if [ "$CURRENT" != "$AFTER" ] ||
exit 1
fi

# Try again, but now force it.
# Try again, but now force the major update.
echo "Force update to 10.1.1"
CURRENT="$AFTER"
out=$("$GH_INSTALL_BIN_DIR/cli" update --yes-all 2>&1) || {
Expand All @@ -105,3 +105,42 @@ if ! echo "$out" | grep -q "Update Info:" ||
echo "! Expected update info to be present in output."
exit 1
fi

echo "Update to pre-release 10.1.2-rc1"
# Reset to trigger update
if ! git -C "$GH_TEST_REPO" reset --hard v10.1.2-rc1 >/dev/null; then
echo "! Could not reset server to trigger update."
exit 1
fi

echo "Try update to 10.1.2-rc1"
CURRENT="$AFTER"
out=$(EXECUTE_UPDATE="" "$GH_INSTALL_BIN_DIR/cli" update --yes-all 2>&1) || {
echo "$out"
echo "! Failed to run update"
exit 1
}
AFTER="$(git -C ~/.githooks/release rev-parse HEAD)"

if [ "$CURRENT" != "$AFTER" ] ||
[ "$(git -C "$GH_TEST_REPO" rev-parse v10.1.1)" != "$AFTER" ]; then
echo "$out"
echo "! Release clone was updated, but it should not have!"
exit 1
fi

echo "Force update to 10.1.2-rc1"
CURRENT="$AFTER"
out=$(EXECUTE_UPDATE="" "$GH_INSTALL_BIN_DIR/cli" update --yes-all --use-pre-release 2>&1) || {
echo "$out"
echo "! Failed to run update"
exit 1
}
AFTER="$(git -C ~/.githooks/release rev-parse HEAD)"

if [ "$CURRENT" = "$AFTER" ] ||
[ "$(git -C "$GH_TEST_REPO" rev-parse v10.1.2-rc1)" != "$AFTER" ]; then
echo "$out"
echo "! Release clone was not updated, but it should have!"
exit 1
fi

0 comments on commit 680cdc5

Please sign in to comment.