Skip to content

Commit 53440ae

Browse files
author
Chris McDonnell
committed
Migrate deprecated AllBranchesLogCmd to AllBranchesLogCmds
1 parent 4e3ead5 commit 53440ae

File tree

8 files changed

+150
-25
lines changed

8 files changed

+150
-25
lines changed

docs/Config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ git:
335335
# Command used when displaying the current branch git log in the main window
336336
branchLogCmd: git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --
337337

338-
# Command used to display git log of all branches in the main window.
339-
# Deprecated: Use `allBranchesLogCmds` instead.
340-
allBranchesLogCmd: git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium
338+
# Commands used to display git log of all branches in the main window, they will be cycled in order of appearance
339+
allBranchesLogCmds:
340+
- git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium
341341

342342
# If true, do not spawn a separate process when using GPG
343343
overrideGpg: false

pkg/commands/git_commands/branch.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,7 @@ func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error {
248248

249249
func (self *BranchCommands) AllBranchesLogCmdObj() oscommands.ICmdObj {
250250
// Only choose between non-empty, non-identical commands
251-
candidates := lo.Uniq(lo.WithoutEmpty(append([]string{
252-
self.UserConfig().Git.AllBranchesLogCmd,
253-
},
254-
self.UserConfig().Git.AllBranchesLogCmds...,
255-
)))
251+
candidates := lo.Uniq(lo.WithoutEmpty(self.UserConfig().Git.AllBranchesLogCmds))
256252

257253
n := len(candidates)
258254

pkg/config/app_config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ func computeMigratedConfig(path string, content []byte) ([]byte, error) {
281281
return nil, fmt.Errorf("Couldn't migrate config file at `%s`: %s", path, err)
282282
}
283283

284+
err = migrateAllBranchesLogCmd(&rootNode)
285+
if err != nil {
286+
return nil, fmt.Errorf("Couldn't migrate config file at `%s`: %s", path, err)
287+
}
288+
284289
// Add more migrations here...
285290

286291
if !reflect.DeepEqual(rootNode, originalCopy) {
@@ -341,6 +346,44 @@ func changeCommitPrefixesMap(rootNode *yaml.Node) error {
341346
})
342347
}
343348

349+
// This migration is special because users have already defined
350+
// a single elemnt at `allBranchesLogCmd` and the sequence at `allBranchesLogCmds`.
351+
// Some users have explicitly set `allBranchesLogCmd` to be an empty string in order
352+
// to remove it, so in that case we just delete the element, and add nothing to the list
353+
func migrateAllBranchesLogCmd(rootNode *yaml.Node) error {
354+
return yaml_utils.TransformNode(rootNode, []string{"git"}, func(gitNode *yaml.Node) error {
355+
cmdKeyNode, cmdValueNode := yaml_utils.LookupKey(gitNode, "allBranchesLogCmd")
356+
// Nothing to do if they do not have the deprecated item
357+
if cmdKeyNode == nil {
358+
return nil
359+
}
360+
361+
cmdsKeyNode, cmdsValueNode := yaml_utils.LookupKey(gitNode, "allBranchesLogCmds")
362+
if cmdsKeyNode == nil {
363+
// Create dummy node and attach it onto the root git node
364+
cmdsKeyNode = &yaml.Node{Kind: yaml.ScalarNode, Value: "allBranchesLogCmds"}
365+
cmdsValueNode = &yaml.Node{Kind: yaml.SequenceNode, Content: []*yaml.Node{}}
366+
gitNode.Content = append(gitNode.Content,
367+
cmdsKeyNode,
368+
cmdsValueNode,
369+
)
370+
}
371+
372+
if cmdValueNode.Value != "" {
373+
if cmdsValueNode.Kind != yaml.SequenceNode {
374+
return fmt.Errorf("You should have an allBranchesLogCmds defined as a sequence!")
375+
}
376+
// Prepending the individual element to make it show up first in the list, which was prior behavior
377+
cmdsValueNode.Content = append([]*yaml.Node{{Kind: yaml.ScalarNode, Value: cmdValueNode.Value}}, cmdsValueNode.Content...)
378+
}
379+
380+
// Clear out the existing allBranchesLogCmd, now that we have migrated it into the list
381+
_, _ = yaml_utils.RemoveKey(gitNode, "allBranchesLogCmd")
382+
383+
return nil
384+
})
385+
}
386+
344387
func (c *AppConfig) GetDebug() bool {
345388
return c.debug
346389
}

pkg/config/app_config_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,82 @@ git:
8787
})
8888
}
8989
}
90+
91+
func TestAllBranchesLogCmdMigrations(t *testing.T) {
92+
scenarios := []struct {
93+
name string
94+
input string
95+
expected string
96+
}{
97+
{
98+
name: "Incomplete Configuration Passes uneventfully",
99+
input: "git:",
100+
expected: "git:",
101+
}, {
102+
name: "Single Cmd with no Cmds",
103+
input: `git:
104+
allBranchesLogCmd: git log --graph --oneline
105+
`,
106+
expected: `git:
107+
allBranchesLogCmds:
108+
- git log --graph --oneline
109+
`,
110+
}, {
111+
name: "Cmd with one existing Cmds",
112+
input: `git:
113+
allBranchesLogCmd: git log --graph --oneline
114+
allBranchesLogCmds:
115+
- git log --graph --oneline --pretty
116+
`,
117+
expected: `git:
118+
allBranchesLogCmds:
119+
- git log --graph --oneline
120+
- git log --graph --oneline --pretty
121+
`,
122+
}, {
123+
name: "Only Cmds set have no changes",
124+
input: `git:
125+
allBranchesLogCmds:
126+
- git log
127+
`,
128+
expected: `git:
129+
allBranchesLogCmds:
130+
- git log
131+
`,
132+
}, {
133+
name: "Removes Empty Cmd When at end of yaml",
134+
input: `git:
135+
allBranchesLogCmds:
136+
- git log --graph --oneline
137+
allBranchesLogCmd:
138+
`,
139+
expected: `git:
140+
allBranchesLogCmds:
141+
- git log --graph --oneline
142+
`,
143+
}, {
144+
name: "Removes Empty Cmd With Keys Afterwards",
145+
input: `git:
146+
allBranchesLogCmds:
147+
- git log --graph --oneline
148+
allBranchesLogCmd:
149+
foo: bar
150+
`,
151+
expected: `git:
152+
allBranchesLogCmds:
153+
- git log --graph --oneline
154+
foo: bar
155+
`,
156+
},
157+
}
158+
159+
for _, s := range scenarios {
160+
t.Run(s.name, func(t *testing.T) {
161+
actual, err := computeMigratedConfig("path doesn't matter", []byte(s.input))
162+
if err != nil {
163+
t.Error(err)
164+
}
165+
assert.Equal(t, s.expected, string(actual))
166+
})
167+
}
168+
}

