1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

support discarding changes in dir

This commit is contained in:
Jesse Duffield
2021-03-15 22:29:34 +11:00
parent f871724ae6
commit 418621a9ff
4 changed files with 106 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package commands
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
@ -140,6 +141,47 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error {
return c.DiscardUnstagedFileChanges(file)
}
func (c *GitCommand) DiscardAllDirChanges(node *models.StatusLineNode) error {
if err := c.RemoveUntrackedDirFiles(node); err != nil {
return err
}
quotedPath := c.OSCommand.Quote(node.GetPath())
if err := c.OSCommand.RunCommand("git checkout HEAD -- %s", quotedPath); err != nil {
return err
}
return nil
}
func (c *GitCommand) DiscardUnstagedDirChanges(node *models.StatusLineNode) error {
if err := c.RemoveUntrackedDirFiles(node); err != nil {
return err
}
quotedPath := c.OSCommand.Quote(node.GetPath())
if err := c.OSCommand.RunCommand("git checkout -- %s", quotedPath); err != nil {
return err
}
return nil
}
func (c *GitCommand) RemoveUntrackedDirFiles(node *models.StatusLineNode) error {
untrackedFilePaths := node.GetPathsMatching(
func(n *models.StatusLineNode) bool { return n.File != nil && !n.File.GetIsTracked() },
)
for _, path := range untrackedFilePaths {
err := os.Remove(path)
if err != nil {
return err
}
}
return nil
}
// DiscardUnstagedFileChanges directly
func (c *GitCommand) DiscardUnstagedFileChanges(file *models.File) error {
quotedFileName := c.OSCommand.Quote(file.Name)

View File

@ -188,3 +188,18 @@ func (s *StatusLineNode) compressAux() *StatusLineNode {
func (s *StatusLineNode) HasExactlyOneChild() bool {
return len(s.Children) == 1
}
// This ignores the root
func (s *StatusLineNode) GetPathsMatching(test func(*StatusLineNode) bool) []string {
paths := []string{}
if test(s) {
paths = append(paths, s.GetPath())
}
for _, child := range s.Children {
paths = append(paths, child.GetPathsMatching(test)...)
}
return paths
}