Refactor Ignore/Exclude to accept multiple filenames

The gitignore-applier layer now takes a slice, looping over paths. The
files_controller plumbing still feeds a single-element slice so behavior
is unchanged for the single-file case; this prepares for wiring the
binding up to multi-select.
This commit is contained in:
phanium 2026-07-25 21:43:13 +08:00
parent a5b4477a20
commit 6dca12131f
2 changed files with 22 additions and 8 deletions

View file

@ -367,19 +367,29 @@ func escapeFilename(filename string) string {
return "/" + re.ReplaceAllString(filename, `\${0}`)
}
// Ignore adds a file to the gitignore for the repo
func (self *WorkingTreeCommands) Ignore(filename string) error {
return self.os.AppendLineToFile(".gitignore", escapeFilename(filename))
// Ignore adds the given files to the gitignore for the repo
func (self *WorkingTreeCommands) Ignore(filenames []string) error {
for _, filename := range filenames {
if err := self.os.AppendLineToFile(".gitignore", escapeFilename(filename)); err != nil {
return err
}
}
return nil
}
// Exclude adds a file to the .git/info/exclude for the repo
func (self *WorkingTreeCommands) Exclude(filename string) error {
// Exclude adds the given files to the .git/info/exclude for the repo
func (self *WorkingTreeCommands) Exclude(filenames []string) error {
infoDir := filepath.Join(self.repoPaths.repoGitDirPath, "info")
if err := os.MkdirAll(infoDir, 0o755); err != nil {
return err
}
excludeFile := filepath.Join(infoDir, "exclude")
return self.os.AppendLineToFile(excludeFile, escapeFilename(filename))
for _, filename := range filenames {
if err := self.os.AppendLineToFile(excludeFile, escapeFilename(filename)); err != nil {
return err
}
}
return nil
}
// WorktreeFileDiff returns the diff of a file

View file

@ -1020,7 +1020,9 @@ func (self *FilesController) ignore(node *filetree.FileNode) error {
if node.GetPath() == ".gitignore" {
return errors.New(self.c.Tr.Actions.IgnoreFileErr)
}
return self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, self.c.Git().WorkingTree.Ignore)
return self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, func(name string) error {
return self.c.Git().WorkingTree.Ignore([]string{name})
})
}
func (self *FilesController) exclude(node *filetree.FileNode) error {
@ -1028,7 +1030,9 @@ func (self *FilesController) exclude(node *filetree.FileNode) error {
return errors.New(self.c.Tr.Actions.ExcludeGitIgnoreErr)
}
return self.ignoreOrExcludeFile(node, self.c.Tr.ExcludeTracked, self.c.Tr.ExcludeTrackedPrompt, self.c.Tr.Actions.ExcludeFile, self.c.Git().WorkingTree.Exclude)
return self.ignoreOrExcludeFile(node, self.c.Tr.ExcludeTracked, self.c.Tr.ExcludeTrackedPrompt, self.c.Tr.Actions.ExcludeFile, func(name string) error {
return self.c.Git().WorkingTree.Exclude([]string{name})
})
}
func (self *FilesController) ignoreOrExcludeMenu(node *filetree.FileNode) error {