Skip to content

Commit 5e52377

Browse files
author
PhatPhuckDave
committed
Implement "Ignore by file extension" command
That works exactly like the "ignore" command But instead of ignoring a file it ignores *.<extension> of the selected file
1 parent a0dd3be commit 5e52377

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

pkg/config/user_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ type KeybindingFilesConfig struct {
449449
FindBaseCommitForFixup string `yaml:"findBaseCommitForFixup"`
450450
ConfirmDiscard string `yaml:"confirmDiscard"`
451451
IgnoreFile string `yaml:"ignoreFile"`
452+
IgnoreFileExtension string `yaml:"ignoreFileExtension"`
452453
RefreshFiles string `yaml:"refreshFiles"`
453454
StashAllChanges string `yaml:"stashAllChanges"`
454455
ViewStashOptions string `yaml:"viewStashOptions"`
@@ -908,6 +909,7 @@ func GetDefaultConfig() *UserConfig {
908909
CommitChangesWithEditor: "C",
909910
FindBaseCommitForFixup: "<c-f>",
910911
IgnoreFile: "i",
912+
IgnoreFileExtension: "I",
911913
RefreshFiles: "r",
912914
StashAllChanges: "s",
913915
ViewStashOptions: "S",

pkg/gui/controllers/files_controller.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67
"strings"
78

89
"github.com/jesseduffield/gocui"
@@ -108,6 +109,12 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
108109
Description: self.c.Tr.Actions.IgnoreExcludeFile,
109110
OpensMenu: true,
110111
},
112+
{
113+
Key: opts.GetKey(opts.Config.Files.IgnoreFileExtension),
114+
Handler: self.withItem(self.ignoreExtension),
115+
GetDisabledReason: self.require(self.singleItemSelected()),
116+
Description: self.c.Tr.IgnoreFileExtension,
117+
},
111118
{
112119
Key: opts.GetKey(opts.Config.Files.RefreshFiles),
113120
Handler: self.refresh,
@@ -650,6 +657,24 @@ func (self *FilesController) ignore(node *filetree.FileNode) error {
650657
return self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, self.c.Git().WorkingTree.Ignore)
651658
}
652659

660+
func (self *FilesController) ignoreExtension(node *filetree.FileNode) error {
661+
if node.GetPath() == ".gitignore" {
662+
return errors.New(self.c.Tr.Actions.IgnoreFileErr)
663+
}
664+
665+
path := node.GetPath()
666+
ext := filepath.Ext(path)
667+
if ext == "" {
668+
return fmt.Errorf("No file extension to ignore")
669+
}
670+
671+
pattern := "*" + ext
672+
673+
return self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, func(string) error {
674+
return self.c.Git().WorkingTree.Ignore(pattern)
675+
})
676+
}
677+
653678
func (self *FilesController) exclude(node *filetree.FileNode) error {
654679
if node.GetPath() == ".gitignore" {
655680
return errors.New(self.c.Tr.Actions.ExcludeGitIgnoreErr)
@@ -672,6 +697,16 @@ func (self *FilesController) ignoreOrExcludeMenu(node *filetree.FileNode) error
672697
},
673698
Key: 'i',
674699
},
700+
{
701+
LabelColumns: []string{self.c.Tr.IgnoreFileExtension},
702+
OnPress: func() error {
703+
if err := self.ignoreExtension(node); err != nil {
704+
return err
705+
}
706+
return nil
707+
},
708+
Key: 'I',
709+
},
675710
{
676711
LabelColumns: []string{self.c.Tr.ExcludeFile},
677712
OnPress: func() error {

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ type TranslationSet struct {
249249
OpenFileTooltip string
250250
OpenInEditor string
251251
IgnoreFile string
252+
IgnoreFileExtension string
252253
ExcludeFile string
253254
RefreshFiles string
254255
Merge string
@@ -1283,6 +1284,7 @@ func EnglishTranslationSet() *TranslationSet {
12831284
OpenFileTooltip: "Open file in default application.",
12841285
OpenInEditor: "Open in editor",
12851286
IgnoreFile: `Add to .gitignore`,
1287+
IgnoreFileExtension: `Add extension to .gitignore`,
12861288
ExcludeFile: `Add to .git/info/exclude`,
12871289
RefreshFiles: `Refresh files`,
12881290
Merge: `Merge`,

0 commit comments

Comments
 (0)