aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhansmi <hansmi@users.noreply.github.com>2021-01-27 10:49:40 +0100
committerGitHub <noreply@github.com>2021-01-27 10:49:40 +0100
commit024d62b96ac19b40473f04931f9c12c8e24e0f7a (patch)
treeb49e2ad17080f3a1236f0fb502a2895c11ae0073
parent1b1a61ad07f40197d3b9164821a096abd1710628 (diff)
downloadgo-git-024d62b96ac19b40473f04931f9c12c8e24e0f7a.tar.gz
worktree: Don't remove root directory when cleaning (#230)
When using a separate worktree directory while working on a bare repository, cleaning with CleanOptions{Dir: true} would also remove the root worktree directory if empty. Signed-off-by: Michael Hanselmann <public@hansmi.ch>
-rw-r--r--worktree.go2
-rw-r--r--worktree_test.go32
2 files changed, 33 insertions, 1 deletions
diff --git a/worktree.go b/worktree.go
index 73e638a..4aeae01 100644
--- a/worktree.go
+++ b/worktree.go
@@ -771,7 +771,7 @@ func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files
}
}
- if opts.Dir {
+ if opts.Dir && dir != "" {
return doCleanDirectories(w.Filesystem, dir)
}
return nil
diff --git a/worktree_test.go b/worktree_test.go
index 8a7586a..1086735 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -1826,7 +1826,39 @@ func (s *WorktreeSuite) TestClean(c *C) {
// An empty dir should be deleted, as well.
_, err = fs.Lstat("pkgA")
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.")
+}
+
+func (s *WorktreeSuite) TestCleanBare(c *C) {
+ storer := memory.NewStorage()
+
+ r, err := Init(storer, nil)
+ c.Assert(err, IsNil)
+ c.Assert(r, NotNil)
+
+ wtfs := memfs.New()
+
+ err = wtfs.MkdirAll("worktree", os.ModePerm)
+ c.Assert(err, IsNil)
+
+ wtfs, err = wtfs.Chroot("worktree")
+ c.Assert(err, IsNil)
+
+ r, err = Open(storer, wtfs)
+ c.Assert(err, IsNil)
+ wt, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ _, err = wt.Filesystem.Lstat(".")
+ c.Assert(err, IsNil)
+
+ // Clean with Dir: true.
+ err = wt.Clean(&CleanOptions{Dir: true})
+ c.Assert(err, IsNil)
+
+ // Root worktree directory must remain after cleaning
+ _, err = wt.Filesystem.Lstat(".")
+ c.Assert(err, IsNil)
}
func (s *WorktreeSuite) TestAlternatesRepo(c *C) {