sync-to-codex-plugin.sh 15 KB

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