1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-31 23:19:40 +02:00

allow ignoring directories

This commit is contained in:
Jesse Duffield 2021-03-16 09:07:00 +11:00
parent c9de6c003b
commit cd0532b4d6
4 changed files with 57 additions and 12 deletions

View File

@ -31,7 +31,7 @@ func (c *GitCommand) StageAll() error {
return c.OSCommand.RunCommand("git add -A") return c.OSCommand.RunCommand("git add -A")
} }
// UnstageAll stages all files // UnstageAll unstages all files
func (c *GitCommand) UnstageAll() error { func (c *GitCommand) UnstageAll() error {
return c.OSCommand.RunCommand("git reset") return c.OSCommand.RunCommand("git reset")
} }

View File

@ -144,13 +144,19 @@ func (s *StatusLineNode) sortChildren() {
s.Children = sortedChildren s.Children = sortedChildren
} }
// returns true if any descendant file is tracked
func (s *StatusLineNode) GetIsTracked() bool { func (s *StatusLineNode) GetIsTracked() bool {
if s.File != nil { if s.File != nil {
return s.File.GetIsTracked() return s.File.GetIsTracked()
} }
// pretty sure I'm allowed to do this for _, child := range s.Children {
return true if child.GetIsTracked() {
return true
}
}
return false
} }
func (s *StatusLineNode) GetPath() string { func (s *StatusLineNode) GetPath() string {
@ -211,3 +217,19 @@ func (s *StatusLineNode) ID() string {
func (s *StatusLineNode) Description() string { func (s *StatusLineNode) Description() string {
return s.GetPath() return s.GetPath()
} }
func (s *StatusLineNode) ForEachFile(cb func(*File) error) error {
if s.File != nil {
if err := cb(s.File); err != nil {
return err
}
}
for _, child := range s.Children {
if err := child.ForEachFile(cb); err != nil {
return err
}
}
return nil
}

View File

@ -294,24 +294,43 @@ func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error {
return gui.selectFile(false) return gui.selectFile(false)
} }
func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleIgnoreFile() error {
file := gui.getSelectedFile() node := gui.getSelectedStatusNode()
if file == nil { if node == nil {
return nil return nil
} }
if file.Name == ".gitignore" {
if node.GetPath() == ".gitignore" {
return gui.createErrorPanel("Cannot ignore .gitignore") return gui.createErrorPanel("Cannot ignore .gitignore")
} }
if file.Tracked { unstageFiles := func() error {
return node.ForEachFile(func(file *models.File) error {
if file.HasStagedChanges {
if err := gui.GitCommand.UnStageFile(file.Name, file.Tracked); err != nil {
return err
}
}
return nil
})
}
if node.GetIsTracked() {
return gui.ask(askOpts{ return gui.ask(askOpts{
title: gui.Tr.IgnoreTracked, title: gui.Tr.IgnoreTracked,
prompt: gui.Tr.IgnoreTrackedPrompt, prompt: gui.Tr.IgnoreTrackedPrompt,
handleConfirm: func() error { handleConfirm: func() error {
if err := gui.GitCommand.Ignore(file.Name); err != nil { // not 100% sure if this is necessary but I'll assume it is
if err := unstageFiles(); err != nil {
return err return err
} }
if err := gui.GitCommand.RemoveTrackedFiles(file.Name); err != nil {
if err := gui.GitCommand.RemoveTrackedFiles(node.GetPath()); err != nil {
return err
}
if err := gui.GitCommand.Ignore(node.GetPath()); err != nil {
return err return err
} }
return gui.refreshSidePanels(refreshOptions{scope: []int{FILES}}) return gui.refreshSidePanels(refreshOptions{scope: []int{FILES}})
@ -319,7 +338,11 @@ func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error {
}) })
} }
if err := gui.GitCommand.Ignore(file.Name); err != nil { if err := unstageFiles(); err != nil {
return err
}
if err := gui.GitCommand.Ignore(node.GetPath()); err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }

View File

@ -430,7 +430,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "files", ViewName: "files",
Contexts: []string{FILES_CONTEXT_KEY}, Contexts: []string{FILES_CONTEXT_KEY},
Key: gui.getKey(config.Files.IgnoreFile), Key: gui.getKey(config.Files.IgnoreFile),
Handler: gui.handleIgnoreFile, Handler: gui.wrappedHandler(gui.handleIgnoreFile),
Description: gui.Tr.LcIgnoreFile, Description: gui.Tr.LcIgnoreFile,
}, },
{ {