Skip to content

Commit 462a15b

Browse files
committed
combine-diff: Fix path quoting
Earlier when showing combined diff, the filenames on the ---/+++ header lines were quoted incorrectly. a/ (or b/) prefix was output literally and then the path was output, with c-quoting. This fixes the quoting logic, and while at it, adjusts the code to use the customizable prefix (a_prefix and b_prefix) introduced recently. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 25db465 commit 462a15b

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

combine-diff.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,28 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
646646
sline->p_lno[i] = sline->p_lno[j];
647647
}
648648

649-
static void dump_quoted_path(const char *prefix, const char *path,
649+
static void dump_quoted_path(const char *head,
650+
const char *prefix,
651+
const char *path,
650652
const char *c_meta, const char *c_reset)
651653
{
652-
printf("%s%s", c_meta, prefix);
653-
quote_c_style(path, NULL, stdout, 0);
654-
printf("%s\n", c_reset);
654+
static struct strbuf buf = STRBUF_INIT;
655+
656+
strbuf_reset(&buf);
657+
strbuf_addstr(&buf, c_meta);
658+
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+
}
669+
strbuf_addstr(&buf, c_reset);
670+
puts(buf.buf);
655671
}
656672

657673
static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
@@ -793,7 +809,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
793809
if (rev->loginfo && !rev->no_commit_id)
794810
show_log(rev, opt->msg_sep);
795811
dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
796-
elem->path, c_meta, c_reset);
812+
"", elem->path, c_meta, c_reset);
797813
printf("%sindex ", c_meta);
798814
for (i = 0; i < num_parent; i++) {
799815
abb = find_unique_abbrev(elem->parent[i].sha1,
@@ -829,14 +845,19 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
829845
printf("%s\n", c_reset);
830846
}
831847
if (added)
832-
dump_quoted_path("--- /dev/", "null", c_meta, c_reset);
848+
dump_quoted_path("--- ", "", "/dev/null",
849+
c_meta, c_reset);
833850
else
834-
dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
851+
dump_quoted_path("--- ", opt->a_prefix, elem->path,
852+
c_meta, c_reset);
835853
if (deleted)
836-
dump_quoted_path("+++ /dev/", "null", c_meta, c_reset);
854+
dump_quoted_path("+++ ", "", "/dev/null",
855+
c_meta, c_reset);
837856
else
838-
dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
839-
dump_sline(sline, cnt, num_parent, DIFF_OPT_TST(opt, COLOR_DIFF));
857+
dump_quoted_path("+++ ", opt->b_prefix, elem->path,
858+
c_meta, c_reset);
859+
dump_sline(sline, cnt, num_parent,
860+
DIFF_OPT_TST(opt, COLOR_DIFF));
840861
}
841862
free(result);
842863

0 commit comments

Comments
 (0)