|
| 1 | +# Sync Upstream |
| 2 | + |
| 3 | +Sync code from the upstream llm-d org to a user's fork and open a PR to opendatahub-io. |
| 4 | + |
| 5 | +## Input |
| 6 | + |
| 7 | +Ask the user: **"Sync to upstream/main HEAD or a specific commit SHA?"** |
| 8 | + |
| 9 | +- If the user provides a **commit SHA**, use that as the target commit |
| 10 | +- If no SHA is given, default to `upstream/main` HEAD |
| 11 | + |
| 12 | +## Workflow |
| 13 | + |
| 14 | +1. **Pre-flight checks**: Verify `origin` remote points to the user's fork (not upstream or opendatahub) |
| 15 | +2. **Fetch remotes**: Add/update `upstream` and `opendatahub` remotes, fetch `upstream/main` and `opendatahub/main` |
| 16 | +3. **Resolve target commit**: Use the user-provided SHA, or default to `upstream/main` HEAD. Verify it exists on `upstream/main` |
| 17 | +4. **Check for duplicates**: If a sync branch or PR for this SHA already exists, inform the user and stop |
| 18 | +5. **Create sync branch**: Create `sync/upstream-<short_sha>` based on `opendatahub/main` |
| 19 | +6. **Merge upstream**: Merge the target upstream commit into the sync branch. Resolve conflicts if needed |
| 20 | +7. **Push branch**: Push to `origin` |
| 21 | +8. **Confirm PR creation**: Ask the user whether to open a PR or stop with the branch pushed only |
| 22 | +9. **Open PR** (if confirmed): Open a PR to `opendatahub-io/workload-variant-autoscaler` targeting `main` |
| 23 | + |
| 24 | +## Commands |
| 25 | + |
| 26 | +```bash |
| 27 | +# 0. Save current branch to restore later |
| 28 | +ORIGINAL_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 29 | + |
| 30 | +# 1. Pre-flight: verify origin is the user's fork |
| 31 | +ORIGIN_URL=$(git remote get-url origin) |
| 32 | +if echo "${ORIGIN_URL}" | grep -qE '(llm-d/llm-d-workload-variant-autoscaler|opendatahub-io/workload-variant-autoscaler)'; then |
| 33 | + echo "Error: origin remote points to upstream or opendatahub, not your fork" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# 2. Set up remotes and fetch |
| 38 | +git remote add upstream https://github.com/llm-d/llm-d-workload-variant-autoscaler.git 2>/dev/null || true |
| 39 | +git remote set-url upstream https://github.com/llm-d/llm-d-workload-variant-autoscaler.git |
| 40 | +git remote add opendatahub https://github.com/opendatahub-io/workload-variant-autoscaler.git 2>/dev/null || true |
| 41 | +git remote set-url opendatahub https://github.com/opendatahub-io/workload-variant-autoscaler.git |
| 42 | +git fetch upstream main |
| 43 | +git fetch opendatahub main |
| 44 | + |
| 45 | +# 3. Resolve target commit |
| 46 | +TARGET_COMMIT="${USER_SHA:-upstream/main}" |
| 47 | +FULL_SHA=$(git rev-parse "${TARGET_COMMIT}") |
| 48 | +SHORT_SHA=$(git rev-parse --short "${TARGET_COMMIT}") |
| 49 | +git merge-base --is-ancestor "${FULL_SHA}" upstream/main || { echo "Error: commit not on upstream/main"; exit 1; } |
| 50 | + |
| 51 | +# 4. Check for duplicates |
| 52 | +BRANCH="sync/upstream-${SHORT_SHA}" |
| 53 | +if git show-ref --verify --quiet "refs/heads/${BRANCH}" || git show-ref --verify --quiet "refs/remotes/origin/${BRANCH}"; then |
| 54 | + echo "Warning: branch ${BRANCH} already exists" |
| 55 | + # Ask user whether to force-update or skip |
| 56 | +fi |
| 57 | + |
| 58 | +# 5. Create branch from opendatahub/main |
| 59 | +git checkout -b "${BRANCH}" opendatahub/main |
| 60 | + |
| 61 | +# 6. Merge upstream commit into the sync branch |
| 62 | +git merge --no-ff "${FULL_SHA}" --no-edit -m "Sync upstream llm-d/llm-d-workload-variant-autoscaler ${SHORT_SHA}" |
| 63 | +``` |
| 64 | + |
| 65 | +## Conflict Resolution |
| 66 | + |
| 67 | +If step 6 produces merge conflicts: |
| 68 | + |
| 69 | +1. List conflicted files with `git diff --name-only --diff-filter=U` |
| 70 | +2. Show the conflicts to the user |
| 71 | +3. Attempt to resolve trivial conflicts automatically (whitespace, import order) |
| 72 | +4. For non-trivial conflicts, show the diff and ask the user how to resolve each file |
| 73 | +5. After all conflicts are resolved: |
| 74 | + ```bash |
| 75 | + git add -u |
| 76 | + git commit --no-edit |
| 77 | + ``` |
| 78 | + |
| 79 | +## Push and Open PR |
| 80 | + |
| 81 | +```bash |
| 82 | +# 7. Push to origin (user's fork) |
| 83 | +git push -u origin "${BRANCH}" |
| 84 | +``` |
| 85 | + |
| 86 | +**Before creating the PR, ask the user whether they want to open a PR to opendatahub-io or stop here (branch pushed only).** |
| 87 | + |
| 88 | +If the user chooses to open the PR: |
| 89 | + |
| 90 | +```bash |
| 91 | +# 8. Open PR to opendatahub-io |
| 92 | +FORK_OWNER=$(git remote get-url origin | sed -E 's|.*[:/]([^/]+)/[^/]+(.git)?$|\1|') |
| 93 | + |
| 94 | +gh pr create \ |
| 95 | + --repo opendatahub-io/workload-variant-autoscaler \ |
| 96 | + --base main \ |
| 97 | + --head "${FORK_OWNER}:${BRANCH}" \ |
| 98 | + --title "[sync] upstream llm-d main branch ${SHORT_SHA} [$(date -u +%Y-%m-%d)]" \ |
| 99 | + --body "Syncs llm-d/llm-d-workload-variant-autoscaler main branch into ODH main branch. |
| 100 | +
|
| 101 | +Upstream commit: https://github.com/llm-d/llm-d-workload-variant-autoscaler/commit/${FULL_SHA}" |
| 102 | + |
| 103 | +# 9. Return to the original branch |
| 104 | +git checkout "${ORIGINAL_BRANCH}" |
| 105 | +``` |
| 106 | + |
| 107 | +If `gh pr create` fails, inform the user that the branch has been pushed to `origin/${BRANCH}` and ask them to create the PR manually at `https://github.com/opendatahub-io/workload-variant-autoscaler/compare/main...${FORK_OWNER}:${BRANCH}`. |
| 108 | + |
| 109 | +## Error Handling |
| 110 | + |
| 111 | +- If the user-provided SHA does not exist on `upstream/main`, report the error and ask for a valid SHA |
| 112 | +- If conflicts cannot be resolved, abort the merge (`git merge --abort`), clean up (`git checkout "${ORIGINAL_BRANCH}" && git branch -D "${BRANCH}"`), and inform the user |
| 113 | +- If the branch already exists, ask the user whether to force-update or skip |
| 114 | +- On any failure after branch creation, clean up with `git checkout "${ORIGINAL_BRANCH}" && git branch -D "${BRANCH}"` |
| 115 | +- Always return the PR URL on success |
0 commit comments