@@ -472,7 +472,9 @@ static void clone_local(const char *src_repo, const char *dest_repo)
472472}
473473
474474static const char * junk_work_tree ;
475+ static int junk_work_tree_flags ;
475476static const char * junk_git_dir ;
477+ static int junk_git_dir_flags ;
476478static enum {
477479 JUNK_LEAVE_NONE ,
478480 JUNK_LEAVE_REPO ,
@@ -501,12 +503,12 @@ static void remove_junk(void)
501503
502504 if (junk_git_dir ) {
503505 strbuf_addstr (& sb , junk_git_dir );
504- remove_dir_recursively (& sb , 0 );
506+ remove_dir_recursively (& sb , junk_git_dir_flags );
505507 strbuf_reset (& sb );
506508 }
507509 if (junk_work_tree ) {
508510 strbuf_addstr (& sb , junk_work_tree );
509- remove_dir_recursively (& sb , 0 );
511+ remove_dir_recursively (& sb , junk_work_tree_flags );
510512 }
511513 strbuf_release (& sb );
512514}
@@ -863,10 +865,15 @@ static void dissociate_from_references(void)
863865 free (alternates );
864866}
865867
868+ static int dir_exists (const char * path )
869+ {
870+ struct stat sb ;
871+ return !stat (path , & sb );
872+ }
873+
866874int cmd_clone (int argc , const char * * argv , const char * prefix )
867875{
868876 int is_bundle = 0 , is_local ;
869- struct stat buf ;
870877 const char * repo_name , * repo , * work_tree , * git_dir ;
871878 char * path , * dir ;
872879 int dest_exists ;
@@ -940,7 +947,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
940947 dir = guess_dir_name (repo_name , is_bundle , option_bare );
941948 strip_trailing_slashes (dir );
942949
943- dest_exists = ! stat (dir , & buf );
950+ dest_exists = dir_exists (dir );
944951 if (dest_exists && !is_empty_dir (dir ))
945952 die (_ ("destination path '%s' already exists and is not "
946953 "an empty directory." ), dir );
@@ -951,7 +958,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
951958 work_tree = NULL ;
952959 else {
953960 work_tree = getenv ("GIT_WORK_TREE" );
954- if (work_tree && ! stat (work_tree , & buf ))
961+ if (work_tree && dir_exists (work_tree ))
955962 die (_ ("working tree '%s' already exists." ), work_tree );
956963 }
957964
@@ -969,14 +976,24 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
969976 if (safe_create_leading_directories_const (work_tree ) < 0 )
970977 die_errno (_ ("could not create leading directories of '%s'" ),
971978 work_tree );
972- if (!dest_exists && mkdir (work_tree , 0777 ))
979+ if (dest_exists )
980+ junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL ;
981+ else if (mkdir (work_tree , 0777 ))
973982 die_errno (_ ("could not create work tree dir '%s'" ),
974983 work_tree );
975984 junk_work_tree = work_tree ;
976985 set_git_work_tree (work_tree );
977986 }
978987
979- junk_git_dir = real_git_dir ? real_git_dir : git_dir ;
988+ if (real_git_dir ) {
989+ if (dir_exists (real_git_dir ))
990+ junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL ;
991+ junk_git_dir = real_git_dir ;
992+ } else {
993+ if (dest_exists )
994+ junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL ;
995+ junk_git_dir = git_dir ;
996+ }
980997 if (safe_create_leading_directories_const (git_dir ) < 0 )
981998 die (_ ("could not create leading directories of '%s'" ), git_dir );
982999
0 commit comments