Do not remove worktree for default branch

This commit is contained in:
2021-11-26 17:02:07 +01:00
parent b967b6dca3
commit 1e6c9407b6
2 changed files with 22 additions and 1 deletions

View File

@@ -192,6 +192,9 @@ can also use the following:
$ grm wt clean $ 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 ### Converting an existing repository
It is possible to convert an existing directory to a worktree setup, using `grm It is possible to convert an existing directory to a worktree setup, using `grm

View File

@@ -1171,7 +1171,21 @@ pub fn run() {
cmd::WorktreeAction::Clean(_args) => { cmd::WorktreeAction::Clean(_args) => {
let repo = get_repo(&dir); let repo = get_repo(&dir);
let worktrees = get_worktrees(&repo); 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); let repo_dir = &dir.join(&worktree);
if repo_dir.exists() { if repo_dir.exists() {
match remove_worktree(worktree, repo_dir, false, &repo) { match remove_worktree(worktree, repo_dir, false, &repo) {
@@ -1197,6 +1211,7 @@ pub fn run() {
)); ));
} }
} }
for entry in std::fs::read_dir(&dir).unwrap() { for entry in std::fs::read_dir(&dir).unwrap() {
let dirname = path_as_string( let dirname = path_as_string(
&entry &entry
@@ -1209,6 +1224,9 @@ pub fn run() {
if dirname == GIT_MAIN_WORKTREE_DIRECTORY { if dirname == GIT_MAIN_WORKTREE_DIRECTORY {
continue; continue;
} }
if dirname == default_branch_name {
continue;
}
if !&worktrees.contains(&dirname) { if !&worktrees.contains(&dirname) {
print_warning(&format!( print_warning(&format!(
"Found {}, which is not a valid worktree directory!", "Found {}, which is not a valid worktree directory!",