Download this file
| 1 | # Sourced by the release scripts: a worktree this run makes, and only that |
| 2 | # one removed at the end. The destination is a fresh, unique, empty |
| 3 | # directory (mktemp), so it can never be someone's existing worktree; the |
| 4 | # ownership flag is set only once `git worktree add` has succeeded, so a |
| 5 | # failed add removes nothing but the empty directory this run made. |
| 6 | # |
| 7 | # own_worktree_add REPO COMMIT PREFIX # sets OWN_WT to its path |
| 8 | # own_worktree_cleanup REPO # in the EXIT trap |
| 9 | OWN_WT="" |
| 10 | OWN_WT_ADDED=0 |
| 11 | OWN_WT_MADE=0 |
| 12 | |
| 13 | own_worktree_add() { |
| 14 | _repo=$1 _commit=$2 _prefix=$3 |
| 15 | OWN_WT=$(mktemp -d "${TMPDIR:-/tmp}/$_prefix.XXXXXX") || return 1 |
| 16 | OWN_WT_MADE=1 |
| 17 | # mktemp made it, empty: anything else there is a collision, refused. |
| 18 | if [ -n "$(ls -A "$OWN_WT")" ]; then echo "worktree: $OWN_WT is not empty" >&2; return 1; fi |
| 19 | git -C "$_repo" worktree add -q --detach "$OWN_WT" "$_commit" || return 1 |
| 20 | OWN_WT_ADDED=1 |
| 21 | } |
| 22 | |
| 23 | own_worktree_cleanup() { |
| 24 | _repo=$1 |
| 25 | if [ "$OWN_WT_ADDED" = 1 ] && [ -n "$OWN_WT" ]; then |
| 26 | git -C "$_repo" worktree remove --force "$OWN_WT" 2>/dev/null || true |
| 27 | elif [ "$OWN_WT_MADE" = 1 ] && [ -n "$OWN_WT" ]; then |
| 28 | rmdir "$OWN_WT" 2>/dev/null || true |
| 29 | fi |
| 30 | } |
| 31 | |