Skip to content

Commit

Permalink
Auto-upgrade outdated push-verify config option (#2209)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Apr 6, 2023
1 parent e59f86c commit 643736a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 27 deletions.
36 changes: 36 additions & 0 deletions features/config/push_hook/upgrade.feature
@@ -0,0 +1,36 @@
Feature: automatically upgrade outdated configuration

Scenario Outline:
Given <LOCATION> 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 <COMMAND>"
Then it prints:
"""
I found the deprecated <LOCATION> setting "git-town.push-verify".
I am upgrading this setting to the new format "git-town.push-hook".
"""
And <LOCATION> setting "push-hook" is now "true"
And <LOCATION> 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 |
4 changes: 2 additions & 2 deletions features/config/push_new_branches/upgrade.feature
Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions src/config/core.go
Expand Up @@ -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
Expand Down
86 changes: 61 additions & 25 deletions src/config/git_town.go
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 643736a

Please sign in to comment.