From db2e220447f7b02278d64417c8f05f73710f5b8b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 10 Aug 2015 17:48:22 +0200 Subject: 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 Acked-by: Sebastian Schuberth Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- builtin/clone.c | 3 ++- t/t5603-clone-dirname.sh | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index e18839d107..ed484cb6f4 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -174,7 +174,8 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare) /* * Strip .{bundle,git}. */ - strip_suffix(start, is_bundle ? ".bundle" : ".git" , &len); + len = end - start; + strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git"); if (is_bare) dir = xstrfmt("%.*s.git", (int)len, start); diff --git a/t/t5603-clone-dirname.sh b/t/t5603-clone-dirname.sh index 52f61a090a..765cc434ef 100755 --- a/t/t5603-clone-dirname.sh +++ b/t/t5603-clone-dirname.sh @@ -47,7 +47,7 @@ test_clone_dir host:foo foo.git bare test_clone_dir host:foo.git foo test_clone_dir host:foo.git foo.git bare test_clone_dir host:foo/.git foo -test_clone_dir host:foo/.git foo.git bare fail +test_clone_dir host:foo/.git foo.git bare # similar, but using ssh URL rather than host:path syntax test_clone_dir ssh://host/foo foo @@ -55,22 +55,22 @@ test_clone_dir ssh://host/foo foo.git bare test_clone_dir ssh://host/foo.git foo test_clone_dir ssh://host/foo.git foo.git bare test_clone_dir ssh://host/foo/.git foo -test_clone_dir ssh://host/foo/.git foo.git bare fail +test_clone_dir ssh://host/foo/.git foo.git bare # we should remove trailing slashes and .git suffixes test_clone_dir ssh://host/foo/ foo test_clone_dir ssh://host/foo/// foo test_clone_dir ssh://host/foo/.git/ foo -test_clone_dir ssh://host/foo.git/ foo fail -test_clone_dir ssh://host/foo.git/// foo fail +test_clone_dir ssh://host/foo.git/ foo +test_clone_dir ssh://host/foo.git/// foo test_clone_dir ssh://host/foo///.git/ foo test_clone_dir ssh://host/foo/.git/// foo test_clone_dir host:foo/ foo test_clone_dir host:foo/// foo -test_clone_dir host:foo.git/ foo fail +test_clone_dir host:foo.git/ foo test_clone_dir host:foo/.git/ foo -test_clone_dir host:foo.git/// foo fail +test_clone_dir host:foo.git/// foo test_clone_dir host:foo///.git/ foo test_clone_dir host:foo/.git/// foo -- cgit v1.2.1