#!/usr/bin/env zsh # Checks out the first argument in a worktree at a temporary directory. Then # spawns an interactive shell inside of it. # When the shell closes the worktree is tried to be removed. Until that works # without problems (e.g. dirty), a new shell is spawned to resolve all conflicts # (e.g. stashing). Finally the temporary directory is deleted. # # Instead of dropping in an interactive shell, the commands to execute can be # passed via stdin. # TODO: If any conflicts arise, all further shells should be interactive instead # of looping forever. local REPO_NAME REPO_DIR REPO_NAME="${$(git rev-parse --show-toplevel):t}" || return REPO_DIR="$(mktemp -d -p "" "worktree.XXX.$REPO_NAME.$1")" git worktree add "$REPO_DIR" "$1" [[ -e "$REPO_DIR" ]] || return 1 pushd -q "$REPO_DIR" # Start subshell "$SHELL" errc=$? # 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" errc=$? popd -q done git worktree prune return $errc