Skip to content

Commit 3f44924

Browse files
gitsterdscho
authored andcommitted
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 <[email protected]>
1 parent 1f00ff4 commit 3f44924

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

quote.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
360360
quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
361361
strbuf_release(&sb);
362362

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+
363370
return out->buf;
364371
}
365372

quote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void write_name_quoted_relative(const char *name, const char *prefix,
7373

7474
/* quote path as relative to the given prefix */
7575
char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags);
76+
#define QUOTE_PATH_QUOTE_SP 01
7677

7778
/* quoting as a string literal for other languages */
7879
void perl_quote_buf(struct strbuf *sb, const char *src);

wt-status.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,21 +1882,12 @@ static void wt_shortstatus_status(struct string_list_item *it,
18821882
const char *one;
18831883

18841884
if (d->rename_source) {
1885-
one = quote_path(d->rename_source, s->prefix, &onebuf, 0);
1886-
if (*one != '"' && strchr(one, ' ') != NULL) {
1887-
putchar('"');
1888-
strbuf_addch(&onebuf, '"');
1889-
one = onebuf.buf;
1890-
}
1885+
one = quote_path(d->rename_source, s->prefix, &onebuf,
1886+
QUOTE_PATH_QUOTE_SP);
18911887
printf("%s -> ", one);
18921888
strbuf_release(&onebuf);
18931889
}
1894-
one = quote_path(it->string, s->prefix, &onebuf, 0);
1895-
if (*one != '"' && strchr(one, ' ') != NULL) {
1896-
putchar('"');
1897-
strbuf_addch(&onebuf, '"');
1898-
one = onebuf.buf;
1899-
}
1890+
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
19001891
printf("%s\n", one);
19011892
strbuf_release(&onebuf);
19021893
}

0 commit comments

Comments
 (0)