@matheusbsbs / matheus-bagliano / commits / 44a68fd526
Fork 0

clone: fix creation of explicitly named target directory

'git clone <repo> path/' (note the trailing slash) fails, because the entire path is interpreted as leading directories. So when mkdir tries to create the actual path, it already exists. This makes sure trailing slashes are removed. Signed-off-by: Clemens Buchacher <drizzd@aon.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Clemens Buchacher committed Sep 3, 2008 at 20:55 UTC 44a68fd526a70f0aaf213143e22f1257f296e724
2 files changed +32 -3
builtin-clone.c
+14 -3
@@ -147,6 +147,15 @@ static int is_directory(const char *path)
147 return !stat(path, &buf) && S_ISDIR(buf.st_mode);
148 }
149
150 +static void strip_trailing_slashes(char *dir)
151 +{
152 + char *end = dir + strlen(dir);
153 +
154 + while (dir < end - 1 && is_dir_sep(end[-1]))
155 + end--;
156 + *end = '\0';
157 +}
158 +
159 static void setup_reference(const char *repo)
160 {
161 const char *ref_git;
@@ -397,6 +406,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
406 dir = xstrdup(argv[1]);
407 else
408 dir = guess_dir_name(repo_name, is_bundle, option_bare);
409 + strip_trailing_slashes(dir);
410
411 if (!stat(dir, &buf))
412 die("destination directory '%s' already exists.", dir);
@@ -422,10 +432,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
432 if (!option_bare) {
433 junk_work_tree = work_tree;
434 if (safe_create_leading_directories_const(work_tree) < 0)
425 - die("could not create leading directories of '%s'",
426 - work_tree);
435 + die("could not create leading directories of '%s': %s",
436 + work_tree, strerror(errno));
437 if (mkdir(work_tree, 0755))
428 - die("could not create work tree dir '%s'.", work_tree);
438 + die("could not create work tree dir '%s': %s.",
439 + work_tree, strerror(errno));
440 set_git_work_tree(work_tree);
441 }
442 junk_git_dir = git_dir;
t/t5601-clone.sh
+18
@@ -107,4 +107,22 @@ test_expect_success 'clone --mirror does not repeat tags' '
107
108 '
109
110 +test_expect_success 'clone to destination with trailing /' '
111 +
112 + git clone src target-1/ &&
113 + T=$( cd target-1 && git rev-parse HEAD ) &&
114 + S=$( cd src && git rev-parse HEAD ) &&
115 + test "$T" = "$S"
116 +
117 +'
118 +
119 +test_expect_success 'clone to destination with extra trailing /' '
120 +
121 + git clone src target-2/// &&
122 + T=$( cd target-2 && git rev-parse HEAD ) &&
123 + S=$( cd src && git rev-parse HEAD ) &&
124 + test "$T" = "$S"
125 +
126 +'
127 +
128 test_done