@matheusbsbs / matheus-bagliano / commits / 7be5761073
Fork 0

diff.c: Output the text graph padding before each diff line

Change output from diff with -p/--dirstat/--binary/--numstat/--stat/ --shortstat/--check/--summary options to align with graph paddings. Thanks Jeff King <peff@peff.net> for reporting the '--summary' bug and his initial patch. Signed-off-by: Bo Yang <struggleyb.nku@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bo Yang committed May 26, 2010 at 15:23 UTC 7be5761073fde260d3aca10883e8688bd30cbccf
1 file changed +147 -53
diff.c
+147 -53
@@ -490,6 +490,13 @@ static void emit_rewrite_diff(const char *name_a,
490 char *data_one, *data_two;
491 size_t size_one, size_two;
492 struct emit_callback ecbdata;
493 + char *line_prefix = "";
494 + struct strbuf *msgbuf;
495 +
496 + if (o && o->output_prefix) {
497 + msgbuf = o->output_prefix(o, o->output_prefix_data);
498 + line_prefix = msgbuf->buf;
499 + }
500
501 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
502 a_prefix = o->b_prefix;
@@ -531,9 +538,10 @@ static void emit_rewrite_diff(const char *name_a,
538 lc_a = count_lines(data_one, size_one);
539 lc_b = count_lines(data_two, size_two);
540 fprintf(o->file,
534 - "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
535 - metainfo, a_name.buf, name_a_tab, reset,
536 - metainfo, b_name.buf, name_b_tab, reset, fraginfo);
541 + "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
542 + line_prefix, metainfo, a_name.buf, name_a_tab, reset,
543 + line_prefix, metainfo, b_name.buf, name_b_tab, reset,
544 + line_prefix, fraginfo);
545 print_line_count(o->file, lc_a);
546 fprintf(o->file, " +");
547 print_line_count(o->file, lc_b);
@@ -846,6 +854,14 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
854 const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
855 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
856 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
857 + struct diff_options *o = ecbdata->opt;
858 + char *line_prefix = "";
859 + struct strbuf *msgbuf;
860 +
861 + if (o && o->output_prefix) {
862 + msgbuf = o->output_prefix(o, o->output_prefix_data);
863 + line_prefix = msgbuf->buf;
864 + }
865
866 if (ecbdata->header) {
867 fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
@@ -860,10 +876,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
876 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
877 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
878
863 - fprintf(ecbdata->opt->file, "%s--- %s%s%s\n",
864 - meta, ecbdata->label_path[0], reset, name_a_tab);
865 - fprintf(ecbdata->opt->file, "%s+++ %s%s%s\n",
866 - meta, ecbdata->label_path[1], reset, name_b_tab);
879 + fprintf(ecbdata->opt->file, "%s%s--- %s%s%s\n",
880 + line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
881 + fprintf(ecbdata->opt->file, "%s%s+++ %s%s%s\n",
882 + line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
883 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
884 }
885
@@ -1100,10 +1116,17 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1116 int total_files = data->nr;
1117 int width, name_width;
1118 const char *reset, *set, *add_c, *del_c;
1119 + const char *line_prefix = "";
1120 + struct strbuf *msg = NULL;
1121
1122 if (data->nr == 0)
1123 return;
1124
1125 + if (options->output_prefix) {
1126 + msg = options->output_prefix(options, options->output_prefix_data);
1127 + line_prefix = msg->buf;
1128 + }
1129 +
1130 width = options->stat_width ? options->stat_width : 80;
1131 name_width = options->stat_name_width ? options->stat_name_width : 50;
1132
@@ -1173,6 +1196,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1196 }
1197
1198 if (data->files[i]->is_binary) {
1199 + fprintf(options->file, "%s", line_prefix);
1200 show_name(options->file, prefix, name, len);
1201 fprintf(options->file, " Bin ");
1202 fprintf(options->file, "%s%"PRIuMAX"%s",
@@ -1185,6 +1209,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1209 continue;
1210 }
1211 else if (data->files[i]->is_unmerged) {
1212 + fprintf(options->file, "%s", line_prefix);
1213 show_name(options->file, prefix, name, len);
1214 fprintf(options->file, " Unmerged\n");
1215 continue;
@@ -1207,6 +1232,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1232 add = scale_linear(add, width, max_change);
1233 del = scale_linear(del, width, max_change);
1234 }
1235 + fprintf(options->file, "%s", line_prefix);
1236 show_name(options->file, prefix, name, len);
1237 fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
1238 added + deleted ? " " : "");
@@ -1214,6 +1240,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1240 show_graph(options->file, '-', del, del_c, reset);
1241 fprintf(options->file, "\n");
1242 }
1243 + fprintf(options->file, "%s", line_prefix);
1244 fprintf(options->file,
1245 " %d files changed, %d insertions(+), %d deletions(-)\n",
1246 total_files, adds, dels);
@@ -1240,6 +1267,12 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
1267 }
1268 }
1269 }
1270 + if (options->output_prefix) {
1271 + struct strbuf *msg = NULL;
1272 + msg = options->output_prefix(options,
1273 + options->output_prefix_data);
1274 + fprintf(options->file, "%s", msg->buf);
1275 + }
1276 fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
1277 total_files, adds, dels);
1278 }
@@ -1254,6 +1287,13 @@ static void show_numstat(struct diffstat_t *data, struct diff_options *options)
1287 for (i = 0; i < data->nr; i++) {
1288 struct diffstat_file *file = data->files[i];
1289
1290 + if (options->output_prefix) {
1291 + struct strbuf *msg = NULL;
1292 + msg = options->output_prefix(options,
1293 + options->output_prefix_data);
1294 + fprintf(options->file, "%s", msg->buf);
1295 + }
1296 +
1297 if (file->is_binary)
1298 fprintf(options->file, "-\t-\t");
1299 else
@@ -1289,10 +1329,18 @@ struct dirstat_dir {
1329 int alloc, nr, percent, cumulative;
1330 };
1331
1292 -static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
1332 +static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
1333 + unsigned long changed, const char *base, int baselen)
1334 {
1335 unsigned long this_dir = 0;
1336 unsigned int sources = 0;
1337 + const char *line_prefix = "";
1338 + struct strbuf *msg = NULL;
1339 +
1340 + if (opt->output_prefix) {
1341 + msg = opt->output_prefix(opt, opt->output_prefix_data);
1342 + line_prefix = msg->buf;
1343 + }
1344
1345 while (dir->nr) {
1346 struct dirstat_file *f = dir->files;
@@ -1307,7 +1355,7 @@ static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long ch
1355 slash = strchr(f->name + baselen, '/');
1356 if (slash) {
1357 int newbaselen = slash + 1 - f->name;
1310 - this = gather_dirstat(file, dir, changed, f->name, newbaselen);
1358 + this = gather_dirstat(opt, dir, changed, f->name, newbaselen);
1359 sources++;
1360 } else {
1361 this = f->changed;
@@ -1329,7 +1377,8 @@ static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long ch
1377 if (permille) {
1378 int percent = permille / 10;
1379 if (percent >= dir->percent) {
1332 - fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1380 + fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
1381 + percent, permille % 10, baselen, base);
1382 if (!dir->cumulative)
1383 return 0;
1384 }
@@ -1409,7 +1458,7 @@ static void show_dirstat(struct diff_options *options)
1458
1459 /* Show all directories with more than x% of the changes */
1460 qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
1412 - gather_dirstat(options->file, &dir, changed, "", 0);
1461 + gather_dirstat(options, &dir, changed, "", 0);
1462 }
1463
1464 static void free_diffstat_info(struct diffstat_t *diffstat)
@@ -1467,6 +1516,15 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1516 const char *reset = diff_get_color(color_diff, DIFF_RESET);
1517 const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
1518 char *err;
1519 + char *line_prefix = "";
1520 + struct strbuf *msgbuf;
1521 +
1522 + assert(data->o);
1523 + if (data->o->output_prefix) {
1524 + msgbuf = data->o->output_prefix(data->o,
1525 + data->o->output_prefix_data);
1526 + line_prefix = msgbuf->buf;
1527 + }
1528
1529 if (line[0] == '+') {
1530 unsigned bad;
@@ -1474,16 +1532,16 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1532 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
1533 data->status |= 1;
1534 fprintf(data->o->file,
1477 - "%s:%d: leftover conflict marker\n",
1478 - data->filename, data->lineno);
1535 + "%s%s:%d: leftover conflict marker\n",
1536 + line_prefix, data->filename, data->lineno);
1537 }
1538 bad = ws_check(line + 1, len - 1, data->ws_rule);
1539 if (!bad)
1540 return;
1541 data->status |= bad;
1542 err = whitespace_error_string(bad);
1485 - fprintf(data->o->file, "%s:%d: %s.\n",
1486 - data->filename, data->lineno, err);
1543 + fprintf(data->o->file, "%s%s:%d: %s.\n",
1544 + line_prefix, data->filename, data->lineno, err);
1545 free(err);
1546 emit_line(data->o, set, reset, line, 1);
1547 ws_check_emit(line + 1, len - 1, data->ws_rule,
@@ -1523,7 +1581,7 @@ static unsigned char *deflate_it(char *data,
1581 return deflated;
1582 }
1583
1526 -static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1584 +static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
1585 {
1586 void *cp;
1587 void *delta;
@@ -1552,13 +1610,13 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1610 }
1611
1612 if (delta && delta_size < deflate_size) {
1555 - fprintf(file, "delta %lu\n", orig_size);
1613 + fprintf(file, "%sdelta %lu\n", prefix, orig_size);
1614 free(deflated);
1615 data = delta;
1616 data_size = delta_size;
1617 }
1618 else {
1561 - fprintf(file, "literal %lu\n", two->size);
1619 + fprintf(file, "%sliteral %lu\n", prefix, two->size);
1620 free(delta);
1621 data = deflated;
1622 data_size = deflate_size;
@@ -1576,18 +1634,19 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1634 line[0] = bytes - 26 + 'a' - 1;
1635 encode_85(line + 1, cp, bytes);
1636 cp = (char *) cp + bytes;
1637 + fprintf(file, "%s", prefix);
1638 fputs(line, file);
1639 fputc('\n', file);
1640 }
1582 - fprintf(file, "\n");
1641 + fprintf(file, "%s\n", prefix);
1642 free(data);
1643 }
1644
1586 -static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
1645 +static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
1646 {
1588 - fprintf(file, "GIT binary patch\n");
1589 - emit_binary_diff_body(file, one, two);
1590 - emit_binary_diff_body(file, two, one);
1647 + fprintf(file, "%sGIT binary patch\n", prefix);
1648 + emit_binary_diff_body(file, one, two, prefix);
1649 + emit_binary_diff_body(file, two, one, prefix);
1650 }
1651
1652 static void diff_filespec_load_driver(struct diff_filespec *one)
@@ -1676,6 +1735,13 @@ static void builtin_diff(const char *name_a,
1735 struct userdiff_driver *textconv_one = NULL;
1736 struct userdiff_driver *textconv_two = NULL;
1737 struct strbuf header = STRBUF_INIT;
1738 + struct strbuf *msgbuf;
1739 + char *line_prefix = "";
1740 +
1741 + if (o->output_prefix) {
1742 + msgbuf = o->output_prefix(o, o->output_prefix_data);
1743 + line_prefix = msgbuf->buf;
1744 + }
1745
1746 if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
1747 (!one->mode || S_ISGITLINK(one->mode)) &&
@@ -1710,22 +1776,22 @@ static void builtin_diff(const char *name_a,
1776 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
1777 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1778 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1713 - strbuf_addf(&header, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1779 + strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, set, a_one, b_two, reset);
1780 if (lbl[0][0] == '/') {
1781 /* /dev/null */
1716 - strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
1782 + strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, set, two->mode, reset);
1783 if (xfrm_msg && xfrm_msg[0])
1784 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1785 }
1786 else if (lbl[1][0] == '/') {
1721 - strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1787 + strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, set, one->mode, reset);
1788 if (xfrm_msg && xfrm_msg[0])
1789 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1790 }
1791 else {
1792 if (one->mode != two->mode) {
1727 - strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
1728 - strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
1793 + strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, set, one->mode, reset);
1794 + strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, set, two->mode, reset);
1795 }
1796 if (xfrm_msg && xfrm_msg[0])
1797 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
@@ -1760,10 +1826,10 @@ static void builtin_diff(const char *name_a,
1826 fprintf(o->file, "%s", header.buf);
1827 strbuf_reset(&header);
1828 if (DIFF_OPT_TST(o, BINARY))
1763 - emit_binary_diff(o->file, &mf1, &mf2);
1829 + emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
1830 else
1765 - fprintf(o->file, "Binary files %s and %s differ\n",
1766 - lbl[0], lbl[1]);
1831 + fprintf(o->file, "%sBinary files %s and %s differ\n",
1832 + line_prefix, lbl[0], lbl[1]);
1833 o->found_changes = 1;
1834 }
1835 else {
@@ -2389,28 +2455,36 @@ static void fill_metainfo(struct strbuf *msg,
2455 struct diff_options *o,
2456 struct diff_filepair *p)
2457 {
2458 + struct strbuf *msgbuf;
2459 + char *line_prefix = "";
2460 +
2461 + if (o->output_prefix) {
2462 + msgbuf = o->output_prefix(o, o->output_prefix_data);
2463 + line_prefix = msgbuf->buf;
2464 + }
2465 +
2466 strbuf_init(msg, PATH_MAX * 2 + 300);
2467 switch (p->status) {
2468 case DIFF_STATUS_COPIED:
2395 - strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2396 - strbuf_addstr(msg, "\ncopy from ");
2469 + strbuf_addf(msg, "%ssimilarity index %d%%", line_prefix, similarity_index(p));
2470 + strbuf_addf(msg, "\n%scopy from ", line_prefix);
2471 quote_c_style(name, msg, NULL, 0);
2398 - strbuf_addstr(msg, "\ncopy to ");
2472 + strbuf_addf(msg, "\n%scopy to ", line_prefix);
2473 quote_c_style(other, msg, NULL, 0);
2474 strbuf_addch(msg, '\n');
2475 break;
2476 case DIFF_STATUS_RENAMED:
2403 - strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2404 - strbuf_addstr(msg, "\nrename from ");
2477 + strbuf_addf(msg, "%ssimilarity index %d%%", line_prefix, similarity_index(p));
2478 + strbuf_addf(msg, "\n%srename from ", line_prefix);
2479 quote_c_style(name, msg, NULL, 0);
2406 - strbuf_addstr(msg, "\nrename to ");
2480 + strbuf_addf(msg, "\n%srename to ", line_prefix);
2481 quote_c_style(other, msg, NULL, 0);
2482 strbuf_addch(msg, '\n');
2483 break;
2484 case DIFF_STATUS_MODIFIED:
2485 if (p->score) {
2412 - strbuf_addf(msg, "dissimilarity index %d%%\n",
2413 - similarity_index(p));
2486 + strbuf_addf(msg, "%sdissimilarity index %d%%\n",
2487 + line_prefix, similarity_index(p));
2488 break;
2489 }
2490 /* fallthru */
@@ -2427,8 +2501,8 @@ static void fill_metainfo(struct strbuf *msg,
2501 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2502 abbrev = 40;
2503 }
2430 - strbuf_addf(msg, "index %.*s..%.*s",
2431 - abbrev, sha1_to_hex(one->sha1),
2504 + strbuf_addf(msg, "%sindex %.*s..%.*s",
2505 + line_prefix, abbrev, sha1_to_hex(one->sha1),
2506 abbrev, sha1_to_hex(two->sha1));
2507 if (one->mode == two->mode)
2508 strbuf_addf(msg, " %06o", one->mode);
@@ -3132,6 +3206,11 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
3206 {
3207 int line_termination = opt->line_termination;
3208 int inter_name_termination = line_termination ? '\t' : '\0';
3209 + if (opt->output_prefix) {
3210 + struct strbuf *msg = NULL;
3211 + msg = opt->output_prefix(opt, opt->output_prefix_data);
3212 + fprintf(opt->file, "%s", msg->buf);
3213 + }
3214
3215 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
3216 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
@@ -3377,48 +3456,62 @@ static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_f
3456 }
3457
3458
3380 -static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
3459 +static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name,
3460 + const char *line_prefix)
3461 {
3462 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
3383 - fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
3384 - show_name ? ' ' : '\n');
3463 + fprintf(file, "%s mode change %06o => %06o%c", line_prefix, p->one->mode,
3464 + p->two->mode, show_name ? ' ' : '\n');
3465 if (show_name) {
3466 write_name_quoted(p->two->path, file, '\n');
3467 }
3468 }
3469 }
3470
3391 -static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
3471 +static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p,
3472 + const char *line_prefix)
3473 {
3474 char *names = pprint_rename(p->one->path, p->two->path);
3475
3476 fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
3477 free(names);
3397 - show_mode_change(file, p, 0);
3478 + show_mode_change(file, p, 0, line_prefix);
3479 }
3480
3400 -static void diff_summary(FILE *file, struct diff_filepair *p)
3481 +static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
3482 {
3483 + FILE *file = opt->file;
3484 + char *line_prefix = "";
3485 +
3486 + if (opt->output_prefix) {
3487 + struct strbuf *buf = opt->output_prefix(opt, opt->output_prefix_data);
3488 + line_prefix = buf->buf;
3489 + }
3490 +
3491 switch(p->status) {
3492 case DIFF_STATUS_DELETED:
3493 + fputs(line_prefix, file);
3494 show_file_mode_name(file, "delete", p->one);
3495 break;
3496 case DIFF_STATUS_ADDED:
3497 + fputs(line_prefix, file);
3498 show_file_mode_name(file, "create", p->two);
3499 break;
3500 case DIFF_STATUS_COPIED:
3410 - show_rename_copy(file, "copy", p);
3501 + fputs(line_prefix, file);
3502 + show_rename_copy(file, "copy", p, line_prefix);
3503 break;
3504 case DIFF_STATUS_RENAMED:
3413 - show_rename_copy(file, "rename", p);
3505 + fputs(line_prefix, file);
3506 + show_rename_copy(file, "rename", p, line_prefix);
3507 break;
3508 default:
3509 if (p->score) {
3417 - fputs(" rewrite ", file);
3510 + fprintf(file, "%s rewrite ", line_prefix);
3511 write_name_quoted(p->two->path, file, ' ');
3512 fprintf(file, "(%d%%)\n", similarity_index(p));
3513 }
3421 - show_mode_change(file, p, !p->score);
3514 + show_mode_change(file, p, !p->score, line_prefix);
3515 break;
3516 }
3517 }
@@ -3627,8 +3720,9 @@ void diff_flush(struct diff_options *options)
3720 show_dirstat(options);
3721
3722 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
3630 - for (i = 0; i < q->nr; i++)
3631 - diff_summary(options->file, q->queue[i]);
3723 + for (i = 0; i < q->nr; i++) {
3724 + diff_summary(options, q->queue[i]);
3725 + }
3726 separator++;
3727 }
3728