Skip to content

Commit

Permalink
Show a confirmation when changing a config that can't be auto-reloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Aug 6, 2024
1 parent 23b5fff commit 2500dfb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -331,13 +332,18 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context

gui.g.SetFocusHandler(func(Focused bool) error {
if Focused {
oldConfig := gui.Config.GetUserConfig()
reloadErr, didChange := gui.Config.ReloadChangedUserConfigFiles()
if didChange && reloadErr == nil {
gui.c.Log.Info("User config changed - reloading")
reloadErr = gui.onUserConfigLoaded()
if err := gui.resetKeybindings(); err != nil {
return err
}

if err := gui.checkForChangedConfigsThatDontAutoReload(oldConfig, gui.Config.GetUserConfig()); err != nil {
return err
}
}

gui.c.Log.Info("Receiving focus - refreshing")
Expand Down Expand Up @@ -434,6 +440,54 @@ func (gui *Gui) onUserConfigLoaded() error {
return nil
}

func (gui *Gui) checkForChangedConfigsThatDontAutoReload(oldConfig *config.UserConfig, newConfig *config.UserConfig) error {
configsThatDontAutoReload := []string{
"Git.AutoFetch",
"Git.AutoRefresh",
"Refresher.RefreshInterval",
"Refresher.FetchInterval",
"Update.Method",
"Update.Days",
}

changedConfigs := []string{}
for _, config := range configsThatDontAutoReload {
old := reflect.ValueOf(oldConfig).Elem()
new := reflect.ValueOf(newConfig).Elem()
fieldNames := strings.Split(config, ".")
// navigate to the leaves in old and new config
for _, fieldName := range fieldNames {
old = old.FieldByName(fieldName)
new = new.FieldByName(fieldName)
}
// if the value has changed, ...
if !old.Equal(new) {
// ... convert the field names to the user-facing names by
// lower-casing the first letter, ...
userFacingName := strings.Join(lo.Map(fieldNames, func(f string, _ int) string {
return strings.ToLower(f[:1]) + f[1:]
}), ".")
// ... and append it to the list of changed configs
changedConfigs = append(changedConfigs, userFacingName)
}
}

if len(changedConfigs) == 0 {
return nil
}

message := utils.ResolvePlaceholderString(
gui.c.Tr.NonReloadableConfigWarning,
map[string]string{
"configs": strings.Join(changedConfigs, "\n"),
},
)
return gui.c.Confirm(types.ConfirmOpts{
Title: gui.c.Tr.NonReloadableConfigWarningTitle,
Prompt: message,
})
}

// resetState reuses the repo state from our repo state map, if the repo was
// open before; otherwise it creates a new one.
func (gui *Gui) resetState(startArgs appTypes.StartArgs) types.Context {
Expand Down
8 changes: 8 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ type TranslationSet struct {
MergeToolPrompt string
IntroPopupMessage string
DeprecatedEditConfigWarning string
NonReloadableConfigWarningTitle string
NonReloadableConfigWarning string
GitconfigParseErr string
EditFile string
EditFileTooltip string
Expand Down Expand Up @@ -985,6 +987,10 @@ for up-to-date information how to configure your editor.
`

const englishNonReloadableConfigWarning = `The following config settings were changed, but the change doesn't take effect immediately. Please quit and restart lazygit for changes to take effect:
{{configs}}`

// exporting this so we can use it in tests
func EnglishTranslationSet() *TranslationSet {
return &TranslationSet{
Expand Down Expand Up @@ -1199,6 +1205,8 @@ func EnglishTranslationSet() *TranslationSet {
MergeToolPrompt: "Are you sure you want to open `git mergetool`?",
IntroPopupMessage: englishIntroPopupMessage,
DeprecatedEditConfigWarning: englishDeprecatedEditConfigWarning,
NonReloadableConfigWarningTitle: "Config changed",
NonReloadableConfigWarning: englishNonReloadableConfigWarning,
GitconfigParseErr: `Gogit failed to parse your gitconfig file due to the presence of unquoted '\' characters. Removing these should fix the issue.`,
EditFile: `Edit file`,
EditFileTooltip: "Open file in external editor.",
Expand Down

0 comments on commit 2500dfb

Please sign in to comment.