jj-ws-add (974B)
1 #!/usr/bin/env zsh 2 set -euo pipefail 3 4 if (( # == 0 )); then 5 print -u2 "Usage: jj ws-add <destination_path>" 6 exit 1 7 fi 8 9 # Resolve absolute destination path and workspace name. 10 DEST_ABS="${1:a}" 11 WS_NAME="${DEST_ABS:t}" 12 13 # Move to current workspace root to ensure Git commands find the primary repo. 14 # We do this in case we're already in a workspace. 15 cd "$JJ_WORKSPACE_ROOT" 16 MAIN_ABS="$PWD" 17 18 # Create the new jj workspace. 19 jj workspace add "$DEST_ABS" 20 21 # Create a temporary Git worktree. 22 TEMP_GIT="${DEST_ABS}_temp_git" 23 git worktree add --detach --no-checkout "$TEMP_GIT" HEAD >/dev/null 2>&1 24 25 # Align Git worktree metadata and link pointers permanently. 26 mv ".git/worktrees/${TEMP_GIT:t}" ".git/worktrees/$WS_NAME" 27 print "gitdir: $MAIN_ABS/.git/worktrees/$WS_NAME" > "$DEST_ABS/.git" 28 print "$DEST_ABS/.git" > ".git/worktrees/$WS_NAME/gitdir" 29 rm -rf "$TEMP_GIT/.git" 30 rmdir "$TEMP_GIT" 31 32 # Populate Git's index for the new workspace. 33 git -C "$DEST_ABS" reset >/dev/null 2>&1