@@ -313,13 +313,143 @@ latest toolchain ($LATEST_TOOLCHAIN):
313
313
rm -rf $TMP_DIR
314
314
}
315
315
316
+ function get_line_number_from_changelog() {
317
+ local PATH_TO_CHANGELOG=$1
318
+ local PATTERN=$2
319
+ echo $( grep -n -i " $PATTERN " " $PATH_TO_CHANGELOG " | cut -d : -f 1)
320
+ }
321
+
322
+ function latest_changelog_snippet() {
323
+ local PATH_TO_CHANGELOG=$1
324
+ local PREVIOUS_RELEASE_MARKER=$2
325
+ local START_MARKER_LINE=$( get_line_number_from_changelog $PATH_TO_CHANGELOG " \[Unreleased\]" )
326
+
327
+ if [ -z " $START_MARKER_LINE " ]; then
328
+ echo " Could not find the start of the recent CHANGELOG entries"
329
+ return 1
330
+ fi
331
+
332
+ local START=$(( $START_MARKER_LINE + 1 ))
333
+ local STOP=$( get_line_number_from_changelog $PATH_TO_CHANGELOG " $PREVIOUS_RELEASE_MARKER " )
334
+
335
+ if [ -z " $STOP " ]; then
336
+ echo " Could not find the end of the recent CHANGELOG entries"
337
+ return 1
338
+ fi
339
+
340
+ local END=$(( $STOP - 1 ))
341
+ # Print out the lines from START..=END
342
+ sed -n " $START ,${END} p;${STOP} q" CHANGELOG.md
343
+ }
344
+
345
+ function rustfmt_to_rustc_subtree_pull() {
346
+ local CLONE_DIR=$1
347
+ local RUSTLANG_RUST_URL=$2
348
+ local RUSTLANG_RUST_FORK_URL=$3
349
+ local SUBTREE_PULL_BRANCH=$4
350
+
351
+ # Assumes that `get_main_branch_name` is run from within the rustfmt repo.
352
+ # When running in GitHub actions that should be true. We want to grab this
353
+ # info before we clone and cd into the rust-lang/rust repo below.
354
+ local RUSTFMT_REMOTE_HEAD=$( get_main_branch_name " origin" )
355
+ echo " RUSTFMT_REMOTE_HEAD: $RUSTFMT_REMOTE_HEAD "
356
+ local UPSTREAM_RUSTFMT=$( rustfmt_git_url)
357
+ echo " UPSTREAM_RUSTFMT: $UPSTREAM_RUSTFMT "
358
+
359
+ # get_patched_subtree "$CLONE_DIR/git"
360
+ # Will CD into the rust directory
361
+ clone_rustlang_rust " $CLONE_DIR /rust" " $RUSTLANG_RUST_FORK_URL .git"
362
+ git remote add upstream $RUSTLANG_RUST_URL
363
+ RUSTLANGE_RUST_HEAD=$( get_main_branch_name " upstream" )
364
+ git fetch upstream $RUSTLANGE_RUST_HEAD
365
+ git switch -c $SUBTREE_PULL_BRANCH upstream/$RUSTLANGE_RUST_HEAD
366
+ git subtree pull -P src/tools/rustfmt $UPSTREAM_RUSTFMT $RUSTFMT_REMOTE_HEAD
367
+
368
+ if [ $? -ne 0 ]; then
369
+ echo " Could not pull changes from $UPSTREAM_RUSTFMT $RUSTFMT_REMOTE_HEAD into $RUSTLANG_RUST_URL "
370
+ return 1
371
+ fi
372
+ git push origin $SUBTREE_PULL_BRANCH
373
+ }
374
+
375
+ function create_subtree_pull_pull_request() {
376
+ TODAY=$1
377
+ CHANGELOG_SNIPPET=$2
378
+ PR_BRANCH=$3
379
+ UPSTREAM_RUSTLANG_RUST_REPO_URL=$4
380
+
381
+ local PR_TITLE=" Sync rustfmt subtree $TODAY "
382
+ local PR_MESSAGE=" r? @ghost
383
+ $CHANGELOG_SNIPPET
384
+ "
385
+ # This assumes that we're currently in the forked rust-lang/rust directory
386
+ # in order to create a PR from the fork to upstream rust-lang/rust
387
+ local PR_URL=$(
388
+ gh pr create \
389
+ --title " $PR_TITLE " \
390
+ --body " $PR_MESSAGE " \
391
+ --head " $PR_BRANCH " \
392
+ --repo " $UPSTREAM_RUSTLANG_RUST_REPO_URL " \
393
+ --label " subtree-sync"
394
+ )
395
+
396
+ if [ -z " $PR_URL " ]; then
397
+ echo " Failed to Create subtree-pull Pull Request "
398
+ else
399
+ echo " Created Pull Request For ($PR_URL ):
400
+
401
+ $PR_TITLE
402
+ $PR_MESSAGE
403
+ "
404
+ fi
405
+ }
406
+
407
+ function run_rustfmt_subtree_pull() {
408
+ set -e
409
+ local RUSTLANG_RUST_URL=$1
410
+ local RUSTLANG_RUST_FORK_URL=$2
411
+ local PREVIOUS_RELEASE_MARKER=$3
412
+
413
+ local CWD=$( pwd)
414
+ local TMP_DIR=$( mktemp -d)
415
+ local TODAY=$( gdate --rfc-3339=date)
416
+ local SUBTREE_PULL_BRANCH=" sync-from-rustfmt-$TODAY "
417
+
418
+ rustfmt_to_rustc_subtree_pull \
419
+ $TMP_DIR \
420
+ $RUSTLANG_RUST_URL \
421
+ $RUSTLANG_RUST_FORK_URL \
422
+ $SUBTREE_PULL_BRANCH \
423
+
424
+ LATEST_CHANGELOG_SNIPPET=$( latest_changelog_snippet " $CWD /CHANGELOG.md" $PREVIOUS_RELEASE_MARKER )
425
+ if [ $? -ne 0 ] || [ -z " $LATEST_CHANGELOG_SNIPPET " ]; then
426
+ echo " Could not find the latest CHANGELOG snippet"
427
+ return 1
428
+ fi
429
+
430
+ create_subtree_pull_pull_request \
431
+ $TODAY \
432
+ $LATEST_CHANGELOG_SNIPPET \
433
+ $SUBTREE_PULL_BRANCH \
434
+ $RUSTLANG_RUST_URL
435
+
436
+
437
+ if [ $? -ne 0 ]; then
438
+ echo " subtree pull failed ❌"
439
+ fi
440
+
441
+ rm -rf $TMP_DIR
442
+ }
443
+
316
444
function print_help() {
317
445
echo " Tools to help automate subtree syncs
318
446
319
447
usage: subtree_sync.sh <command> [<args>]
320
448
321
449
commands:
322
450
subtree-push Push changes from rust-lang/rust back to rustfmt.
451
+ subtree-pull Pull changes from rustfmt into rust-lang/rust.
452
+ To avoid merge conflicts run the subtree-push first.
323
453
"
324
454
}
325
455
@@ -334,6 +464,11 @@ function main() {
334
464
install_latest_nightly
335
465
run_rustfmt_subtree_push $RUSTLANG_RUST_URL
336
466
;;
467
+ subtree-pull)
468
+ local RUSTLANG_RUST_FORK_URL=$3
469
+ local PREVIOUS_RELEASE_MARKER=$4
470
+ run_rustfmt_subtree_pull $RUSTLANG_RUST_URL $RUSTLANG_RUST_FORK_URL $PREVIOUS_RELEASE_MARKER
471
+ ;;
337
472
* )
338
473
print_help
339
474
;;
0 commit comments