Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(file_controller): Add support for to exclude file by adding them to .git/info/exclude #2016

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,18 @@ func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) e
return self.cmd.New("git checkout -- " + quotedFileName).Run()
}

func (self *WorkingTreeCommands) Omit(filename, destination string) error {
gozes marked this conversation as resolved.
Show resolved Hide resolved
return self.os.AppendLineToFile(destination, filename)
}

// 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.Omit(filename, ".gitignore")
}

// Exclude adds a file to the .git/info/exclude for the repo
func (self *WorkingTreeCommands) Exclude(filename string) error {
return self.Omit(filename, ".git/info/exclude")
}

// WorktreeFileDiff returns the diff of a file
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type KeybindingFilesConfig struct {
AmendLastCommit string `yaml:"amendLastCommit"`
CommitChangesWithEditor string `yaml:"commitChangesWithEditor"`
IgnoreFile string `yaml:"ignoreFile"`
ExcludeFile string `yaml:"excludeFile"`
RefreshFiles string `yaml:"refreshFiles"`
StashAllChanges string `yaml:"stashAllChanges"`
ViewStashOptions string `yaml:"viewStashOptions"`
Expand Down Expand Up @@ -488,6 +489,7 @@ func GetDefaultConfig() *UserConfig {
AmendLastCommit: "A",
CommitChangesWithEditor: "C",
IgnoreFile: "i",
ExcludeFile: "I",
RefreshFiles: "r",
StashAllChanges: "s",
ViewStashOptions: "S",
Expand Down
79 changes: 60 additions & 19 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
Handler: self.checkSelectedFileNode(self.ignore),
Description: self.c.Tr.LcIgnoreFile,
},
{
Key: opts.GetKey(opts.Config.Files.ExcludeFile),
Handler: self.checkSelectedFileNode(self.exclude),
Description: self.c.Tr.LcExcludeFile,
},
{
Key: opts.GetKey(opts.Config.Files.RefreshFiles),
Handler: self.refresh,
Expand Down Expand Up @@ -302,49 +307,61 @@ func (self *FilesController) stageAll() error {
return self.contexts.Files.HandleFocus()
}

func (self *FilesController) ignore(node *filetree.FileNode) error {
if node.GetPath() == ".gitignore" {
return self.c.ErrorMsg("Cannot ignore .gitignore")
}
func (self *FilesController) unstageFiles(node *filetree.FileNode) error {

unstageFiles := func() error {
return node.ForEachFile(func(file *models.File) error {
if file.HasStagedChanges {
if err := self.git.WorkingTree.UnStageFile(file.Names(), file.Tracked); err != nil {
return err
}
return node.ForEachFile(func(file *models.File) error {
if file.HasStagedChanges {
if err := self.git.WorkingTree.UnStageFile(file.Names(), file.Tracked); err != nil {
return err
}
}

return nil
})
}
return nil
})
}

func (self *FilesController) checkTracking(node *filetree.FileNode, trText string, trPrompt string, trAction string, f func(string) error) error {

if node.GetIsTracked() {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.IgnoreTracked,
Prompt: self.c.Tr.IgnoreTrackedPrompt,
Title: trText,
Prompt: trPrompt,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.IgnoreFile)
self.c.LogAction(trAction)
// not 100% sure if this is necessary but I'll assume it is
if err := unstageFiles(); err != nil {
if err := self.unstageFiles(node); err != nil {
return err
}

if err := self.git.WorkingTree.RemoveTrackedFiles(node.GetPath()); err != nil {
return err
}

if err := self.git.WorkingTree.Ignore(node.GetPath()); err != nil {
if err := f(node.GetPath()); err != nil {
return err
}

return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}})
},
})
}
return nil
}