pkg/config/user_config.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,6 @@ type GitConfig struct {
249249
AutoStageResolvedConflicts bool `yaml:"autoStageResolvedConflicts"`
250250
// Command used when displaying the current branch git log in the main window
251251
BranchLogCmd string `yaml:"branchLogCmd"`
252-
// Command used to display git log of all branches in the main window.
253-
// Deprecated: Use `allBranchesLogCmds` instead.
254-
AllBranchesLogCmd string `yaml:"allBranchesLogCmd"`
255252
// Commands used to display git log of all branches in the main window, they will be cycled in order of appearance
256253
AllBranchesLogCmds []string `yaml:"allBranchesLogCmds"`
257254
// If true, do not spawn a separate process when using GPG
@@ -786,7 +783,7 @@ func GetDefaultConfig() *UserConfig {
786783
FetchAll: true,
787784
AutoStageResolvedConflicts: true,
788785
BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --",
789-
AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium",
786+
AllBranchesLogCmds: []string{"git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium"},
790787
DisableForcePushing: false,
791788
CommitPrefixes: map[string][]CommitPrefixConfig(nil),
792789
BranchPrefix: "",

pkg/integration/tests/status/log_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ var LogCmd = NewIntegrationTest(NewIntegrationTestArgs{
1010
ExtraCmdArgs: []string{},
1111
Skip: false,
1212
SetupConfig: func(config *config.AppConfig) {
13-
config.GetUserConfig().Git.AllBranchesLogCmd = `echo "view1"`
14-
config.GetUserConfig().Git.AllBranchesLogCmds = []string{`echo "view2"`}
13+
config.GetUserConfig().Git.AllBranchesLogCmds = []string{`echo "view1"`, `echo "view2"`}
1514
},
1615
SetupRepo: func(shell *Shell) {},
1716
Run: func(t *TestDriver, keys config.KeybindingConfig) {

pkg/utils/yaml_utils/yaml_utils.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func updateYamlNode(node *yaml.Node, path []string, value string) (bool, error)
6161
}
6262

6363
key := path[0]
64-
if _, valueNode := lookupKey(node, key); valueNode != nil {
64+
if _, valueNode := LookupKey(node, key); valueNode != nil {
6565
return updateYamlNode(valueNode, path[1:], value)
6666
}
6767

@@ -90,7 +90,7 @@ func updateYamlNode(node *yaml.Node, path []string, value string) (bool, error)
9090
return updateYamlNode(newNode, path[1:], value)
9191
}
9292

93-
func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
93+
func LookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
9494
for i := 0; i < len(node.Content)-1; i += 2 {
9595
if node.Content[i].Value == key {
9696
return node.Content[i], node.Content[i+1]
@@ -100,6 +100,19 @@ func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
100100
return nil, nil
101101
}
102102

103+
// Returns the key and value if they were present
104+
func RemoveKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
105+
for i := 0; i < len(node.Content)-1; i += 2 {
106+
if node.Content[i].Value == key {
107+
key, value := node.Content[i], node.Content[i+1]
108+
node.Content = append(node.Content[:i], node.Content[i+2:]...)
109+
return key, value
110+
}
111+
}
112+
113+
return nil, nil
114+
}
115+
103116
// Walks a yaml document from the root node to the specified path, and then applies the transformation to that node.
104117
// If the requested path is not defined in the document, no changes are made to the document.
105118
func TransformNode(rootNode *yaml.Node, path []string, transform func(node *yaml.Node) error) error {
@@ -123,7 +136,7 @@ func transformNode(node *yaml.Node, path []string, transform func(node *yaml.Nod
123136
return transform(node)
124137
}
125138

126-
keyNode, valueNode := lookupKey(node, path[0])
139+
keyNode, valueNode := LookupKey(node, path[0])
127140
if keyNode == nil {
128141
return nil
129142
}
@@ -154,15 +167,15 @@ func renameYamlKey(node *yaml.Node, path []string, newKey string) error {
154167
return errors.New("yaml node in path is not a dictionary")
155168
}
156169

157-
keyNode, valueNode := lookupKey(node, path[0])
170+
keyNode, valueNode := LookupKey(node, path[0])
158171
if keyNode == nil {
159172
return nil
160173
}
161174

162175
// end of path reached: rename key
163176
if len(path) == 1 {
164177
// Check that new key doesn't exist yet
165-
if newKeyNode, _ := lookupKey(node, newKey); newKeyNode != nil {
178+
if newKeyNode, _ := LookupKey(node, newKey); newKeyNode != nil {
166179
return fmt.Errorf("new key `%s' already exists", newKey)
167180
}
168181

schema/config.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,15 @@
312312
"description": "Command used when displaying the current branch git log in the main window",
313313
"default": "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --"
314314
},
315-
"allBranchesLogCmd": {
316-
"type": "string",
317-
"description": "Command used to display git log of all branches in the main window.\nDeprecated: Use `allBranchesLogCmds` instead.",
318-
"default": "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium"
319-
},
320315
"allBranchesLogCmds": {
321316
"items": {
322317
"type": "string"
323318
},
324319
"type": "array",
325-
"description": "Commands used to display git log of all branches in the main window, they will be cycled in order of appearance"
320+
"description": "Commands used to display git log of all branches in the main window, they will be cycled in order of appearance",
321+
"default": [
322+
"git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium"
323+
]
326324
},
327325
"overrideGpg": {
328326
"type": "boolean",

0 commit comments

Comments
 (0)