git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] dir.c: do not trip over difference in "/"

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Mar 25, 2011, 15:52 UTC
Message-ID
<b28c22b42c43f5dced45bee8ba4c76965b736d9a.1301068238.git.git@drmicha.warpmail.net>
In-Reply-To
<1301060989-7246-1-git-send-email-pclouds@gmail.com>

get_relative_cwd() tries to determine a common prefix for dir and cwd. The fix in 490544b (get_cwd_relative(): do not misinterpret suffix as subdirectory, 2010-05-22) made the logic less naive (so that foo-bar is not misdetected as being within foo) but broke some other cases, in particular foo not being detected as being within foo/ any more.

Fix it by taking into a account that a directory name may or may not end in /.

Document with a test.
Reported-by: Matthijs Kooijman <matthijs@stdin.nl>
Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
So I think this would be a proper fix.

I tried to be nice and base it on the commit which I bisected the problem to. I saw much too late that their were later (failed) attempts at fixing this and a major rewrite of the test, which is quite a nuisance. Anyway, this problem needs a fix, and by taking get_cwd_relative() from this patch on top of next the problem is fixed. I have not looked into resolving the merge conflict in the test.

 dir.c               |   23 +++++++++++++++--------
 t/t1501-worktree.sh |   13 +++++++++++++
 2 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/dir.c b/dir.c
index 5615f33..b090cd0 100644
--- a/dir.c
+++ b/dir.c
@@ -956,16 +956,23 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
 		dir++;
 		cwd++;
 	}
-	if (*dir)
+
+	/* dir has more than just '/' left */
+	if (*dir && ((*dir != '/') || *(dir+1)))
 		return NULL;
-	switch (*cwd) {
-	case '\0':
+	/* *dir is now '\0' or '/' followed by '\0' */
+	if (cwd == buffer) /* we did not move */
+		return (*cwd) ? NULL : cwd;
+	/* we have moved at least by 1 */
+	if (*dir) /* == '/' */
+		return (*cwd) ? NULL : cwd;
+	if (!(*cwd)) /* both ended */
 		return cwd;
-	case '/':
-		return cwd + 1;
-	default:
-		return NULL;
-	}
+	if (*(dir-1) == '/') /* must have been common */
+		return cwd;
+	if (*cwd == '/')
+		return cwd+1;
+	return NULL;
 }
 
 int is_inside_dir(const char *dir)
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index bd8b607..f0dbdd8 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -63,6 +63,19 @@ cd sub/dir || exit 1
 test_rev_parse 'subdirectory' false false true sub/dir/
 cd ../../.. || exit 1
 
+say "core.worktree = absolute path/"
+GIT_DIR=$(pwd)/repo.git
+GIT_CONFIG=$GIT_DIR/config
+git config core.worktree "$(pwd)/work/"
+test_rev_parse 'outside'      false false false
+cd work2
+test_rev_parse 'outside2'     false false false
+cd ../work || exit 1
+test_rev_parse 'inside'       false false true ''
+cd sub/dir || exit 1
+test_rev_parse 'subdirectory' false false true sub/dir/
+cd ../../.. || exit 1
+
 say "GIT_WORK_TREE=relative path (override core.worktree)"
 GIT_DIR=$(pwd)/repo.git
 GIT_CONFIG=$GIT_DIR/config
-- 
1.7.4.1.607.g888da
Previous: Nguyen Thai Ngoc DuyNext: Nguyen Thai Ngoc Duy
Message 6 of 7 in “git-add says 'pathspec did not match any files' for git repository in /”
  1. Matthijs KooijmanMar 25, 2011
  2. Nguyen Thai Ngoc DuyMar 25, 2011
  3. setup: return correct prefix if worktree is '/'Nguyễn Thái Ngọc Duy, Mar 25, 2011
  4. Michael J GruberMar 25, 2011
  5. Nguyen Thai Ngoc DuyMar 25, 2011
  6. dir.c: do not trip over difference in "/"Michael J Gruber, Mar 25, 2011
  7. Nguyen Thai Ngoc DuyMar 26, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.