Add worktrees functions
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
# Create a new worktree and branch from within current git directory.
|
||||||
|
ga() {
|
||||||
|
if [[ -z "$1" ]]; then
|
||||||
|
echo "Usage: ga [branch name]"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local branch="$1"
|
||||||
|
local base="$(basename "$PWD")"
|
||||||
|
local path="../${base}--${branch}"
|
||||||
|
|
||||||
|
git worktree add -b "$branch" "$path"
|
||||||
|
mise trust "$path"
|
||||||
|
cd "$path"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Remove worktree and branch from within active worktree directory.
|
||||||
|
gd() {
|
||||||
|
if gum confirm "Remove worktree and branch?"; then
|
||||||
|
local cwd base branch root
|
||||||
|
|
||||||
|
cwd="$(pwd)"
|
||||||
|
worktree="$(basename "$cwd")"
|
||||||
|
|
||||||
|
# split on first `--`
|
||||||
|
root="${worktree%%--*}"
|
||||||
|
branch="${worktree#*--}"
|
||||||
|
|
||||||
|
# Protect against accidentially nuking a non-worktree directory
|
||||||
|
if [[ "$root" != "$worktree" ]]; then
|
||||||
|
cd "../$root"
|
||||||
|
git worktree remove "$worktree" --force
|
||||||
|
git branch -D "$branch"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user