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

[PATCH 1/2] separate quoting and relative path generation

From
Clemens Buchacher <drizzd@aon.at>
Date
Jun 3, 2010, 13:36 UTC
Message-ID
<20100603133631.GA31565@localhost>
In-Reply-To
<7v4ohlatwn.fsf@alter.siamese.dyndns.org>

This is in preparation of relative path support for ls-files, which quotes a path only if the line terminator is not the NUL character.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
On Wed, Jun 02, 2010 at 04:36:56PM -0700, Junio C Hamano wrote:
> * cb/ls-files-cdup (2010-05-26) 1 commit
>  - ls-files: allow relative pathspec
> 
> Doesn't write_name() quote twice when prefix_offset is non-zero?
Yes. Fixed in the following two patches.
 quote.c |   69 +++++++++++++++++++++++++++++++++++++++++---------------------
 quote.h |    8 ++++++-
 2 files changed, 52 insertions(+), 25 deletions(-)
diff --git a/quote.c b/quote.c
index fc93435..2ae2c1f 100644
--- a/quote.c
+++ b/quote.c
@@ -295,42 +295,63 @@ void write_name_quotedpfx(const char *pfx, size_t pfxlen,
 	fputc(terminator, fp);
 }
 
-/* quote path as relative to the given prefix */
-char *quote_path_relative(const char *in, int len,
-			  struct strbuf *out, const char *prefix)
+void write_name_quoted_relative(const char *name, size_t len,
+				const char *prefix, size_t prefix_len,
+				FILE *fp, int terminator)
 {
-	int needquote;
+	struct strbuf sb = STRBUF_INIT;
 
+	char *path = path_relative(name, len, &sb, prefix, prefix_len);
+	write_name_quoted(path, fp, terminator);
+
+	strbuf_release(&sb);
+}
+
+/* give path as relative to prefix */
+char *path_relative(const char *in, int len,
+		    struct strbuf *out, const char *prefix, int prefix_len)
+{
 	if (len < 0)
 		len = strlen(in);
+	if (prefix && prefix_len < 0)
+		prefix_len = strlen(prefix);
 
-	/* "../" prefix itself does not need quoting, but "in" might. */
-	needquote = next_quote_pos(in, len) < len;
 	strbuf_setlen(out, 0);
 	strbuf_grow(out, len);
 
-	if (needquote)
-		strbuf_addch(out, '"');
-	if (prefix) {
-		int off = 0;
-		while (prefix[off] && off < len && prefix[off] == in[off])
-			if (prefix[off] == '/') {
-				prefix += off + 1;
-				in += off + 1;
-				len -= off + 1;
-				off = 0;
-			} else
-				off++;
-
-		for (; *prefix; prefix++)
-			if (*prefix == '/')
+	if (prefix_len > 0) {
+		int off = 0, i = 0;
+		while (i < prefix_len && i < len && prefix[i] == in[i]) {
+			if (prefix[i] == '/')
+				off = i + 1;
+			i++;
+		}
+		in += off;
+		len -= off;
+
+		while (i < prefix_len) {
+			if (prefix[i] == '/')
 				strbuf_addstr(out, "../");
+			i++;
+		}
 	}
+	strbuf_add(out, in, len);
+
+	return out->buf;
+}
+
+/* quote path as relative to the given prefix */
+char *quote_path_relative(const char *in, int len,
+			  struct strbuf *out, const char *prefix)
+{
+	char *rel;
+	size_t rel_len;
 
-	quote_c_style_counted (in, len, out, NULL, 1);
+	path_relative(in, len, out, prefix, -1);
+	rel = strbuf_detach(out, &rel_len);
+	quote_c_style_counted(rel, rel_len, out, NULL, 0);
+	free(rel);
 
-	if (needquote)
-		strbuf_addch(out, '"');
 	if (!out->len)
 		strbuf_addstr(out, "./");
 
diff --git a/quote.h b/quote.h
index f83eb23..e9e0221 100644
--- a/quote.h
+++ b/quote.h
@@ -54,9 +54,15 @@ extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
 extern void write_name_quoted(const char *name, FILE *, int terminator);
 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
                                  const char *name, FILE *, int terminator);
+extern void write_name_quoted_relative(const char *name, size_t len,
+		const char *prefix, size_t prefix_len,
+		FILE *fp, int terminator);
 
+/* give path as relative to prefix */
+extern char *path_relative(const char *in, int len,
+		struct strbuf *out, const char *prefix, int prefix_len);
 /* quote path as relative to the given prefix */
-char *quote_path_relative(const char *in, int len,
+extern char *quote_path_relative(const char *in, int len,
 			  struct strbuf *out, const char *prefix);
 
 /* quoting as a string literal for other languages */
-- 
1.7.1.2.g2a651
Previous: Jonathan NiederNext: Clemens Buchacher
Message 9 of 23 in “What's cooking in git.git (Jun 2010, #01; Wed, 2)”
  1. Junio C HamanoJun 2, 2010
  2. Tay Ray ChuanJun 3, 2010
  3. 3/3 commit::print_summary(): set rev_info.always_show_header to 1Tay Ray Chuan, Jun 4, 2010
  4. Tay Ray ChuanJun 4, 2010
  5. Junio C HamanoJun 5, 2010
  6. commit::print_summary(): set rev_info.always_show_header to 1Tay Ray Chuan, Jun 7, 2010
  7. Michael J GruberJun 3, 2010
  8. mg/rev-parse-option-sifter-deprecationJonathan Nieder, Jun 3, 2010
  9. 1/2 separate quoting and relative path generationClemens Buchacher, Jun 3, 2010
  10. 2/2 ls-files: allow relative pathspecClemens Buchacher, Jun 3, 2010
  11. Junio C HamanoJun 3, 2010
  12. optimize path_relative()Clemens Buchacher, Jun 4, 2010
  13. Clemens BuchacherJun 4, 2010
  14. setup: document prefixClemens Buchacher, Jun 5, 2010
  15. optimize path_relative()Clemens Buchacher, Jun 5, 2010
  16. Junio C HamanoJun 5, 2010
  17. Thomas RastJun 3, 2010
  18. Eyvind BernhardsenJun 3, 2010
  19. Jonathan NiederJun 4, 2010
  20. Junio C HamanoJun 5, 2010
  21. Jonathan NiederJun 5, 2010
  22. Sverre RabbelierJun 5, 2010
  23. Junio C HamanoJun 6, 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.