Skip to content

Commit 67b5842

Browse files
gitsterGit for Windows Build Agent
authored andcommitted
quote_path: code clarification
The implementation we moved from wt-status to enclose a pathname that has a SP in it inside a dq-pair is a bit convoluted. It lets quote_c_style_counted() do its escaping and then (1) if the input string got escaped, which is checked by seeing if the result begins with a double-quote, declare that we are done. If there wasn't any SP in the input, that is OK, and if there was, the result is quoted already so it is OK, too. (2) if the input string did not get escaped, and the result has SP in it, enclose the whole thing in a dq-pair ourselves. Instead we can scan the path upfront to see if the input has SP in it. If so, we tell quote_c_style_counted() not to enclose its output in a dq-pair, and we add a dq-pair ourselves. Whether the input had bytes that quote_c_style_counted() uses backslash quoting, this would give us a desired quoted string. If the input does not have SP in it, we just let quote_c_style_counted() do its thing as usual, which would enclose the result in a dq-pair only when needed. Signed-off-by: Junio C Hamano <[email protected]>
1 parent c81fc7c commit 67b5842

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

quote.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,21 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
356356
{
357357
struct strbuf sb = STRBUF_INIT;
358358
const char *rel = relative_path(in, prefix, &sb);
359+
int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
360+
359361
strbuf_reset(out);
360-
quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
361-
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, "\"");
363+
/*
364+
* If the caller wants us to enclose the output in a dq-pair
365+
* whether quote_c_style_counted() needs to, we do it ourselves
366+
* and tell quote_c_style_counted() not to.
367+
*/
368+
if (force_dq)
367369
strbuf_addch(out, '"');
368-
}
370+
quote_c_style_counted(rel, strlen(rel), out, NULL, force_dq);
371+
if (force_dq)
372+
strbuf_addch(out, '"');
373+
strbuf_release(&sb);
369374

370375
return out->buf;
371376
}

0 commit comments

Comments
 (0)