3
3
use std:: {
4
4
io:: IsTerminal ,
5
5
path:: { Path , PathBuf } ,
6
- process:: Command ,
6
+ process:: { Command , Output } ,
7
7
} ;
8
8
9
9
use crate :: system_tools:: { SystemTools , Tool } ;
@@ -133,6 +133,32 @@ fn git_command(system_tools: &SystemTools, sub_cmd: &str) -> Result<Command, Too
133
133
Ok ( command)
134
134
}
135
135
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
+
136
162
/// Fetch the git repository specified by the given source and place it in the cache directory.
137
163
pub fn git_src (
138
164
system_tools : & SystemTools ,
@@ -200,22 +226,12 @@ pub fn git_src(
200
226
. args ( [
201
227
// Avoid overhead of fetching unused tags.
202
228
"--no-tags" ,
203
- "--progress" ,
204
229
"-n" ,
205
230
source. url ( ) . to_string ( ) . as_str ( ) ,
206
231
] )
207
232
. arg ( cache_path. as_os_str ( ) ) ;
208
233
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) ?;
219
235
}
220
236
221
237
assert ! ( cache_path. exists( ) ) ;
@@ -247,31 +263,17 @@ pub fn git_src(
247
263
command. args ( [ "--depth" , depth. to_string ( ) . as_str ( ) ] ) ;
248
264
}
249
265
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) ?;
260
267
}
261
268
}
262
269
263
270
// 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
+ ) ?;
275
277
276
278
let ref_git = String :: from_utf8 ( output. stdout )
277
279
. map_err ( |_| SourceError :: GitErrorStr ( "failed to parse git rev as utf-8" ) ) ?
@@ -294,36 +296,22 @@ pub fn git_src(
294
296
295
297
fn git_lfs_pull ( git_ref : & str ) -> Result < ( ) , SourceError > {
296
298
// 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" ] )
300
301
. output ( )
301
302
. map_err ( |_| SourceError :: GitErrorStr ( "failed to execute command" ) ) ?;
303
+
302
304
if !output. status . success ( ) {
303
305
return Err ( SourceError :: GitErrorStr (
304
306
"git-lfs not installed, but required" ,
305
307
) ) ;
306
308
}
307
309
308
310
// 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] ) ) ?;
317
312
318
313
// 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" ] ) ) ?;
327
315
328
316
Ok ( ( ) )
329
317
}
0 commit comments