zsh: Make git-checkout-worktree autoloadable

Transform `git-checkout-worktree` into an autoloadable function.
This commit is contained in:
2022-03-31 15:19:56 +02:00
parent 869a1bce6e
commit 9968edaf09
2 changed files with 30 additions and 30 deletions

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env zsh
# Creates a git worktree checking it out the first argument in a temporary
# directory that is deleted again, if the spawned subshell exits.
local GIT_ROOT TEMP_DIR REPO_DIR
GIT_ROOT="$(basename "$(git rev-parse --show-toplevel)")" || return
TEMP_DIR="$(mktemp -d)"
REPO_DIR="$TEMP_DIR/$GIT_ROOT"
git worktree add "$REPO_DIR" "$1"
[[ -e "$REPO_DIR" ]] || return 1
pushd -q "$REPO_DIR"
# Start subshell
"$SHELL"
# Cleanup when exiting
popd -q
# Restart the subshell until every issue is resolved and the worktree is
# removed
until [[ ! -e "$REPO_DIR" ]] || git worktree remove "$REPO_DIR"; do
pushd -q "$REPO_DIR"
"$SHELL"
popd -q
done
git worktree prune
command rm -rf "$TEMP_DIR"