Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit f96d46d

Browse files
authored
Merge pull request #675 from darkowlzz/git-clean
git: worktree, add Clean() method for git clean
2 parents 02723bf + d43f174 commit f96d46d

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

options.go

+5
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,8 @@ type ListOptions struct {
360360
// Auth credentials, if required, to use with the remote repository.
361361
Auth transport.AuthMethod
362362
}
363+
364+
// CleanOptions describes how a clean should be performed.
365+
type CleanOptions struct {
366+
Dir bool
367+
}

worktree.go

+27
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,33 @@ func (w *Worktree) readGitmodulesFile() (*config.Modules, error) {
684684
return m, m.Unmarshal(input)
685685
}
686686

687+
// Clean the worktree by removing untracked files.
688+
func (w *Worktree) Clean(opts *CleanOptions) error {
689+
s, err := w.Status()
690+
if err != nil {
691+
return err
692+
}
693+
694+
// Check Worktree status to be Untracked, obtain absolute path and delete.
695+
for relativePath, status := range s {
696+
// Check if the path contains a directory and if Dir options is false,
697+
// skip the path.
698+
if relativePath != filepath.Base(relativePath) && !opts.Dir {
699+
continue
700+
}
701+
702+
// Remove the file only if it's an untracked file.
703+
if status.Worktree == Untracked {
704+
absPath := filepath.Join(w.Filesystem.Root(), relativePath)
705+
if err := os.Remove(absPath); err != nil {
706+
return err
707+
}
708+
}
709+
}
710+
711+
return nil
712+
}
713+
687714
func rmFileAndDirIfEmpty(fs billy.Filesystem, name string) error {
688715
if err := util.RemoveAll(fs, name); err != nil {
689716
return err

worktree_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,38 @@ func (s *WorktreeSuite) TestMoveToExistent(c *C) {
12521252
c.Assert(hash.IsZero(), Equals, true)
12531253
c.Assert(err, Equals, ErrDestinationExists)
12541254
}
1255+
1256+
func (s *WorktreeSuite) TestClean(c *C) {
1257+
fs := fixtures.ByTag("dirty").One().Worktree()
1258+
1259+
// Open the repo.
1260+
fs, err := fs.Chroot("repo")
1261+
c.Assert(err, IsNil)
1262+
r, err := PlainOpen(fs.Root())
1263+
c.Assert(err, IsNil)
1264+
1265+
wt, err := r.Worktree()
1266+
c.Assert(err, IsNil)
1267+
1268+
// Status before cleaning.
1269+
status, err := wt.Status()
1270+
c.Assert(len(status), Equals, 2)
1271+
1272+
err = wt.Clean(&CleanOptions{})
1273+
c.Assert(err, IsNil)
1274+
1275+
// Status after cleaning.
1276+
status, err = wt.Status()
1277+
c.Assert(err, IsNil)
1278+
1279+
c.Assert(len(status), Equals, 1)
1280+
1281+
// Clean with Dir: true.
1282+
err = wt.Clean(&CleanOptions{Dir: true})
1283+
c.Assert(err, IsNil)
1284+
1285+
status, err = wt.Status()
1286+
c.Assert(err, IsNil)
1287+
1288+
c.Assert(len(status), Equals, 0)
1289+
}

0 commit comments

Comments
 (0)