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

[PATCH v2] get_cwd_relative(): do not misinterpret root path

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 20, 2010, 06:50 UTC
Message-ID
<1290235851-23276-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<20101119204159.GA28622@localhost>

Commit 490544b (get_cwd_relative(): do not misinterpret suffix as subdirectory) handles case where:

dir = "/path/work"; cwd = "/path/work-xyz";

When it comes to the end of get_cwd_relative(), dir is at '\0' and cwd is at '-'. The rest of cwd, "-xyz", clearly cannot be the relative path from dir to cwd. However there is another case where:

dir = "/"; /* or even "c:/" */ cwd = "/path/to/here";

In this special case, while *cwd == 'p', which is not a path separator, the rest of cwd, "path/to/here", can be returned as a relative path from dir to cwd.

Handle this case and make t1509 pass again.
Reported-by: Albert Strasheim <fullung@gmail.com>
Reported-by: Matthijs Kooijman <matthijs@stdin.nl>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 2010/11/20 Clemens Buchacher <drizzd@aon.at>:
 > make_absolute_path() already rewrites dir to contain slashes only.
 > I think it is confusing to use is_dir_sep() here, when we only
 > check for / above.
 I was thinking about GIT_WORK_TREE="c:\" (so dir would be "c:\") when
 I wrote that. It is recognized as absolute path, no normalization
 will be done.
 But then the comparison part earlier in this function would fail because
 getcwd in Windows port always return '/'.
 I will make a patch to convert GIT_WORK_TREE (and GIT_DIR too?) to '/'
 so everything is consistent.
 dir.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/dir.c b/dir.c
index b2dfb69..b687504 100644
--- a/dir.c
+++ b/dir.c
@@ -965,6 +965,12 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
 	case '/':
 		return cwd + 1;
 	default:
+		/*
+		 * dir can end with a path separator when it's root
+		 * directory. Return proper prefix in that case.
+		 */
+		if (dir[-1] == '/')
+			return cwd;
 		return NULL;
 	}
 }
-- 
1.7.3.2.316.gda8b3
Previous: Clemens Buchacher
Message 2 of 2 in “Re: [PATCH] get_cwd_relative(): do not misinterpret root path”
  1. Clemens BuchacherNov 19, 2010
  2. get_cwd_relative(): do not misinterpret root pathNguyễn Thái Ngọc Duy, Nov 20, 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.