1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-29 23:17:32 +02:00

Avoid rewriting the file if nothing changed

This avoids changing the indentation or number of blank lines etc unnecessarily
if nothing has changed.
This commit is contained in:
Stefan Haller 2023-06-24 13:26:51 +02:00
parent 85f293af1a
commit c4a2749a99
2 changed files with 32 additions and 13 deletions

View File

@ -29,7 +29,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
return yamlBytes, errors.New("yaml document is not a dictionary") return yamlBytes, errors.New("yaml document is not a dictionary")
} }
if err := updateYamlNode(body, path, value); err != nil { if didChange, err := updateYamlNode(body, path, value); err != nil || !didChange {
return yamlBytes, err return yamlBytes, err
} }
@ -43,17 +43,20 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
} }
// Recursive function to update the YAML node. // Recursive function to update the YAML node.
func updateYamlNode(node *yaml.Node, path []string, value string) error { func updateYamlNode(node *yaml.Node, path []string, value string) (bool, error) {
if len(path) == 0 { if len(path) == 0 {
if node.Kind != yaml.ScalarNode { if node.Kind != yaml.ScalarNode {
return errors.New("yaml node is not a scalar") return false, errors.New("yaml node is not a scalar")
} }
node.Value = value if node.Value != value {
return nil node.Value = value
return true, nil
}
return false, nil
} }
if node.Kind != yaml.MappingNode { if node.Kind != yaml.MappingNode {
return errors.New("yaml node in path is not a dictionary") return false, errors.New("yaml node in path is not a dictionary")
} }
key := path[0] key := path[0]
@ -72,7 +75,7 @@ func updateYamlNode(node *yaml.Node, path []string, value string) error {
Kind: yaml.ScalarNode, Kind: yaml.ScalarNode,
Value: value, Value: value,
}) })
return nil return true, nil
} }
// otherwise, create the missing intermediate node and continue // otherwise, create the missing intermediate node and continue
@ -113,7 +116,7 @@ func RenameYamlKey(yamlBytes []byte, path []string, newKey string) ([]byte, erro
body := node.Content[0] body := node.Content[0]
if err := renameYamlKey(body, path, newKey); err != nil { if didRename, err := renameYamlKey(body, path, newKey); err != nil || !didRename {
return yamlBytes, err return yamlBytes, err
} }
@ -127,25 +130,25 @@ func RenameYamlKey(yamlBytes []byte, path []string, newKey string) ([]byte, erro
} }
// Recursive function to rename the YAML key. // Recursive function to rename the YAML key.
func renameYamlKey(node *yaml.Node, path []string, newKey string) error { func renameYamlKey(node *yaml.Node, path []string, newKey string) (bool, error) {
if node.Kind != yaml.MappingNode { if node.Kind != yaml.MappingNode {
return errors.New("yaml node in path is not a dictionary") return false, errors.New("yaml node in path is not a dictionary")
} }
keyNode, valueNode := lookupKey(node, path[0]) keyNode, valueNode := lookupKey(node, path[0])
if keyNode == nil { if keyNode == nil {
return nil return false, nil
} }
// end of path reached: rename key // end of path reached: rename key
if len(path) == 1 { if len(path) == 1 {
// Check that new key doesn't exist yet // Check that new key doesn't exist yet
if newKeyNode, _ := lookupKey(node, newKey); newKeyNode != nil { if newKeyNode, _ := lookupKey(node, newKey); newKeyNode != nil {
return fmt.Errorf("new key `%s' already exists", newKey) return false, fmt.Errorf("new key `%s' already exists", newKey)
} }
keyNode.Value = newKey keyNode.Value = newKey
return nil return true, nil
} }
return renameYamlKey(valueNode, path[1:], newKey) return renameYamlKey(valueNode, path[1:], newKey)

View File

@ -64,6 +64,14 @@ func TestUpdateYamlValue(t *testing.T) {
expectedOut: "foo:\n bar:\n baz: qux\n", expectedOut: "foo:\n bar:\n baz: qux\n",
expectedErr: "", expectedErr: "",
}, },
{
name: "don't rewrite file if value didn't change",
in: "foo:\n bar: baz\n",
path: []string{"foo", "bar"},
value: "baz",
expectedOut: "foo:\n bar: baz\n",
expectedErr: "",
},
// Error cases // Error cases
{ {
@ -142,6 +150,14 @@ func TestRenameYamlKey(t *testing.T) {
expectedOut: "qux:\n bar: 5\n", expectedOut: "qux:\n bar: 5\n",
expectedErr: "", expectedErr: "",
}, },
{
name: "don't rewrite file if value didn't change",
in: "foo:\n bar: 5\n",
path: []string{"nonExistingKey"},
newKey: "qux",
expectedOut: "foo:\n bar: 5\n",
expectedErr: "",
},
// Error cases // Error cases
{ {