Skip to content

Commit d562509

Browse files
committed
Fix rewrite_diff() name quoting.
This moves the logic to quote two paths (prefix + path) in C-style introduced in the previous commit from the dump_quoted_path() in combine-diff.c to quote.c, and uses it to fix rewrite_diff() that never C-quoted the pathnames correctly. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 462a15b commit d562509

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

combine-diff.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,16 +656,7 @@ static void dump_quoted_path(const char *head,
656656
strbuf_reset(&buf);
657657
strbuf_addstr(&buf, c_meta);
658658
strbuf_addstr(&buf, head);
659-
if (quote_c_style(prefix, NULL, NULL, 0) ||
660-
quote_c_style(path, NULL, NULL, 0)) {
661-
strbuf_addch(&buf, '"');
662-
quote_c_style(prefix, &buf, NULL, 1);
663-
quote_c_style(path, &buf, NULL, 1);
664-
strbuf_addch(&buf, '"');
665-
} else {
666-
strbuf_addstr(&buf, prefix);
667-
strbuf_addstr(&buf, path);
668-
}
659+
quote_two_c_style(&buf, prefix, path, 0);
669660
strbuf_addstr(&buf, c_reset);
670661
puts(buf.buf);
671662
}

diff.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,25 @@ static void emit_rewrite_diff(const char *name_a,
300300
const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
301301
const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
302302
const char *reset = diff_get_color(color_diff, DIFF_RESET);
303+
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
303304

304305
name_a += (*name_a == '/');
305306
name_b += (*name_b == '/');
306307
name_a_tab = strchr(name_a, ' ') ? "\t" : "";
307308
name_b_tab = strchr(name_b, ' ') ? "\t" : "";
308309

310+
strbuf_reset(&a_name);
311+
strbuf_reset(&b_name);
312+
quote_two_c_style(&a_name, o->a_prefix, name_a, 0);
313+
quote_two_c_style(&b_name, o->b_prefix, name_b, 0);
314+
309315
diff_populate_filespec(one, 0);
310316
diff_populate_filespec(two, 0);
311317
lc_a = count_lines(one->data, one->size);
312318
lc_b = count_lines(two->data, two->size);
313-
printf("%s--- %s%s%s%s\n%s+++ %s%s%s%s\n%s@@ -",
314-
metainfo, o->a_prefix, name_a, name_a_tab, reset,
315-
metainfo, o->b_prefix, name_b, name_b_tab, reset, fraginfo);
319+
printf("%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
320+
metainfo, a_name.buf, name_a_tab, reset,
321+
metainfo, b_name.buf, name_b_tab, reset, fraginfo);
316322
print_line_count(lc_a);
317323
printf(" +");
318324
print_line_count(lc_b);

quote.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,22 @@ size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
213213
return quote_c_style_counted(name, -1, sb, fp, nodq);
214214
}
215215

216+
void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
217+
{
218+
if (quote_c_style(prefix, NULL, NULL, 0) ||
219+
quote_c_style(path, NULL, NULL, 0)) {
220+
if (!nodq)
221+
strbuf_addch(sb, '"');
222+
quote_c_style(prefix, sb, NULL, 1);
223+
quote_c_style(path, sb, NULL, 1);
224+
if (!nodq)
225+
strbuf_addch(sb, '"');
226+
} else {
227+
strbuf_addstr(sb, prefix);
228+
strbuf_addstr(sb, path);
229+
}
230+
}
231+
216232
void write_name_quoted(const char *name, FILE *fp, int terminator)
217233
{
218234
if (terminator) {

quote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern char *sq_dequote(char *);
4141

4242
extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
4343
extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
44+
extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
4445

4546
extern void write_name_quoted(const char *name, FILE *, int terminator);
4647
extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,

0 commit comments

Comments
 (0)