-
Notifications
You must be signed in to change notification settings - Fork 0
Fix conflict resolutions stranding the PR when its base branch moved #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b148a85
Record the old base as a parent when re-parenting (--absorbed)
claude 3a48a8a
Apply suggestion from @Phlogistique
Phlogistique 30bdaaa
Apply suggestion from @Phlogistique
Phlogistique 8883585
Apply suggestion from @Phlogistique
Phlogistique ff28812
Apply suggestion from @Phlogistique
Phlogistique File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Replays the incident that motivated --absorbed (scortexio/gh-stack-mv#36): | ||
| # | ||
| # 1. main <- feature1 <- feature2, where feature1 advanced AFTER feature2 | ||
| # forked (in the incident, autorestack itself advanced it when a | ||
| # grandparent PR merged). feature1's tip is therefore NOT an ancestor of | ||
| # feature2. | ||
| # 2. feature1 is squash-merged; the action's re-parent of feature2 conflicts, | ||
| # so the action posts the resolution comment and stops. | ||
| # 3. The user resolves by running the posted re-parent with --absorbed. | ||
| # | ||
| # The resolution must descend from origin/feature1's tip: feature2's PR is | ||
| # still based on feature1 at that point, and GitHub creates no pull_request | ||
| # runs for a PR that conflicts with its base, so without that ancestry the | ||
| # resume event may never fire and the conflict label stays stuck forever. | ||
|
|
||
| set -ueo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| source "$SCRIPT_DIR/../command_utils.sh" | ||
|
|
||
| simulate_push() { | ||
| log_cmd git update-ref "refs/remotes/origin/$1" "$1" | ||
| } | ||
|
|
||
| TEST_REPO=$(mktemp -d) | ||
| cd "$TEST_REPO" | ||
| echo "Created test repo at $TEST_REPO" | ||
|
|
||
| log_cmd git init -b main | ||
| log_cmd git config user.email "test@example.com" | ||
| log_cmd git config user.name "Test User" | ||
|
|
||
| for i in $(seq 1 10); do echo "line $i" >> file.txt; done | ||
| log_cmd git add file.txt | ||
| log_cmd git commit -m "Initial commit" | ||
| simulate_push main | ||
|
|
||
| # feature1: modify line 2 | ||
| log_cmd git checkout -b feature1 | ||
| sed -i '2s/.*/Feature 1 version A/' file.txt | ||
| log_cmd git add file.txt | ||
| log_cmd git commit -m "feature1: commit A" | ||
| simulate_push feature1 | ||
|
|
||
| # feature2 forks here and modifies the same line 2: its resolution will rewrite | ||
| # the very lines the squash reshapes, which is what made the incident's PR read | ||
| # as conflicting with its base. | ||
| log_cmd git checkout -b feature2 | ||
| sed -i '2s/.*/Feature 2 version/' file.txt | ||
| log_cmd git add file.txt | ||
| log_cmd git commit -m "feature2: change line 2" | ||
| simulate_push feature2 | ||
|
|
||
| # feature1 advances after the fork: its tip is no longer an ancestor of feature2. | ||
| log_cmd git checkout feature1 | ||
| sed -i '2s/.*/Feature 1 version B/' file.txt | ||
| log_cmd git add file.txt | ||
| log_cmd git commit -m "feature1: commit B" | ||
| simulate_push feature1 | ||
| FEATURE1_TIP=$(git rev-parse feature1) | ||
|
|
||
| if git merge-base --is-ancestor "$FEATURE1_TIP" feature2; then | ||
| echo "❌ Setup broken: feature1's tip must not be an ancestor of feature2" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Squash-merge feature1 into main | ||
| log_cmd git checkout main | ||
| log_cmd git merge --squash feature1 | ||
| log_cmd git commit -m "Squash merge feature1" | ||
| SQUASH_COMMIT=$(git rev-parse HEAD) | ||
| simulate_push main | ||
|
|
||
| FEATURE2_BEFORE=$(git rev-parse feature2) | ||
| # Outside the test repo: an untracked file makes git-merge-onto refuse to run. | ||
| COMMENT_FILE=$(mktemp) | ||
|
|
||
| # The action must hit the conflict path: comment, label, no push, branch kept. | ||
| OUT=$(env \ | ||
| SQUASH_COMMIT="$SQUASH_COMMIT" \ | ||
| MERGED_BRANCH=feature1 \ | ||
| PR_NUMBER=1 \ | ||
| TARGET_BRANCH=main \ | ||
| GH="$SCRIPT_DIR/mock_gh.sh" \ | ||
| GIT="$SCRIPT_DIR/mock_git.sh" \ | ||
| MOCK_COMMENT_FILE="$COMMENT_FILE" \ | ||
| "$SCRIPT_DIR/../update-pr-stack.sh" 2>&1) | ||
| echo "$OUT" | ||
|
|
||
| if ! grep -q "Mock: gh pr comment 2" <<<"$OUT"; then | ||
| echo "❌ Expected a conflict comment on the child PR" | ||
| exit 1 | ||
| fi | ||
| if grep -q "push origin :feature1" <<<"$OUT"; then | ||
| echo "❌ The merged branch must be kept while the child is conflicted" | ||
| exit 1 | ||
| fi | ||
| if [[ "$(git rev-parse feature2)" != "$FEATURE2_BEFORE" ]]; then | ||
| echo "❌ feature2 must not move on a conflict" | ||
| exit 1 | ||
| fi | ||
| if ! grep -q -- "--absorbed origin/main origin/feature1" "$COMMENT_FILE"; then | ||
| echo "❌ The posted resolution command must use --absorbed" | ||
| cat "$COMMENT_FILE" | ||
| exit 1 | ||
| fi | ||
| echo "✅ Conflict detected: comment posted with --absorbed, stack left alone" | ||
|
|
||
| # Resolve as the comment instructs, with the vendored copy standing in for | ||
| # `uvx git-merge-onto` (unit tests have no network). | ||
| log_cmd git checkout feature2 | ||
| if python3 "$SCRIPT_DIR/../git-merge-onto" --absorbed origin/main origin/feature1; then | ||
| echo "❌ The re-parent should conflict (both sides rewrote line 2)" | ||
| exit 1 | ||
| fi | ||
| sed -i '/^<<<<<<</,/^>>>>>>>/c\Feature 2 version' file.txt | ||
| log_cmd git add file.txt | ||
| log_cmd git commit --no-edit | ||
| simulate_push feature2 | ||
|
|
||
| # The regression assertion: the resolution descends from the old base's tip. | ||
| if log_cmd git merge-base --is-ancestor "$FEATURE1_TIP" feature2; then | ||
| echo "✅ Resolution descends from origin/feature1's tip (--absorbed recorded it)" | ||
| else | ||
| echo "❌ Resolution does not descend from origin/feature1's tip" | ||
| log_cmd git log --graph --oneline feature2 feature1 | ||
| exit 1 | ||
| fi | ||
| if ! log_cmd git merge-base --is-ancestor "$SQUASH_COMMIT" feature2; then | ||
| echo "❌ Resolution must contain the squash commit (resume checks this)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # And the content is the user's resolution on top of main. | ||
| if [[ "$(sed -n '2p' file.txt)" == "Feature 2 version" ]]; then | ||
| echo "✅ Resolved content kept" | ||
| else | ||
| echo "❌ Resolved content lost: $(sed -n '2p' file.txt)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "" | ||
| echo "All absorbed-resolution tests passed! 🎉" | ||
| echo "Test repository remains at: $TEST_REPO for inspection" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.