Skip to content

Commit 61aeaa1

Browse files
committed
fix git switch
1 parent 68c6fe1 commit 61aeaa1

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

check_diff/src/lib.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::io;
33
use std::path::{Path, PathBuf};
4-
use std::process::{Command, Output};
4+
use std::process::Command;
55
use std::str::Utf8Error;
66
use tracing::info;
77

@@ -66,27 +66,18 @@ pub struct RustfmtRunner {
6666
}
6767

6868
impl RustfmtRunner {
69-
// will be used in future PRs, just added to make the compiler happy
70-
#[allow(dead_code)]
71-
fn run(&self, args: &[&str]) -> io::Result<Output> {
72-
Command::new(&self.binary_path)
73-
.env("LD_LIBRARY_PATH", &self.ld_library_path)
74-
.args(args)
75-
.output()
76-
}
77-
7869
fn get_binary_version(&self) -> Result<String, CheckDiffError> {
7970
let Ok(command) = Command::new(&self.binary_path)
80-
.env("LD_LIB_PATH", &self.ld_library_path)
71+
.env("LD_LIBRARY_PATH", &self.ld_library_path)
8172
.args(["--version"])
8273
.output()
8374
else {
8475
return Err(CheckDiffError::FailedBinaryVersioning(
85-
self.binary_path.to_path_buf(),
76+
self.binary_path.clone(),
8677
));
8778
};
8879

89-
let binary_version = std::str::from_utf8(&command.stdout)?.trim_end();
80+
let binary_version = std::str::from_utf8(&command.stdout)?.trim();
9081
return Ok(binary_version.to_string());
9182
}
9283
}
@@ -161,21 +152,21 @@ pub fn git_fetch(branch_name: &str) -> Result<(), GitError> {
161152
return Ok(());
162153
}
163154

164-
pub fn git_switch(arg: &str, should_detach: bool) -> Result<(), GitError> {
155+
pub fn git_switch(git_ref: &str, should_detach: bool) -> Result<(), GitError> {
165156
let detach_arg = if should_detach { "--detach" } else { "" };
166-
let git_cmd = Command::new("git")
167-
.args(["switch", arg, detach_arg])
157+
let args = ["switch", git_ref, detach_arg];
158+
let output = Command::new("git")
159+
.args(args.iter().filter(|arg| !arg.is_empty()))
168160
.output()?;
169-
170-
if !git_cmd.status.success() {
161+
if !output.status.success() {
162+
tracing::error!("Git switch failed: {output:?}");
171163
let error = GitError::FailedSwitch {
172-
stdout: git_cmd.stdout,
173-
stderr: git_cmd.stderr,
164+
stdout: output.stdout,
165+
stderr: output.stderr,
174166
};
175167
return Err(error);
176168
}
177-
178-
info!("Successfully switched to {}", arg);
169+
info!("Successfully switched to {git_ref}");
179170
return Ok(());
180171
}
181172

@@ -189,14 +180,12 @@ pub fn change_directory_to_path(dest: &Path) -> io::Result<()> {
189180
return Ok(());
190181
}
191182

192-
pub fn get_ld_lib_path() -> Result<String, CheckDiffError> {
183+
pub fn get_ld_library_path() -> Result<String, CheckDiffError> {
193184
let Ok(command) = Command::new("rustc").args(["--print", "sysroot"]).output() else {
194185
return Err(CheckDiffError::FailedCommand("Error getting sysroot"));
195186
};
196-
197187
let sysroot = std::str::from_utf8(&command.stdout)?.trim_end();
198-
199-
let ld_lib_path = format!("{}/lib", sysroot.trim_end());
188+
let ld_lib_path = format!("{}/lib", sysroot);
200189
return Ok(ld_lib_path);
201190
}
202191

@@ -218,7 +207,7 @@ pub fn build_rustfmt_from_src(binary_path: PathBuf) -> Result<RustfmtRunner, Che
218207
// binary can find it's runtime dependencies.
219208
// See https://github.com/rust-lang/rustfmt/issues/5675
220209
// This will prepend the `LD_LIBRARY_PATH` for the master rustfmt binary
221-
let ld_lib_path = get_ld_lib_path()?;
210+
let ld_lib_path = get_ld_library_path()?;
222211

223212
info!("Building rustfmt from source");
224213
let Ok(_) = Command::new("cargo")
@@ -257,16 +246,14 @@ pub fn compile_rustfmt(
257246

258247
let cargo_version = get_cargo_version()?;
259248
info!("Compiling with {}", cargo_version);
260-
let src_runner = build_rustfmt_from_src(dest.join("/rustfmt"))?;
261-
249+
let src_runner = build_rustfmt_from_src(dest.join("src_rustfmt"))?;
262250
let should_detach = commit_hash.is_some();
263251
git_switch(
264252
commit_hash.unwrap_or(feature_branch).as_str(),
265253
should_detach,
266254
)?;
267255

268-
let feature_runner = build_rustfmt_from_src(dest.join("/feature_rustfmt"))?;
269-
256+
let feature_runner = build_rustfmt_from_src(dest.join("feature_rustfmt"))?;
270257
info!("RUSFMT_BIN {}", src_runner.get_binary_version()?);
271258
info!(
272259
"Runtime dependencies for (src) rustfmt -- LD_LIBRARY_PATH: {}",

check_diff/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ struct CliInputs {
2020
}
2121

2222
fn main() {
23+
tracing_subscriber::fmt()
24+
.with_env_filter(tracing_subscriber::EnvFilter::from_env("CHECK_DIFF_LOG"))
25+
.init();
2326
let args = CliInputs::parse();
2427
let tmp_dir = Builder::new().tempdir_in("").unwrap();
2528
info!("Created tmp_dir {:?}", tmp_dir);

0 commit comments

Comments
 (0)