Skip to content
Prev Previous commit
Next Next commit
Make git helper return BootstrapCmd
  • Loading branch information
Kobzol committed Jul 3, 2024
commit a34d0a8d5f2abbbe4bf28d829bbf7490f23d5a9b
38 changes: 16 additions & 22 deletions src/bootstrap/src/core/build_steps/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use build_helper::git::get_git_modified_files;
use ignore::WalkBuilder;
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::process::Command;
use std::sync::mpsc::SyncSender;
use std::sync::Mutex;

Expand Down Expand Up @@ -160,35 +160,29 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
override_builder.add(&format!("!{ignore}")).expect(&ignore);
}
}
let git_available = match helpers::git(None)
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
{
Ok(status) => status.success(),
Err(_) => false,
};
let git_available = build
.run(helpers::git(None).print_on_failure().allow_failure().arg("--version"))
.is_success();

let mut adjective = None;
if git_available {
let in_working_tree = match helpers::git(Some(&build.src))
.arg("rev-parse")
.arg("--is-inside-work-tree")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
{
Ok(status) => status.success(),
Err(_) => false,
};
let in_working_tree = build
.run(
helpers::git(Some(&build.src))
.print_on_failure()
.allow_failure()
.arg("rev-parse")
.arg("--is-inside-work-tree"),
)
.is_success();
if in_working_tree {
let untracked_paths_output = output(
helpers::git(Some(&build.src))
&mut helpers::git(Some(&build.src))
.arg("status")
.arg("--porcelain")
.arg("-z")
.arg("--untracked-files=normal"),
.arg("--untracked-files=normal")
.command,
);
let untracked_paths: Vec<_> = untracked_paths_output
.split_terminator('\0')
Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
// the LLVM shared object file is named `LLVM-12-rust-{version}-nightly`
config.src.join("src/version"),
]);
output(&mut rev_list).trim().to_owned()
output(&mut rev_list.command).trim().to_owned()
} else if let Some(info) = channel::read_commit_info_file(&config.src) {
info.sha.trim().to_owned()
} else {
Expand Down Expand Up @@ -253,7 +253,8 @@ pub(crate) fn is_ci_llvm_modified(config: &Config) -> bool {
// We assume we have access to git, so it's okay to unconditionally pass
// `true` here.
let llvm_sha = detect_llvm_sha(config, true);
let head_sha = output(helpers::git(Some(&config.src)).arg("rev-parse").arg("HEAD"));
let head_sha =
output(&mut helpers::git(Some(&config.src)).arg("rev-parse").arg("HEAD").command);
let head_sha = head_sha.trim();
llvm_sha == head_sha
}
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/build_steps/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::core::build_steps::compile::{Std, Sysroot};
use crate::core::build_steps::tool::{RustcPerf, Tool};
use crate::core::builder::Builder;
use crate::core::config::DebuginfoLevel;
use crate::utils::exec::BootstrapCommand;

/// Performs profiling using `rustc-perf` on a built version of the compiler.
pub fn perf(builder: &Builder<'_>) {
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/build_steps/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ impl Step for Hook {
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
let git = helpers::git(Some(&config.src))
.args(["rev-parse", "--git-common-dir"])
.command
.output()
.map(|output| {
assert!(output.status.success(), "failed to run `git`");
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->

cmd = cmd.delay_failure();
if !builder.config.verbose_tests {
cmd = cmd.quiet();
cmd = cmd.print_on_failure();
}
builder.run(cmd).is_success()
}
Expand Down
19 changes: 15 additions & 4 deletions src/bootstrap/src/core/build_steps/toolstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ fn print_error(tool: &str, submodule: &str) {

fn check_changed_files(toolstates: &HashMap<Box<str>, ToolState>) {
// Changed files
let output =
helpers::git(None).arg("diff").arg("--name-status").arg("HEAD").arg("HEAD^").output();
let output = helpers::git(None)
.arg("diff")
.arg("--name-status")
.arg("HEAD")
.arg("HEAD^")
.command
.output();
let output = match output {
Ok(o) => o,
Err(e) => {
Expand Down Expand Up @@ -324,6 +329,7 @@ fn checkout_toolstate_repo() {
.arg("--depth=1")
.arg(toolstate_repo())
.arg(TOOLSTATE_DIR)
.command
.status();
let success = match status {
Ok(s) => s.success(),
Expand All @@ -337,7 +343,8 @@ fn checkout_toolstate_repo() {
/// Sets up config and authentication for modifying the toolstate repo.
fn prepare_toolstate_config(token: &str) {
fn git_config(key: &str, value: &str) {
let status = helpers::git(None).arg("config").arg("--global").arg(key).arg(value).status();
let status =
helpers::git(None).arg("config").arg("--global").arg(key).arg(value).command.status();
let success = match status {
Ok(s) => s.success(),
Err(_) => false,
Expand Down Expand Up @@ -406,6 +413,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("-a")
.arg("-m")
.arg(&message)
.command
.status());
if !status.success() {
success = true;
Expand All @@ -416,6 +424,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("push")
.arg("origin")
.arg("master")
.command
.status());
// If we successfully push, exit.
if status.success() {
Expand All @@ -428,12 +437,14 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("fetch")
.arg("origin")
.arg("master")
.command
.status());
assert!(status.success());
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
.arg("reset")
.arg("--hard")
.arg("origin/master")
.command
.status());
assert!(status.success());
}
Expand All @@ -449,7 +460,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
/// `publish_toolstate.py` script if the PR passes all tests and is merged to
/// master.
fn publish_test_results(current_toolstate: &ToolstateData) {
let commit = t!(helpers::git(None).arg("rev-parse").arg("HEAD").output());
let commit = t!(helpers::git(None).arg("rev-parse").arg("HEAD").command.output());
let commit = t!(String::from_utf8(commit.stdout));

let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));
Expand Down
26 changes: 16 additions & 10 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ impl Config {
cmd.arg("rev-parse").arg("--show-cdup");
// Discard stderr because we expect this to fail when building from a tarball.
let output = cmd
.command
.stderr(std::process::Stdio::null())
.output()
.ok()
Expand Down Expand Up @@ -2141,7 +2142,7 @@ impl Config {

let mut git = helpers::git(Some(&self.src));
git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
output(&mut git)
output(&mut git.command)
}

/// Bootstrap embeds a version number into the name of shared libraries it uploads in CI.
Expand Down Expand Up @@ -2445,19 +2446,21 @@ impl Config {
};

// Handle running from a directory other than the top level
let top_level =
output(helpers::git(Some(&self.src)).args(["rev-parse", "--show-toplevel"]));
let top_level = output(
&mut helpers::git(Some(&self.src)).args(["rev-parse", "--show-toplevel"]).command,
);
let top_level = top_level.trim_end();
let compiler = format!("{top_level}/compiler/");
let library = format!("{top_level}/library/");

// Look for a version to compare to based on the current commit.
// Only commits merged by bors will have CI artifacts.
let merge_base = output(
helpers::git(Some(&self.src))
&mut helpers::git(Some(&self.src))
.arg("rev-list")
.arg(format!("--author={}", self.stage0_metadata.config.git_merge_commit_email))
.args(["-n1", "--first-parent", "HEAD"]),
.args(["-n1", "--first-parent", "HEAD"])
.command,
);
let commit = merge_base.trim_end();
if commit.is_empty() {
Expand All @@ -2471,6 +2474,7 @@ impl Config {
// Warn if there were changes to the compiler or standard library since the ancestor commit.
let has_changes = !t!(helpers::git(Some(&self.src))
.args(["diff-index", "--quiet", commit, "--", &compiler, &library])
.command
.status())
.success();
if has_changes {
Expand Down Expand Up @@ -2542,17 +2546,19 @@ impl Config {
if_unchanged: bool,
) -> Option<String> {
// Handle running from a directory other than the top level
let top_level =
output(helpers::git(Some(&self.src)).args(["rev-parse", "--show-toplevel"]));
let top_level = output(
&mut helpers::git(Some(&self.src)).args(["rev-parse", "--show-toplevel"]).command,
);
let top_level = top_level.trim_end();

// Look for a version to compare to based on the current commit.
// Only commits merged by bors will have CI artifacts.
let merge_base = output(
helpers::git(Some(&self.src))
&mut helpers::git(Some(&self.src))
.arg("rev-list")
.arg(format!("--author={}", self.stage0_metadata.config.git_merge_commit_email))
.args(["-n1", "--first-parent", "HEAD"]),
.args(["-n1", "--first-parent", "HEAD"])
.command,
);
let commit = merge_base.trim_end();
if commit.is_empty() {
Expand All @@ -2571,7 +2577,7 @@ impl Config {
git.arg(format!("{top_level}/{path}"));
}

let has_changes = !t!(git.status()).success();
let has_changes = !t!(git.command.status()).success();
if has_changes {
if if_unchanged {
if self.verbose > 0 {
Expand Down
46 changes: 30 additions & 16 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,12 @@ impl Build {
let submodule_git = || helpers::git(Some(&absolute_path));

// Determine commit checked out in submodule.
let checked_out_hash = output(submodule_git().args(["rev-parse", "HEAD"]));
let checked_out_hash = output(&mut submodule_git().args(["rev-parse", "HEAD"]).command);
let checked_out_hash = checked_out_hash.trim_end();
// Determine commit that the submodule *should* have.
let recorded =
output(helpers::git(Some(&self.src)).args(["ls-tree", "HEAD"]).arg(relative_path));
let recorded = output(
&mut helpers::git(Some(&self.src)).args(["ls-tree", "HEAD"]).arg(relative_path).command,
);
let actual_hash = recorded
.split_whitespace()
.nth(2)
Expand All @@ -521,6 +522,7 @@ impl Build {
let current_branch = {
let output = helpers::git(Some(&self.src))
.args(["symbolic-ref", "--short", "HEAD"])
.command
.stderr(Stdio::inherit())
.output();
let output = t!(output);
Expand All @@ -546,7 +548,7 @@ impl Build {
git
};
// NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
if !update(true).status().map_or(false, |status| status.success()) {
if !update(true).command.status().map_or(false, |status| status.success()) {
self.run(update(false));
}

Expand Down Expand Up @@ -577,12 +579,15 @@ impl Build {
if !self.config.submodules(self.rust_info()) {
return;
}
let output = output(
helpers::git(Some(&self.src))
.args(["config", "--file"])
.arg(self.config.src.join(".gitmodules"))
.args(["--get-regexp", "path"]),
);
let output = self
.run(
helpers::git(Some(&self.src))
.quiet()
.args(["config", "--file"])
.arg(self.config.src.join(".gitmodules"))
.args(["--get-regexp", "path"]),
)
.stdout();
for line in output.lines() {
// Look for `submodule.$name.path = $path`
// Sample output: `submodule.src/rust-installer.path src/tools/rust-installer`
Expand Down Expand Up @@ -950,7 +955,10 @@ impl Build {
command.command.status().map(|status| status.into()),
matches!(mode, OutputMode::All),
),
OutputMode::OnlyOnFailure => (command.command.output().map(|o| o.into()), true),
mode @ (OutputMode::OnlyOnFailure | OutputMode::Quiet) => (
command.command.output().map(|o| o.into()),
matches!(mode, OutputMode::OnlyOnFailure),
),
};

let output = match output {
Expand Down Expand Up @@ -1480,14 +1488,18 @@ impl Build {
// Figure out how many merge commits happened since we branched off master.
// That's our beta number!
// (Note that we use a `..` range, not the `...` symmetric difference.)
output(
helpers::git(Some(&self.src)).arg("rev-list").arg("--count").arg("--merges").arg(
format!(
self.run(
helpers::git(Some(&self.src))
.quiet()
.arg("rev-list")
.arg("--count")
.arg("--merges")
.arg(format!(
"refs/remotes/origin/{}..HEAD",
self.config.stage0_metadata.config.nightly_branch
),
),
)),
)
.stdout()
});
let n = count.trim().parse().unwrap();
self.prerelease_version.set(Some(n));
Expand Down Expand Up @@ -1914,6 +1926,7 @@ fn envify(s: &str) -> String {
pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
let diff = helpers::git(Some(dir))
.arg("diff")
.command
.output()
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
.unwrap_or_default();
Expand All @@ -1923,6 +1936,7 @@ pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
.arg("--porcelain")
.arg("-z")
.arg("--untracked-files=normal")
.command
.output()
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
.unwrap_or_default();
Expand Down
Loading