55# Usage: rebase-branch.sh <shears-branch> <upstream-branch> [<scripts-dir>]
66#
77# Parameters:
8- # shears-branch - The branch to rebase (e.g., origin/ shears/seen)
8+ # shears-branch - The branch to rebase (e.g., shears/seen)
99# upstream-branch - The upstream branch to rebase onto (e.g., upstream/seen)
1010# scripts-dir - Optional: directory containing this script and agents
1111# (defaults to the directory containing this script)
1212#
1313# Preconditions:
1414# - Must be run from a git repository
15- # - shears-branch and upstream-branch must be fetched
15+ # - origin/< shears-branch> and < upstream-branch> must be fetched
1616# - GH_TOKEN or GITHUB_TOKEN must be set for AI resolution
1717#
1818# Outputs:
@@ -82,53 +82,6 @@ generate_log_l_commands () {
8282}
8383
8484# Function to create recovery bundle on failure
85- create_recovery_archive () {
86- local ai_output=$1
87- local archive_dir=$( mktemp -d)
88- local bundle_file=" $archive_dir /recovery.bundle"
89- local archive_file=" ${WORKTREE_DIR} /recovery-archive.tar.gz"
90-
91- echo " ::group::Creating recovery archive"
92-
93- # Collect refs: HEAD, REBASE_HEAD, and all refs/rewritten/*
94- {
95- echo HEAD
96- echo REBASE_HEAD
97- git for-each-ref --format=' %(refname)' refs/rewritten/
98- } > " $archive_dir /refs-to-bundle.txt"
99-
100- # Create bundle with all progress (may fail if refs don't exist yet)
101- if ! git bundle create " $bundle_file " --stdin < " $archive_dir /refs-to-bundle.txt" ; then
102- echo " ::warning::Could not create recovery bundle"
103- fi
104-
105- # Copy the conflict report if it exists
106- if test -f " $REPORT_FILE " ; then
107- cp " $REPORT_FILE " " $archive_dir /"
108- fi
109-
110- # Save the AI output that caused failure
111- echo " $ai_output " > " $archive_dir /ai-output.txt"
112-
113- # Save rebase state if it exists
114- if test -d .git/rebase-merge; then
115- cp -r .git/rebase-merge " $archive_dir /"
116- fi
117-
118- # Create archive with working tree and recovery files
119- tar -czf " $archive_file " -C " $WORKTREE_DIR " . -C " $archive_dir " .
120-
121- rm -rf " $archive_dir "
122-
123- echo " Recovery archive created: $archive_file "
124- echo " ::endgroup::"
125-
126- # Output for GitHub Actions artifact upload
127- if test -n " $GITHUB_OUTPUT " ; then
128- echo " recovery_archive=$archive_file " >> " $GITHUB_OUTPUT "
129- fi
130- }
131-
13285# Function to resolve a single conflict with AI
13386resolve_conflict_with_ai () {
13487 # Get REBASE_HEAD info once
@@ -285,7 +238,6 @@ $(tail -50 make.log)
285238\`\`\`
286239
287240BUILD_FAIL_EOF
288- create_recovery_archive " $retry_output "
289241 exit 2
290242 fi
291243 echo " ::endgroup::"
@@ -307,7 +259,6 @@ $ai_output
307259\`\`\`
308260
309261EOF
310- create_recovery_archive " $ai_output "
311262 exit 2
312263 ;;
313264 * )
@@ -323,7 +274,6 @@ $ai_output
323274\`\`\`
324275
325276EOF
326- create_recovery_archive " $ai_output "
327277 exit 2
328278 ;;
329279 esac
@@ -383,29 +333,26 @@ command -v copilot >/dev/null 2>&1 || die "copilot CLI not found in PATH"
383333test -n " ${GH_TOKEN:- ${GITHUB_TOKEN:- } } " ||
384334 die " GH_TOKEN or GITHUB_TOKEN must be set"
385335
386- # Validate branches exist
387- git rev-parse --verify " $SHEARS_BRANCH " > /dev/null 2>&1 ||
388- die " Branch not found: $SHEARS_BRANCH "
336+ # Validate branches exist (shears branch is fetched as origin/<branch>)
337+ git rev-parse --verify " origin/ $SHEARS_BRANCH " > /dev/null 2>&1 ||
338+ die " Branch not found: origin/ $SHEARS_BRANCH "
389339git rev-parse --verify " $UPSTREAM_BRANCH " > /dev/null 2>&1 ||
390340 die " Branch not found: $UPSTREAM_BRANCH "
391341
392- # Set up worktree
393- WORKTREE_DIR=$( mktemp -d )
342+ # Set up worktree in current directory, named after the branch
343+ WORKTREE_DIR=" $PWD /rebase-worktree- ${SHEARS_BRANCH ##*/ } "
394344REPORT_FILE=" $WORKTREE_DIR /conflict-report.md"
395- trap ' git worktree remove --force "$WORKTREE_DIR"' EXIT
396345
397346# Output worktree path early so recovery steps can find it on failure
398347if test -n " $GITHUB_OUTPUT " ; then
399348 echo " worktree=$WORKTREE_DIR " >> " $GITHUB_OUTPUT "
400349fi
401350
402- # Extract local branch name from origin/shears/foo -> shears/foo
403- LOCAL_BRANCH=${SHEARS_BRANCH# origin/ }
404-
405- echo " ::group::Setup"
351+ echo " ::group::Setup worktree"
406352echo " Creating worktree at $WORKTREE_DIR ..."
407- git worktree add -b " $LOCAL_BRANCH " " $WORKTREE_DIR " " $SHEARS_BRANCH "
353+ git worktree add -B " $SHEARS_BRANCH " " $WORKTREE_DIR " " origin/ $SHEARS_BRANCH "
408354cd " $WORKTREE_DIR "
355+ echo " ::endgroup::"
409356
410357# Find the old marker
411358OLD_MARKER=$( git rev-parse " HEAD^{/Start.the.merging-rebase}" ) ||
@@ -414,21 +361,21 @@ OLD_UPSTREAM=$(git rev-parse "$OLD_MARKER^1")
414361NEW_UPSTREAM=$( git rev-parse " $UPSTREAM_BRANCH " )
415362TIP_OID=$( git rev-parse HEAD)
416363
417- echo " Old marker: $OLD_MARKER "
418- echo " Old upstream: $OLD_UPSTREAM "
419- echo " New upstream: $NEW_UPSTREAM "
420- echo " Current tip: $TIP_OID "
421- echo " ::endgroup::"
364+ echo " ::notice::Old marker: $OLD_MARKER "
365+ echo " ::notice::Old upstream: $OLD_UPSTREAM "
366+ echo " ::notice::New upstream: $NEW_UPSTREAM "
367+ echo " ::notice::Current tip: $TIP_OID "
422368
423369# Check if shears branch is behind GfW main (commits in main not in shears)
424370# The second parent of the marker points to the GfW branch tip at marker creation time
425371GFW_MAIN_BRANCH=" origin/main"
426372BEHIND_COUNT=$( git rev-list --count " $TIP_OID ..$GFW_MAIN_BRANCH " || echo " 0" )
427373
428374if test " $BEHIND_COUNT " -gt 0; then
429- echo " ::group::Syncing $BEHIND_COUNT commits from $GFW_MAIN_BRANCH "
375+ echo " ::notice::Syncing $BEHIND_COUNT commits from $GFW_MAIN_BRANCH "
376+ echo " ::group::Sync rebase from $GFW_MAIN_BRANCH "
430377 run_rebase_with_ai -r HEAD " $GFW_MAIN_BRANCH "
431- git checkout -B " $LOCAL_BRANCH "
378+ git checkout -B " $SHEARS_BRANCH "
432379 TIP_OID=$( git rev-parse HEAD)
433380 echo " ::endgroup::"
434381fi
504451
505452echo " Rebase completed: $( git rev-parse --short HEAD) "
506453cat " $REPORT_FILE "
507- echo " To push: cd $WORKTREE_DIR && git push --force origin HEAD: ${ SHEARS_BRANCH##*/ } "
454+ echo " To push: git push --force origin $( git rev-parse HEAD) : $ SHEARS_BRANCH"
508455
509- # For GitHub Actions: output variables (worktree already output early)
456+ # For GitHub Actions: output report path
510457if test -n " $GITHUB_OUTPUT " ; then
511458 echo " report=$REPORT_FILE " >> " $GITHUB_OUTPUT "
512- echo " head=$( git rev-parse HEAD) " >> " $GITHUB_OUTPUT "
513459fi
0 commit comments