git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Sixt <j.sixt@viscovery.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Thomas Rast <trast@student.ethz.ch>,
	git@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH] Handle double slashes in make_relative_path()
Date: Fri, 22 Jan 2010 09:36:13 +0100	[thread overview]
Message-ID: <4B59637D.4090503@viscovery.net> (raw)
In-Reply-To: <7vmy06et5c.fsf@alter.siamese.dyndns.org>

Junio C Hamano schrieb:
> Credit for test script, motivation and initial patch goes to Thomas Rast,
> but the bugs in the implementation of this patch are mine..

And with this squashed in it has fewer of them ;-) and is more portable.
The bug was that /foo was incorrectly stripped from /foobar.

-- Hannes

diff --git a/path.c b/path.c
index 78ab54a..3cb19c7 100644
--- a/path.c
+++ b/path.c
@@ -396,15 +396,15 @@ const char *make_relative_path(const char *abs, const char *base)
 	static char buf[PATH_MAX + 1];
 	int i = 0, j = 0;

-	if (!base)
+	if (!base || !base[0])
 		return abs;
 	while (base[i]) {
-		if (base[i] == '/') {
-			if (abs[j] != '/')
+		if (is_dir_sep(base[i])) {
+			if (!is_dir_sep(abs[j]))
 				return abs;
-			while (base[i] == '/')
+			while (is_dir_sep(base[i]))
 				i++;
-			while (abs[j] == '/')
+			while (is_dir_sep(abs[j]))
 				j++;
 			continue;
 		} else if (abs[j] != base[i]) {
@@ -413,7 +413,14 @@ const char *make_relative_path(const char *abs, const char *base)
 		i++;
 		j++;
 	}
-	while (abs[j] == '/')
+	if (
+	    /* "/foo" is a prefix of "/foo" */
+	    abs[j] &&
+	    /* "/foo" is not a prefix of "/foobar" */
+	    !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
+	   )
+		return abs;
+	while (is_dir_sep(abs[j]))
 		j++;
 	if (!abs[j])
 		strcpy(buf, ".");

  reply	other threads:[~2010-01-22  8:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-22  0:07 [PATCH] Handle double slashes in make_relative_path() Thomas Rast
2010-01-22  1:40 ` Junio C Hamano
2010-01-22  3:05   ` Junio C Hamano
2010-01-22  8:36     ` Johannes Sixt [this message]
2010-01-23 11:40       ` Robin Rosenberg
2010-01-23 13:09         ` Johannes Sixt
2010-01-23 13:48           ` Robin Rosenberg
2010-01-23 19:00             ` Johannes Sixt
2010-01-23 20:04           ` Junio C Hamano
2010-01-23 20:14             ` Junio C Hamano
2010-01-23 20:41               ` Johannes Sixt
2010-01-23 21:01                 ` Sverre Rabbelier
2010-01-24 13:57                   ` Thomas Rast
2010-01-24 19:04                     ` Bernhard R. Link
2010-01-24 20:05                       ` Junio C Hamano
2010-01-24 16:44                 ` Johannes Sixt
2010-01-24 18:31                   ` Junio C Hamano
2010-01-25  1:06                     ` Robin Rosenberg
2010-01-22 21:11   ` Thomas Rast
2010-01-22 23:35     ` 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

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

  git send-email \
    --in-reply-to=4B59637D.4090503@viscovery.net \
    --to=j.sixt@viscovery.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=torvalds@linux-foundation.org \
    --cc=trast@student.ethz.ch \
    /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.
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 NNTP newsgroup(s).