@@ -93,57 +93,52 @@ pub fn git_update(repo_path: &Path, repo_url: &str, revision: Option<&str>) -> R
93
93
//
94
94
let tmp_remote_name = random_remote_name ( ) ;
95
95
96
- let command = format ! ( "git remote add \" {}\" \" {}\" " , tmp_remote_name, repo_url) ;
97
- let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
98
-
99
96
// Create a temporary remote
100
- Command :: new ( & command_vec[ 0 ] )
101
- . args ( & command_vec[ 1 ..] )
102
- . current_dir ( repo_path)
103
- . stdout ( Stdio :: null ( ) )
104
- . status ( )
105
- . with_context ( || {
106
- format ! (
107
- "Error with adding {} as a remote named {} in {}" ,
108
- repo_url,
109
- tmp_remote_name,
110
- repo_path. display( )
111
- )
112
- } ) ?;
97
+ safe_command (
98
+ format ! ( "git remote add \" {}\" \" {}\" " , tmp_remote_name, repo_url) ,
99
+ repo_path,
100
+ ) ?
101
+ . current_dir ( repo_path)
102
+ . stdout ( Stdio :: null ( ) )
103
+ . status ( )
104
+ . with_context ( || {
105
+ format ! (
106
+ "Error with adding {} as a remote named {} in {}" ,
107
+ repo_url,
108
+ tmp_remote_name,
109
+ repo_path. display( )
110
+ )
111
+ } ) ?;
113
112
114
113
let revision_str = revision. unwrap_or ( "main" ) ;
115
114
let res = git_to_revision ( repo_path, & tmp_remote_name, revision_str) ;
116
115
117
116
if let Err ( e) = res {
118
117
// Failed to switch to the desired revision. Cleanup!
119
- Command :: new ( "git" )
120
- . args ( vec ! [ "remote" , "rm" , & tmp_remote_name] )
121
- . current_dir ( repo_path)
118
+ safe_command ( format ! ( "git remote rm \" {}\" " , & tmp_remote_name) , repo_path) ?
122
119
. stdout ( Stdio :: null ( ) )
123
120
. status ( )
124
- . with_context ( || format ! ( "Failed to execute process in {}" , repo_path. display( ) ) ) ?;
121
+ . with_context ( || {
122
+ format ! (
123
+ "Failed to remove temporary remote {} in {}" ,
124
+ tmp_remote_name,
125
+ repo_path. display( )
126
+ )
127
+ } ) ?;
125
128
return Err ( e) ;
126
129
}
127
130
128
- let command = format ! ( "git remote set-url origin \" {}\" " , repo_url) ;
129
- let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
130
-
131
- // Success! Cleanup: update the origin remote to remote URL & delete temporary remote.
132
- Command :: new ( & command_vec[ 0 ] )
133
- . args ( & command_vec[ 1 ..] )
134
- . current_dir ( repo_path)
135
- . stdout ( Stdio :: null ( ) )
136
- . status ( )
137
- . with_context ( || {
138
- format ! (
139
- "Failed to set origin remote to {} in {}" ,
140
- repo_url,
141
- repo_path. display( )
142
- )
143
- } ) ?;
144
- Command :: new ( "git" )
145
- . args ( vec ! [ "remote" , "rm" , & tmp_remote_name] )
146
- . current_dir ( repo_path)
131
+ safe_command ( format ! ( "git remote set-url origin \" {}\" " , repo_url) , repo_path) ?
132
+ . stdout ( Stdio :: null ( ) )
133
+ . status ( )
134
+ . with_context ( || {
135
+ format ! (
136
+ "Failed to set origin remote to {} in {}" ,
137
+ repo_url,
138
+ repo_path. display( )
139
+ )
140
+ } ) ?;
141
+ safe_command ( format ! ( "git remote rm \" {}\" " , tmp_remote_name) , repo_path) ?
147
142
. stdout ( Stdio :: null ( ) )
148
143
. status ( )
149
144
. with_context ( || {
@@ -282,7 +277,6 @@ fn git_resolve_revision(repo_path: &Path, remote_name: &str, revision: &str) ->
282
277
} ) ?;
283
278
let stdout = child. stdout . take ( ) . expect ( "failed to capture stdout" ) ;
284
279
let reader = BufReader :: new ( stdout) ;
285
-
286
280
for line in reader. lines ( ) {
287
281
match line {
288
282
Ok ( line) => {
@@ -317,43 +311,41 @@ fn safe_command(command: String, cwd: &Path) -> Result<Command, Error> {
317
311
}
318
312
319
313
fn git_to_revision ( repo_path : & Path , remote_name : & str , revision : & str ) -> Result < ( ) > {
320
- let command = format ! ( "git fetch --quiet \" {}\" \" {}\" " , remote_name, revision) ;
321
- let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
322
-
323
- Command :: new ( & command_vec[ 0 ] )
324
- . args ( & command_vec[ 1 ..] )
325
- . current_dir ( repo_path)
326
- . stdout ( Stdio :: null ( ) )
327
- . status ( )
328
- . with_context ( || {
329
- format ! (
330
- "Error with fetching revision {} in {}" ,
331
- revision,
332
- repo_path. display( )
333
- )
334
- } ) ?;
314
+ // Download the object from the remote
315
+ safe_command (
316
+ format ! ( "git fetch --quiet \" {}\" \" {}\" " , remote_name, revision) ,
317
+ repo_path,
318
+ ) ?
319
+ . status ( )
320
+ . with_context ( || {
321
+ format ! (
322
+ "Error with fetching revision {} in {}" ,
323
+ revision,
324
+ repo_path. display( )
325
+ )
326
+ } ) ?;
335
327
336
- // Normalize the revision into the SHA. let command = format!("git rev-parse \"{}/{}\"", remote_name, revision);
328
+ // Normalize the revision into the SHA.
329
+ // let command = format!("git rev-parse \"{}/{}\"", remote_name, revision);
337
330
let commit_sha = git_resolve_revision ( repo_path, remote_name, revision) ?;
338
331
339
- let command = format ! (
340
- "git -c advice.detachedHead=false checkout --quiet \" {}\" " ,
341
- commit_sha
342
- ) ;
343
- let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
344
-
345
- Command :: new ( & command_vec[ 0 ] )
346
- . args ( & command_vec[ 1 ..] )
347
- . stdout ( Stdio :: null ( ) )
348
- . current_dir ( repo_path)
349
- . status ( )
350
- . with_context ( || {
351
- format ! (
352
- "Failed to checkout SHA {} in {}" ,
353
- commit_sha,
354
- repo_path. display( )
355
- )
356
- } ) ?;
332
+ safe_command (
333
+ format ! (
334
+ "git -c advice.detachedHead=false checkout --quiet \" {}\" " ,
335
+ commit_sha
336
+ ) ,
337
+ repo_path,
338
+ ) ?
339
+ . stdout ( Stdio :: null ( ) )
340
+ . current_dir ( repo_path)
341
+ . status ( )
342
+ . with_context ( || {
343
+ format ! (
344
+ "Failed to checkout SHA {} in {}" ,
345
+ commit_sha,
346
+ repo_path. display( )
347
+ )
348
+ } ) ?;
357
349
358
350
Ok ( ( ) )
359
351
}
0 commit comments