1
1
use clap:: Parser ;
2
2
use std:: env;
3
+ use std:: io:: { self , Write } ;
4
+ use std:: path:: Path ;
3
5
use std:: process:: Command ;
4
6
5
7
/// Inputs for the check_diff script
@@ -18,22 +20,36 @@ struct CliInputs {
18
20
rustfmt_config : Option < Vec < String > > ,
19
21
}
20
22
21
- fn clone_git_repo ( url : String , dest : String ) {
23
+ /// Clone a git repository and cd into it.
24
+ ///
25
+ /// Parameters:
26
+ /// url: git clone url
27
+ /// dest: directory where the repo should be cloned
28
+ fn clone_git_repo ( url : & str , dest : & str ) {
22
29
env:: set_var ( "GIT_TERMINAL_PROMPT" , "0" ) ;
23
- let mut git_cmd = Command :: new ( "git" ) ;
24
- git_cmd. args ( [ "clone" , "--quiet" , url, "--depth" , "1" , "dest" ] ) ;
25
- git_cmd. output ( ) . expect ( "failed to clone repository" ) ;
26
- let mut enter_dest_dir = Command :: new ( "cd" )
27
- . arg ( dest)
30
+ let git_cmd = Command :: new ( "git" )
31
+ . args ( [ "clone" , "--quiet" , url, "--depth" , "1" , dest] )
28
32
. output ( )
29
- . expect ( "failed to enter directory." ) ;
30
- }
33
+ . expect ( "failed to clone repository" ) ;
34
+ // if the git command does not return successfully,
35
+ // cd command will not work as well. So fail fast.
36
+ if !git_cmd. status . success ( ) {
37
+ io:: stdout ( ) . write_all ( & git_cmd. stdout ) . unwrap ( ) ;
38
+ io:: stderr ( ) . write_all ( & git_cmd. stderr ) . unwrap ( ) ;
39
+ return ;
40
+ }
41
+ println ! ( "Successfully clone repository. Entering repository" ) ;
31
42
32
- fn main ( ) {
33
- let args = CliInputs :: parse ( ) ;
43
+ let dest_path = Path :: new ( & dest ) ;
44
+ let _ = env :: set_current_dir ( & dest_path ) . is_ok ( ) ;
34
45
println ! (
35
- "remote_repo_url: {:?}, feature_branch: {:?},
36
- optional_commit_hash: {:?}, optional_rustfmt_config: {:?}" ,
37
- args. remote_repo_url, args. feature_branch, args. commit_hash, args. rustfmt_config
46
+ "Current directory: {}" ,
47
+ env:: current_dir( ) . unwrap( ) . display( )
38
48
) ;
39
49
}
50
+
51
+ fn main ( ) {
52
+ let _args = CliInputs :: parse ( ) ;
53
+ let sample_repo = "https://github.com/rust-lang/rustfmt.git" ;
54
+ clone_git_repo ( sample_repo, "./tmp/" ) ;
55
+ }
0 commit comments