sync-to-codex-plugin.sh 15 KB

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