@@ -2,10 +2,11 @@ use std::path::Path;
22
33use clap:: { App , Arg , SubCommand } ;
44use colored:: * ;
5+ use git2:: { Error as GitError , FetchOptions , RemoteCallbacks } ;
56use git2:: build:: RepoBuilder ;
6- use git2:: Error as GitError ;
77use serde:: { Deserialize , Serialize } ;
88
9+ use crate :: conf;
910use crate :: git:: GitAction ;
1011use crate :: input_args:: InputArgs ;
1112
@@ -81,21 +82,41 @@ pub fn clone(args: InputArgs, mut clone_repos: Vec<GitRepo>) {
8182 }
8283
8384 for remote in remotes_from_args {
84- let mut clone = GitClone { remote_url : remote. remote_url . as_str ( ) , local_path : Path :: new ( remote. local_path . as_str ( ) ) } ;
85+ let mut use_ssh = false ;
86+ if remote. remote_url . contains ( "git@" ) {
87+ use_ssh = true ;
88+ }
89+
90+ let mut clone = GitClone {
91+ remote_url : remote. remote_url . as_str ( ) ,
92+ local_path : Path :: new ( remote. local_path . as_str ( ) ) ,
93+ use_ssh : use_ssh,
94+ } ;
8595 clone. git_action ( ) . expect ( format ! ( "Failed to clone repo {}, " , remote. remote_url) . as_str ( ) ) ;
8696 }
8797}
8898
8999pub struct GitClone < ' a > {
90100 pub remote_url : & ' a str ,
91101 pub local_path : & ' a Path ,
102+ pub use_ssh : bool ,
92103}
93104
94- // Todo: Add spinner to show progress.
95105impl < ' a > GitAction for GitClone < ' a > {
96106 fn git_action ( & mut self ) -> Result < ( ) , GitError > {
97- RepoBuilder :: new ( )
98- . clone ( self . remote_url , self . local_path ) ?;
107+ let mut builder = RepoBuilder :: new ( ) ;
108+
109+ if self . use_ssh {
110+ let mut callback = RemoteCallbacks :: new ( ) ;
111+ callback. credentials ( conf:: ssh_auth_callback) ;
112+
113+ let mut fetch_options = FetchOptions :: new ( ) ;
114+ fetch_options. remote_callbacks ( callback) ;
115+
116+ builder. fetch_options ( fetch_options) ;
117+ }
118+
119+ builder. clone ( self . remote_url , self . local_path ) ?;
99120 println ! ( "{} - {} {} {:#?}" , "Remote repo" . green( ) , self . remote_url. blue( ) , "cloned locally at" . green( ) ,
100121 self . local_path. as_os_str( ) ) ;
101122
0 commit comments