diff --git a/docs/src/worktrees.md b/docs/src/worktrees.md index fd7380d..c808e7b 100644 --- a/docs/src/worktrees.md +++ b/docs/src/worktrees.md @@ -192,6 +192,9 @@ can also use the following: $ grm wt clean ``` +Note that this will not delete the default branch of the repository. It can of +course still be delete with `grm wt delete` if neccessary. + ### Converting an existing repository It is possible to convert an existing directory to a worktree setup, using `grm diff --git a/src/lib.rs b/src/lib.rs index 4244796..efa5edc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1171,7 +1171,21 @@ pub fn run() { cmd::WorktreeAction::Clean(_args) => { let repo = get_repo(&dir); let worktrees = get_worktrees(&repo); - for worktree in &worktrees { + + let default_branch = match get_default_branch(&repo) { + Ok(branch) => branch, + Err(error) => { + print_error(&format!("Failed getting default branch: {}", error)); + process::exit(1); + } + }; + + let default_branch_name = default_branch.name().unwrap().unwrap(); + + for worktree in worktrees + .iter() + .filter(|worktree| *worktree != default_branch_name) + { let repo_dir = &dir.join(&worktree); if repo_dir.exists() { match remove_worktree(worktree, repo_dir, false, &repo) { @@ -1197,6 +1211,7 @@ pub fn run() { )); } } + for entry in std::fs::read_dir(&dir).unwrap() { let dirname = path_as_string( &entry @@ -1209,6 +1224,9 @@ pub fn run() { if dirname == GIT_MAIN_WORKTREE_DIRECTORY { continue; } + if dirname == default_branch_name { + continue; + } if !&worktrees.contains(&dirname) { print_warning(&format!( "Found {}, which is not a valid worktree directory!",