sync-to-codex-plugin.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #!/usr/bin/env bash
  2. #
  3. # sync-to-codex-plugin.sh
  4. #
  5. # Sync this superpowers checkout → prime-radiant-inc/openai-codex-plugins.
  6. # Clones the fork fresh into a temp dir, rsyncs tracked upstream plugin content
  7. # (including committed Codex files under .codex-plugin/ and assets/), preserves
  8. # OpenAI-owned marketplace metadata already in the destination plugin, commits,
  9. # pushes a sync branch, and opens a PR.
  10. # Path/user agnostic — auto-detects upstream from script location.
  11. #
  12. # Deterministic: running twice against the same upstream SHA produces PRs with
  13. # identical diffs, so two back-to-back runs can verify the tool itself.
  14. #
  15. # Usage:
  16. # ./scripts/sync-to-codex-plugin.sh # full run
  17. # ./scripts/sync-to-codex-plugin.sh -n # dry run
  18. # ./scripts/sync-to-codex-plugin.sh -y # skip confirm
  19. # ./scripts/sync-to-codex-plugin.sh --local PATH # existing checkout
  20. # ./scripts/sync-to-codex-plugin.sh --base BRANCH # default: main
  21. # ./scripts/sync-to-codex-plugin.sh --bootstrap # create plugin dir if missing
  22. #
  23. # Bootstrap mode: skips the "plugin must exist on base" requirement and creates
  24. # plugins/superpowers/ when absent, then copies the tracked plugin files from
  25. # upstream just like a normal sync.
  26. #
  27. # Requires: bash, rsync, git, gh (authenticated), python3.
  28. set -euo pipefail
  29. # =============================================================================
  30. # Config — edit as upstream or canonical plugin shape evolves
  31. # =============================================================================
  32. FORK="prime-radiant-inc/openai-codex-plugins"
  33. DEFAULT_BASE="main"
  34. DEST_REL="plugins/superpowers"
  35. # Paths in upstream that should NOT land in the embedded plugin.
  36. # All patterns use a leading "/" to anchor them to the source root.
  37. # Unanchored patterns like "scripts/" would match any directory named
  38. # "scripts" at any depth — including legitimate nested dirs like
  39. # skills/brainstorming/scripts/. Anchoring prevents that.
  40. # (.DS_Store is intentionally unanchored — Finder creates them everywhere.)
  41. EXCLUDES=(
  42. # Dotfiles and infra — top-level only
  43. "/.claude/"
  44. "/.claude-plugin/"
  45. "/.codex/"
  46. "/.cursor-plugin/"
  47. "/.git/"
  48. "/.gitattributes"
  49. "/.github/"
  50. "/.gitignore"
  51. "/.opencode/"
  52. "/.pi/"
  53. "/.version-bump.json"
  54. "/.worktrees/"
  55. ".DS_Store"
  56. # Root ceremony files
  57. "/AGENTS.md"
  58. "/CHANGELOG.md"
  59. "/CLAUDE.md"
  60. "/GEMINI.md"
  61. "/RELEASE-NOTES.md"
  62. "/gemini-extension.json"
  63. "/package.json"
  64. # Directories not shipped by canonical Codex plugins
  65. "/commands/"
  66. "/docs/"
  67. "/evals/"
  68. "/lib/"
  69. "/scripts/"
  70. "/tests/"
  71. "/tmp/"
  72. )
  73. # =============================================================================
  74. # Ignored-path helpers
  75. # =============================================================================
  76. IGNORED_DIR_EXCLUDES=()
  77. path_has_directory_exclude() {
  78. local path="$1"
  79. local dir
  80. if [[ ${#IGNORED_DIR_EXCLUDES[@]} -eq 0 ]]; then
  81. return 1
  82. fi
  83. for dir in "${IGNORED_DIR_EXCLUDES[@]}"; do
  84. [[ "$path" == "$dir"* ]] && return 0
  85. done
  86. return 1
  87. }
  88. ignored_directory_has_tracked_descendants() {
  89. local path="$1"
  90. [[ -n "$(git -C "$UPSTREAM" ls-files --cached -- "$path/")" ]]
  91. }
  92. append_git_ignored_directory_excludes() {
  93. local path
  94. local lookup_path
  95. while IFS= read -r -d '' path; do
  96. [[ "$path" == */ ]] || continue
  97. lookup_path="${path%/}"
  98. if ! ignored_directory_has_tracked_descendants "$lookup_path"; then
  99. IGNORED_DIR_EXCLUDES+=("$path")
  100. RSYNC_ARGS+=(--exclude="/$path")
  101. fi
  102. done < <(git -C "$UPSTREAM" ls-files --others --ignored --exclude-standard --directory -z)
  103. }
  104. append_git_ignored_file_excludes() {
  105. local path
  106. while IFS= read -r -d '' path; do
  107. path_has_directory_exclude "$path" && continue
  108. RSYNC_ARGS+=(--exclude="/$path")
  109. done < <(git -C "$UPSTREAM" ls-files --others --ignored --exclude-standard -z)
  110. }
  111. # =============================================================================
  112. # Args
  113. # =============================================================================
  114. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  115. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  116. BASE="$DEFAULT_BASE"
  117. DRY_RUN=0
  118. YES=0
  119. LOCAL_CHECKOUT=""
  120. BOOTSTRAP=0
  121. usage() {
  122. sed -n '/^# Usage:/,/^# Requires:/s/^# \{0,1\}//p' "$0"
  123. exit "${1:-0}"
  124. }
  125. while [[ $# -gt 0 ]]; do
  126. case "$1" in
  127. -n|--dry-run) DRY_RUN=1; shift ;;
  128. -y|--yes) YES=1; shift ;;
  129. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  130. --base) BASE="$2"; shift 2 ;;
  131. --bootstrap) BOOTSTRAP=1; shift ;;
  132. -h|--help) usage 0 ;;
  133. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  134. esac
  135. done
  136. # =============================================================================
  137. # Preflight
  138. # =============================================================================
  139. die() { echo "ERROR: $*" >&2; exit 1; }
  140. command -v rsync >/dev/null || die "rsync not found in PATH"
  141. command -v git >/dev/null || die "git not found in PATH"
  142. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  143. command -v python3 >/dev/null || die "python3 not found in PATH"
  144. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  145. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  146. [[ -f "$UPSTREAM/.codex-plugin/plugin.json" ]] || die "committed Codex manifest missing at $UPSTREAM/.codex-plugin/plugin.json"
  147. # Read the upstream version from the committed Codex manifest.
  148. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/.codex-plugin/plugin.json")"
  149. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from committed Codex manifest"
  150. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  151. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  152. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  153. confirm() {
  154. [[ $YES -eq 1 ]] && return 0
  155. read -rp "$1 [y/N] " ans
  156. [[ "$ans" == "y" || "$ans" == "Y" ]]
  157. }
  158. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  159. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  160. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  161. fi
  162. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  163. if [[ -n "$UPSTREAM_STATUS" ]]; then
  164. echo "WARNING: upstream has uncommitted changes:"
  165. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  166. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  167. confirm "Continue anyway?" || exit 1
  168. fi
  169. # =============================================================================
  170. # Prepare destination (clone fork fresh, or use --local)
  171. # =============================================================================
  172. CLEANUP_DIR=""
  173. cleanup() {
  174. if [[ -n "$CLEANUP_DIR" ]]; then
  175. rm -rf "$CLEANUP_DIR"
  176. fi
  177. }
  178. trap cleanup EXIT
  179. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  180. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  181. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  182. else
  183. echo "Cloning $FORK..."
  184. CLEANUP_DIR="$(mktemp -d)"
  185. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  186. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  187. fi
  188. DEST="$DEST_REPO/$DEST_REL"
  189. PREVIEW_REPO="$DEST_REPO"
  190. PREVIEW_DEST="$DEST"
  191. SYNC_SOURCE=""
  192. overlay_destination_paths() {
  193. local repo="$1"
  194. local path
  195. local source_path
  196. local preview_path
  197. while IFS= read -r -d '' path; do
  198. source_path="$repo/$path"
  199. preview_path="$PREVIEW_REPO/$path"
  200. if [[ -e "$source_path" ]]; then
  201. mkdir -p "$(dirname "$preview_path")"
  202. cp -R "$source_path" "$preview_path"
  203. else
  204. rm -rf "$preview_path"
  205. fi
  206. done
  207. }
  208. copy_local_destination_overlay() {
  209. overlay_destination_paths "$DEST_REPO" < <(
  210. git -C "$DEST_REPO" diff --name-only -z -- "$DEST_REL"
  211. )
  212. overlay_destination_paths "$DEST_REPO" < <(
  213. git -C "$DEST_REPO" diff --cached --name-only -z -- "$DEST_REL"
  214. )
  215. overlay_destination_paths "$DEST_REPO" < <(
  216. git -C "$DEST_REPO" ls-files --others --exclude-standard -z -- "$DEST_REL"
  217. )
  218. overlay_destination_paths "$DEST_REPO" < <(
  219. git -C "$DEST_REPO" ls-files --others --ignored --exclude-standard -z -- "$DEST_REL"
  220. )
  221. }
  222. local_checkout_has_uncommitted_destination_changes() {
  223. [[ -n "$(git -C "$DEST_REPO" status --porcelain=1 --untracked-files=all --ignored=matching -- "$DEST_REL")" ]]
  224. }
  225. prepare_preview_checkout() {
  226. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  227. [[ -n "$CLEANUP_DIR" ]] || CLEANUP_DIR="$(mktemp -d)"
  228. PREVIEW_REPO="$CLEANUP_DIR/preview"
  229. git clone -q --no-local "$DEST_REPO" "$PREVIEW_REPO"
  230. PREVIEW_DEST="$PREVIEW_REPO/$DEST_REL"
  231. fi
  232. git -C "$PREVIEW_REPO" checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  233. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  234. copy_local_destination_overlay
  235. fi
  236. if [[ $BOOTSTRAP -ne 1 ]]; then
  237. [[ -d "$PREVIEW_DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — use --bootstrap, or pass --base <branch>"
  238. fi
  239. }
  240. prepare_apply_checkout() {
  241. git -C "$DEST_REPO" checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  242. if [[ $BOOTSTRAP -ne 1 ]]; then
  243. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — use --bootstrap, or pass --base <branch>"
  244. fi
  245. }
  246. apply_to_preview_checkout() {
  247. if [[ $BOOTSTRAP -eq 1 ]]; then
  248. mkdir -p "$PREVIEW_DEST"
  249. fi
  250. rsync "${RSYNC_ARGS[@]}" "$SYNC_SOURCE/" "$PREVIEW_DEST/"
  251. }
  252. preview_checkout_has_changes() {
  253. [[ -n "$(git -C "$PREVIEW_REPO" status --porcelain "$DEST_REL")" ]]
  254. }
  255. prepare_preview_checkout
  256. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  257. if [[ $BOOTSTRAP -eq 1 ]]; then
  258. SYNC_BRANCH="bootstrap/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  259. else
  260. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  261. fi
  262. # =============================================================================
  263. # Build rsync args
  264. # =============================================================================
  265. RSYNC_ARGS=(-av --delete --delete-excluded)
  266. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  267. append_git_ignored_directory_excludes
  268. append_git_ignored_file_excludes
  269. copy_preserved_destination_metadata() {
  270. local destination="$1"
  271. local source="$2"
  272. local path
  273. local rel
  274. [[ -d "$destination/skills" ]] || return 0
  275. while IFS= read -r -d '' path; do
  276. rel="${path#"$destination"/}"
  277. mkdir -p "$source/$(dirname "$rel")"
  278. cp -p "$path" "$source/$rel"
  279. done < <(find "$destination/skills" -path '*/agents/openai.yaml' -type f -print0)
  280. }
  281. prepare_sync_source() {
  282. local destination="$1"
  283. [[ -n "$CLEANUP_DIR" ]] || CLEANUP_DIR="$(mktemp -d)"
  284. SYNC_SOURCE="$CLEANUP_DIR/source-overlay"
  285. rm -rf "$SYNC_SOURCE"
  286. mkdir -p "$SYNC_SOURCE"
  287. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$SYNC_SOURCE/" >/dev/null
  288. copy_preserved_destination_metadata "$destination" "$SYNC_SOURCE"
  289. }
  290. prepare_sync_source "$PREVIEW_DEST"
  291. # =============================================================================
  292. # Dry run preview (always shown)
  293. # =============================================================================
  294. echo ""
  295. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  296. echo "Version: $UPSTREAM_VERSION"
  297. echo "Fork: $FORK"
  298. echo "Base: $BASE"
  299. echo "Branch: $SYNC_BRANCH"
  300. if [[ $BOOTSTRAP -eq 1 ]]; then
  301. echo "Mode: BOOTSTRAP (creating plugins/superpowers/ when absent)"
  302. fi
  303. echo ""
  304. echo "=== Preview (rsync --dry-run) ==="
  305. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$SYNC_SOURCE/" "$PREVIEW_DEST/"
  306. echo "=== End preview ==="
  307. echo ""
  308. if [[ $DRY_RUN -eq 1 ]]; then
  309. echo ""
  310. echo "Dry run only. Nothing was changed or pushed."
  311. exit 0
  312. fi
  313. # =============================================================================
  314. # Apply
  315. # =============================================================================
  316. echo ""
  317. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  318. echo ""
  319. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  320. if local_checkout_has_uncommitted_destination_changes; then
  321. die "local checkout has uncommitted changes under '$DEST_REL' — commit, stash, or discard them before syncing"
  322. fi
  323. apply_to_preview_checkout
  324. if ! preview_checkout_has_changes; then
  325. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  326. exit 0
  327. fi
  328. fi
  329. prepare_apply_checkout
  330. cd "$DEST_REPO"
  331. git checkout -q -b "$SYNC_BRANCH"
  332. echo "Syncing upstream content..."
  333. if [[ $BOOTSTRAP -eq 1 ]]; then
  334. mkdir -p "$DEST"
  335. fi
  336. rsync "${RSYNC_ARGS[@]}" "$SYNC_SOURCE/" "$DEST/"
  337. # Bail early if nothing actually changed
  338. cd "$DEST_REPO"
  339. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  340. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  341. exit 0
  342. fi
  343. # =============================================================================
  344. # Commit, push, open PR
  345. # =============================================================================
  346. git add "$DEST_REL"
  347. if [[ $BOOTSTRAP -eq 1 ]]; then
  348. COMMIT_TITLE="bootstrap superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  349. PR_BODY="Initial bootstrap of the superpowers plugin from upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  350. Creates \`plugins/superpowers/\` by copying the tracked plugin files from upstream, including \`.codex-plugin/plugin.json\`, \`assets/\`, and \`hooks/\`.
  351. Run via: \`scripts/sync-to-codex-plugin.sh --bootstrap\`
  352. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  353. This is a one-time bootstrap. Subsequent syncs will be normal (non-bootstrap) runs using the same tracked upstream plugin files."
  354. else
  355. COMMIT_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  356. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  357. Copies the tracked plugin files from upstream, including the committed Codex manifest, assets, and hooks.
  358. Run via: \`scripts/sync-to-codex-plugin.sh\`
  359. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  360. Running the sync tool again against the same upstream SHA should produce a PR with an identical diff — use that to verify the tool is behaving."
  361. fi
  362. git commit --quiet -m "$COMMIT_TITLE
  363. Automated sync via scripts/sync-to-codex-plugin.sh
  364. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  365. Branch: $SYNC_BRANCH"
  366. echo "Pushing $SYNC_BRANCH to $FORK..."
  367. git push -u origin "$SYNC_BRANCH" --quiet
  368. echo "Opening PR..."
  369. PR_URL="$(gh pr create \
  370. --repo "$FORK" \
  371. --base "$BASE" \
  372. --head "$SYNC_BRANCH" \
  373. --title "$COMMIT_TITLE" \
  374. --body "$PR_BODY")"
  375. PR_NUM="${PR_URL##*/}"
  376. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  377. echo ""
  378. echo "PR opened: $PR_URL"
  379. echo "Diff view: $DIFF_URL"