@matheusbsbs / matheus-bagliano / commits / 8e21d63b02
Fork 0

clone: create intermediate directories of destination repo

The shell version used to use "mkdir -p" to create the repo path, but the C version just calls "mkdir". Let's replicate the old behavior. We have to create the git and worktree leading dirs separately; while most of the time, the worktree dir contains the git dir (as .git), the user can override this using GIT_WORK_TREE. We can reuse safe_create_leading_directories, but we need to make a copy of our const buffer to do so. Since merge-recursive uses the same pattern, we can factor this out into a global function. This has two other cleanup advantages for merge-recursive: 1. mkdir_p wasn't a very good name. "mkdir -p foo/bar" actually creates bar, but this function just creates the leading directories. 2. mkdir_p took a mode argument, but it was completely ignored. Acked-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 25, 2008 at 01:41 UTC 8e21d63b02f1b26f7695ca515e51e4622a995af2
5 files changed +39 -11
builtin-clone.c
+5
@@ -400,6 +400,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
400
401 if (!option_bare) {
402 junk_work_tree = work_tree;
403 + if (safe_create_leading_directories_const(work_tree) < 0)
404 + die("could not create leading directories of '%s'",
405 + work_tree);
406 if (mkdir(work_tree, 0755))
407 die("could not create work tree dir '%s'.", work_tree);
408 set_git_work_tree(work_tree);
@@ -410,6 +413,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
413
414 setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
415
416 + if (safe_create_leading_directories_const(git_dir) < 0)
417 + die("could not create leading directories of '%s'", git_dir);
418 set_git_dir(make_absolute_path(git_dir));
419
420 fprintf(stderr, "Initialize %s\n", git_dir);
builtin-merge-recursive.c
+2 -11
@@ -481,15 +481,6 @@ static char *unique_path(const char *path, const char *branch)
481 return newpath;
482 }
483
484 -static int mkdir_p(const char *path, unsigned long mode)
485 -{
486 - /* path points to cache entries, so xstrdup before messing with it */
487 - char *buf = xstrdup(path);
488 - int result = safe_create_leading_directories(buf);
489 - free(buf);
490 - return result;
491 -}
492 -
484 static void flush_buffer(int fd, const char *buf, unsigned long size)
485 {
486 while (size > 0) {
@@ -512,7 +503,7 @@ static int make_room_for_path(const char *path)
503 int status;
504 const char *msg = "failed to create path '%s'%s";
505
515 - status = mkdir_p(path, 0777);
506 + status = safe_create_leading_directories_const(path);
507 if (status) {
508 if (status == -3) {
509 /* something else exists */
@@ -583,7 +574,7 @@ static void update_file_flags(const unsigned char *sha,
574 close(fd);
575 } else if (S_ISLNK(mode)) {
576 char *lnk = xmemdupz(buf, size);
586 - mkdir_p(path, 0777);
577 + safe_create_leading_directories_const(path);
578 unlink(path);
579 symlink(lnk, path);
580 free(lnk);
cache.h
+1
@@ -517,6 +517,7 @@ enum sharedrepo {
517 int git_config_perm(const char *var, const char *value);
518 int adjust_shared_perm(const char *path);
519 int safe_create_leading_directories(char *path);
520 +int safe_create_leading_directories_const(const char *path);
521 char *enter_repo(char *path, int strict);
522 static inline int is_absolute_path(const char *path)
523 {
sha1_file.c
+9
@@ -116,6 +116,15 @@ int safe_create_leading_directories(char *path)
116 return 0;
117 }
118
119 +int safe_create_leading_directories_const(const char *path)
120 +{
121 + /* path points to cache entries, so xstrdup before messing with it */
122 + char *buf = xstrdup(path);
123 + int result = safe_create_leading_directories(buf);
124 + free(buf);
125 + return result;
126 +}
127 +
128 char *sha1_to_hex(const unsigned char *sha1)
129 {
130 static int bufno;
t/t5601-clone.sh
+22
@@ -30,4 +30,26 @@ test_expect_success 'clone checks out files' '
30
31 '
32
33 +test_expect_success 'clone respects GIT_WORK_TREE' '
34 +
35 + GIT_WORK_TREE=worktree git clone src bare &&
36 + test -f bare/config &&
37 + test -f worktree/file
38 +
39 +'
40 +
41 +test_expect_success 'clone creates intermediate directories' '
42 +
43 + git clone src long/path/to/dst &&
44 + test -f long/path/to/dst/file
45 +
46 +'
47 +
48 +test_expect_success 'clone creates intermediate directories for bare repo' '
49 +
50 + git clone --bare src long/path/to/bare/dst &&
51 + test -f long/path/to/bare/dst/config
52 +
53 +'
54 +
55 test_done