Improve PR migration: fetch branches locally + 3-way merge

Instead of fetching the API diff (which has context-sensitive patches
that break after josh-filtered reset), fetch the archived repo's
branches directly as a second remote and compute the diff locally.
Apply with git apply --3way for resilience against context mismatches.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 20:51:22 +03:00
parent 553f006174
commit fbacec7f6f

View File

@@ -336,7 +336,8 @@ onboard_flow() {
}
# ─── Migrate One PR ──────────────────────────────────────────────
# Applies a PR's diff from the archived repo to the new subrepo.
# Fetches the PR's branch from the archived repo, computes a local diff,
# and applies it to the new subrepo with --3way for resilience.
# Usage: migrate_one_pr <pr_number>
#
# Expects: JOSH_SYNC_TARGET_NAME, SUBREPO_API, SUBREPO_TOKEN, BOT_NAME, BOT_EMAIL loaded
@@ -376,15 +377,7 @@ migrate_one_pr() {
log "INFO" "Migrating PR #${pr_number}: \"${title}\" (${base} <- ${head})"
# 2. Get diff from archived repo
local diff
diff=$(get_pr_diff "$archived_api" "$archived_token" "$pr_number")
if [ -z "$diff" ]; then
log "WARN" "Empty diff for PR #${pr_number} — skipping"
return 1
fi
# 3. Clone new subrepo, apply patch
# 2. Clone new subrepo, add archived repo as second remote
# Save cwd so we can restore it (function runs in caller's shell, not subshell)
local original_dir
original_dir=$(pwd)
@@ -401,10 +394,32 @@ migrate_one_pr() {
git config user.name "$BOT_NAME"
git config user.email "$BOT_EMAIL"
# Build authenticated URL for the archived repo
local archived_url archived_clone_url
archived_url=$(echo "$onboard_state" | jq -r '.archived_url')
if [ "${SUBREPO_AUTH:-https}" = "ssh" ]; then
archived_clone_url="$archived_url"
else
# shellcheck disable=SC2001
archived_clone_url=$(echo "$archived_url" | sed "s|https://|https://${BOT_USER}:${SUBREPO_TOKEN}@|")
fi
# Fetch the PR's head and base branches from the archived repo
git remote add archived "$archived_clone_url"
git fetch archived "$head" "$base" 2>&1 \
|| die "Failed to fetch branches from archived repo"
# 3. Compute diff locally and apply with --3way
git checkout -B "$head" >&2
if echo "$diff" | git apply --check 2>/dev/null; then
echo "$diff" | git apply
local diff
diff=$(git diff "archived/${base}..archived/${head}")
if [ -z "$diff" ]; then
log "WARN" "Empty diff for PR #${pr_number} — skipping"
return 1
fi
if echo "$diff" | git apply --3way 2>&1; then
git add -A
git commit -m "${title}
@@ -429,8 +444,8 @@ Migrated from archived repo PR #${pr_number}" >&2
'.migrated_prs += [{"old_number":$old, "new_number":$new_num, "title":$title}]')
write_onboard_state "$target_name" "$onboard_state"
else
log "ERROR" "Patch doesn't apply cleanly for PR #${pr_number} — skipping"
log "ERROR" "Manual migration needed: get diff from archived repo and resolve conflicts"
log "ERROR" "Could not apply changes for PR #${pr_number} even with 3-way merge"
log "ERROR" "Manual migration needed: branch '${head}' from archived repo"
return 1
fi
}