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 15 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
6 changes: 6 additions & 0 deletions pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ func (self *WorkingTreeCommands) Ignore(filename string) error {
return self.os.AppendLineToFile(".gitignore", filename)
}

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

}

// WorktreeFileDiff returns the diff of a file
func (self *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string {
// for now we assume an error means the file was deleted
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ type KeybindingFilesConfig struct {
CommitChangesWithoutHook string `yaml:"commitChangesWithoutHook"`
AmendLastCommit string `yaml:"amendLastCommit"`
CommitChangesWithEditor string `yaml:"commitChangesWithEditor"`
IgnoreFile string `yaml:"ignoreFile"`
IgnoreOrExcludeFile string `yaml:"IgnoreOrExcludeFile"`
RefreshFiles string `yaml:"refreshFiles"`
StashAllChanges string `yaml:"stashAllChanges"`
ViewStashOptions string `yaml:"viewStashOptions"`
Expand Down Expand Up @@ -487,7 +487,7 @@ func GetDefaultConfig() *UserConfig {
CommitChangesWithoutHook: "w",
AmendLastCommit: "A",
CommitChangesWithEditor: "C",
IgnoreFile: "i",
IgnoreOrExcludeFile: "i",
RefreshFiles: "r",
StashAllChanges: "s",
ViewStashOptions: "S",
Expand Down
119 changes: 94 additions & 25 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
Description: self.c.Tr.LcOpenFile,
},
{
Key: opts.GetKey(opts.Config.Files.IgnoreFile),
Handler: self.checkSelectedFileNode(self.ignore),
Description: self.c.Tr.LcIgnoreFile,
Key: opts.GetKey(opts.Config.Files.IgnoreOrExcludeFile),
Handler: self.checkSelectedFileNode(self.ignoreOrExcludeMenu),
Description: self.c.Tr.Actions.IgnoreExcludeFile,
},
{
Key: opts.GetKey(opts.Config.Files.RefreshFiles),
Expand Down Expand Up @@ -302,59 +302,128 @@ 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, bool) {

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}})
},
})
}), true
}
return nil, false
}

self.c.LogAction(self.c.Tr.Actions.IgnoreFile)
func (self *FilesController) ignoreOrExcludeFile(node *filetree.FileNode, trText string, trPrompt string, trAction string, f func(string) error) error {

if err := unstageFiles(); err != nil {
trackingErr, earlyExit := self.checkTracking(node, trText, trPrompt, trAction, f)
gozes marked this conversation as resolved.
Show resolved Hide resolved

if trackingErr != nil {
return trackingErr
}

if earlyExit {
return nil
}

self.c.LogAction(trAction)

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

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

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

func (self *FilesController) ignore(node *filetree.FileNode) error {
if node.GetPath() == ".gitignore" {
return self.c.ErrorMsg("Cannot ignore .gitignore")
gozes marked this conversation as resolved.
Show resolved Hide resolved
}
err := self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, self.git.WorkingTree.Ignore)
if err != nil {
return err
}

return nil
}

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")
}

if node.GetPath() == ".gitignore" {
return self.c.ErrorMsg("Cannot exclude .gitignore")
}

err := self.ignoreOrExcludeFile(node, self.c.Tr.ExcludeTracked, self.c.Tr.ExcludeTrackedPrompt, self.c.Tr.Actions.ExcludeFile, self.git.WorkingTree.Exclude)
if err != nil {
return err
}
return nil

}

func (self *FilesController) ignoreOrExcludeMenu(node *filetree.FileNode) error {
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.Actions.IgnoreExcludeFile,
Items: []*types.MenuItem{
{
LabelColumns: []string{self.c.Tr.LcIgnoreFile},
OnPress: func() error {
if err := self.ignore(node); err != nil {
return self.c.Error(err)
}
return nil
},
Key: 'i',
},
{
LabelColumns: []string{self.c.Tr.LcExcludeFile},
OnPress: func() error {
if err := self.exclude(node); err != nil {
return self.c.Error(err)
}
return nil
},
Key: 'e',
}},
})
}

