Skip to content

Commit 06b1e04

Browse files
committed
Remove several usages of the as_command_mut method
Passes `&Builder<'_>` to additional places, so that they could use the `BootstrapCommand` APIs directly, rather than going through `as_command_mut`.
1 parent e111c8e commit 06b1e04

File tree

5 files changed

+78
-115
lines changed

5 files changed

+78
-115
lines changed

src/bootstrap/src/core/build_steps/llvm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub fn prebuilt_llvm_config(builder: &Builder<'_>, target: TargetSelection) -> L
125125
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
126126
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
127127
generate_smart_stamp_hash(
128+
builder,
128129
&builder.config.src.join("src/llvm-project"),
129130
builder.in_tree_llvm_info.sha().unwrap_or_default(),
130131
)

src/bootstrap/src/core/build_steps/setup.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,18 @@ impl Step for Hook {
474474
if config.dry_run() {
475475
return;
476476
}
477-
t!(install_git_hook_maybe(config));
477+
t!(install_git_hook_maybe(builder, config));
478478
}
479479
}
480480

481481
// install a git hook to automatically run tidy, if they want
482-
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
482+
fn install_git_hook_maybe(builder: &Builder<'_>, config: &Config) -> io::Result<()> {
483483
let git = helpers::git(Some(&config.src))
484+
.capture()
484485
.args(["rev-parse", "--git-common-dir"])
485-
.as_command_mut()
486-
.output()
487-
.map(|output| {
488-
assert!(output.status.success(), "failed to run `git`");
489-
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
490-
})?;
486+
.run(builder)
487+
.stdout();
488+
let git = PathBuf::from(git.trim());
491489
let hooks_dir = git.join("hooks");
492490
let dst = hooks_dir.join("pre-push");
493491
if dst.exists() {

src/bootstrap/src/core/build_steps/toolstate.rs

+33-67
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,16 @@ fn print_error(tool: &str, submodule: &str) {
9999
crate::exit!(3);
100100
}
101101

102-
fn check_changed_files(toolstates: &HashMap<Box<str>, ToolState>) {
102+
fn check_changed_files(builder: &Builder<'_>, toolstates: &HashMap<Box<str>, ToolState>) {
103103
// Changed files
104104
let output = helpers::git(None)
105+
.capture()
105106
.arg("diff")
106107
.arg("--name-status")
107108
.arg("HEAD")
108109
.arg("HEAD^")
109-
.as_command_mut()
110-
.output();
111-
let output = match output {
112-
Ok(o) => o,
113-
Err(e) => {
114-
eprintln!("Failed to get changed files: {e:?}");
115-
crate::exit!(1);
116-
}
117-
};
118-
119-
let output = t!(String::from_utf8(output.stdout));
110+
.run(builder)
111+
.stdout();
120112

121113
for (tool, submodule) in STABLE_TOOLS.iter().chain(NIGHTLY_TOOLS.iter()) {
122114
let changed = output.lines().any(|l| l.starts_with('M') && l.ends_with(submodule));
@@ -186,8 +178,8 @@ impl Step for ToolStateCheck {
186178
crate::exit!(1);
187179
}
188180

189-
check_changed_files(&toolstates);
190-
checkout_toolstate_repo();
181+
check_changed_files(builder, &toolstates);
182+
checkout_toolstate_repo(builder);
191183
let old_toolstate = read_old_toolstate();
192184

193185
for (tool, _) in STABLE_TOOLS.iter() {
@@ -231,7 +223,7 @@ impl Step for ToolStateCheck {
231223
}
232224

233225
if builder.config.channel == "nightly" && env::var_os("TOOLSTATE_PUBLISH").is_some() {
234-
commit_toolstate_change(&toolstates);
226+
commit_toolstate_change(builder, &toolstates);
235227
}
236228
}
237229

@@ -315,55 +307,34 @@ fn toolstate_repo() -> String {
315307
const TOOLSTATE_DIR: &str = "rust-toolstate";
316308

317309
/// Checks out the toolstate repo into `TOOLSTATE_DIR`.
318-
fn checkout_toolstate_repo() {
310+
fn checkout_toolstate_repo(builder: &Builder<'_>) {
319311
if let Ok(token) = env::var("TOOLSTATE_REPO_ACCESS_TOKEN") {
320-
prepare_toolstate_config(&token);
312+
prepare_toolstate_config(builder, &token);
321313
}
322314
if Path::new(TOOLSTATE_DIR).exists() {
323315
eprintln!("Cleaning old toolstate directory...");
324316
t!(fs::remove_dir_all(TOOLSTATE_DIR));
325317
}
326318

327-
let status = helpers::git(None)
319+
helpers::git(None)
328320
.arg("clone")
329321
.arg("--depth=1")
330322
.arg(toolstate_repo())
331323
.arg(TOOLSTATE_DIR)
332-
.as_command_mut()
333-
.status();
334-
let success = match status {
335-
Ok(s) => s.success(),
336-
Err(_) => false,
337-
};
338-
if !success {
339-
panic!("git clone unsuccessful (status: {status:?})");
340-
}
324+
.run(builder);
341325
}
342326

343327
/// Sets up config and authentication for modifying the toolstate repo.
344-
fn prepare_toolstate_config(token: &str) {
345-
fn git_config(key: &str, value: &str) {
346-
let status = helpers::git(None)
347-
.arg("config")
348-
.arg("--global")
349-
.arg(key)
350-
.arg(value)
351-
.as_command_mut()
352-
.status();
353-
let success = match status {
354-
Ok(s) => s.success(),
355-
Err(_) => false,
356-
};
357-
if !success {
358-
panic!("git config key={key} value={value} failed (status: {status:?})");
359-
}
328+
fn prepare_toolstate_config(builder: &Builder<'_>, token: &str) {
329+
fn git_config(builder: &Builder<'_>, key: &str, value: &str) {
330+
helpers::git(None).arg("config").arg("--global").arg(key).arg(value).run(builder);
360331
}
361332

362333
// If changing anything here, then please check that `src/ci/publish_toolstate.sh` is up to date
363334
// as well.
364-
git_config("user.email", "[email protected]");
365-
git_config("user.name", "Rust Toolstate Update");
366-
git_config("credential.helper", "store");
335+
git_config(builder, "user.email", "[email protected]");
336+
git_config(builder, "user.name", "Rust Toolstate Update");
337+
git_config(builder, "credential.helper", "store");
367338

368339
let credential = format!("https://{token}:[email protected]\n",);
369340
let git_credential_path = PathBuf::from(t!(env::var("HOME"))).join(".git-credentials");
@@ -403,55 +374,51 @@ fn read_old_toolstate() -> Vec<RepoState> {
403374
///
404375
/// * See <https://help.github.com/articles/about-commit-email-addresses/>
405376
/// if a private email by GitHub is wanted.
406-
fn commit_toolstate_change(current_toolstate: &ToolstateData) {
377+
fn commit_toolstate_change(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
407378
let message = format!("({} CI update)", OS.expect("linux/windows only"));
408379
let mut success = false;
409380
for _ in 1..=5 {
410381
// Upload the test results (the new commit-to-toolstate mapping) to the toolstate repo.
411382
// This does *not* change the "current toolstate"; that only happens post-landing
412383
// via `src/ci/docker/publish_toolstate.sh`.
413-
publish_test_results(current_toolstate);
384+
publish_test_results(builder, current_toolstate);
414385

415386
// `git commit` failing means nothing to commit.
416-
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
387+
let status = helpers::git(Some(Path::new(TOOLSTATE_DIR)))
388+
.allow_failure()
417389
.arg("commit")
418390
.arg("-a")
419391
.arg("-m")
420392
.arg(&message)
421-
.as_command_mut()
422-
.status());
423-
if !status.success() {
393+
.run(builder);
394+
if !status.is_success() {
424395
success = true;
425396
break;
426397
}
427398

428-
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
399+
let status = helpers::git(Some(Path::new(TOOLSTATE_DIR)))
400+
.allow_failure()
429401
.arg("push")
430402
.arg("origin")
431403
.arg("master")
432-
.as_command_mut()
433-
.status());
404+
.run(builder);
434405
// If we successfully push, exit.
435-
if status.success() {
406+
if status.is_success() {
436407
success = true;
437408
break;
438409
}
439410
eprintln!("Sleeping for 3 seconds before retrying push");
440411
std::thread::sleep(std::time::Duration::from_secs(3));
441-
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
412+
helpers::git(Some(Path::new(TOOLSTATE_DIR)))
442413
.arg("fetch")
443414
.arg("origin")
444415
.arg("master")
445-
.as_command_mut()
446-
.status());
447-
assert!(status.success());
448-
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
416+
.run(builder);
417+
helpers::git(Some(Path::new(TOOLSTATE_DIR)))
449418
.arg("reset")
450419
.arg("--hard")
451420
.arg("origin/master")
452-
.as_command_mut()
453-
.status());
454-
assert!(status.success());
421+
.run(builder);
455422
}
456423

457424
if !success {
@@ -464,9 +431,8 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
464431
/// These results will later be promoted to `latest.json` by the
465432
/// `publish_toolstate.py` script if the PR passes all tests and is merged to
466433
/// master.
467-
fn publish_test_results(current_toolstate: &ToolstateData) {
468-
let commit = t!(helpers::git(None).arg("rev-parse").arg("HEAD").as_command_mut().output());
469-
let commit = t!(String::from_utf8(commit.stdout));
434+
fn publish_test_results(builder: &Builder<'_>, current_toolstate: &ToolstateData) {
435+
let commit = helpers::git(None).capture().arg("rev-parse").arg("HEAD").run(builder).stdout();
470436

471437
let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));
472438

src/bootstrap/src/lib.rs

+31-33
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::fmt::Display;
2323
use std::fs::{self, File};
2424
use std::io;
2525
use std::path::{Path, PathBuf};
26-
use std::process::{Command, Stdio};
26+
use std::process::Command;
2727
use std::str;
2828
use std::sync::OnceLock;
2929
use std::time::SystemTime;
@@ -37,7 +37,7 @@ use utils::channel::GitInfo;
3737
use utils::helpers::hex_encode;
3838

3939
use crate::core::builder;
40-
use crate::core::builder::Kind;
40+
use crate::core::builder::{Builder, Kind};
4141
use crate::core::config::{flags, LldMode};
4242
use crate::core::config::{DryRun, Target};
4343
use crate::core::config::{LlvmLibunwind, TargetSelection};
@@ -490,18 +490,18 @@ impl Build {
490490
return;
491491
}
492492

493-
let submodule_git = || helpers::git(Some(&absolute_path));
493+
let submodule_git = || helpers::git(Some(&absolute_path)).capture_stdout();
494494

495495
// Determine commit checked out in submodule.
496-
let checked_out_hash = output(submodule_git().args(["rev-parse", "HEAD"]).as_command_mut());
496+
let checked_out_hash = submodule_git().args(["rev-parse", "HEAD"]).run(self).stdout();
497497
let checked_out_hash = checked_out_hash.trim_end();
498498
// Determine commit that the submodule *should* have.
499-
let recorded = output(
500-
helpers::git(Some(&self.src))
501-
.args(["ls-tree", "HEAD"])
502-
.arg(relative_path)
503-
.as_command_mut(),
504-
);
499+
let recorded = helpers::git(Some(&self.src))
500+
.capture_stdout()
501+
.args(["ls-tree", "HEAD"])
502+
.arg(relative_path)
503+
.run(self)
504+
.stdout();
505505
let actual_hash = recorded
506506
.split_whitespace()
507507
.nth(2)
@@ -522,21 +522,14 @@ impl Build {
522522
let update = |progress: bool| {
523523
// Git is buggy and will try to fetch submodules from the tracking branch for *this* repository,
524524
// even though that has no relation to the upstream for the submodule.
525-
let current_branch = {
526-
let output = helpers::git(Some(&self.src))
527-
.args(["symbolic-ref", "--short", "HEAD"])
528-
.as_command_mut()
529-
.stderr(Stdio::inherit())
530-
.output();
531-
let output = t!(output);
532-
if output.status.success() {
533-
Some(String::from_utf8(output.stdout).unwrap().trim().to_owned())
534-
} else {
535-
None
536-
}
537-
};
525+
let current_branch = helpers::git(Some(&self.src))
526+
.capture_stdout()
527+
.args(["symbolic-ref", "--short", "HEAD"])
528+
.run(self)
529+
.stdout_if_ok()
530+
.map(|s| s.trim().to_owned());
538531

539-
let mut git = helpers::git(Some(&self.src));
532+
let mut git = helpers::git(Some(&self.src)).allow_failure();
540533
if let Some(branch) = current_branch {
541534
// If there is a tag named after the current branch, git will try to disambiguate by prepending `heads/` to the branch name.
542535
// This syntax isn't accepted by `branch.{branch}`. Strip it.
@@ -550,8 +543,7 @@ impl Build {
550543
git.arg(relative_path);
551544
git
552545
};
553-
// NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
554-
if !update(true).as_command_mut().status().map_or(false, |status| status.success()) {
546+
if !update(true).run(self).is_success() {
555547
update(false).run(self);
556548
}
557549

@@ -1943,22 +1935,28 @@ fn envify(s: &str) -> String {
19431935
///
19441936
/// In case of errors during `git` command execution (e.g., in tarball sources), default values
19451937
/// are used to prevent panics.
1946-
pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
1938+
pub fn generate_smart_stamp_hash(
1939+
builder: &Builder<'_>,
1940+
dir: &Path,
1941+
additional_input: &str,
1942+
) -> String {
19471943
let diff = helpers::git(Some(dir))
1944+
.capture_stdout()
1945+
.allow_failure()
19481946
.arg("diff")
1949-
.as_command_mut()
1950-
.output()
1951-
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
1947+
.run(builder)
1948+
.stdout_if_ok()
19521949
.unwrap_or_default();
19531950

19541951
let status = helpers::git(Some(dir))
1952+
.capture_stdout()
1953+
.allow_failure()
19551954
.arg("status")
19561955
.arg("--porcelain")
19571956
.arg("-z")
19581957
.arg("--untracked-files=normal")
1959-
.as_command_mut()
1960-
.output()
1961-
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
1958+
.run(builder)
1959+
.stdout_if_ok()
19621960
.unwrap_or_default();
19631961

19641962
let mut hasher = sha2::Sha256::new();

src/bootstrap/src/utils/tarball.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,13 @@ impl<'a> Tarball<'a> {
369369
// gets the same timestamp.
370370
if self.builder.rust_info().is_managed_git_subrepository() {
371371
// %ct means committer date
372-
let timestamp = helpers::output(
373-
helpers::git(Some(&self.builder.src))
374-
.arg("log")
375-
.arg("-1")
376-
.arg("--format=%ct")
377-
.as_command_mut(),
378-
);
372+
let timestamp = helpers::git(Some(&self.builder.src))
373+
.capture_stdout()
374+
.arg("log")
375+
.arg("-1")
376+
.arg("--format=%ct")
377+
.run(self.builder)
378+
.stdout();
379379
cmd.args(["--override-file-mtime", timestamp.trim()]);
380380
}
381381

0 commit comments

Comments
 (0)