git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: Git List <git@vger.kernel.org>,
	Sebastian Schuberth <sschuberth@gmail.com>,
	Junio C Hamano <gitster@pobox.com>, Johannes Sixt <j6t@kdbg.org>,
	Jiang Xin <worldhello.net@gmail.com>
Subject: [PATCH v5 3/3] Use simpler relative_path when set_git_dir
Date: Mon, 14 Oct 2013 10:29:40 +0800	[thread overview]
Message-ID: <f4c0808819d91003a90860b67e2b4121c673a5db.1381717700.git.worldhello.net@gmail.com> (raw)
In-Reply-To: <cover.1381717700.git.worldhello.net@gmail.com>
In-Reply-To: <cover.1381717700.git.worldhello.net@gmail.com>

Using a relative_path as git_dir first appears in v1.5.6-1-g044bbbc.
It will make git_dir shorter only if git_dir is inside work_tree,
and this will increase performance. But my last refactor effort on
relative_path function (commit v1.8.3-rc2-12-ge02ca72) changed that.
Always use relative_path as git_dir may bring troubles like
$gmane/234434.

Because new relative_path is a combination of original relative_path
from path.c and original path_relative from quote.c, so in order to
restore the origin implementation, save the original relative_path
as remove_leading_path, and call it in setup.c.

Suggested-by: Karsten Blees <karsten.blees@gmail.com>
Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 cache.h |  1 +
 path.c  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 setup.c |  5 +----
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/cache.h b/cache.h
index 8e42256..94475bd 100644
--- a/cache.h
+++ b/cache.h
@@ -737,6 +737,7 @@ int is_directory(const char *);
 const char *real_path(const char *path);
 const char *real_path_if_valid(const char *path);
 const char *absolute_path(const char *path);
+const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy(char *dst, const char *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
diff --git a/path.c b/path.c
index 0c16dc5..fa62da5 100644
--- a/path.c
+++ b/path.c
@@ -558,6 +558,51 @@ const char *relative_path(const char *in, const char *prefix,
 }
 
 /*
+ * A simpler implementation of relative_path
+ *
+ * Get relative path by removing "prefix" from "in". This function
+ * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
+ * to increase performance when traversing the path to work_tree.
+ */
+const char *remove_leading_path(const char *in, const char *prefix)
+{
+	static char buf[PATH_MAX + 1];
+	int i = 0, j = 0;
+
+	if (!prefix || !prefix[0])
+		return in;
+	while (prefix[i]) {
+		if (is_dir_sep(prefix[i])) {
+			if (!is_dir_sep(in[j]))
+				return in;
+			while (is_dir_sep(prefix[i]))
+				i++;
+			while (is_dir_sep(in[j]))
+				j++;
+			continue;
+		} else if (in[j] != prefix[i]) {
+			return in;
+		}
+		i++;
+		j++;
+	}
+	if (
+	    /* "/foo" is a prefix of "/foo" */
+	    in[j] &&
+	    /* "/foo" is not a prefix of "/foobar" */
+	    !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
+	   )
+		return in;
+	while (is_dir_sep(in[j]))
+		j++;
+	if (!in[j])
+		strcpy(buf, ".");
+	else
+		strcpy(buf, in + j);
+	return buf;
+}
+
+/*
  * It is okay if dst == src, but they should not overlap otherwise.
  *
  * Performs the following normalizations on src, storing the result in dst:
diff --git a/setup.c b/setup.c
index 0d9ea62..dad39c1 100644
--- a/setup.c
+++ b/setup.c
@@ -360,7 +360,6 @@ int is_inside_work_tree(void)
 
 void setup_work_tree(void)
 {
-	struct strbuf sb = STRBUF_INIT;
 	const char *work_tree, *git_dir;
 	static int initialized = 0;
 
@@ -380,10 +379,8 @@ void setup_work_tree(void)
 	if (getenv(GIT_WORK_TREE_ENVIRONMENT))
 		setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
 
-	set_git_dir(relative_path(git_dir, work_tree, &sb));
+	set_git_dir(remove_leading_path(git_dir, work_tree));
 	initialized = 1;
-
-	strbuf_release(&sb);
 }
 
 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
-- 
1.8.4

  parent reply	other threads:[~2013-10-14  2:30 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                 ` [PATCH v3 2/3] relative_path should honor DOS and UNC paths Jiang Xin
2013-09-17 16:12                   ` 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                         ` Jiang Xin [this message]
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

  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=f4c0808819d91003a90860b67e2b4121c673a5db.1381717700.git.worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=jrnieder@gmail.com \
    --cc=sschuberth@gmail.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).