Skip to content

Commit 98373de

Browse files
authored
feat: log git errors (#1691)
1 parent a8e4a7a commit 98373de

File tree

1 file changed

+40
-52
lines changed

1 file changed

+40
-52
lines changed

src/source/git_source.rs

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{
44
io::IsTerminal,
55
path::{Path, PathBuf},
6-
process::Command,
6+
process::{Command, Output},
77
};
88

99
use crate::system_tools::{SystemTools, Tool};
@@ -133,6 +133,32 @@ fn git_command(system_tools: &SystemTools, sub_cmd: &str) -> Result<Command, Too
133133
Ok(command)
134134
}
135135

136+
/// Run a git command and log precisely what went wrong.
137+
fn run_git_command(command: &mut Command) -> Result<Output, SourceError> {
138+
let output = command
139+
.output()
140+
.map_err(|_err| SourceError::GitErrorStr("could not execute git"))?;
141+
142+
if !output.status.success() {
143+
tracing::error!("Command failed: {:?}", command);
144+
tracing::error!(
145+
"Command output: {}",
146+
String::from_utf8_lossy(&output.stdout)
147+
);
148+
tracing::error!(
149+
"Command stderr: {}",
150+
String::from_utf8_lossy(&output.stderr)
151+
);
152+
153+
return Err(SourceError::GitError(format!(
154+
"failed to run command: {:?}",
155+
command
156+
)));
157+
}
158+
159+
Ok(output)
160+
}
161+
136162
/// Fetch the git repository specified by the given source and place it in the cache directory.
137163
pub fn git_src(
138164
system_tools: &SystemTools,
@@ -200,22 +226,12 @@ pub fn git_src(
200226
.args([
201227
// Avoid overhead of fetching unused tags.
202228
"--no-tags",
203-
"--progress",
204229
"-n",
205230
source.url().to_string().as_str(),
206231
])
207232
.arg(cache_path.as_os_str());
208233

209-
let output = command
210-
.output()
211-
.map_err(|_e| SourceError::GitErrorStr("Failed to execute clone command"))?;
212-
213-
if !output.status.success() {
214-
return Err(SourceError::GitError(format!(
215-
"Git clone failed for source: {}",
216-
String::from_utf8_lossy(&output.stderr)
217-
)));
218-
}
234+
let _ = run_git_command(&mut command)?;
219235
}
220236

221237
assert!(cache_path.exists());
@@ -247,31 +263,17 @@ pub fn git_src(
247263
command.args(["--depth", depth.to_string().as_str()]);
248264
}
249265

250-
let output = command
251-
.output()
252-
.map_err(|_| SourceError::ValidationFailed)?;
253-
254-
if !output.status.success() {
255-
tracing::error!("Command failed: {:?}", command);
256-
return Err(SourceError::GitErrorStr(
257-
"failed to execute clone from file",
258-
));
259-
}
266+
let _ = run_git_command(&mut command)?;
260267
}
261268
}
262269

263270
// Resolve the reference and set the head to the specified revision.
264-
let output = Command::new("git")
265-
.current_dir(&cache_path)
266-
// make sure that we get the commit, not the annotated tag
267-
.args(["rev-parse", &format!("{}^{{commit}}", rev)])
268-
.output()
269-
.map_err(|_| SourceError::GitErrorStr("git rev-parse failed"))?;
270-
271-
if !output.status.success() {
272-
tracing::error!("Command failed: `git rev-parse \"{}\"`", &rev);
273-
return Err(SourceError::GitErrorStr("failed to get valid hash for rev"));
274-
}
271+
let output = run_git_command(
272+
Command::new("git")
273+
.current_dir(&cache_path)
274+
// make sure that we get the commit, not the annotated tag
275+
.args(["rev-parse", &format!("{}^{{commit}}", rev)]),
276+
)?;
275277

276278
let ref_git = String::from_utf8(output.stdout)
277279
.map_err(|_| SourceError::GitErrorStr("failed to parse git rev as utf-8"))?
@@ -294,36 +296,22 @@ pub fn git_src(
294296

295297
fn git_lfs_pull(git_ref: &str) -> Result<(), SourceError> {
296298
// verify git-lfs is installed
297-
let mut command = Command::new("git");
298-
command.args(["lfs", "ls-files"]);
299-
let output = command
299+
let output = Command::new("git")
300+
.args(["lfs", "ls-files"])
300301
.output()
301302
.map_err(|_| SourceError::GitErrorStr("failed to execute command"))?;
303+
302304
if !output.status.success() {
303305
return Err(SourceError::GitErrorStr(
304306
"git-lfs not installed, but required",
305307
));
306308
}
307309

308310
// git lfs fetch
309-
let mut command = Command::new("git");
310-
command.args(["lfs", "fetch", "origin", git_ref]);
311-
let output = command
312-
.output()
313-
.map_err(|_| SourceError::GitErrorStr("failed to execute command"))?;
314-
if !output.status.success() {
315-
return Err(SourceError::GitErrorStr("`git lfs fetch` failed!"));
316-
}
311+
run_git_command(Command::new("git").args(["lfs", "fetch", "origin", git_ref]))?;
317312

318313
// git lfs checkout
319-
let mut command = Command::new("git");
320-
command.args(["lfs", "checkout"]);
321-
let output = command
322-
.output()
323-
.map_err(|_| SourceError::GitErrorStr("failed to execute command"))?;
324-
if !output.status.success() {
325-
return Err(SourceError::GitErrorStr("`git lfs checkout` failed!"));
326-
}
314+
run_git_command(Command::new("git").args(["lfs", "checkout"]))?;
327315

328316
Ok(())
329317
}

0 commit comments

Comments
 (0)