aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--options.go5
-rw-r--r--worktree.go27
2 files changed, 32 insertions, 0 deletions
diff --git a/options.go b/options.go
index d2cec4b..e5745ea 100644
--- a/options.go
+++ b/options.go
@@ -360,3 +360,8 @@ type ListOptions struct {
// Auth credentials, if required, to use with the remote repository.
Auth transport.AuthMethod
}
+
+// CleanOptions describes how a clean should be performed.
+type CleanOptions struct {
+ Dir bool
+}
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