Skip to content

Add option approximating git clean -x flag. #995

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

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,10 @@ const (

// CleanOptions describes how a clean should be performed.
type CleanOptions struct {
// Dir recurses into nested directories.
Dir bool
// All removes all changes, even those excluded by gitignore.
All bool
}

// GrepOptions describes how a grep should be performed.
Expand Down
7 changes: 4 additions & 3 deletions worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,11 @@ func (w *Worktree) Clean(opts *CleanOptions) error {
if err != nil {
return err
}
m := gitignore.NewMatcher([]gitignore.Pattern{})
return w.doClean(s, opts, root, files)
}

func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files []os.FileInfo) error {
func (w *Worktree) doClean(status Status, matcher gitignore.Matcher, opts *CleanOptions, dir string, files []os.FileInfo) error {
for _, fi := range files {
if fi.Name() == GitDirName {
continue
Expand All @@ -881,12 +882,12 @@ func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files
if err != nil {
return err
}
err = w.doClean(status, opts, path, subfiles)
err = w.doClean(status, matcher, opts, path, subfiles)
if err != nil {
return err
}
} else {
if status.IsUntracked(path) {
if status.IsUntracked(path) || (opts.All && matcher.Match(strings.Split(path, string(os.PathSeparator)), false)) {
if err := w.Filesystem.Remove(path); err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ func (w *Worktree) diffStagingWithWorktree(reverse, excludeIgnoredChanges bool)
return c, nil
}

func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {
func (w *Worktree) gitignoreMatcher() (gitignore.Matcher, error) {
patterns, err := gitignore.ReadPatterns(w.Filesystem, nil)
if err != nil {
return changes
return nil, err
}

patterns = append(patterns, w.Excludes...)

if len(patterns) == 0 {
return gitignore.NewMatcher(patterns), nil
}

func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {
m, err := w.gitignoreMatcher()
if err != nil {
return changes
}

m := gitignore.NewMatcher(patterns)

var res merkletrie.Changes
for _, ch := range changes {
var path []string
Expand Down
39 changes: 39 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,45 @@ func (s *WorktreeSuite) TestClean(c *C) {
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.")
}

func (s *WorktreeSuite) TestCleanAll(c *C) {
fs := fixtures.Basic().ByTag("worktree").One().Worktree()
r, err := PlainOpen(fs.Root())
c.Assert(err, IsNil)
w, err := r.Worktree()
c.Assert(err, IsNil)

err = util.WriteFile(w.Filesystem, ".gitignore", []byte("foo\n"), 0755)
c.Assert(err, IsNil)

_, err = w.Add(".")
c.Assert(err, IsNil)

commitOpts := &CommitOptions{Author: &object.Signature{Name: "foo", Email: "foo@foo.foo", When: time.Now()}}
_, err = w.Commit("Add gitignore", commitOpts)
c.Assert(err, IsNil)

status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(len(status), Equals, 0)

err = util.WriteFile(w.Filesystem, "foo", []byte("foo\n"), 0755)
c.Assert(err, IsNil)

status, err = w.Status()
c.Assert(err, IsNil)
c.Assert(len(status), Equals, 0)

err = w.Clean(&CleanOptions{All: true, Dir: true})
c.Assert(err, IsNil)

status, err = w.Status()
c.Assert(err, IsNil)
c.Assert(len(status), Equals, 0)

_, err = fs.Lstat("foo")
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.")
}

func (s *WorktreeSuite) TestCleanBare(c *C) {
storer := memory.NewStorage()

Expand Down