@@ -222,6 +222,10 @@ impl GitSource {
222222 }
223223 /// # 把浅克隆的仓库变成深克隆
224224 fn unshallow ( & self , target_dir : & CacheDir ) -> Result < ( ) , String > {
225+ if self . is_shallow ( target_dir) ? == false {
226+ return Ok ( ( ) ) ;
227+ }
228+
225229 let mut cmd = Command :: new ( "git" ) ;
226230 cmd. current_dir ( & target_dir. path ) ;
227231 cmd. arg ( "fetch" ) . arg ( "--unshallow" ) ;
@@ -245,6 +249,30 @@ impl GitSource {
245249 return Ok ( ( ) ) ;
246250 }
247251
252+ /// 判断当前仓库是否是浅克隆
253+ fn is_shallow ( & self , target_dir : & CacheDir ) -> Result < bool , String > {
254+ let mut cmd = Command :: new ( "git" ) ;
255+ cmd. current_dir ( & target_dir. path ) ;
256+ cmd. arg ( "rev-parse" ) . arg ( "--is-shallow-repository" ) ;
257+
258+ let proc: std:: process:: Child = cmd
259+ . stderr ( Stdio :: piped ( ) )
260+ . spawn ( )
261+ . map_err ( |e| e. to_string ( ) ) ?;
262+ let output = proc. wait_with_output ( ) . map_err ( |e| e. to_string ( ) ) ?;
263+
264+ if !output. status . success ( ) {
265+ return Err ( format ! (
266+ "Failed to check if shallow {}, message: {}" ,
267+ target_dir. path. display( ) ,
268+ StdioUtils :: tail_n_str( StdioUtils :: stderr_to_lines( & output. stderr) , 5 )
269+ ) ) ;
270+ }
271+
272+ let is_shallow = String :: from_utf8_lossy ( & output. stdout ) . trim ( ) == "true" ;
273+ return Ok ( is_shallow) ;
274+ }
275+
248276 fn fetch_all ( & self , target_dir : & CacheDir ) -> Result < ( ) , String > {
249277 self . set_fetch_config ( target_dir) ?;
250278 let mut cmd = Command :: new ( "git" ) ;
0 commit comments