small updates to git funcs

This commit is contained in:
RingOfStorms (Joshua Bell) 2025-09-19 17:07:19 -05:00
parent ecddf8ad8d
commit 41ea974e49
2 changed files with 43 additions and 15 deletions

View file

@ -76,20 +76,48 @@ branchdel() {
fi
echo "Removing worktree at: $target_wt"
if git -C "$repo_dir" worktree remove "$target_wt" 2>/dev/null; then
rm -rf -- "$target_wt" 2>/dev/null || true
echo "Removed worktree: $target_wt"
# delete local branch if it exists
if git -C "$repo_dir" show-ref --verify --quiet "refs/heads/$branch"; then
git -C "$repo_dir" branch -D "$branch" 2>/dev/null || true
echo "Deleted local branch: $branch"
# helper: attempt a guarded, forceful removal of a directory
remove_dir_forcefully() {
local dir="$1"
if [ -z "$dir" ]; then
return 1
fi
return 0
fi
# try with --force as a fallback
if git -C "$repo_dir" worktree remove --force "$target_wt" 2>/dev/null; then
rm -rf -- "$target_wt" 2>/dev/null || true
echo "Removed worktree (forced): $target_wt"
# resolve absolute paths
local abs_dir abs_repo
abs_dir=$(readlink -f -- "$dir" 2>/dev/null) || abs_dir="$dir"
abs_repo=$(readlink -f -- "$repo_dir" 2>/dev/null) || abs_repo="$repo_dir"
# safety checks: do not remove repository root or /
if [ "$abs_dir" = "/" ] || [ "$abs_dir" = "$abs_repo" ]; then
echo "Refusing to remove unsafe path: $abs_dir" >&2
return 1
fi
# try plain rm -rf
rm -rf -- "$abs_dir" 2>/dev/null && return 0
# fix permissions then try again
chmod -R u+rwx "$abs_dir" 2>/dev/null || true
rm -rf -- "$abs_dir" 2>/dev/null && return 0
# try removing contents first, then remove directory
if find "$abs_dir" -mindepth 1 -exec rm -rf -- {} + 2>/dev/null; then
rmdir "$abs_dir" 2>/dev/null || true
fi
# final existence check
[ ! -e "$abs_dir" ]
}
# try unregistering the worktree, prefer normal then fallback to --force
if git -C "$repo_dir" worktree remove "$target_wt" 2>/dev/null || git -C "$repo_dir" worktree remove --force "$target_wt" 2>/dev/null; then
if remove_dir_forcefully "$target_wt"; then
echo "Removed worktree: $target_wt"
else
echo "Worktree removed from git, but failed to fully delete directory: $target_wt" >&2
echo "Attempted to force-delete; you may need to remove it manually with sudo." >&2
fi
# delete local branch if it exists
if git -C "$repo_dir" show-ref --verify --quiet "refs/heads/$branch"; then
git -C "$repo_dir" branch -D "$branch" 2>/dev/null || true