@matheusbsbs / matheus-bagliano / commits / f3fc4a1b86
Fork 0

quote_path: optionally allow quoting a path with SP in it

Some code in wt-status.c special case a path with SP in it, which usually does not have to be c-quoted, and ensure that such a path does get quoted. Move the logic to quote_path() and give it a bit in the flags word, QUOTE_PATH_QUOTE_SP. No behaviour change intended. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 10, 2020 at 10:01 UTC f3fc4a1b8680c114defd98ce6f2429f8946a5dc1
3 files changed +11 -12
quote.c
+7
@@ -360,6 +360,13 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
360 quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
361 strbuf_release(&sb);
362
363 + if ((flags & QUOTE_PATH_QUOTE_SP) &&
364 + (out->buf[0] != '"' && strchr(out->buf, ' '))) {
365 + /* Ensure the whole thing is quoted if the path has SP in it */
366 + strbuf_insertstr(out, 0, "\"");
367 + strbuf_addch(out, '"');
368 + }
369 +
370 return out->buf;
371 }
372
quote.h
+1
@@ -73,6 +73,7 @@ void write_name_quoted_relative(const char *name, const char *prefix,
73
74 /* quote path as relative to the given prefix */
75 char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags);
76 +#define QUOTE_PATH_QUOTE_SP 01
77
78 /* quoting as a string literal for other languages */
79 void perl_quote_buf(struct strbuf *sb, const char *src);
wt-status.c
+3 -12
@@ -1877,21 +1877,12 @@ static void wt_shortstatus_status(struct string_list_item *it,
1877 const char *one;
1878
1879 if (d->rename_source) {
1880 - one = quote_path(d->rename_source, s->prefix, &onebuf, 0);
1881 - if (*one != '"' && strchr(one, ' ') != NULL) {
1882 - putchar('"');
1883 - strbuf_addch(&onebuf, '"');
1884 - one = onebuf.buf;
1885 - }
1880 + one = quote_path(d->rename_source, s->prefix, &onebuf,
1881 + QUOTE_PATH_QUOTE_SP);
1882 printf("%s -> ", one);
1883 strbuf_release(&onebuf);
1884 }
1889 - one = quote_path(it->string, s->prefix, &onebuf, 0);
1890 - if (*one != '"' && strchr(one, ' ') != NULL) {
1891 - putchar('"');
1892 - strbuf_addch(&onebuf, '"');
1893 - one = onebuf.buf;
1894 - }
1885 + one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1886 printf("%s\n", one);
1887 strbuf_release(&onebuf);
1888 }