Skip to content

Commit db2e220

Browse files
peffgitster
authored andcommitted
clone: use computed length in guess_dir_name
Commit 7e837c6 (clone: simplify string handling in guess_dir_name(), 2015-07-09) changed clone to use strip_suffix instead of hand-rolled pointer manipulation. However, strip_suffix will strip from the end of a NUL-terminated string, and we may have already stripped some characters (like directory separators, or "/.git"). This leads to commands like: git clone host:foo.git/ failing to strip the ".git". We must instead convert our pointer arithmetic into a computed length and feed that to strip_suffix_mem, which will then reduce the length further for us. It would be nicer if we could drop the pointer manipulation entirely, and just continually strip using strip_suffix. But that doesn't quite work for two reasons: 1. The early suffixes we're stripping are not constant; we need to look for is_dir_sep, which could be one of several characters. 2. Mid-way through the stripping we compute the pointer "start", which shows us the beginning of the pathname. Which really give us two lengths to work with: the offset from the start of the string, and from the start of the path. By using pointers for the early part, we can just compute the length from "start" when we need it. Signed-off-by: Jeff King <[email protected]> Acked-by: Sebastian Schuberth <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6a31e0 commit db2e220

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

builtin/clone.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
174174
/*
175175
* Strip .{bundle,git}.
176176
*/
177-
strip_suffix(start, is_bundle ? ".bundle" : ".git" , &len);
177+
len = end - start;
178+
strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
178179

179180
if (is_bare)
180181
dir = xstrfmt("%.*s.git", (int)len, start);

t/t5603-clone-dirname.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,30 @@ test_clone_dir host:foo foo.git bare
4747
test_clone_dir host:foo.git foo
4848
test_clone_dir host:foo.git foo.git bare
4949
test_clone_dir host:foo/.git foo
50-
test_clone_dir host:foo/.git foo.git bare fail
50+
test_clone_dir host:foo/.git foo.git bare
5151

5252
# similar, but using ssh URL rather than host:path syntax
5353
test_clone_dir ssh://host/foo foo
5454
test_clone_dir ssh://host/foo foo.git bare
5555
test_clone_dir ssh://host/foo.git foo
5656
test_clone_dir ssh://host/foo.git foo.git bare
5757
test_clone_dir ssh://host/foo/.git foo
58-
test_clone_dir ssh://host/foo/.git foo.git bare fail
58+
test_clone_dir ssh://host/foo/.git foo.git bare
5959

6060
# we should remove trailing slashes and .git suffixes
6161
test_clone_dir ssh://host/foo/ foo
6262
test_clone_dir ssh://host/foo/// foo
6363
test_clone_dir ssh://host/foo/.git/ foo
64-
test_clone_dir ssh://host/foo.git/ foo fail
65-
test_clone_dir ssh://host/foo.git/// foo fail
64+
test_clone_dir ssh://host/foo.git/ foo
65+
test_clone_dir ssh://host/foo.git/// foo
6666
test_clone_dir ssh://host/foo///.git/ foo
6767
test_clone_dir ssh://host/foo/.git/// foo
6868

6969
test_clone_dir host:foo/ foo
7070
test_clone_dir host:foo/// foo
71-
test_clone_dir host:foo.git/ foo fail
71+
test_clone_dir host:foo.git/ foo
7272
test_clone_dir host:foo/.git/ foo
73-
test_clone_dir host:foo.git/// foo fail
73+
test_clone_dir host:foo.git/// foo
7474
test_clone_dir host:foo///.git/ foo
7575
test_clone_dir host:foo/.git/// foo
7676

0 commit comments

Comments
 (0)