Based on Junio's suggestion, this patch moves quote_path() from wt-status.c to quote.c and renames 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, so it will honor core.quotepath specified in configuration. The whole path is quoted now (as it was before in the scripted version of git status before it was rewritten in C). I have put strbuf_setlen() before strbuf_grow(), because otherwise strbuf_grow() will increase the buffer on top of the context of the buffer that we discard by setting the buffer length to zero. Signed-off-by: Dmitry Potapov <dpotapov@xxxxxxxxx> --- This version of patch has been changed to put in double quotes only those names that have escaped characters in them. Names containing spaces or single quotes are not quoted, but it is consistent with how all other functions work (for instance, write_name_quotedpfx). If we want quote names consisting of spaces and single quotes, I will prepare a separate patch. quote.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ quote.h | 4 ++++ wt-status.c | 47 ++--------------------------------------------- 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/quote.c b/quote.c index 40702f6..133f714 100644 --- a/quote.c +++ b/quote.c @@ -260,6 +260,51 @@ 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 = 0; + + if (len < 0) + len = strlen(in); + + strbuf_setlen(out, 0); + needquote = next_quote_pos (in, len) != len; + if (needquote) + { + strbuf_grow(out, len*3/2); + strbuf_addch(out, '"'); + } + else + strbuf_grow(out, len); + + 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. * diff --git a/quote.h b/quote.h index 4da110e..527c152 100644 --- a/quote.h +++ b/quote.h @@ -47,6 +47,10 @@ 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); +/* quote path as relative to the given prefix */ +char *quote_path_relative(const char *in, int len, + struct strbuf *out, const char *prefix); + /* quoting as a string literal for other languages */ extern void perl_quote_print(FILE *stream, const char *src); extern void python_quote_print(FILE *stream, const char *src); diff --git a/wt-status.c b/wt-status.c index 32d780a..701d13d 100644 --- a/wt-status.c +++ b/wt-status.c @@ -7,6 +7,7 @@ #include "diff.h" #include "revision.h" #include "diffcore.h" +#include "quote.h" int wt_status_relative_paths = 1; int wt_status_use_color = -1; @@ -82,51 +83,7 @@ static void wt_status_print_trailer(struct wt_status *s) color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); } -static char *quote_path(const char *in, int len, - struct strbuf *out, const char *prefix) -{ - if (len < 0) - len = strlen(in); - - strbuf_grow(out, len); - strbuf_setlen(out, 0); - 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, "../"); - } - - for ( ; len > 0; in++, len--) { - int ch = *in; - - switch (ch) { - case '\n': - strbuf_addstr(out, "\\n"); - break; - case '\r': - strbuf_addstr(out, "\\r"); - break; - default: - strbuf_addch(out, ch); - continue; - } - } - - if (!out->len) - strbuf_addstr(out, "./"); - - return out->buf; -} +#define quote_path quote_path_relative static void wt_status_print_filepair(struct wt_status *s, int t, struct diff_filepair *p) -- 1.5.4.3.486.g3d6a2 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html