Skip to content

Instantly share code, notes, and snippets.

@claytongentry
Created September 24, 2025 17:30
Show Gist options
  • Select an option

  • Save claytongentry/7db4041d8fba004ae65072dfb539aadd to your computer and use it in GitHub Desktop.

Select an option

Save claytongentry/7db4041d8fba004ae65072dfb539aadd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
if [ $# -ne 1 ]; then
echo "Usage: $0 <branch-name>" >&2
exit 1
fi
if ! command -v codex >/dev/null 2>&1; then
echo "codex CLI not found on PATH; install codex before running this script." >&2
exit 1
fi
branch="$1"
repo_root="$(git rev-parse --show-toplevel)"
worktrees_dir="$repo_root/../wt"
worktree_path="$worktrees_dir/$branch"
codex_branch="codex/$branch"
mkdir -p "$worktrees_dir"
if [ -d "$worktree_path" ]; then
echo "Worktree directory already exists: $worktree_path" >&2
exit 1
fi
if git -C "$repo_root" show-ref --verify --quiet "refs/heads/$codex_branch"; then
echo "Branch already exists locally: $codex_branch" >&2
exit 1
fi
# keep local refs fresh before creating the worktree
if git -C "$repo_root" fetch origin; then
if ! git -C "$repo_root" rev-parse --verify --quiet origin/main >/dev/null; then
echo "origin/main not found; ensure the remote exists before using this script." >&2
exit 1
fi
else
echo "Failed to fetch origin; resolve the issue and retry." >&2
exit 1
fi
git -C "$repo_root" worktree add "$worktree_path" -b "$codex_branch" origin/main
for dir in _build deps; do
src="$repo_root/$dir"
if [ -d "$src" ]; then
cp -R "$src" "$worktree_path"
else
echo "Warning: $dir directory not found in $repo_root; skipping copy." >&2
fi
done
cd "$worktree_path"
echo "Worktree created at $worktree_path"
echo "Launching codex..."
exec codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment