|
1 | 1 | #![cfg_attr(feature = "deny-warnings", deny(warnings))]
|
2 | 2 |
|
| 3 | +use std::str; |
| 4 | + |
3 | 5 | /// This macro creates the version string during compilation from the
|
4 | 6 | /// current environment
|
5 | 7 | #[macro_export]
|
@@ -101,49 +103,45 @@ impl std::fmt::Debug for VersionInfo {
|
101 | 103 |
|
102 | 104 | #[must_use]
|
103 | 105 | pub fn get_commit_hash() -> Option<String> {
|
104 |
| - std::process::Command::new("git") |
| 106 | + let output = std::process::Command::new("git") |
105 | 107 | .args(["rev-parse", "--short", "HEAD"])
|
106 | 108 | .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() |
109 | 112 | }
|
110 | 113 |
|
111 | 114 | #[must_use]
|
112 | 115 | pub fn get_commit_date() -> Option<String> {
|
113 |
| - std::process::Command::new("git") |
| 116 | + let output = std::process::Command::new("git") |
114 | 117 | .args(["log", "-1", "--date=short", "--pretty=format:%cd"])
|
115 | 118 | .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() |
118 | 122 | }
|
119 | 123 |
|
120 | 124 | #[must_use]
|
121 | 125 | 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 | + } |
144 | 139 | }
|
145 |
| - }, |
| 140 | + } |
146 | 141 | }
|
| 142 | + |
| 143 | + // default to nightly |
| 144 | + String::from("nightly") |
147 | 145 | }
|
148 | 146 |
|
149 | 147 | #[cfg(test)]
|
|
0 commit comments