Skip to content

Commit 1b36ed1

Browse files
fix: defect in extension process during rename duplicate file (#1158)
1 parent 07dc196 commit 1b36ed1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pkg/util/path.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,17 @@ func CheckDuplicateAndRename(path string) (string, error) {
5050
return "", err
5151
}
5252

53-
index := strings.LastIndex(name, ".")
53+
ext := syspath.Ext(name)
5454
var nameTpl string
55-
if index == -1 {
55+
// Special case: if the extension is the entire filename (like .gitignore),
56+
// or if index of last dot is 0 (starts with dot), treat it as no extension
57+
if ext == "" || ext == name || (len(ext) > 0 && strings.LastIndex(name, ".") == 0) {
58+
// No extension or hidden file without extension
5659
nameTpl = name + " (%d)"
5760
} else {
58-
nameTpl = name[:index] + " (%d)" + name[index:]
61+
// Has extension
62+
nameWithoutExt := name[:len(name)-len(ext)]
63+
nameTpl = nameWithoutExt + " (%d)" + ext
5964
}
6065
for i := 1; ; i++ {
6166
newName := fmt.Sprintf(nameTpl, i)

pkg/util/path_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,28 @@ func TestSafeRemove(t *testing.T) {
106106
}
107107

108108
func TestCheckDuplicateAndRename(t *testing.T) {
109+
// Test with extension
109110
doCheckDuplicateAndRename(t, []string{}, "a.txt", "a.txt")
110111
doCheckDuplicateAndRename(t, []string{"a.txt"}, "a.txt", "a (1).txt")
111112
doCheckDuplicateAndRename(t, []string{"a.txt", "a (1).txt"}, "a.txt", "a (2).txt")
112113

114+
// Test without extension
113115
doCheckDuplicateAndRename(t, []string{}, "a", "a")
114116
doCheckDuplicateAndRename(t, []string{"a"}, "a", "a (1)")
115117
doCheckDuplicateAndRename(t, []string{"a", "a (1)"}, "a", "a (2)")
118+
119+
// Test hidden files (starting with dot)
120+
doCheckDuplicateAndRename(t, []string{}, ".gitignore", ".gitignore")
121+
doCheckDuplicateAndRename(t, []string{".gitignore"}, ".gitignore", ".gitignore (1)")
122+
doCheckDuplicateAndRename(t, []string{".gitignore", ".gitignore (1)"}, ".gitignore", ".gitignore (2)")
123+
124+
// Test hidden files with extension
125+
doCheckDuplicateAndRename(t, []string{}, ".config.json", ".config.json")
126+
doCheckDuplicateAndRename(t, []string{".config.json"}, ".config.json", ".config (1).json")
127+
128+
// Test multiple dots
129+
doCheckDuplicateAndRename(t, []string{}, "test.tar.gz", "test.tar.gz")
130+
doCheckDuplicateAndRename(t, []string{"test.tar.gz"}, "test.tar.gz", "test.tar (1).gz")
116131
}
117132

118133
func doCheckDuplicateAndRename(t *testing.T, exitsPaths []string, path string, except string) {

0 commit comments

Comments
 (0)