func (self *FilesController) HandleWIPCommitPress() error {
skipHookPrefix := self.c.UserConfig.Git.SkipHookPrefix
if skipHookPrefix == "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/i18n/chinese.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func chineseTranslationSet() TranslationSet {
UnstageFile: "取消暂存文件",
UnstageAllFiles: "取消暂存所有文件",
StageAllFiles: "暂存所有文件",
IgnoreFile: "忽略文件",
IgnoreExcludeFile: "忽略文件",
Commit: "提交 (Commit)",
EditFile: "编辑文件",
Push: "推送 (Push)",
Expand Down
12 changes: 11 additions & 1 deletion 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 @@ -560,7 +563,9 @@ type Actions struct {
UnstageFile string
UnstageAllFiles string
StageAllFiles string
IgnoreExcludeFile string
IgnoreFile string
ExcludeFile string
Commit string
EditFile string
Push string
Expand Down Expand Up @@ -780,6 +785,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 +978,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 @@ -1169,7 +1177,9 @@ func EnglishTranslationSet() TranslationSet {
UnstageFile: "Unstage file",
UnstageAllFiles: "Unstage all files",
StageAllFiles: "Stage all files",
IgnoreFile: "Ignore file",
IgnoreExcludeFile: "Ignore or Exclude file",
IgnoreFile: "Ignore or Exclude file",
ExcludeFile: "Exclude file",
Commit: "Commit",
EditFile: "Edit file",
Push: "Push",
Expand Down
2 changes: 1 addition & 1 deletion pkg/i18n/japanese.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func japaneseTranslationSet() TranslationSet {
UnstageFile: "ファイルをアンステージ",
UnstageAllFiles: "すべてのファイルをアンステージ",
StageAllFiles: "すべてのファイルをステージ",
IgnoreFile: "ファイルをignore",
IgnoreExcludeFile: "ファイルをignore",
Commit: "コミット",
EditFile: "ファイルを編集",
Push: "Push",
Expand Down
2 changes: 1 addition & 1 deletion pkg/i18n/korean.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func koreanTranslationSet() TranslationSet {
UnstageFile: "Unstage file",
UnstageAllFiles: "Unstage all files",
StageAllFiles: "Stage all files",
IgnoreFile: "Ignore file",
IgnoreExcludeFile: "Ignore file",
Commit: "커밋",
EditFile: "파일 수정",
Push: "푸시",
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
1 change: 1 addition & 0 deletions test/integration/excludeGitIgnore/expected/repo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions test/integration/excludeGitIgnore/recording.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":2674,"Mod":0,"Key":256,"Ch":105},{"Timestamp":4846,"Mod":0,"Key":256,"Ch":101},{"Timestamp":8064,"Mod":0,"Key":13,"Ch":13},{"Timestamp":8515,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":170,"Height":55}]}
11 changes: 11 additions & 0 deletions test/integration/excludeGitIgnore/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

cd $1

git init


echo test1 > .gitignore

4 changes: 4 additions & 0 deletions test/integration/excludeGitIgnore/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "In this test .gitignore is added to .git/info/exclude using the ignore or exclude menu to check that this operation is to allowed",
"speed": 5
}
Empty file.
1 change: 1 addition & 0 deletions test/integration/excludeMenu/expected/repo/.git_keep/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions test/integration/excludeMenu/expected/repo/.git_keep/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

myfile1
1 change: 1 addition & 0 deletions test/integration/excludeMenu/expected/repo/myfile1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions test/integration/excludeMenu/recording.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":1418,"Mod":0,"Key":256,"Ch":105},{"Timestamp":1725,"Mod":0,"Key":256,"Ch":101},{"Timestamp":3207,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":170,"Height":55}]}
11 changes: 11 additions & 0 deletions test/integration/excludeMenu/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

cd $1

git init


echo test1 > myfile1

4 changes: 4 additions & 0 deletions test/integration/excludeMenu/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "In this test a file is added to .git/info/exclude using the ignore or exclude menu",
"speed": 5
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions test/integration/gitignoreMenu/expected/repo/.git_keep/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
2 changes: 2 additions & 0 deletions test/integration/gitignoreMenu/expected/repo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

myfile1
1 change: 1 addition & 0 deletions test/integration/gitignoreMenu/recording.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":3418,"Mod":0,"Key":256,"Ch":105},{"Timestamp":3721,"Mod":0,"Key":256,"Ch":105},{"Timestamp":5154,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":170,"Height":55}]}