Skip to content

Commit 387b873

Browse files
committed
Make stderr ignore more explicit
1 parent 42a3047 commit 387b873

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/josh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::config::JoshConfig;
2-
use crate::utils::check_output;
2+
use crate::utils::run_command;
33
use anyhow::Context;
44
use std::net::{SocketAddr, TcpStream};
55
use std::path::PathBuf;
@@ -61,7 +61,7 @@ impl JoshProxy {
6161
}
6262

6363
pub fn try_install_josh() -> Option<JoshProxy> {
64-
check_output(&[
64+
run_command(&[
6565
"cargo",
6666
"install",
6767
"--locked",

src/sync.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::SyncContext;
22
use crate::josh::JoshProxy;
3-
use crate::utils::{check_output, check_output_at, ensure_clean_git_state};
3+
use crate::utils::{StderrMode, ensure_clean_git_state, run_command, run_command_at};
44
use anyhow::{Context, Error};
55
use std::path::{Path, PathBuf};
66

@@ -36,7 +36,7 @@ impl GitSync {
3636
pub fn rustc_pull(&self) -> Result<PullResult, RustcPullError> {
3737
// The upstream commit that we want to pull
3838
let upstream_sha = {
39-
let out = check_output([
39+
let out = run_command([
4040
"git",
4141
"ls-remote",
4242
&format!("https://github.com/{UPSTREAM_REPO}"),
@@ -62,7 +62,7 @@ impl GitSync {
6262
&self.context.config.construct_josh_filter(),
6363
);
6464

65-
let orig_head = check_output(["git", "rev-parse", "HEAD"])?;
65+
let orig_head = run_command(["git", "rev-parse", "HEAD"])?;
6666
println!(
6767
"previous upstream base: {:?}",
6868
self.context.last_upstream_sha
@@ -101,8 +101,8 @@ To prepare for merging from {UPSTREAM_REPO}."#,
101101
.last_upstream_sha_path
102102
.to_string_lossy()
103103
.to_string();
104-
check_output(&["git", "add", &config_path])?;
105-
check_output(&[
104+
run_command(&["git", "add", &config_path])?;
105+
run_command(&[
106106
"git",
107107
"commit",
108108
&config_path,
@@ -116,24 +116,23 @@ To prepare for merging from {UPSTREAM_REPO}."#,
116116
let mut git_reset = GitResetOnDrop::new(orig_head);
117117

118118
// Fetch given rustc commit.
119-
check_output(&["git", "fetch", &josh_url])
120-
.context("cannot fetch git state through Josh")?;
119+
run_command(&["git", "fetch", &josh_url]).context("cannot fetch git state through Josh")?;
121120

122121
// This should not add any new root commits. So count those before and after merging.
123122
let num_roots = || -> anyhow::Result<u32> {
124123
Ok(
125-
check_output(&["git", "rev-list", "HEAD", "--max-parents=0", "--count"])
124+
run_command(&["git", "rev-list", "HEAD", "--max-parents=0", "--count"])
126125
.context("failed to determine the number of root commits")?
127126
.parse::<u32>()?,
128127
)
129128
};
130129
let num_roots_before = num_roots()?;
131130

132131
let sha =
133-
check_output(&["git", "rev-parse", "HEAD"]).context("failed to get current commit")?;
132+
run_command(&["git", "rev-parse", "HEAD"]).context("failed to get current commit")?;
134133

135134
// The filtered SHA of upstream
136-
let incoming_ref = check_output(["git", "rev-parse", "FETCH_HEAD"])?;
135+
let incoming_ref = run_command(["git", "rev-parse", "FETCH_HEAD"])?;
137136
println!("incoming ref: {incoming_ref}");
138137

139138
let merge_message = format!(
@@ -149,7 +148,7 @@ Filtered ref: {incoming_ref}
149148
);
150149

151150
// Merge the fetched commit.
152-
check_output(&[
151+
run_command(&[
153152
"git",
154153
"merge",
155154
"FETCH_HEAD",
@@ -161,7 +160,7 @@ Filtered ref: {incoming_ref}
161160
.context("FAILED to merge new commits, something went wrong")?;
162161

163162
let current_sha =
164-
check_output(&["git", "rev-parse", "HEAD"]).context("FAILED to get current commit")?;
163+
run_command(&["git", "rev-parse", "HEAD"]).context("FAILED to get current commit")?;
165164
if current_sha == sha {
166165
eprintln!(
167166
"No merge was performed, no changes to pull were found. Rolling back the preparation commit."
@@ -210,10 +209,10 @@ Filtered ref: {incoming_ref}
210209
println!("Preparing {user_upstream_url} (base: {base_upstream_sha})...");
211210

212211
// Check if the remote branch doesn't already exist
213-
if check_output_at(
212+
if run_command_at(
214213
&["git", "fetch", &user_upstream_url, branch],
215214
&rustc_git,
216-
true,
215+
StderrMode::Print,
217216
)
218217
.is_ok()
219218
{
@@ -223,45 +222,45 @@ Filtered ref: {incoming_ref}
223222
}
224223

225224
// Download the base upstream SHA
226-
check_output_at(
225+
run_command_at(
227226
&[
228227
"git",
229228
"fetch",
230229
&format!("https://github.com/{UPSTREAM_REPO}"),
231230
&base_upstream_sha,
232231
],
233232
&rustc_git,
234-
false,
233+
StderrMode::Print,
235234
)
236235
.context("cannot download latest upstream SHA")?;
237236

238237
// And push it to the user's fork's branch
239-
check_output_at(
238+
run_command_at(
240239
&[
241240
"git",
242241
"push",
243242
&user_upstream_url,
244243
&format!("{base_upstream_sha}:refs/heads/{branch}"),
245244
],
246245
&rustc_git,
247-
true,
246+
StderrMode::Ignore,
248247
)
249248
.context("cannot push to your fork")?;
250249
println!();
251250

252251
// Do the actual push from the subtree git repo
253252
println!("Pushing changes...");
254-
check_output(&["git", "push", &josh_url, &format!("HEAD:{branch}")])?;
253+
run_command(&["git", "push", &josh_url, &format!("HEAD:{branch}")])?;
255254
println!();
256255

257256
// Do a round-trip check to make sure the push worked as expected.
258-
check_output_at(
257+
run_command_at(
259258
&["git", "fetch", &josh_url, &branch],
260259
&std::env::current_dir().unwrap(),
261-
true,
260+
StderrMode::Ignore,
262261
)?;
263-
let head = check_output(&["git", "rev-parse", "HEAD"])?;
264-
let fetch_head = check_output(&["git", "rev-parse", "FETCH_HEAD"])?;
262+
let head = run_command(&["git", "rev-parse", "HEAD"])?;
263+
let fetch_head = run_command(&["git", "rev-parse", "FETCH_HEAD"])?;
265264
if head != fetch_head {
266265
return Err(anyhow::anyhow!(
267266
"Josh created a non-roundtrip push! Do NOT merge this into rustc!\n\
@@ -294,7 +293,7 @@ fn prepare_rustc_checkout() -> anyhow::Result<PathBuf> {
294293
println!(
295294
"Cloning rustc into `{path}`. Use RUSTC_GIT environment variable to override the location of the checkout"
296295
);
297-
check_output(&[
296+
run_command(&[
298297
"git",
299298
"clone",
300299
"--filter=blob:none",
@@ -329,7 +328,7 @@ impl Drop for GitResetOnDrop {
329328
fn drop(&mut self) {
330329
if !self.disarmed {
331330
eprintln!("Reverting HEAD to {}", self.reset_to);
332-
check_output(&["git", "reset", "--hard", &self.reset_to])
331+
run_command(&["git", "reset", "--hard", &self.reset_to])
333332
.expect(&format!("cannot reset current branch to {}", self.reset_to));
334333
}
335334
}

src/utils.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ use std::path::Path;
22
use std::process::{Command, Stdio};
33

44
/// Run command and return its stdout.
5-
pub fn check_output<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<String> {
6-
check_output_at(args, &std::env::current_dir()?, false)
5+
pub fn run_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<String> {
6+
run_command_at(args, &std::env::current_dir()?, StderrMode::Print)
77
}
88

9-
pub fn check_output_at<'a, Args: AsRef<[&'a str]>>(
9+
pub enum StderrMode {
10+
Ignore,
11+
Print,
12+
}
13+
14+
pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
1015
args: Args,
1116
workdir: &Path,
12-
ignore_stderr: bool,
17+
stderr: StderrMode,
1318
) -> anyhow::Result<String> {
1419
let args = args.as_ref();
1520

1621
let mut cmd = Command::new(args[0]);
1722
cmd.current_dir(workdir);
1823
cmd.args(&args[1..]);
1924

20-
if ignore_stderr {
25+
if matches!(stderr, StderrMode::Ignore) {
2126
cmd.stderr(Stdio::null());
2227
}
2328
eprintln!("+ {cmd:?}");
@@ -35,7 +40,7 @@ pub fn check_output_at<'a, Args: AsRef<[&'a str]>>(
3540

3641
/// Fail if there are files that need to be checked in.
3742
pub fn ensure_clean_git_state() {
38-
let read = check_output(["git", "status", "--untracked-files=no", "--porcelain"])
43+
let read = run_command(["git", "status", "--untracked-files=no", "--porcelain"])
3944
.expect("cannot figure out if git state is clean");
4045
assert!(read.is_empty(), "working directory must be clean");
4146
}

0 commit comments

Comments
 (0)