aboutsummaryrefslogtreecommitdiffstats
path: root/worktree.go
diff options
context:
space:
mode:
authorSunny <me@darkowlzz.space>2017-12-04 19:10:16 +0530
committerSunny <me@darkowlzz.space>2017-12-04 19:10:16 +0530
commite7b02be89f2d98fcfe900d71b3b7ecdca8441ed2 (patch)
tree447fe09072b98181e1721b9eae178cfbca86e139 /worktree.go
parent44c364fe3b7b8cdc0f9623afe870d6781a97ebb4 (diff)
downloadgo-git-e7b02be89f2d98fcfe900d71b3b7ecdca8441ed2.tar.gz
git: worktree, add Clean() method for git clean
This change implement git clean with a `Dir` option. By default, clean removes only the untracked files in the working directory. If `Dir` option is set to true, untracked files under other directories are also cleaned.
Diffstat (limited to 'worktree.go')
-rw-r--r--worktree.go27
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