Skip to content

Commit f411dcc

Browse files
committed
escape files that start with '#' or '!'
1 parent cd22862 commit f411dcc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

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

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

234+
func escapeFilename(filename string) string {
235+
escapedFilename := filename
236+
if strings.HasPrefix(filename, "#") || strings.HasPrefix(filename, "!") {
237+
escapedFilename = "\\" + filename
238+
}
239+
return escapedFilename
240+
}
241+
233242
// Ignore adds a file to the gitignore for the repo
234243
func (self *WorkingTreeCommands) Ignore(filename string) error {
235-
return self.os.AppendLineToFile(".gitignore", filename)
244+
return self.os.AppendLineToFile(".gitignore", escapeFilename(filename))
236245
}
237246

238247
// Exclude adds a file to the .git/info/exclude for the repo
239248
func (self *WorkingTreeCommands) Exclude(filename string) error {
240249
excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
241-
return self.os.AppendLineToFile(excludeFile, filename)
250+
return self.os.AppendLineToFile(excludeFile, escapeFilename(filename))
242251
}
243252

244253
// WorktreeFileDiff returns the diff of a file

0 commit comments

Comments
 (0)