forked from tomups/worktrees-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwtremove.sh
More file actions
executable file
·100 lines (82 loc) · 2.03 KB
/
wtremove.sh
File metadata and controls
executable file
·100 lines (82 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
# Source: https://github.com/llimllib/personal_code/blob/master/homedir/.local/bin/rmtree
RED="\033[0;31m"
YELLOW="\033[0;33m"
CLEAR="\033[0m"
VERBOSE=
function usage {
cat <<"EOF"
rmtree [-vh] <worktree delete>
remove a worktree from a git repository.
FLAGS:
-h: print this help
-v: verbose mode
EOF
exit 1
}
function die {
# if verbose was set, and we're exiting early, make sure that we set +x to
# stop the shell echoing verbosely
if [ -n "$VERBOSE" ]; then
set +x
fi
printf '%b%s%b\n' "$RED" "$1" "$CLEAR"
exit 1
}
function err {
printf '%b%s%b\n' "$YELLOW" "$1" "$CLEAR"
}
function warn {
printf '%b%s%b\n' "$YELLOW" "$1" "$CLEAR"
}
# rmtree <dir> will remove a worktree's directory, then prune the worktree list
# and delete the branch
function rmtree {
if [ -n "$VERBOSE" ]; then
set -x
fi
# verify that the first argument is a directory that exists, that we want
# to remove
if [ -z "$1" ]; then
die "You must provide a directory name that is a worktree to remove"
fi
is_worktree=$(git rev-parse --is-inside-work-tree)
if $is_worktree; then
parent_dir=".."
else
parent_dir="."
fi
# for each argument, delete the directory and remove the worktree
while [ -n "$1" ]; do
final_dir="$parent_dir/$1"
if [ ! -d "$final_dir" ]; then
err "Unable to find directory $final_dir, skipping"
shift
continue
fi
warn "removing $1"
branch_name=${1//_//}
rm -rf "$final_dir"
git worktree prune && git branch -D "$branch_name"
shift
done
}
while true; do
case $1 in
help | -h | --help)
usage
;;
-v | --verbose)
VERBOSE=true
shift
;;
-m | --main-branch)
MAIN_BRANCH=$2
shift
;;
*)
break
;;
esac
done
rmtree "$@"