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

[PATCH 5/6] prefix_filename: simplify windows #ifdef

From
Jeff King <peff@peff.net>
Date
Mar 21, 2017, 01:30 UTC
Message-ID
<20170321013040.cl42hlrtwix6qa23@sigill.intra.peff.net>
In-Reply-To
<20170321011838.rdhnbfwbigm4s4e3@sigill.intra.peff.net>

The prefix_filename function used to do an early return when there was no prefix on non-Windows platforms, but always allocated on Windows so that it could call convert_slashes().

Now that the function always allocates, we can unify the logic and make convert_slashes() the only conditional part.

Signed-off-by: Jeff King <peff@peff.net>
---
I wondered here if a compiler might complain about the dead assignment
to pfx_len in the is_absolute_path() conditional. On Windows, that's
used as an offset for calling into convert_slashes(), but on other
platforms we never look at it again.

However, neither gcc nor clang complained, so I'm inclined to go with what I have here.

 abspath.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/abspath.c b/abspath.c
index 4addd1fde..7f1cfe979 100644
--- a/abspath.c
+++ b/abspath.c
@@ -251,18 +251,15 @@ char *prefix_filename(const char *pfx, const char *arg)
 	struct strbuf path = STRBUF_INIT;
 	size_t pfx_len = pfx ? strlen(pfx) : 0;
 
-#ifndef GIT_WINDOWS_NATIVE
-	if (!pfx_len || is_absolute_path(arg))
-		return xstrdup(arg);
-	strbuf_add(&path, pfx, pfx_len);
-	strbuf_addstr(&path, arg);
-#else
-	/* don't add prefix to absolute paths, but still replace '\' by '/' */
-	if (is_absolute_path(arg))
+	if (!pfx_len)
+		; /* nothing to prefix */
+	else if (is_absolute_path(arg))
 		pfx_len = 0;
-	else if (pfx_len)
+	else
 		strbuf_add(&path, pfx, pfx_len);
+
 	strbuf_addstr(&path, arg);
+#ifdef GIT_WINDOWS_NATIVE
 	convert_slashes(path.buf + pfx_len);
 #endif
 	return strbuf_detach(&path, NULL);
-- 
2.12.1.683.gcd02edfec
Previous: Jeff KingNext: Jeff King
Message 9 of 11 in “prefix_filename cleanups”
  1. 0/6 prefix_filename cleanupsJeff King, Mar 21, 2017
  2. 1/6 hash-object: fix buffer reuse with --path in a subdirectoryJeff King, Mar 21, 2017
  3. 2/6 prefix_filename: move docstring to header fileJeff King, Mar 21, 2017
  4. 3/6 prefix_filename: drop length parameterJeff King, Mar 21, 2017
  5. 4/6 prefix_filename: return newly allocated stringJeff King, Mar 21, 2017
  6. Junio C HamanoMar 21, 2017
  7. Jeff KingMar 21, 2017
  8. Jeff KingMar 21, 2017
  9. 5/6 prefix_filename: simplify windows #ifdefJeff King, Mar 21, 2017
  10. 6/6 bundle: use prefix_filename with bundle pathJeff King, Mar 21, 2017
  11. Junio C HamanoMar 21, 2017

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.