git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: sunshine@sunshineco.com, ps@pks.im, peff@peff.net,
	pclouds@gmail.com, gitster@pobox.com
Subject: [PATCH v5 2/5] clone: use computed length in guess_dir_name
Date: Mon, 10 Aug 2015 17:48:22 +0200	[thread overview]
Message-ID: <1439221705-20336-3-git-send-email-ps@pks.im> (raw)
In-Reply-To: <1439221705-20336-1-git-send-email-ps@pks.im>

From: Jeff King <peff@peff.net>

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 <peff@peff.net>
---
 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 303a3a7..bf45199 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 b009a87..10f5d09 100755
--- a/t/t5603-clone-dirname.sh
+++ b/t/t5603-clone-dirname.sh
@@ -46,7 +46,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
@@ -54,20 +54,20 @@ 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 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 fail
+test_clone_dir host:foo.git/ foo
+test_clone_dir host:foo.git/// foo
 test_clone_dir host:foo///.git/ foo
 test_clone_dir host:foo/.git/// foo
 
-- 
2.5.0

  parent reply	other threads:[~2015-08-10 15:48 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-27 11:48 [PATCH] clone: fix repo name when cloning a server's root Patrick Steinhardt
2015-07-27 12:51 ` Duy Nguyen
2015-07-27 12:59   ` Patrick Steinhardt
2015-07-27 14:29   ` Junio C Hamano
2015-07-29 15:51 ` [PATCH v2 0/6] " Patrick Steinhardt
2015-07-29 15:51   ` [PATCH v2 1/6] tests: fix broken && chains in t1509-root-worktree Patrick Steinhardt
2015-07-29 15:51   ` [PATCH v2 2/6] tests: fix cleanup after tests " Patrick Steinhardt
2015-07-29 15:51   ` [PATCH v2 3/6] connect: expose parse_connect_url() Patrick Steinhardt
2015-07-29 15:51   ` [PATCH v2 4/6] connect: move error check to caller of parse_connect_url Patrick Steinhardt
2015-07-29 20:32     ` Eric Sunshine
2015-07-30 12:19       ` Patrick Steinhardt
2015-07-29 15:51   ` [PATCH v2 5/6] clone: fix hostname parsing when guessing dir Patrick Steinhardt
2015-07-29 17:42     ` Junio C Hamano
2015-07-30 12:18       ` Patrick Steinhardt
2015-07-30 16:30         ` Junio C Hamano
2015-07-30 16:53           ` Junio C Hamano
2015-08-03  8:34             ` Patrick Steinhardt
2015-08-03 16:37               ` Jeff King
2015-08-03 19:43                 ` Junio C Hamano
2015-07-29 15:51   ` [PATCH v2 6/6] clone: add tests for cloning with empty path Patrick Steinhardt
2015-07-30 18:18     ` Eric Sunshine
2015-07-31  0:58       ` Junio C Hamano
2015-07-31  8:45         ` Patrick Steinhardt
2015-08-04 11:29 ` [PATCH v3 0/6] fix repo name when cloning a server's root Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 1/6] tests: fix broken && chains in t1509-root-worktree Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 2/6] tests: fix cleanup after tests " Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 3/6] clone: do not include authentication data in guessed dir Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 4/6] clone: do not use port number as dir name Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 5/6] clone: abort if no dir name could be guessed Patrick Steinhardt
2015-08-04 11:29   ` [PATCH v3 6/6] clone: add tests for cloning with empty path Patrick Steinhardt
2015-08-04 18:37     ` Eric Sunshine
2015-08-05 17:34   ` [PATCH v3 0/6] fix repo name when cloning a server's root Junio C Hamano
2015-08-05 21:19     ` Jeff King
2015-08-06  7:22       ` Torsten Bögershausen
2015-08-06  8:00         ` Junio C Hamano
2015-08-05 10:06 ` [PATCH v4 0/3] " Patrick Steinhardt
2015-08-05 10:06   ` [PATCH v4 1/3] clone: do not include authentication data in guessed dir Patrick Steinhardt
2015-08-05 17:43     ` Junio C Hamano
2015-08-05 19:36       ` Junio C Hamano
2015-08-05 19:41         ` Junio C Hamano
2015-08-06  9:47           ` Patrick Steinhardt
2015-08-07 20:45             ` Junio C Hamano
2015-08-08 17:37               ` Patrick Steinhardt
2015-08-05 10:06   ` [PATCH v4 2/3] clone: do not use port number as dir name Patrick Steinhardt
2015-08-05 10:06   ` [PATCH v4 3/3] clone: abort if no dir name could be guessed Patrick Steinhardt
2015-08-05 17:44     ` Junio C Hamano
2015-08-10 15:48 ` [PATCH v5 0/5] Improve guessing of repository names Patrick Steinhardt
2015-08-10 15:48   ` [PATCH v5 1/5] clone: add tests for output directory Patrick Steinhardt
2015-08-10 15:48   ` Patrick Steinhardt [this message]
2015-08-10 15:48   ` [PATCH v5 3/5] clone: do not include authentication data in guessed dir Patrick Steinhardt
2015-08-10 15:48   ` [PATCH v5 4/5] clone: do not use port number as dir name Patrick Steinhardt
2015-08-10 15:48   ` [PATCH v5 5/5] clone: abort if no dir name could be guessed Patrick Steinhardt
2015-08-10 18:07   ` [PATCH v5 0/5] Improve guessing of repository names Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1439221705-20336-3-git-send-email-ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).