-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Print migration changes to the console when migrating config file #4548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@ChrisMcD1 You have been around in this code a lot, feel like reviewing this? |
ca02207
to
2e4ba67
Compare
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferencesFootnotes
|
What do you think about printing the entire file contents of the new file? |
Hm, I don't know. My config file is pretty long, I don't think I would want it printed entirely. Especially if I then can't tell what changed. (And some people have copied the entire default config into their config file.) Also, most people running into the issue with read-only config files seem to be using home-manager, and from what I understand a copy of the entire config doesn't help them, because they need to enter it in home-manager's own format. (I might have gotten that wrong though.) At first I had toyed with the idea of printing a diff, and actually had that working, but I found that a context diff is less useful than I thought, because in longer sections you can't see which section you're in; so that's why I settled on the current approach of printing change instructions. |
} | ||
|
||
// Add more migrations here... | ||
|
||
if reflect.DeepEqual(rootNode, originalCopy) { | ||
return content, nil | ||
return nil, false, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are going to switch to returning nil
in all cases when we don't make changes, could we just use a check against changedContent == nill
instead?
No strong preference for that, I just thought of the idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, that's what I initially did. It has the theoretical problem that if we ever write a migrator that only removes a key without replacement, then you can't distinguish the case that nothing was done from the case that the config file only contained that key, and we removed it. (You might argue that the Yaml library will probably return an empty bytes slice instead of nil in that case, so we could still distinguish that, but I'm actually not sure if that's really the case, and I don't think we should rely on it.)
The extra bool is a bit clearer and safer.
pkg/config/app_config.go
Outdated
for pair := changes.Oldest(); pair != nil; pair = pair.Next() { | ||
changesText += fmt.Sprintf("- %s\n", pair.Key) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this library has support for iterators, so this could be more cleanly expressed as:
for change := range changes.KeysFromOldest() {
changesText += fmt.Sprintf("- %s\n", change)
}
https://github.com/wk8/go-ordered-map?tab=readme-ov-file#iterator-support-go--123
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's only available in master though, not in the latest release that we're using (which is from 2023). I didn't want to update to the master version.
It's becoming less of an issue now that we encapsulate this in the generics module, see below.
pkg/config/app_config.go
Outdated
@@ -220,7 +221,9 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig, isGuiInitialize | |||
// from one container to another, or changing the type of a key (e.g. from bool | |||
// to an enum). | |||
func migrateUserConfig(path string, content []byte, isGuiInitialized bool) ([]byte, error) { | |||
changedContent, didChange, err := computeMigratedConfig(path, content) | |||
changes := orderedmap.New[string, bool]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do a bool
instead of an struct{}
? I don't know my idiomatic go that well, but I thought struct{}
was how to say "I want a set, but that doesn't exist in go, so my values are empty"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question; I simply modelled this after generics/set.Set without thinking much about it. I suppose in that case it was done with a bool because it makes checking for containment slightly easier (return set.m[value]
rather than _, found := set.m[value]; return found
).
Anyway, this discussion shows that we shouldn't use a map in client code, but provide a proper OrderedSet abstraction instead; I was just too lazy to do that initially. See jesseduffield/generics#2 (still using a bool there, simply for consistency with set, but it's now an implementation detail and doesn't matter), and 0138b9e.
2e4ba67
to
0138b9e
Compare
0138b9e
to
b317b9e
Compare
It looks like the discussion has stalled. @ChrisMcD1, I think your concerns above have all been addressed, is that true? Any further thoughts from your side? @jesseduffield Are you interested in reviewing this too? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, looks good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
It's a bit silly to find out by string comparison whether computeMigratedConfig did something, when it knows this already and can just return the information. This doesn't make a huge difference to the production code; the string comparison isn't very expensive, so this isn't a big deal. However, it makes the tests clearer; we don't have to bother specifying an expected output string if the didChange flag is false, and in particular we can get rid of the ugly "This test intentionally uses non-standard indentation" bit in one of the tests.
Most migrations happen at startup when loading the global config file, at a time where the GUI hasn't been initialized yet. We can safely print to the console at that point. However, it is also possible that repo-local config files need to be migrated, and this happens when the GUI has already started, at which point we had better not print anything to stdout; this totally messes up the UI. In this commit we simply suppress the logging when the GUI is running already. This is probably good enough, because the logging is mostly useful in the case that writing back the migrated config file fails, so that users understand better why lazygit doesn't start up; and this is very unlikely to happen for repo-local config files, because why would users make them read-only.
This might be useful to see in general (users will normally only see it after they quit lazygit again, but still). But it is especially useful when writing back the config file fails for some reason, because users can then make these changes manually if they want. We do this only at startup, when the GUI hasn't started yet. This is probably good enough, because it is much less likely that writing back a migrated repo-local config fails because it is not writeable.
This is for the unlikely case that a repo-local config file can't be written back after migration; in this case we can't log the migration changes to the console, so include them in the error popup instead.
b317b9e
to
cabcd54
Compare
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.50.0` -> `v0.51.0` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>jesseduffield/lazygit (jesseduffield/lazygit)</summary> ### [`v0.51.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.51.0) [Compare Source](jesseduffield/lazygit@v0.50.0...v0.51.0) <!-- Release notes generated using configuration in .github/release.yml at v0.51.0 --> #### What's Changed ##### Enhancements 🔥 - Clean up the configuration of where a custom command's output goes by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4525 - Add custom patch command "Move patch into new commit before the original commit" by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4552 - Make '>' first jump to the beginning of the branch, and only then to the first commit by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4544 - Add an alternate keybinding (default <c-s>) for ConfirmInEditor by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4532 - Print migration changes to the console when migrating config file by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4548 ##### Fixes 🔧 - Migrate deprecated AllBranchesLogCmd to AllBranchesLogCmds by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4345 - Clear preserved commit message when entering CommitEditorPanel by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4558 - Split behavior of rendering allBranchesLogCmd and switching to next cmd by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4574 - Fix possible crash with auto-forwarding branches by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4565 - Fix main view occasionally scrolling to the top on its own when focused by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4573 - Fix home and end keys in prompts by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4554 - Fix crash when clicking in the status view by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4567 ##### Maintenance ⚙️ - Clean up utils package by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4538 ##### Docs 📖 - reword documentation for git.autoForwardBranches by [@​sean-xyz](https://github.com/sean-xyz) in jesseduffield/lazygit#4545 #### New Contributors - [@​sean-xyz](https://github.com/sean-xyz) made their first contribution in jesseduffield/lazygit#4545 **Full Changelog**: jesseduffield/lazygit@v0.50.0...v0.51.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4yMi4wIiwidXBkYXRlZEluVmVyIjoiNDAuMjMuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
This might be useful to see in general (users will normally only see it after they quit lazygit again, but still). But it is especially useful when writing back the config file fails; some users have their config file in a read-only location, so we had reports of lazygit no longer starting up when migration was necessary. #4210 was supposed to improve this a bit, but it didn't tell users what changes need to be made to the config file. Now we tell them, and users can then make these changes manually if they want.
We do this only at startup, when the GUI hasn't started yet. This is probably good enough, because it is much less likely that writing back a migrated repo-local config fails because it is not writeable.
Example output:
The branch also contains a lot of code cleanups.
go generate ./...
)