@@ -473,7 +473,9 @@ static void clone_local(const char *src_repo, const char *dest_repo)
473
473
}
474
474
475
475
static const char * junk_work_tree ;
476
+ static int junk_work_tree_flags ;
476
477
static const char * junk_git_dir ;
478
+ static int junk_git_dir_flags ;
477
479
static enum {
478
480
JUNK_LEAVE_NONE ,
479
481
JUNK_LEAVE_REPO ,
@@ -502,12 +504,12 @@ static void remove_junk(void)
502
504
503
505
if (junk_git_dir ) {
504
506
strbuf_addstr (& sb , junk_git_dir );
505
- remove_dir_recursively (& sb , 0 );
507
+ remove_dir_recursively (& sb , junk_git_dir_flags );
506
508
strbuf_reset (& sb );
507
509
}
508
510
if (junk_work_tree ) {
509
511
strbuf_addstr (& sb , junk_work_tree );
510
- remove_dir_recursively (& sb , 0 );
512
+ remove_dir_recursively (& sb , junk_work_tree_flags );
511
513
}
512
514
strbuf_release (& sb );
513
515
}
@@ -863,10 +865,15 @@ static void dissociate_from_references(void)
863
865
free (alternates );
864
866
}
865
867
868
+ static int dir_exists (const char * path )
869
+ {
870
+ struct stat sb ;
871
+ return !stat (path , & sb );
872
+ }
873
+
866
874
int cmd_clone (int argc , const char * * argv , const char * prefix )
867
875
{
868
876
int is_bundle = 0 , is_local ;
869
- struct stat buf ;
870
877
const char * repo_name , * repo , * work_tree , * git_dir ;
871
878
char * path , * dir ;
872
879
int dest_exists ;
@@ -940,7 +947,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
940
947
dir = guess_dir_name (repo_name , is_bundle , option_bare );
941
948
strip_trailing_slashes (dir );
942
949
943
- dest_exists = ! stat (dir , & buf );
950
+ dest_exists = dir_exists (dir );
944
951
if (dest_exists && !is_empty_dir (dir ))
945
952
die (_ ("destination path '%s' already exists and is not "
946
953
"an empty directory." ), dir );
@@ -951,7 +958,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
951
958
work_tree = NULL ;
952
959
else {
953
960
work_tree = getenv ("GIT_WORK_TREE" );
954
- if (work_tree && ! stat (work_tree , & buf ))
961
+ if (work_tree && dir_exists (work_tree ))
955
962
die (_ ("working tree '%s' already exists." ), work_tree );
956
963
}
957
964
@@ -969,14 +976,24 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
969
976
if (safe_create_leading_directories_const (work_tree ) < 0 )
970
977
die_errno (_ ("could not create leading directories of '%s'" ),
971
978
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 ))
973
982
die_errno (_ ("could not create work tree dir '%s'" ),
974
983
work_tree );
975
984
junk_work_tree = work_tree ;
976
985
set_git_work_tree (work_tree );
977
986
}
978
987
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
+ }
980
997
if (safe_create_leading_directories_const (git_dir ) < 0 )
981
998
die (_ ("could not create leading directories of '%s'" ), git_dir );
982
999
0 commit comments