|
1 | 1 | mod changelog; |
2 | 2 |
|
3 | | -use std::process::{Command, Stdio}; |
4 | | -use std::thread; |
5 | | -use std::time::Duration; |
6 | | - |
7 | | -use anyhow::{Context as _, bail}; |
8 | | -use directories::ProjectDirs; |
9 | | -use stdx::JodChild; |
10 | 3 | use xshell::{Shell, cmd}; |
11 | 4 |
|
12 | 5 | use crate::{date_iso, flags, is_release_tag, project_root}; |
@@ -59,171 +52,3 @@ impl flags::Release { |
59 | 52 | Ok(()) |
60 | 53 | } |
61 | 54 | } |
62 | | - |
63 | | -// git sync implementation adapted from https://github.com/rust-lang/miri/blob/62039ac/miri-script/src/commands.rs |
64 | | -impl flags::RustcPull { |
65 | | - pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> { |
66 | | - sh.change_dir(project_root()); |
67 | | - let commit = self.commit.map(Result::Ok).unwrap_or_else(|| { |
68 | | - let rust_repo_head = |
69 | | - cmd!(sh, "git ls-remote https://github.com/rust-lang/rust/ HEAD").read()?; |
70 | | - rust_repo_head |
71 | | - .split_whitespace() |
72 | | - .next() |
73 | | - .map(|front| front.trim().to_owned()) |
74 | | - .ok_or_else(|| anyhow::format_err!("Could not obtain Rust repo HEAD from remote.")) |
75 | | - })?; |
76 | | - // Make sure the repo is clean. |
77 | | - if !cmd!(sh, "git status --untracked-files=no --porcelain").read()?.is_empty() { |
78 | | - bail!("working directory must be clean before running `cargo xtask pull`"); |
79 | | - } |
80 | | - // This should not add any new root commits. So count those before and after merging. |
81 | | - let num_roots = || -> anyhow::Result<u32> { |
82 | | - Ok(cmd!(sh, "git rev-list HEAD --max-parents=0 --count") |
83 | | - .read() |
84 | | - .context("failed to determine the number of root commits")? |
85 | | - .parse::<u32>()?) |
86 | | - }; |
87 | | - let num_roots_before = num_roots()?; |
88 | | - // Make sure josh is running. |
89 | | - let josh = start_josh()?; |
90 | | - |
91 | | - // Update rust-version file. As a separate commit, since making it part of |
92 | | - // the merge has confused the heck out of josh in the past. |
93 | | - // We pass `--no-verify` to avoid running any git hooks that might exist, |
94 | | - // in case they dirty the repository. |
95 | | - sh.write_file("rust-version", format!("{commit}\n"))?; |
96 | | - const PREPARING_COMMIT_MESSAGE: &str = "Preparing for merge from rust-lang/rust"; |
97 | | - cmd!(sh, "git commit rust-version --no-verify -m {PREPARING_COMMIT_MESSAGE}") |
98 | | - .run() |
99 | | - .context("FAILED to commit rust-version file, something went wrong")?; |
100 | | - |
101 | | - // Fetch given rustc commit. |
102 | | - cmd!(sh, "git fetch http://localhost:{JOSH_PORT}/rust-lang/rust.git@{commit}{JOSH_FILTER}.git") |
103 | | - .run() |
104 | | - .inspect_err(|_| { |
105 | | - // Try to un-do the previous `git commit`, to leave the repo in the state we found it it. |
106 | | - cmd!(sh, "git reset --hard HEAD^") |
107 | | - .run() |
108 | | - .expect("FAILED to clean up again after failed `git fetch`, sorry for that"); |
109 | | - }) |
110 | | - .context("FAILED to fetch new commits, something went wrong (committing the rust-version file has been undone)")?; |
111 | | - |
112 | | - // Merge the fetched commit. |
113 | | - const MERGE_COMMIT_MESSAGE: &str = "Merge from rust-lang/rust"; |
114 | | - cmd!(sh, "git merge FETCH_HEAD --no-verify --no-ff -m {MERGE_COMMIT_MESSAGE}") |
115 | | - .run() |
116 | | - .context("FAILED to merge new commits, something went wrong")?; |
117 | | - |
118 | | - // Check that the number of roots did not increase. |
119 | | - if num_roots()? != num_roots_before { |
120 | | - bail!("Josh created a new root commit. This is probably not the history you want."); |
121 | | - } |
122 | | - |
123 | | - drop(josh); |
124 | | - Ok(()) |
125 | | - } |
126 | | -} |
127 | | - |
128 | | -impl flags::RustcPush { |
129 | | - pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> { |
130 | | - let branch = self.branch.as_deref().unwrap_or("sync-from-ra"); |
131 | | - let rust_path = self.rust_path; |
132 | | - let rust_fork = self.rust_fork; |
133 | | - |
134 | | - sh.change_dir(project_root()); |
135 | | - let base = sh.read_file("rust-version")?.trim().to_owned(); |
136 | | - // Make sure the repo is clean. |
137 | | - if !cmd!(sh, "git status --untracked-files=no --porcelain").read()?.is_empty() { |
138 | | - bail!("working directory must be clean before running `cargo xtask push`"); |
139 | | - } |
140 | | - // Make sure josh is running. |
141 | | - let josh = start_josh()?; |
142 | | - |
143 | | - // Find a repo we can do our preparation in. |
144 | | - sh.change_dir(rust_path); |
145 | | - |
146 | | - // Prepare the branch. Pushing works much better if we use as base exactly |
147 | | - // the commit that we pulled from last time, so we use the `rust-version` |
148 | | - // file to find out which commit that would be. |
149 | | - println!("Preparing {rust_fork} (base: {base})..."); |
150 | | - if cmd!(sh, "git fetch https://github.com/{rust_fork} {branch}") |
151 | | - .ignore_stderr() |
152 | | - .read() |
153 | | - .is_ok() |
154 | | - { |
155 | | - bail!( |
156 | | - "The branch `{branch}` seems to already exist in `https://github.com/{rust_fork}`. Please delete it and try again." |
157 | | - ); |
158 | | - } |
159 | | - cmd!(sh, "git fetch https://github.com/rust-lang/rust {base}").run()?; |
160 | | - cmd!(sh, "git push https://github.com/{rust_fork} {base}:refs/heads/{branch}") |
161 | | - .ignore_stdout() |
162 | | - .ignore_stderr() // silence the "create GitHub PR" message |
163 | | - .run()?; |
164 | | - println!(); |
165 | | - |
166 | | - // Do the actual push. |
167 | | - sh.change_dir(project_root()); |
168 | | - println!("Pushing rust-analyzer changes..."); |
169 | | - cmd!( |
170 | | - sh, |
171 | | - "git push http://localhost:{JOSH_PORT}/{rust_fork}.git{JOSH_FILTER}.git HEAD:{branch}" |
172 | | - ) |
173 | | - .run()?; |
174 | | - println!(); |
175 | | - |
176 | | - // Do a round-trip check to make sure the push worked as expected. |
177 | | - cmd!( |
178 | | - sh, |
179 | | - "git fetch http://localhost:{JOSH_PORT}/{rust_fork}.git{JOSH_FILTER}.git {branch}" |
180 | | - ) |
181 | | - .ignore_stderr() |
182 | | - .read()?; |
183 | | - let head = cmd!(sh, "git rev-parse HEAD").read()?; |
184 | | - let fetch_head = cmd!(sh, "git rev-parse FETCH_HEAD").read()?; |
185 | | - if head != fetch_head { |
186 | | - bail!( |
187 | | - "Josh created a non-roundtrip push! Do NOT merge this into rustc!\n\ |
188 | | - Expected {head}, got {fetch_head}." |
189 | | - ); |
190 | | - } |
191 | | - println!( |
192 | | - "Confirmed that the push round-trips back to rust-analyzer properly. Please create a rustc PR:" |
193 | | - ); |
194 | | - // https://github.com/github-linguist/linguist/compare/master...octocat:linguist:master |
195 | | - let fork_path = rust_fork.replace('/', ":"); |
196 | | - println!( |
197 | | - " https://github.com/rust-lang/rust/compare/{fork_path}:{branch}?quick_pull=1&title=Subtree+update+of+rust-analyzer&body=r?+@ghost" |
198 | | - ); |
199 | | - |
200 | | - drop(josh); |
201 | | - Ok(()) |
202 | | - } |
203 | | -} |
204 | | - |
205 | | -/// Used for rustc syncs. |
206 | | -const JOSH_FILTER: &str = ":rev(55d9a533b309119c8acd13061581b43ae8840823:prefix=src/tools/rust-analyzer):/src/tools/rust-analyzer"; |
207 | | -const JOSH_PORT: &str = "42042"; |
208 | | - |
209 | | -fn start_josh() -> anyhow::Result<impl Drop> { |
210 | | - // Determine cache directory. |
211 | | - let local_dir = { |
212 | | - let user_dirs = ProjectDirs::from("org", "rust-lang", "rust-analyzer-josh").unwrap(); |
213 | | - user_dirs.cache_dir().to_owned() |
214 | | - }; |
215 | | - |
216 | | - // Start josh, silencing its output. |
217 | | - let mut cmd = Command::new("josh-proxy"); |
218 | | - cmd.arg("--local").arg(local_dir); |
219 | | - cmd.arg("--remote").arg("https://github.com"); |
220 | | - cmd.arg("--port").arg(JOSH_PORT); |
221 | | - cmd.arg("--no-background"); |
222 | | - cmd.stdout(Stdio::null()); |
223 | | - cmd.stderr(Stdio::null()); |
224 | | - let josh = cmd.spawn().context("failed to start josh-proxy, make sure it is installed")?; |
225 | | - // Give it some time so hopefully the port is open. (100ms was not enough.) |
226 | | - thread::sleep(Duration::from_millis(200)); |
227 | | - |
228 | | - Ok(JodChild(josh)) |
229 | | -} |
0 commit comments