sync-to-codex-plugin.sh 15 KB

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