Skip to content

Commit

Permalink
clone: fix creation of explicitly named target directory
Browse files Browse the repository at this point in the history
'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>
  • Loading branch information
Clemens Buchacher authored and Junio C Hamano committed Sep 3, 2008
1 parent db3a954 commit 44a68fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions builtin-clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ static int is_directory(const char *path)
return !stat(path, &buf) && S_ISDIR(buf.st_mode);
}

static void strip_trailing_slashes(char *dir)
{
char *end = dir + strlen(dir);

while (dir < end - 1 && is_dir_sep(end[-1]))
end--;
*end = '\0';
}

static void setup_reference(const char *repo)
{
const char *ref_git;
Expand Down Expand Up @@ -397,6 +406,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
dir = xstrdup(argv[1]);
else
dir = guess_dir_name(repo_name, is_bundle, option_bare);
strip_trailing_slashes(dir);

if (!stat(dir, &buf))
die("destination directory '%s' already exists.", dir);
Expand All @@ -422,10 +432,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
if (!option_bare) {
junk_work_tree = work_tree;
if (safe_create_leading_directories_const(work_tree) < 0)
die("could not create leading directories of '%s'",
work_tree);
die("could not create leading directories of '%s': %s",
work_tree, strerror(errno));
if (mkdir(work_tree, 0755))
die("could not create work tree dir '%s'.", work_tree);
die("could not create work tree dir '%s': %s.",
work_tree, strerror(errno));
set_git_work_tree(work_tree);
}
junk_git_dir = git_dir;
Expand Down
18 changes: 18 additions & 0 deletions t/t5601-clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,22 @@ test_expect_success 'clone --mirror does not repeat tags' '
'

test_expect_success 'clone to destination with trailing /' '
git clone src target-1/ &&
T=$( cd target-1 && git rev-parse HEAD ) &&
S=$( cd src && git rev-parse HEAD ) &&
test "$T" = "$S"
'

test_expect_success 'clone to destination with extra trailing /' '
git clone src target-2/// &&
T=$( cd target-2 && git rev-parse HEAD ) &&
S=$( cd src && git rev-parse HEAD ) &&
test "$T" = "$S"
'

test_done

0 comments on commit 44a68fd

Please sign in to comment.