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

[PATCH] Handle double slashes in make_relative_path()

From
Thomas Rast <trast@student.ethz.ch>
Date
Jan 22, 2010, 00:07 UTC
Message-ID
<379d55c6a4110736aadb8ace3b050de879a9deab.1264118830.git.trast@student.ethz.ch>
If you say
  git --git-dir=/some//path --work-tree=/some/ add <somefile>

then setup_work_tree() will call into make_relative_path() with abs="/some//path" and base="/some". (Note how the latter has already lost its trailing slash. One unfortunate user managed to trigger this because his $HOME ended in a slash.)

This means that when checking whether 'abs' is a path under 'base', we need to skip *two* slashes where the previous code only accounted for one. Fix it to handle an arbitrary number of slashes at that position.

Noticed-by: eldenz on freenode
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 path.c              |    6 +++---
 t/t1501-worktree.sh |    6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/path.c b/path.c
index 2ec950b..a195bab 100644
--- a/path.c
+++ b/path.c
@@ -400,10 +400,10 @@ int set_shared_perm(const char *path, int mode)
 	baselen = strlen(base);
 	if (prefixcmp(abs, base))
 		return abs;
-	if (abs[baselen] == '/')
-		baselen++;
-	else if (base[baselen - 1] != '/')
+	if (abs[baselen] != '/' && base[baselen - 1] != '/')
 		return abs;
+	while (abs[baselen] == '/')
+		baselen++;
 	strcpy(buf, abs + baselen);
 	return buf;
 }
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 74e6443..9df3012 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -189,4 +189,10 @@ test_expect_success 'absolute pathspec should fail gracefully' '
 	)
 '
 
+test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
+	: > dummy_file
+	echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
+	git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
+'
+
 test_done
-- 
1.6.6.1.532.g594fe
Next: Junio C Hamano
Message 1 of 20 in “Handle double slashes in make_relative_path()”
  1. Handle double slashes in make_relative_path()Thomas Rast, Jan 22, 2010
  2. Junio C HamanoJan 22, 2010
  3. Junio C HamanoJan 22, 2010
  4. Johannes SixtJan 22, 2010
  5. Robin RosenbergJan 23, 2010
  6. Johannes SixtJan 23, 2010
  7. Robin RosenbergJan 23, 2010
  8. Johannes SixtJan 23, 2010
  9. Junio C HamanoJan 23, 2010
  10. Junio C HamanoJan 23, 2010
  11. Johannes SixtJan 23, 2010
  12. Sverre RabbelierJan 23, 2010
  13. Thomas RastJan 24, 2010
  14. Bernhard R. LinkJan 24, 2010
  15. Junio C HamanoJan 24, 2010
  16. Johannes SixtJan 24, 2010
  17. Junio C HamanoJan 24, 2010
  18. Robin RosenbergJan 25, 2010
  19. Thomas RastJan 22, 2010
  20. Junio C HamanoJan 22, 2010

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.