10000 worktree: add equivalent of git clean -x. · go-git/go-git@fe142dd · GitHub
[go: up one dir, main page]

Skip to content

Commit fe142dd

Browse files
committed
worktree: add equivalent of git clean -x.
1 parent a6e934f commit fe142dd

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,10 @@ const (
685685

686686
// CleanOptions describes how a clean should be performed.
687687
type CleanOptions struct {
688+
// Dir recurses into nested directories.
688689
Dir bool
690+
// All removes all changes, even those excluded by gitignore.
691+
All bool
689692
}
690693

691694
// GrepOptions describes how a grep should be performed.

worktree.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,11 @@ func (w *Worktree) Clean(opts *CleanOptions) error {
861861
if err != nil {
862862
return err
863863
}
864+
m := gitignore.NewMatcher([]gitignore.Pattern{})
864865
return w.doClean(s, opts, root, files)
865866
}
866867

867-
func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files []os.FileInfo) error {
868+
func (w *Worktree) doClean(status Status, matcher gitignore.Matcher, opts *CleanOptions, dir string, files []os.FileInfo) error {
868869
for _, fi := range files {
869870
if fi.Name() == GitDirName {
870871
continue
@@ -881,12 +882,12 @@ func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files
881882
if err != nil {
882883
return err
883884
}
884-
err = w.doClean(status, opts, path, subfiles)
885+
err = w.doClean(status, matcher, opts, path, subfiles)
885886
if err != nil {
886887
return err
887888
}
888889
} else {
889-
if status.IsUntracked(path) {
890+
if status.IsUntracked(path) || (opts.All && matcher.Match(strings.Split(path, string(os.PathSeparator)), false)) {
890891
if err := w.Filesystem.Remove(path); err != nil {
891892
return err
892893
}

worktree_status.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,20 @@ func (w *Worktree) diffStagingWithWorktree(reverse, excludeIgnoredChanges bool)
144144
return c, nil
145145
}
146146

147-
func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {
147+
func (w *Worktree) gitignoreMatcher() (gitignore.Matcher, error) {
148148
patterns, err := gitignore.ReadPatterns(w.Filesystem, nil)
149149
if err != nil {
150-
return changes
150+
return nil, err
151151
}
152-
153152
patterns = append(patterns, w.Excludes...)
154-
155-
if len(patterns) == 0 {
153+
return gitignore.NewMatcher(patterns), nil
154+
}
155+
156+
func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {
157+
m, err := w.gitignoreMatcher()
158+
if err != nil {
156159
return changes
157160
}
158-
159-
m := gitignore.NewMatcher(patterns)
160-
161161
var res merkletrie.Changes
162162
for _, ch := range changes {
163163
var path []string

worktree_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,45 @@ func (s *WorktreeSuite) TestClean(c *C) {
22042204
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.")
22052205
}
22062206

2207+
func (s *WorktreeSuite) TestCleanAll(c *C) {
2208+
fs := fixtures.Basic().ByTag("worktree").One().Worktree()
2209+
r, err := PlainOpen(fs.Root())
2210+
c.Assert(err, IsNil)
2211+
w, err := r.Worktree()
2212+
c.Assert(err, IsNil)
2213+
2214+
err = util.WriteFile(w.Filesystem, ".gitignore", []byte("foo\n"), 0755)
2215+
c.Assert(err, IsNil)
2216+
2217+
_, err = w.Add(".")
2218+
c.Assert(err, IsNil)
2219+
2220+
commitOpts := &CommitOptions{Author: &object.Signature{Name: "foo", Email: "foo@foo.foo", When: time.Now()}}
2221+
_, err = w.Commit("Add gitignore", commitOpts)
2222+
c.Assert(err, IsNil)
2223+
2224+
status, err := w.Status()
2225+
c.Assert(err, IsNil)
2226+
c.Assert(len(status), Equals, 0)
2227+
2228+
err = util.WriteFile(w.Filesystem, "foo", []byte("foo\n"), 0755)
2229+
c.Assert(err, IsNil)
2230+
2231+
status, err = w.Status()
2232+
c.Assert(err, IsNil)
2233+
c.Assert(len(status), Equals, 0)
2234+
2235+
err = w.Clean(&CleanOptions{All: true, Dir: true})
2236+
c.Assert(err, IsNil)
2237+
2238+
status, err = w.Status()
2239+
c.Assert(err, IsNil)
2240+
c.Assert(len(status), Equals, 0)
2241+
2242+
_, err = fs.Lstat("foo")
2243+
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.")
2244+
}
2245+
22072246
func (s *WorktreeSuite) TestCleanBare(c *C) {
22082247
storer := memory.NewStorage()
22092248

0 commit comments

Comments
 (0)
0