git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Git List" <git@vger.kernel.org>, Tvangeste <i.4m.l33t@yandex.ru>,
	"Johannes Sixt" <j6t@kdbg.org>,
	"Karsten Blees" <karsten.blees@gmail.com>,
	"Torsten Bögershausen" <tboegi@web.de>,
	"Jiang Xin" <worldhello.net@gmail.com>
Subject: [PATCH v3 2/3] relative_path should honor DOS and UNC paths
Date: Tue, 17 Sep 2013 16:30:23 +0800	[thread overview]
Message-ID: <2c56935842ceef4d5933c299dd2d55286eb0ba3a.1379406453.git.worldhello.net@gmail.com> (raw)
In-Reply-To: <1c0d845aca9a9ca65a7e1d481a75a0f6f4220a89.1379406453.git.worldhello.net@gmail.com>
In-Reply-To: <CANYiYbH9pLMx4gu1qONhy-+++ojUhPSd9F=sdRTmGWH3pSUTqQ@mail.gmail.com>

Tvangeste found that the "relative_path" function could not work
properly on Windows if "in" and "prefix" have DOS driver prefix
(such as "C:/windows"). And the "relative_path" function won't
work properly if either "in" or "prefix" is a UNC path (like
"//host/share"). ($gmane/234434)

E.g., When execute: test-path-utils relative_path "C:/a/b" "D:/x/y",
should return "C:/a/b", but returns "../../C:/a/b", which is wrong.

So make relative_path honor DOS and UNC paths, and add test cases
for it in t0060.

Reported-by: Tvangeste <i.4m.l33t@yandex.ru>
Helped-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 compat/mingw.h        |  9 +++++++++
 git-compat-util.h     |  4 ++++
 path.c                | 25 +++++++++++++++++++++++++
 t/t0060-path-utils.sh |  8 ++++++++
 4 files changed, 46 insertions(+)

diff --git a/compat/mingw.h b/compat/mingw.h
index bd0a88b..06e9f49 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -311,6 +311,15 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format
 
 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
+static inline int is_unc_path(const char *path)
+{
+	if (!is_dir_sep(*path) || !is_dir_sep(*(path+1)) || is_dir_sep(*(path+2)))
+		return 0;
+	for (path += 2; *path; path++)
+		if (is_dir_sep(*path))
+			return 1;
+	return 0;
+}
 static inline char *mingw_find_last_dir_sep(const char *path)
 {
 	char *ret = NULL;
diff --git a/git-compat-util.h b/git-compat-util.h
index 9549de6..93c2206 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -274,6 +274,10 @@ extern char *gitbasename(char *);
 #define is_dir_sep(c) ((c) == '/')
 #endif
 
+#ifndef is_unc_path
+#define is_unc_path(path) 0
+#endif
+
 #ifndef find_last_dir_sep
 #define find_last_dir_sep(path) strrchr(path, '/')
 #endif
diff --git a/path.c b/path.c
index 9fd28bcd..544d10d 100644
--- a/path.c
+++ b/path.c
@@ -434,6 +434,21 @@ int adjust_shared_perm(const char *path)
 	return 0;
 }
 
