diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-12-04 23:47:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-04 23:47:55 +0100 |
commit | f96d46d38f67604f204711ea0e2832e6e047e2ad (patch) | |
tree | e0c303923da76c8fd6b2471262a657b06181dd2a /worktree.go | |
parent | 02723bf0e5b97fc7855467b2fad700a4f318ab3e (diff) | |
parent | d43f17481253b76b558bfe4cc1e1b17bfb0c74e4 (diff) | |
download | go-git-f96d46d38f67604f204711ea0e2832e6e047e2ad.tar.gz |
Merge pull request #675 from darkowlzz/git-clean
git: worktree, add Clean() method for git clean
Diffstat (limited to 'worktree.go')
-rw-r--r-- | worktree.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/worktree.go b/worktree.go index 67d7f08..e87f567 100644 --- a/worktree.go +++ b/worktree.go @@ -684,6 +684,33 @@ func (w *Worktree) readGitmodulesFile() (*config.Modules, error) { return m, m.Unmarshal(input) } +// Clean the worktree by removing untracked files. +func (w *Worktree) Clean(opts *CleanOptions) error { + s, err := w.Status() + if err != nil { + return err + } + + // Check Worktree status to be Untracked, obtain absolute path and delete. + for relativePath, status := range s { + // Check if the path contains a directory and if Dir options is false, + // skip the path. + if relativePath != filepath.Base(relativePath) && !opts.Dir { + continue + } + + // Remove the file only if it's an untracked file. + if status.Worktree == Untracked { + absPath := filepath.Join(w.Filesystem.Root(), relativePath) + if err := os.Remove(absPath); err != nil { + return err + } + } + } + + return nil +} + func rmFileAndDirIfEmpty(fs billy.Filesystem, name string) error { if err := util.RemoveAll(fs, name); err != nil { return err |