From f411dcc6addd496b6806645c0e746074feff56c6 Mon Sep 17 00:00:00 2001 From: Azhar Madar Shaik Date: Tue, 1 Apr 2025 11:26:14 +0530 Subject: [PATCH 1/2] escape files that start with '#' or '!' --- pkg/commands/git_commands/working_tree.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index e1339ee56d9..975d72a4e66 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/models" @@ -230,15 +231,23 @@ func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) e return self.cmd.New(cmdArgs).Run() } +func escapeFilename(filename string) string { + escapedFilename := filename + if strings.HasPrefix(filename, "#") || strings.HasPrefix(filename, "!") { + escapedFilename = "\\" + filename + } + return escapedFilename +} + // Ignore adds a file to the gitignore for the repo func (self *WorkingTreeCommands) Ignore(filename string) error { - return self.os.AppendLineToFile(".gitignore", filename) + return self.os.AppendLineToFile(".gitignore", escapeFilename(filename)) } // Exclude adds a file to the .git/info/exclude for the repo func (self *WorkingTreeCommands) Exclude(filename string) error { excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude") - return self.os.AppendLineToFile(excludeFile, filename) + return self.os.AppendLineToFile(excludeFile, escapeFilename(filename)) } // WorktreeFileDiff returns the diff of a file From e6837c6d437539aa0a7946ac069839c6f64eda54 Mon Sep 17 00:00:00 2001 From: Azhar Madar Shaik Date: Sat, 5 Apr 2025 22:09:31 +0530 Subject: [PATCH 2/2] support escaping [] in filenames --- pkg/commands/git_commands/working_tree.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 975d72a4e66..43ee275674c 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -236,6 +236,8 @@ func escapeFilename(filename string) string { if strings.HasPrefix(filename, "#") || strings.HasPrefix(filename, "!") { escapedFilename = "\\" + filename } + escapedFilename = strings.ReplaceAll(escapedFilename, "[", "\\[") + escapedFilename = strings.ReplaceAll(escapedFilename, "]", "\\]") return escapedFilename }