Skip to content

Commit 6beded4

Browse files
committed
Add tooling for josh syncs
Create a justfile that handles pulling from and pushing to rust-lang/rust. This can be invoked with the following: $ just rustc-pull $ just rustc-push
1 parent 40d89f4 commit 6beded4

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

justfile

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name := "compiler-builtins"
2+
# Override these environment variables to test against a fork
3+
upstream_repo := env("UPSTREAM_ORG", "rust-lang") + "/rust"
4+
upstream_ref := env("UPSTREAM_REF", "HEAD")
5+
upstream_url := "https://github.com/" + upstream_repo
6+
7+
josh_port := "42042"
8+
josh_filter := ":/library/compiler-builtins"
9+
josh_url_base := "http://localhost:" + josh_port
10+
josh_cache_dir := cache_directory() / "rust-lang.compiler-builtins-josh"
11+
12+
# Set traps to kill Josh on shell exit or failure, then launch Josh.
13+
start_josh := \
14+
"trap 'exit $EXIT_CODE' INT TERM\n" + \
15+
"trap 'EXIT_CODE=$?; kill 0' EXIT\n" + \
16+
just_executable() + " start-josh &\n" + \
17+
just_executable() + " _ensure-josh-running\n"
18+
19+
# TODO make these messages better
20+
update_version_msg := """
21+
Preparing for merge from rustc
22+
"""
23+
rustc_to_builtins_msg := """
24+
Merge from rustc
25+
"""
26+
27+
default:
28+
just --list
29+
30+
# Exit with failure if the working directory has uncommitted files
31+
_ensure-clean:
32+
#!/bin/bash
33+
set -eaxo pipefail
34+
if [ -n "$(git status --untracked-files=no --porcelain)" ]; then
35+
echo "working directory must be clean"
36+
exit 1
37+
fi
38+
39+
# Make sure Josh is reachable.
40+
_ensure-josh-running:
41+
#!/bin/bash
42+
set -eaxo pipefail
43+
for _ in $(seq 1 100); do
44+
sleep 0.01s
45+
46+
# Exit with success if we can connect to the port
47+
if nc -z 127.0.0.1 "{{ josh_port }}"; then
48+
exit
49+
fi
50+
done
51+
52+
echo "Even after waiting for 1s, josh-proxy is still not available."
53+
exit 1
54+
55+
# Launch Josh, for running commands manually
56+
start-josh:
57+
josh-proxy --local '{{ josh_cache_dir }}' \
58+
--remote 'https://github.com' \
59+
--port '{{ josh_port }}' \
60+
--no-background
61+
62+
# Update this repo with changes from rust-lang/rust
63+
rustc-pull: _ensure-clean
64+
#!/bin/bash
65+
set -eaxo pipefail
66+
67+
commit="$(git ls-remote "{{ upstream_url }}" "{{ upstream_ref }}" | cut -w -f1)"
68+
josh_url="{{ josh_url_base }}/{{ upstream_repo }}.git@${commit}{{ josh_filter }}.git"
69+
70+
{{ start_josh }}
71+
72+
previous_base_commit="$(cat rust-version)"
73+
if [ "$previous_base_commit" = "$commit" ]; then
74+
echo "Nothing to pull; commit at $commit"
75+
exit 1
76+
fi
77+
78+
orig_head="$(git rev-parse HEAD)"
79+
echo "$commit" > "rust-version"
80+
git commit rust-version --no-verify -m '{{ update_version_msg }}'
81+
82+
if ! git fetch "$josh_url"; then
83+
echo "FAILED to fetch new commits, something went wrong."
84+
git reset --hard "$orig_head"
85+
echo "(committing the rust-version file has been undone)"
86+
exit 1
87+
fi
88+
89+
num_roots_before="$(git rev-list HEAD --max-parents=0 --count)"
90+
sha="$(git rev-parse HEAD)"
91+
git merge FETCH_HEAD --no-verify --no-ff -m '{{ rustc_to_builtins_msg }}'
92+
new_sha="$(git rev-parse HEAD)"
93+
94+
if [ "$sha" = "$new_sha" ]; then
95+
git reset --hard "$orig_head"
96+
echo "No merge was performed, no changes to pull were found. Rolled back the preparation commit."
97+
exit 1
98+
fi
99+
100+
num_roots="$(git rev-list HEAD --max-parents=0 --count)"
101+
102+
if [ "$num_roots" -ne "$num_roots_before" ]; then
103+
echo "Josh created a new root commit. This is probably not the history you want."
104+
exit 1
105+
fi
106+
107+
# Create a pull request to rust-lang/rust with changes from this repo
108+
rustc-push github_user branch="update-builtins": _ensure-clean
109+
#!/bin/bash
110+
set -eaxo pipefail
111+
112+
base="$(cat rust-version)"
113+
branch="{{ branch }}"
114+
github_user="{{ github_user }}"
115+
josh_url="{{ josh_url_base }}/{{ github_user }}/rust.git{{ josh_filter }}.git"
116+
user_rust_url="[email protected]:{{ github_user }}/rust.git"
117+
118+
if [ -z "$RUSTC_GIT" ]; then
119+
echo "The RUSTC_GIT environment variable must be set"
120+
exit 1
121+
fi
122+
123+
{{ start_josh }}
124+
125+
(
126+
# Execute in the rustc directory
127+
cd "$RUSTC_GIT"
128+
129+
echo "Preparing $github_user/rust (base: $base)..."
130+
131+
if git fetch "$user_rust_url" "$branch" 2>&1 > /dev/null; then
132+
echo "The branch '$branch' seems to already exist in '$user_rust_url'. \
133+
Please delete it and try again."
134+
exit 1
135+
fi
136+
137+
git fetch "https://github.com/{{ upstream_repo }}" "$base"
138+
git push "$user_rust_url" "$base:refs/heads/$branch" --no-verify
139+
)
140+
141+
# Do the actual push.
142+
echo "Pushing changes..."
143+
git push "$josh_url" "HEAD:$branch"
144+
145+
# Do a round-trip check to make sure the push worked as expected
146+
git fetch "$josh_url" "$branch"
147+
head="$(git rev-parse HEAD)"
148+
fetch_head="$(git rev-parse FETCH_HEAD)"
149+
150+
if [ "$head" != "$fetch_head" ]; then
151+
echo "Josh created a non-roundtrip push! Do NOT merge this into rustc!"
152+
echo "Expected '$head', got '$fetch_head'."
153+
exit 1
154+
fi
155+
156+
echo "Confirmed that the push round-trips back to {{ name }} properly. Please create a rustc PR:"
157+
# Open PR with `subtree update` title to silence the `no-merges` triagebot check
158+
echo " {{ upstream_url }}/compare/$github_user:$branch?quick_pull=1&title=rustc-dev-guide+subtree+update&body=r?+@ghost"

0 commit comments

Comments
 (0)