1
0

sync-to-codex-plugin.sh 15 KB

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