mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 12:55:47 +02:00
Escape special characters when git-ignoring files
This commit is contained in:
parent
41f89d86f0
commit
b0ab6529c1
2 changed files with 9 additions and 23 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
|
@ -230,15 +231,21 @@ func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) e
|
||||||
return self.cmd.New(cmdArgs).Run()
|
return self.cmd.New(cmdArgs).Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Escapes special characters in a filename for gitignore and exclude files
|
||||||
|
func escapeFilename(filename string) string {
|
||||||
|
re := regexp.MustCompile(`^[!#]|[\[\]*]`)
|
||||||
|
return re.ReplaceAllString(filename, `\${0}`)
|
||||||
|
}
|
||||||
|
|
||||||
// Ignore adds a file to the gitignore for the repo
|
// Ignore adds a file to the gitignore for the repo
|
||||||
func (self *WorkingTreeCommands) Ignore(filename string) error {
|
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
|
// Exclude adds a file to the .git/info/exclude for the repo
|
||||||
func (self *WorkingTreeCommands) Exclude(filename string) error {
|
func (self *WorkingTreeCommands) Exclude(filename string) error {
|
||||||
excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
|
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
|
// WorktreeFileDiff returns the diff of a file
|
||||||
|
|
|
@ -55,33 +55,12 @@ var GitignoreSpecialCharacters = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
excludeFile("file[x]")
|
excludeFile("file[x]")
|
||||||
|
|
||||||
t.Views().Files().
|
t.Views().Files().
|
||||||
/* EXPECTED:
|
|
||||||
Lines(
|
Lines(
|
||||||
Equals("▼ /"),
|
Equals("▼ /"),
|
||||||
Equals(" ?? .gitignore"),
|
Equals(" ?? .gitignore"),
|
||||||
Equals(" ?? abc_def"),
|
Equals(" ?? abc_def"),
|
||||||
)
|
)
|
||||||
ACTUAL:
|
|
||||||
As you can see, it did ignore the 'file!abc' and 'file#abc' files
|
|
||||||
correctly. Those don't need to be quoted because # and ! are only
|
|
||||||
special at the beginning.
|
|
||||||
|
|
||||||
Most of the other files are not ignored properly because their
|
|
||||||
special characters need to be escaped. For * it's the other way
|
|
||||||
round: while it does hide 'abc*def', it also hides 'abc_def',
|
|
||||||
which we don't want.
|
|
||||||
*/
|
|
||||||
Lines(
|
|
||||||
Equals("▼ /"),
|
|
||||||
Equals(" ?? !file"),
|
|
||||||
Equals(" ?? #file"),
|
|
||||||
Equals(" ?? .gitignore"),
|
|
||||||
Equals(" ?? file[x]"),
|
|
||||||
)
|
|
||||||
|
|
||||||
/* EXPECTED:
|
|
||||||
t.FileSystem().FileContent(".gitignore", Equals("\\#file\nfile#abc\n\\!file\nfile!abc\nabc\\*def\nfile\\[x\\]\n"))
|
t.FileSystem().FileContent(".gitignore", Equals("\\#file\nfile#abc\n\\!file\nfile!abc\nabc\\*def\nfile\\[x\\]\n"))
|
||||||
ACTUAL: */
|
|
||||||
t.FileSystem().FileContent(".gitignore", Equals("#file\nfile#abc\n!file\nfile!abc\nabc*def\nfile[x]\n"))
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue