{"thread":{"id":"12555","subject":"[PATCH v2] renaming quote_path() as quote_path_relative()","startedAt":"2008-03-07T02:30:58Z","lastAt":"2008-03-07T02:30:58Z","messageCount":1,"participants":["Dmitry Potapov"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"71314","messageId":"20080307023058.GB5305@dpotapov.dyndns.org","threadId":"12555","inReplyTo":null,"subject":"[PATCH v2] renaming quote_path() as quote_path_relative()","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2008-03-07T02:30:58Z","receivedAt":"2008-03-07T02:30:58Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"Based on Junio's suggestion, this patch moves quote_path() from\nwt-status.c to quote.c and renames it as quote_path_relative(),\nbecause it is a better name for a public function.\n\nAlso, instead of handcrafted quoting, quote_c_style_counted() is now\nused, so it will honor core.quotepath specified in configuration.\nThe whole path is quoted now (as it was before in the scripted version\nof git status before it was rewritten in C).\n\nI have put strbuf_setlen() before strbuf_grow(), because otherwise\nstrbuf_grow() will increase the buffer on top of the context of the\nbuffer that we discard by setting the buffer length to zero.\n\nSigned-off-by: Dmitry Potapov <dpotapov@gmail.com>\n---\n quote.c     |   35 +++++++++++++++++++++++++++++++++++\n quote.h     |    4 ++++\n wt-status.c |   47 ++---------------------------------------------\n 3 files changed, 41 insertions(+), 45 deletions(-)\n\ndiff --git a/quote.c b/quote.c\nindex d061626..722dbd4 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -260,6 +260,41 @@ extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,\n \tfputc(terminator, fp);\n }\n \n+/* quote path as relative to the given prefix */\n+char *quote_path_relative(const char *in, int len,\n+                          struct strbuf *out, const char *prefix)\n+{\n+\tif (len < 0)\n+\t\tlen = strlen(in);\n+\n+\tstrbuf_setlen(out, 0);\n+\tstrbuf_grow(out, len+2);\n+\tstrbuf_addch(out, '\"');\n+\tif (prefix) {\n+\t\tint off = 0;\n+\t\twhile (prefix[off] && off < len && prefix[off] == in[off])\n+\t\t\tif (prefix[off] == '/') {\n+\t\t\t\tprefix += off + 1;\n+\t\t\t\tin += off + 1;\n+\t\t\t\tlen -= off + 1;\n+\t\t\t\toff = 0;\n+\t\t\t} else\n+\t\t\t\toff++;\n+\n+\t\tfor (; *prefix; prefix++)\n+\t\t\tif (*prefix == '/')\n+\t\t\t\tstrbuf_addstr(out, \"../\");\n+\t}\n+\n+\tquote_c_style_counted (in, len, out, NULL, 1);\n+\tstrbuf_addch(out, '\"');\n+\n+\tif (!out->len)\n+\t\tstrbuf_addstr(out, \"./\");\n+\n+\treturn out->buf;\n+}\n+\n /*\n  * C-style name unquoting.\n  *\ndiff --git a/quote.h b/quote.h\nindex 4da110e..527c152 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -47,6 +47,10 @@ extern void write_name_quoted(const char *name, FILE *, int terminator);\n extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,\n                                  const char *name, FILE *, int terminator);\n \n+/* quote path as relative to the given prefix */\n+char *quote_path_relative(const char *in, int len,\n+                          struct strbuf *out, const char *prefix);\n+\n /* quoting as a string literal for other languages */\n extern void perl_quote_print(FILE *stream, const char *src);\n extern void python_quote_print(FILE *stream, const char *src);\ndiff --git a/wt-status.c b/wt-status.c\nindex 32d780a..701d13d 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -7,6 +7,7 @@\n #include \"diff.h\"\n #include \"revision.h\"\n #include \"diffcore.h\"\n+#include \"quote.h\"\n \n int wt_status_relative_paths = 1;\n int wt_status_use_color = -1;\n@@ -82,51 +83,7 @@ static void wt_status_print_trailer(struct wt_status *s)\n \tcolor_fprintf_ln(s->fp, color(WT_STATUS_HEADER), \"#\");\n }\n \n-static char *quote_path(const char *in, int len,\n-\t\t\tstruct strbuf *out, const char *prefix)\n-{\n-\tif (len < 0)\n-\t\tlen = strlen(in);\n-\n-\tstrbuf_grow(out, len);\n-\tstrbuf_setlen(out, 0);\n-\tif (prefix) {\n-\t\tint off = 0;\n-\t\twhile (prefix[off] && off < len && prefix[off] == in[off])\n-\t\t\tif (prefix[off] == '/') {\n-\t\t\t\tprefix += off + 1;\n-\t\t\t\tin += off + 1;\n-\t\t\t\tlen -= off + 1;\n-\t\t\t\toff = 0;\n-\t\t\t} else\n-\t\t\t\toff++;\n-\n-\t\tfor (; *prefix; prefix++)\n-\t\t\tif (*prefix == '/')\n-\t\t\t\tstrbuf_addstr(out, \"../\");\n-\t}\n-\n-\tfor ( ; len > 0; in++, len--) {\n-\t\tint ch = *in;\n-\n-\t\tswitch (ch) {\n-\t\tcase '\\n':\n-\t\t\tstrbuf_addstr(out, \"\\\\n\");\n-\t\t\tbreak;\n-\t\tcase '\\r':\n-\t\t\tstrbuf_addstr(out, \"\\\\r\");\n-\t\t\tbreak;\n-\t\tdefault:\n-\t\t\tstrbuf_addch(out, ch);\n-\t\t\tcontinue;\n-\t\t}\n-\t}\n-\n-\tif (!out->len)\n-\t\tstrbuf_addstr(out, \"./\");\n"}]}