Skip to content

Commit 0f40dfe

Browse files
authored
Merge pull request #2551 from carapace-sh/git-actionchanges
git: ActionChanges - small refactor
2 parents 5a307ed + a6bf867 commit 0f40dfe

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

pkg/actions/tools/git/change.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,35 @@ func ActionChanges(opts ChangeOpts) carapace.Action {
3636
if root, err := rootDir(c); err != nil {
3737
return carapace.ActionMessage(err.Error())
3838
} else {
39+
evaluatedDir, err := filepath.EvalSymlinks(c.Dir)
40+
if err != nil {
41+
return carapace.ActionMessage(err.Error())
42+
}
43+
3944
untracked := make([]string, 0)
4045
for _, line := range strings.Split(string(output), "\n") {
41-
if len(line) > 3 {
42-
if (opts.Staged && line[1] == ' ') ||
43-
(opts.Unstaged && line[1] != ' ' && line[1] != '!') ||
44-
(opts.Ignored && line[1] == '!') {
45-
path := line[3:]
46-
if splitted := strings.SplitN(path, " -> ", 2); len(splitted) > 1 { // renamed
47-
path = splitted[1]
48-
}
46+
switch {
47+
case len(line) < 4:
48+
// skip
49+
case opts.Staged && line[1] == ' ',
50+
opts.Unstaged && line[1] != ' ' && line[1] != '!',
51+
opts.Ignored && line[1] == '!':
52+
53+
path := line[3:]
54+
if splitted := strings.SplitN(path, " -> ", 2); len(splitted) > 1 { // renamed
55+
path = splitted[1]
56+
}
57+
58+
relativePath, err := filepath.Rel(evaluatedDir, root+"/"+path)
59+
if err != nil {
60+
return carapace.ActionMessage(err.Error())
61+
}
4962

50-
evaluatedDir, err := filepath.EvalSymlinks(c.Dir)
51-
if err != nil {
52-
return carapace.ActionMessage(err.Error())
53-
}
54-
if relativePath, err := filepath.Rel(evaluatedDir, root+"/"+path); err != nil {
55-
return carapace.ActionMessage(err.Error())
56-
} else {
57-
if status := line[:2]; strings.Contains(status, "D") { // deleted
58-
untracked = append(untracked, relativePath, status, style.ForPathExt(relativePath, c))
59-
} else {
60-
untracked = append(untracked, relativePath, status, style.ForPath(relativePath, c))
61-
}
62-
}
63+
switch status := line[:2]; {
64+
case strings.Contains(status, "D"): // deleted
65+
untracked = append(untracked, relativePath, status, style.ForPathExt(relativePath, c))
66+
default:
67+
untracked = append(untracked, relativePath, status, style.ForPath(relativePath, c))
6368
}
6469
}
6570
}

0 commit comments

Comments
 (0)