Skip to content

Commit d68c116

Browse files
Escape special characters in filenames when git-ignoring files (#4475)
- **PR Description** Some characters in file names need to be escaped so that they work correctly in .gitignore files. These include `#` and `!` (only at the beginning of a filename), as well as `[`, `]`, and `*` anywhere in the name. Fixes #4075 Fixes #4445 - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] If a new UserConfig entry was added, make sure it can be hot-reloaded (see [here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents d70a405 + b0ab652 commit d68c116

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

Diff for: pkg/commands/git_commands/working_tree.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"regexp"
78

89
"github.com/go-errors/errors"
910
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -230,15 +231,21 @@ func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) e
230231
return self.cmd.New(cmdArgs).Run()
231232
}
232233

234+
// Escapes special characters in a filename for gitignore and exclude files
235+
func escapeFilename(filename string) string {
236+
re := regexp.MustCompile(`^[!#]|[\[\]*]`)
237+
return re.ReplaceAllString(filename, `\${0}`)
238+
}
239+
233240
// Ignore adds a file to the gitignore for the repo
234241
func (self *WorkingTreeCommands) Ignore(filename string) error {
235-
return self.os.AppendLineToFile(".gitignore", filename)
242+
return self.os.AppendLineToFile(".gitignore", escapeFilename(filename))
236243
}
237244

238245
// Exclude adds a file to the .git/info/exclude for the repo
239246
func (self *WorkingTreeCommands) Exclude(filename string) error {
240247
excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
241-
return self.os.AppendLineToFile(excludeFile, filename)
248+
return self.os.AppendLineToFile(excludeFile, escapeFilename(filename))
242249
}
243250

244251
// WorktreeFileDiff returns the diff of a file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package file
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var GitignoreSpecialCharacters = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Ignore files with special characters in their names",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
},
14+
SetupRepo: func(shell *Shell) {
15+
shell.CreateFile(".gitignore", "")
16+
shell.CreateFile("#file", "")
17+
shell.CreateFile("file#abc", "")
18+
shell.CreateFile("!file", "")
19+
shell.CreateFile("file!abc", "")
20+
shell.CreateFile("abc*def", "")
21+
shell.CreateFile("abc_def", "")
22+
shell.CreateFile("file[x]", "")
23+
},
24+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
25+
excludeFile := func(fileName string) {
26+
t.Views().Files().
27+
NavigateToLine(Contains(fileName)).
28+
Press(keys.Files.IgnoreFile)
29+
30+
t.ExpectPopup().Menu().
31+
Title(Equals("Ignore or exclude file")).
32+
Select(Contains("Add to .gitignore")).
33+
Confirm()
34+
}
35+
36+
t.Views().Files().
37+
Focus().
38+
Lines(
39+
Equals("▼ /"),
40+
Equals(" ?? !file"),
41+
Equals(" ?? #file"),
42+
Equals(" ?? .gitignore"),
43+
Equals(" ?? abc*def"),
44+
Equals(" ?? abc_def"),
45+
Equals(" ?? file!abc"),
46+
Equals(" ?? file#abc"),
47+
Equals(" ?? file[x]"),
48+
)
49+
50+
excludeFile("#file")
51+
excludeFile("file#abc")
52+
excludeFile("!file")
53+
excludeFile("file!abc")
54+
excludeFile("abc*def")
55+
excludeFile("file[x]")
56+
57+
t.Views().Files().
58+
Lines(
59+
Equals("▼ /"),
60+
Equals(" ?? .gitignore"),
61+
Equals(" ?? abc_def"),
62+
)
63+
64+
t.FileSystem().FileContent(".gitignore", Equals("\\#file\nfile#abc\n\\!file\nfile!abc\nabc\\*def\nfile\\[x\\]\n"))
65+
},
66+
})

Diff for: pkg/integration/tests/test_list.go

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ var tests = []*components.IntegrationTest{
197197
file.DiscardVariousChanges,
198198
file.DiscardVariousChangesRangeSelect,
199199
file.Gitignore,
200+
file.GitignoreSpecialCharacters,
200201
file.RememberCommitMessageAfterFail,
201202
file.RenameSimilarityThresholdChange,
202203
file.RenamedFiles,

0 commit comments

Comments
 (0)