# Sourced by the release scripts: a worktree this run makes, and only that # one removed at the end. The destination is a fresh, unique, empty # directory (mktemp), so it can never be someone's existing worktree; the # ownership flag is set only once `git worktree add` has succeeded, so a # failed add removes nothing but the empty directory this run made. # # own_worktree_add REPO COMMIT PREFIX # sets OWN_WT to its path # own_worktree_cleanup REPO # in the EXIT trap OWN_WT="" OWN_WT_ADDED=0 OWN_WT_MADE=0 own_worktree_add() { _repo=$1 _commit=$2 _prefix=$3 OWN_WT=$(mktemp -d "${TMPDIR:-/tmp}/$_prefix.XXXXXX") || return 1 OWN_WT_MADE=1 # mktemp made it, empty: anything else there is a collision, refused. if [ -n "$(ls -A "$OWN_WT")" ]; then echo "worktree: $OWN_WT is not empty" >&2; return 1; fi git -C "$_repo" worktree add -q --detach "$OWN_WT" "$_commit" || return 1 OWN_WT_ADDED=1 } own_worktree_cleanup() { _repo=$1 if [ "$OWN_WT_ADDED" = 1 ] && [ -n "$OWN_WT" ]; then git -C "$_repo" worktree remove --force "$OWN_WT" 2>/dev/null || true elif [ "$OWN_WT_MADE" = 1 ] && [ -n "$OWN_WT" ]; then rmdir "$OWN_WT" 2>/dev/null || true fi }