mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-18 03:49:50 -06:00
Don't rewrite state.yml if an update doesn't change it
lazygit writes state.yml on every startup to record the current version, although it's usually the same as the stored one. Switching to the repo that is already the most recent one rewrites it too. Every write is a chance for a crash to leave a broken file behind, and the follow-up work on update checks adds updates that often decide not to change anything. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
073b76c30c
commit
72ab657cc7
2 files changed
+32
-8
No files matched your search
+12
-8
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -47,7 +48,15 @@ func (c *AppConfig) UpdateAppState(update func(state *AppState)) error {
|
||||
// If the file can't be parsed, we start from the defaults and replace it.
|
||||
appState, _ := parseAppState(content)
|
||||
update(appState)
|
||||
return c.writeAppStateFile(appState)
|
||||
|
||||
newContent, err := yaml.Marshal(appState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bytes.Equal(newContent, content) {
|
||||
return nil
|
||||
}
|
||||
return c.writeAppStateFile(newContent)
|
||||
}
|
||||
|
||||
func (c *AppConfig) readAppStateFile() ([]byte, error) {
|
||||
@@ -73,17 +82,12 @@ func parseAppState(content []byte) (*AppState, error) {
|
||||
return appState, nil
|
||||
}
|
||||
|
||||
func (c *AppConfig) writeAppStateFile(appState *AppState) error {
|
||||
func (c *AppConfig) writeAppStateFile(content []byte) error {
|
||||
if c.appStatePath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := yaml.Marshal(appState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(c.appStatePath, content, 0o644)
|
||||
err := os.WriteFile(c.appStatePath, content, 0o644)
|
||||
if err != nil && os.IsPermission(err) {
|
||||
// apparently when people have read-only permissions they prefer us to fail silently
|
||||
return nil
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -79,3 +81,21 @@ func TestAppStateUpdateKeepsUpdatesOfOtherInstances(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{"from B", "from A"}, appState.ShellCommandsHistory)
|
||||
}
|
||||
|
||||
func TestAppStateUpdateSkipsWritingUnchangedState(t *testing.T) {
|
||||
configDir := useTempConfigDir(t)
|
||||
statePath := filepath.Join(configDir, stateFileName)
|
||||
appConfig, err := newAppConfig(t)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, appConfig.UpdateAppState(addToHistory("ls")))
|
||||
past := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
assert.NoError(t, os.Chtimes(statePath, past, past))
|
||||
|
||||
assert.NoError(t, appConfig.UpdateAppState(func(state *AppState) {
|
||||
state.ShellCommandsHistory = lo.Uniq(append([]string{"ls"}, state.ShellCommandsHistory...))
|
||||
}))
|
||||
|
||||
info, err := os.Stat(statePath)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, info.ModTime().Equal(past))
|
||||
}
|
||||
Reference in new issue
Block a user