diff --git a/features/config/push_hook/upgrade.feature b/features/config/push_hook/upgrade.feature new file mode 100644 index 00000000000..ddf295bef8f --- /dev/null +++ b/features/config/push_hook/upgrade.feature @@ -0,0 +1,36 @@ +Feature: automatically upgrade outdated configuration + + Scenario Outline: + Given setting "push-verify" is "true" + And the current branch is a feature branch "feature" + And tool "open" is installed + And the origin is "git@github.com:git-town/git-town.git" + When I run "git-town " + Then it prints: + """ + I found the deprecated setting "git-town.push-verify". + I am upgrading this setting to the new format "git-town.push-hook". + """ + And setting "push-hook" is now "true" + And setting "push-verify" no longer exists + + Examples: + | COMMAND | LOCATION | + | config | local | + | config | global | + | config push-hook | local | + | config push-hook | global | + | append foo | local | + | append foo | global | + | hack foo | local | + | hack foo | global | + | prepend foo | local | + | prepend foo | global | + | sync | local | + | sync | global | + | kill | local | + | kill | global | + | new-pull-request | local | + | new-pull-request | global | + | rename-branch bar | local | + | rename-branch bar | global | diff --git a/features/config/push_new_branches/upgrade.feature b/features/config/push_new_branches/upgrade.feature index b8e091df63c..026d62e29ab 100644 --- a/features/config/push_new_branches/upgrade.feature +++ b/features/config/push_new_branches/upgrade.feature @@ -14,12 +14,12 @@ Feature: automatically upgrade outdated configuration Examples: | COMMAND | LOCATION | - | append foo | local | - | append foo | global | | config | local | | config | global | | config push-new-branches | local | | config push-new-branches | global | + | append foo | local | + | append foo | global | | hack foo | local | | hack foo | global | | prepend foo | local | diff --git a/src/config/core.go b/src/config/core.go index 4f0eed3ee78..baa218f72fd 100644 --- a/src/config/core.go +++ b/src/config/core.go @@ -8,6 +8,7 @@ const ( CodeHostingDriverKey = "git-town.code-hosting-driver" CodeHostingOriginHostnameKey = "git-town.code-hosting-origin-hostname" DeprecatedNewBranchPushFlagKey = "git-town.new-branch-push-flag" + DeprecatedPushVerifyKey = "git-town.push-verify" GiteaTokenKey = "git-town.gitea-token" //nolint:gosec GithubTokenKey = "git-town.github-token" //nolint:gosec GitlabTokenKey = "git-town.gitlab-token" //nolint:gosec diff --git a/src/config/git_town.go b/src/config/git_town.go index 9927aaa221e..f9f6605ad64 100644 --- a/src/config/git_town.go +++ b/src/config/git_town.go @@ -89,6 +89,14 @@ func (gt *GitTown) DeprecatedNewBranchPushFlagLocal() string { return gt.localConfigCache[DeprecatedNewBranchPushFlagKey] } +func (gt *GitTown) DeprecatedPushVerifyFlagGlobal() string { + return gt.globalConfigCache[DeprecatedPushVerifyKey] +} + +func (gt *GitTown) DeprecatedPushVerifyFlagLocal() string { + return gt.localConfigCache[DeprecatedPushVerifyKey] +} + // GitAlias provides the currently set alias for the given Git Town command. func (gt *GitTown) GitAlias(aliasType AliasType) string { return gt.GlobalConfigValue("alias." + string(aliasType)) @@ -257,6 +265,10 @@ func (gt *GitTown) PullBranchStrategy() (PullBranchStrategy, error) { // PushHook provides the currently configured push-hook setting. func (gt *GitTown) PushHook() (bool, error) { + err := gt.updateDeprecatedSetting(DeprecatedPushVerifyKey, PushHookKey) + if err != nil { + return false, err + } setting := gt.LocalOrGlobalConfigValue(PushHookKey) if setting == "" { return true, nil @@ -270,6 +282,10 @@ func (gt *GitTown) PushHook() (bool, error) { // PushHook provides the currently configured push-hook setting. func (gt *GitTown) PushHookGlobal() (bool, error) { + err := gt.updateDeprecatedGlobalSetting(DeprecatedPushVerifyKey, PushHookKey) + if err != nil { + return false, err + } setting := gt.GlobalConfigValue(PushHookKey) if setting == "" { return true, nil @@ -426,31 +442,9 @@ func (gt *GitTown) SetTestOrigin(value string) error { // ShouldNewBranchPush indicates whether the current repository is configured to push // freshly created branches up to origin. func (gt *GitTown) ShouldNewBranchPush() (bool, error) { - oldLocalConfig := gt.DeprecatedNewBranchPushFlagLocal() - if oldLocalConfig != "" { - fmt.Printf("I found the deprecated local setting %q.\n", DeprecatedNewBranchPushFlagKey) - fmt.Printf("I am upgrading this setting to the new format %q.\n", PushNewBranchesKey) - err := gt.RemoveLocalConfigValue(DeprecatedNewBranchPushFlagKey) - if err != nil { - return false, err - } - _, err = gt.SetLocalConfigValue(PushNewBranchesKey, oldLocalConfig) - if err != nil { - return false, err - } - } - oldGlobalConfig := gt.DeprecatedNewBranchPushFlagGlobal() - if oldGlobalConfig != "" { - fmt.Printf("I found the deprecated global setting %q.\n", DeprecatedNewBranchPushFlagKey) - fmt.Printf("I am upgrading this setting to the new format %q.\n", PushNewBranchesKey) - _, err := gt.RemoveGlobalConfigValue(DeprecatedNewBranchPushFlagKey) - if err != nil { - return false, err - } - _, err = gt.SetGlobalConfigValue(PushNewBranchesKey, oldGlobalConfig) - if err != nil { - return false, err - } + err := gt.updateDeprecatedSetting(DeprecatedNewBranchPushFlagKey, PushNewBranchesKey) + if err != nil { + return false, err } config := gt.LocalOrGlobalConfigValue(PushNewBranchesKey) if config == "" { @@ -466,6 +460,10 @@ func (gt *GitTown) ShouldNewBranchPush() (bool, error) { // ShouldNewBranchPushGlobal indictes whether the global configuration requires to push // freshly created branches to origin. func (gt *GitTown) ShouldNewBranchPushGlobal() (bool, error) { + err := gt.updateDeprecatedGlobalSetting(DeprecatedNewBranchPushFlagKey, PushNewBranchesKey) + if err != nil { + return false, err + } config := gt.GlobalConfigValue(PushNewBranchesKey) if config == "" { return false, nil @@ -504,3 +502,41 @@ func (gt *GitTown) SyncStrategyGlobal() (SyncStrategy, error) { setting := gt.GlobalConfigValue(SyncStrategyKey) return ToSyncStrategy(setting) } + +func (gt *GitTown) updateDeprecatedSetting(deprecatedKey, newKey string) error { + err := gt.updateDeprecatedLocalSetting(deprecatedKey, newKey) + if err != nil { + return err + } + return gt.updateDeprecatedGlobalSetting(deprecatedKey, newKey) +} + +func (gt *GitTown) updateDeprecatedGlobalSetting(deprecatedKey, newKey string) error { + deprecatedSetting := gt.GlobalConfigValue(deprecatedKey) + if deprecatedSetting != "" { + fmt.Printf("I found the deprecated global setting %q.\n", deprecatedKey) + fmt.Printf("I am upgrading this setting to the new format %q.\n", newKey) + _, err := gt.RemoveGlobalConfigValue(deprecatedKey) + if err != nil { + return err + } + _, err = gt.SetGlobalConfigValue(newKey, deprecatedSetting) + return err + } + return nil +} + +func (gt *GitTown) updateDeprecatedLocalSetting(deprecatedKey, newKey string) error { + deprecatedSetting := gt.LocalConfigValue(deprecatedKey) + if deprecatedSetting != "" { + fmt.Printf("I found the deprecated local setting %q.\n", deprecatedKey) + fmt.Printf("I am upgrading this setting to the new format %q.\n", newKey) + err := gt.RemoveLocalConfigValue(deprecatedKey) + if err != nil { + return err + } + _, err = gt.SetLocalConfigValue(newKey, deprecatedSetting) + return err + } + return nil +}