Skip to content

Commit b7b7e54

Browse files
committed
WIP: subtree_pull_automation
1 parent bdcdd90 commit b7b7e54

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

.github/workflows/subree_pull.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Git Subtree Push
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
rustlang_rust_url:
6+
description: 'rust-lang/rust repository URL'
7+
default: 'https://github.com/rust-lang/rust'
8+
required: true
9+
rustlang_rust_fork_url=$2
10+
local RUSTLANG_RUST_URL=$1
11+
local RUSTLANG_RUST_FORK_URL=$2
12+
local PREVIOUS_RELEASE_MARKER=$3
13+
14+
jobs:
15+
subtree-push:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: checkout
20+
uses: actions/checkout@v4
21+
with:
22+
# 0 indicates all history for all branches and tags.
23+
# https://github.com/actions/checkout?tab=readme-ov-file#fetch-all-history-for-all-tags-and-branches
24+
# Grabbing everything should help us avoid issues where `git commit --no-ff` complains that
25+
# it can't merge unrelated histories.
26+
fetch-depth: 0
27+
28+
# - name: Setup Rustfmt Bot Git Details
29+
# run: |
30+
# git config user.name "rustfmt bot"
31+
# git config user.email "[email protected]"
32+
33+
- name: subtree-pull
34+
env:
35+
# Need to set the `GH_TOKEN` env variable so we can use the GitHub CLI in `/ci/subtree_sync.sh`
36+
GH_TOKEN: ${{ github.token }}
37+
run: ${GITHUB_WORKSPACE}/ci/subtree_sync.sh subtree-pull ${{ inputs.rustlang_rust_url }}

ci/subtree_sync.sh

+135
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,143 @@ latest toolchain ($LATEST_TOOLCHAIN):
313313
rm -rf $TMP_DIR
314314
}
315315

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+
316444
function print_help() {
317445
echo "Tools to help automate subtree syncs
318446
319447
usage: subtree_sync.sh <command> [<args>]
320448
321449
commands:
322450
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.
323453
"
324454
}
325455

@@ -334,6 +464,11 @@ function main() {
334464
install_latest_nightly
335465
run_rustfmt_subtree_push $RUSTLANG_RUST_URL
336466
;;
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+
;;
337472
*)
338473
print_help
339474
;;

0 commit comments

Comments
 (0)