+static int have_same_root(const char *path1, const char *path2)
+{
+	int is_abs1, is_abs2;
+
+	is_abs1 = is_absolute_path(path1);
+	is_abs2 = is_absolute_path(path2);
+	if (is_abs1 && is_abs2) {
+		if (is_unc_path(path1) ^ is_unc_path(path2))
+			return 0;
+		return tolower(path1[0]) == tolower(path2[0]);
+	} else {
+		return !is_abs1 && !is_abs2;
+	}
+}
+
 /*
  * Give path as relative to prefix.
  *
@@ -454,6 +469,16 @@ const char *relative_path(const char *in, const char *prefix,
 	else if (!prefix_len)
 		return in;
 
+	if (have_same_root(in, prefix)) {
+		/* bypass dos_drive, for "c:" is identical to "C:" */
+		if (has_dos_drive_prefix(in)) {
+			i = 2;
+			j = 2;
+		}
+	} else {
+		return in;
+	}
+
 	while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
 		if (is_dir_sep(prefix[i])) {
 			while (is_dir_sep(prefix[i]))
diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
index 92976e0..830b6d5 100755
--- a/t/t0060-path-utils.sh
+++ b/t/t0060-path-utils.sh
@@ -210,6 +210,14 @@ relative_path foo/a/b/		foo/a/b		./
 relative_path foo/a		foo/a/b		../
 relative_path foo/x/y		foo/a/b		../../x/y
 relative_path foo/a/c		foo/a/b		../c
+relative_path foo/a/b		/foo/x/y	foo/a/b
+relative_path /foo/a/b		foo/x/y		/foo/a/b
+relative_path d:/a/b		D:/a/c		../b		MINGW
+relative_path C:/a/b		D:/a/c		C:/a/b		MINGW
+relative_path //host1/a/b	//host1/a/c	../b
+relative_path //host1/a/b	//host2/a/c	../../../host1/a/b
+relative_path //host1/a/b	/foo/a/c	//host1/a/b	MINGW
+relative_path /foo/a/b		//host2/a/c	/foo/a/b	MINGW
 relative_path foo/a/b		"<empty>"	foo/a/b
 relative_path foo/a/b 		"<null>"	foo/a/b
 relative_path "<empty>"		/foo/a/b	./
-- 
1.8.4.460.g2f083d1

  parent reply	other threads:[~2013-09-17  8:32 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-10 13:14 Regression in e02ca72: git svn rebase is broken on Windows Tvangeste
2013-09-10 16:13 ` Johannes Schindelin
2013-09-10 17:43   ` Tvangeste
2013-09-10 18:02     ` Johannes Schindelin
2013-09-10 20:53       ` Tvangeste
2013-09-10 16:52 ` Johannes Sixt
2013-09-10 17:44   ` Tvangeste
2013-09-10 17:06 ` Junio C Hamano
2013-09-10 22:17   ` Karsten Blees
2013-09-11  4:41     ` Jiang Xin
2013-09-11  3:19   ` Jiang Xin
2013-09-11  5:43     ` Johannes Sixt
2013-09-12  9:12   ` [PATCH 1/2] relative_path should honor dos_drive_prefix Jiang Xin
2013-09-12  9:32     ` Tvangeste
2013-09-12 14:13     ` Torsten Bögershausen
2013-09-12 15:48       ` Junio C Hamano
2013-09-12 17:22       ` Johannes Sixt
2013-09-12 19:11         ` Junio C Hamano
2013-09-13  4:55           ` Jiang Xin
2013-09-13  5:53             ` Junio C Hamano
2013-09-17  8:24               ` Jiang Xin
2013-09-17  8:30                 ` [PATCH v3 1/3] test: use unambigous leading path (/foo) for mingw Jiang Xin
2013-09-17  8:30                 ` Jiang Xin [this message]
2013-09-17 16:12                   ` [PATCH v3 2/3] relative_path should honor DOS and UNC paths Junio C Hamano
2013-09-18  9:02                     ` Jiang Xin
2013-09-18 16:00                       ` Junio C Hamano
     [not found]                     ` <5239BA98.9000205@gmail.com>
2013-09-18 15:09                       ` Torsten Bögershausen
2013-09-17  8:30                 ` [PATCH v3 3/3] Use simpler relative_path when set_git_dir Jiang Xin
2013-09-17 19:32                 ` [PATCH 1/2] relative_path should honor dos_drive_prefix Johannes Sixt
2013-09-18 14:29                   ` Torsten Bögershausen
2013-09-20  1:26                   ` Jiang Xin
2013-09-20  2:38                     ` [PATCH v4 0/3] relative path regression fix Jiang Xin
2013-09-20  2:38                     ` [PATCH v4 1/3] test: use unambigous leading path (/foo) for mingw Jiang Xin
2013-10-10 20:32                       ` Sebastian Schuberth
2013-10-14  1:33                         ` Jiang Xin
2013-10-14  2:29                         ` [PATCH v5 0/3] relative path regression fix Jiang Xin
2013-10-14  2:29                         ` [PATCH v5 1/3] test: use unambigous leading path (/foo) for MSYS Jiang Xin
2013-10-14  6:50                           ` Sebastian Schuberth
2013-10-14 19:40                           ` Eric Sunshine
2013-10-14  2:29                         ` [PATCH v5 2/3] relative_path should honor dos-drive-prefix Jiang Xin
2013-10-14  2:29                         ` [PATCH v5 3/3] Use simpler relative_path when set_git_dir Jiang Xin
2013-09-20  2:38                     ` [PATCH v4 2/3] relative_path should honor dos-driver-prefix Jiang Xin
2013-10-10 20:34                       ` Sebastian Schuberth
2013-09-20  2:38                     ` [PATCH v4 3/3] Use simpler relative_path when set_git_dir Jiang Xin
2013-09-13 13:59             ` [PATCH 1/2] relative_path should honor dos_drive_prefix Torsten Bögershausen
2013-09-12 15:45     ` Karsten Blees
2013-09-12  9:12   ` [PATCH 2/2] Use simpler relative_path when set_git_dir Jiang Xin
2013-09-12 17:36     ` Johannes Sixt
2013-09-13  5:08   ` [PATCH v2 0/3] fixes for relative_path Jiang Xin
2013-09-13  5:08   ` [PATCH v2 1/3] test: use unambigous leading path (/foo) for mingw Jiang Xin
2013-09-13 19:51     ` Junio C Hamano
2013-09-14  6:52       ` Torsten Bögershausen
2013-09-17 16:19         ` Junio C Hamano
2013-09-13  5:08   ` [PATCH v2 2/3] relative_path should honor dos_drive_prefix Jiang Xin
2013-09-14  6:11     ` Torsten Bögershausen
2013-09-13  5:08   ` [PATCH v2 3/3] Use simpler relative_path when set_git_dir Jiang Xin

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=2c56935842ceef4d5933c299dd2d55286eb0ba3a.1379406453.git.worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=i.4m.l33t@yandex.ru \
    --cc=j6t@kdbg.org \
    --cc=karsten.blees@gmail.com \
    --cc=tboegi@web.de \
    /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).