Skip to content

Commit

Permalink
worktree: Don't remove root directory when cleaning (#230)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hansmi committed Jan 27, 2021
1 parent 1b1a61a commit 024d62b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion worktree.go
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions worktree_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 024d62b

Please sign in to comment.