File tree 2 files changed +9
-23
lines changed
2 files changed +9
-23
lines changed Original file line number Diff line number Diff line change 4
4
"fmt"
5
5
"os"
6
6
"path/filepath"
7
+ "regexp"
7
8
8
9
"github.com/go-errors/errors"
9
10
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -230,15 +231,21 @@ func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) e
230
231
return self .cmd .New (cmdArgs ).Run ()
231
232
}
232
233
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
+
233
240
// Ignore adds a file to the gitignore for the repo
234
241
func (self * WorkingTreeCommands ) Ignore (filename string ) error {
235
- return self .os .AppendLineToFile (".gitignore" , filename )
242
+ return self .os .AppendLineToFile (".gitignore" , escapeFilename ( filename ) )
236
243
}
237
244
238
245
// Exclude adds a file to the .git/info/exclude for the repo
239
246
func (self * WorkingTreeCommands ) Exclude (filename string ) error {
240
247
excludeFile := filepath .Join (self .repoPaths .repoGitDirPath , "info" , "exclude" )
241
- return self .os .AppendLineToFile (excludeFile , filename )
248
+ return self .os .AppendLineToFile (excludeFile , escapeFilename ( filename ) )
242
249
}
243
250
244
251
// WorktreeFileDiff returns the diff of a file
Original file line number Diff line number Diff line change @@ -55,33 +55,12 @@ var GitignoreSpecialCharacters = NewIntegrationTest(NewIntegrationTestArgs{
55
55
excludeFile ("file[x]" )
56
56
57
57
t .Views ().Files ().
58
- /* EXPECTED:
59
58
Lines (
60
59
Equals ("▼ /" ),
61
60
Equals (" ?? .gitignore" ),
62
61
Equals (" ?? abc_def" ),
63
62
)
64
- ACTUAL:
65
- As you can see, it did ignore the 'file!abc' and 'file#abc' files
66
- correctly. Those don't need to be quoted because # and ! are only
67
- special at the beginning.
68
63
69
- Most of the other files are not ignored properly because their
70
- special characters need to be escaped. For * it's the other way
71
- round: while it does hide 'abc*def', it also hides 'abc_def',
72
- which we don't want.
73
- */
74
- Lines (
75
- Equals ("▼ /" ),
76
- Equals (" ?? !file" ),
77
- Equals (" ?? #file" ),
78
- Equals (" ?? .gitignore" ),
79
- Equals (" ?? file[x]" ),
80
- )
81
-
82
- /* EXPECTED:
83
64
t .FileSystem ().FileContent (".gitignore" , Equals ("\\ #file\n file#abc\n \\ !file\n file!abc\n abc\\ *def\n file\\ [x\\ ]\n " ))
84
- ACTUAL: */
85
- t .FileSystem ().FileContent (".gitignore" , Equals ("#file\n file#abc\n !file\n file!abc\n abc*def\n file[x]\n " ))
86
65
},
87
66
})
You can’t perform that action at this time.
0 commit comments