Skip to content

Commit e611c8e

Browse files
committed
Auto merge of rust-lang#13217 - dtolnay-contrib:toolsutil, r=flip1995
Check exit status of subcommands spawned by rustc_tools_util The git commands `git rev-parse --short HEAD` and `git log -1 --date=short --pretty=format:%cd` that clippy runs from its build script might fail with **"fatal: not a git repository (or any of the parent directories): .git"** if clippy is being built from a source tarball rather than a git repository. That message is written by git to stderr, and nothing is written to stdout. For `clippy-driver --version` this PR wouldn't make a difference because it treats empty stdout and failed spawns (`git` is not installed) identically: https://github.com/rust-lang/rust-clippy/blob/7ac242c3d0d3ee867a6c9cdcbdec986c408ac36f/rustc_tools_util/src/lib.rs#L35-L42 But other users of `rustc_tools_util` should be able to expect that the distinction between Some and None is meaningful. They shouldn't need extra code to handle None vs Some-and-empty vs Some-and-nonempty. --- changelog: none
2 parents 7ac242c + 234ea1f commit e611c8e

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

rustc_tools_util/src/lib.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22

3+
use std::str;
4+
35
/// This macro creates the version string during compilation from the
46
/// current environment
57
#[macro_export]
@@ -101,49 +103,45 @@ impl std::fmt::Debug for VersionInfo {
101103

102104
#[must_use]
103105
pub fn get_commit_hash() -> Option<String> {
104-
std::process::Command::new("git")
106+
let output = std::process::Command::new("git")
105107
.args(["rev-parse", "--short", "HEAD"])
106108
.output()
107-
.ok()
108-
.and_then(|r| String::from_utf8(r.stdout).ok())
109+
.ok()?;
110+
let stdout = output.status.success().then_some(output.stdout)?;
111+
String::from_utf8(stdout).ok()
109112
}
110113

111114
#[must_use]
112115
pub fn get_commit_date() -> Option<String> {
113-
std::process::Command::new("git")
116+
let output = std::process::Command::new("git")
114117
.args(["log", "-1", "--date=short", "--pretty=format:%cd"])
115118
.output()
116-
.ok()
117-
.and_then(|r| String::from_utf8(r.stdout).ok())
119+
.ok()?;
120+
let stdout = output.status.success().then_some(output.stdout)?;
121+
String::from_utf8(stdout).ok()
118122
}
119123

120124
#[must_use]
121125
pub fn get_channel() -> String {
122-
match std::env::var("CFG_RELEASE_CHANNEL") {
123-
Ok(channel) => channel,
124-
Err(_) => {
125-
// if that failed, try to ask rustc -V, do some parsing and find out
126-
match std::process::Command::new("rustc")
127-
.arg("-V")
128-
.output()
129-
.ok()
130-
.and_then(|r| String::from_utf8(r.stdout).ok())
131-
{
132-
Some(rustc_output) => {
133-
if rustc_output.contains("beta") {
134-
String::from("beta")
135-
} else if rustc_output.contains("stable") {
136-
String::from("stable")
137-
} else {
138-
// default to nightly if we fail to parse
139-
String::from("nightly")
140-
}
141-
},
142-
// default to nightly
143-
None => String::from("nightly"),
126+
if let Ok(channel) = std::env::var("CFG_RELEASE_CHANNEL") {
127+
return channel;
128+
}
129+
130+
// if that failed, try to ask rustc -V, do some parsing and find out
131+
if let Ok(output) = std::process::Command::new("rustc").arg("-V").output() {
132+
if output.status.success() {
133+
if let Ok(rustc_output) = str::from_utf8(&output.stdout) {
134+
if rustc_output.contains("beta") {
135+
return String::from("beta");
136+
} else if rustc_output.contains("stable") {
137+
return String::from("stable");
138+
}
144139
}
145-
},
140+
}
146141
}
142+
143+
// default to nightly
144+
String::from("nightly")
147145
}
148146

149147
#[cfg(test)]

0 commit comments

Comments
 (0)