summaryrefslogtreecommitdiff
path: root/quote.c
diff options
context:
space:
mode:
authorDmitry Potapov <dpotapov@gmail.com>2008-03-07 05:30:58 +0300
committerJunio C Hamano <gitster@pobox.com>2008-03-07 21:22:25 -0800
commita734d0b10bd0f5554abb3acdf11426040cfc4df0 (patch)
treec5a2aa55520903d376121336f00679bc0780529c /quote.c
parent79418599e7221ef10641cf9cc07018c25b4d0310 (diff)
downloadgit-a734d0b10bd0f5554abb3acdf11426040cfc4df0.tar.gz
Make private quote_path() in wt-status.c available as quote_path_relative()
Move quote_path() from wt-status.c to quote.c and rename it as quote_path_relative(), because it is a better name for a public function. Also, instead of handcrafted quoting, quote_c_style_counted() is now used, to make its quoting more consistent with the rest of the system, also honoring core.quotepath specified in configuration. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'quote.c')
-rw-r--r--quote.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/quote.c b/quote.c
index d061626c34..e38ba39b64 100644
--- a/quote.c
+++ b/quote.c
@@ -260,6 +260,48 @@ extern 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)
+{
+ int needquote;
+
+ if (len < 0)
+ len = strlen(in);
+
+ /* "../" 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 == '/')
+ strbuf_addstr(out, "../");
+ }
+
+ quote_c_style_counted (in, len, out, NULL, 1);
+
+ if (needquote)
+ strbuf_addch(out, '"');
+ if (!out->len)
+ strbuf_addstr(out, "./");
+
+ return out->buf;
+}
+
/*
* C-style name unquoting.
*