func (self *FilesController) ignore(node *filetree.FileNode) error {
if node.GetPath() == ".gitignore" {
return self.c.ErrorMsg("Cannot ignore .gitignore")
}

trakckingErr := self.checkTracking(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreFile, self.git.WorkingTree.Ignore)
gozes marked this conversation as resolved.
Show resolved Hide resolved

if trakckingErr != nil {
return trakckingErr
}

self.c.LogAction(self.c.Tr.Actions.IgnoreFile)

if err := unstageFiles(); err != nil {
if err := self.unstageFiles(node); err != nil {
return err
}

Expand All @@ -355,6 +372,30 @@ func (self *FilesController) ignore(node *filetree.FileNode) error {
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}})
}

func (self *FilesController) exclude(node *filetree.FileNode) error {
gozes marked this conversation as resolved.
Show resolved Hide resolved
if node.GetPath() == ".git/info/exclude" {
return self.c.ErrorMsg("Cannot exclude .git/info/exclude")
}

trakckingErr := self.checkTracking(node, self.c.Tr.ExcludeTracked, self.c.Tr.ExcludeTrackedPrompt, self.c.Tr.Actions.ExcludeFile, self.git.WorkingTree.Exclude)

if trakckingErr != nil {
return trakckingErr
}

self.c.LogAction(self.c.Tr.Actions.ExcludeFile)

if err := self.unstageFiles(node); err != nil {
return err
}

if err := self.git.WorkingTree.Exclude(node.GetPath()); err != nil {
return self.c.Error(err)
}

return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}})
}

func (self *FilesController) HandleWIPCommitPress() error {
skipHookPrefix := self.c.UserConfig.Git.SkipHookPrefix
if skipHookPrefix == "" {
Expand Down
8 changes: 8 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type TranslationSet struct {
LcEditFile string
LcOpenFile string
LcIgnoreFile string
LcExcludeFile string
LcRefreshFiles string
LcMergeIntoCurrentBranch string
ConfirmQuit string
Expand Down Expand Up @@ -345,7 +346,9 @@ type TranslationSet struct {
NotAGitFlowBranch string
NewBranchNamePrompt string
IgnoreTracked string
ExcludeTracked string
IgnoreTrackedPrompt string
ExcludeTrackedPrompt string
LcViewResetToUpstreamOptions string
LcNextScreenMode string
LcPrevScreenMode string
Expand Down Expand Up @@ -561,6 +564,7 @@ type Actions struct {
UnstageAllFiles string
StageAllFiles string
IgnoreFile string
ExcludeFile string
Commit string
EditFile string
Push string
Expand Down Expand Up @@ -780,6 +784,7 @@ func EnglishTranslationSet() TranslationSet {
LcEditFile: `edit file`,
LcOpenFile: `open file`,
LcIgnoreFile: `add to .gitignore`,
LcExcludeFile: `add to .git/info/exclude`,
LcRefreshFiles: `refresh files`,
LcMergeIntoCurrentBranch: `merge into currently checked out branch`,
ConfirmQuit: `Are you sure you want to quit?`,
Expand Down Expand Up @@ -972,6 +977,8 @@ func EnglishTranslationSet() TranslationSet {
NewGitFlowBranchPrompt: "new {{.branchType}} name:",
IgnoreTracked: "Ignore tracked file",
IgnoreTrackedPrompt: "Are you sure you want to ignore a tracked file?",
ExcludeTracked: "Exclude tracked file",
ExcludeTrackedPrompt: "Are you sure you want to exclude a tracked file?",
LcViewResetToUpstreamOptions: "view upstream reset options",
LcNextScreenMode: "next screen mode (normal/half/fullscreen)",
LcPrevScreenMode: "prev screen mode",
Expand Down Expand Up @@ -1170,6 +1177,7 @@ func EnglishTranslationSet() TranslationSet {
UnstageAllFiles: "Unstage all files",
StageAllFiles: "Stage all files",
IgnoreFile: "Ignore file",
ExcludeFile: "Exclude file",
Commit: "Commit",
EditFile: "Edit file",
Push: "Push",
Expand Down