@matheusbsbs / matheus-bagliano / commits / c0c77734bf
Fork 0

Write diff output to a file in struct diff_options

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Mar 9, 2008 at 22:43 UTC c0c77734bf313455364b3be7d3537d40e1138bcc
2 files changed +169 -140
diff.c
+166 -140
@@ -256,40 +256,41 @@ static int count_lines(const char *data, int size)
256 return count;
257 }
258
259 -static void print_line_count(int count)
259 +static void print_line_count(FILE *file, int count)
260 {
261 switch (count) {
262 case 0:
263 - printf("0,0");
263 + fprintf(file, "0,0");
264 break;
265 case 1:
266 - printf("1");
266 + fprintf(file, "1");
267 break;
268 default:
269 - printf("1,%d", count);
269 + fprintf(file, "1,%d", count);
270 break;
271 }
272 }
273
274 -static void copy_file_with_prefix(int prefix, const char *data, int size,
274 +static void copy_file_with_prefix(FILE *file,
275 + int prefix, const char *data, int size,
276 const char *set, const char *reset)
277 {
278 int ch, nl_just_seen = 1;
279 while (0 < size--) {
280 ch = *data++;
281 if (nl_just_seen) {
281 - fputs(set, stdout);
282 - putchar(prefix);
282 + fputs(set, file);
283 + putc(prefix, file);
284 }
285 if (ch == '\n') {
286 nl_just_seen = 1;
286 - fputs(reset, stdout);
287 + fputs(reset, file);
288 } else
289 nl_just_seen = 0;
289 - putchar(ch);
290 + putc(ch, file);
291 }
292 if (!nl_just_seen)
292 - printf("%s\n\\ No newline at end of file\n", reset);
293 + fprintf(file, "%s\n\\ No newline at end of file\n", reset);
294 }
295
296 static void emit_rewrite_diff(const char *name_a,
@@ -322,17 +323,18 @@ static void emit_rewrite_diff(const char *name_a,
323 diff_populate_filespec(two, 0);
324 lc_a = count_lines(one->data, one->size);
325 lc_b = count_lines(two->data, two->size);
325 - printf("%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
326 - metainfo, a_name.buf, name_a_tab, reset,
327 - metainfo, b_name.buf, name_b_tab, reset, fraginfo);
328 - print_line_count(lc_a);
329 - printf(" +");
330 - print_line_count(lc_b);
331 - printf(" @@%s\n", reset);
326 + fprintf(o->file,
327 + "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
328 + metainfo, a_name.buf, name_a_tab, reset,
329 + metainfo, b_name.buf, name_b_tab, reset, fraginfo);
330 + print_line_count(o->file, lc_a);
331 + fprintf(o->file, " +");
332 + print_line_count(o->file, lc_b);
333 + fprintf(o->file, " @@%s\n", reset);
334 if (lc_a)
333 - copy_file_with_prefix('-', one->data, one->size, old, reset);
335 + copy_file_with_prefix(o->file, '-', one->data, one->size, old, reset);
336 if (lc_b)
335 - copy_file_with_prefix('+', two->data, two->size, new, reset);
337 + copy_file_with_prefix(o->file, '+', two->data, two->size, new, reset);
338 }
339
340 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
@@ -372,9 +374,10 @@ static void diff_words_append(char *line, unsigned long len,
374 struct diff_words_data {
375 struct xdiff_emit_state xm;
376 struct diff_words_buffer minus, plus;
377 + FILE *file;
378 };
379
377 -static void print_word(struct diff_words_buffer *buffer, int len, int color,
380 +static void print_word(FILE *file, struct diff_words_buffer *buffer, int len, int color,
381 int suppress_newline)
382 {
383 const char *ptr;
@@ -391,15 +394,15 @@ static void print_word(struct diff_words_buffer *buffer, int len, int color,
394 len--;
395 }
396
394 - fputs(diff_get_color(1, color), stdout);
395 - fwrite(ptr, len, 1, stdout);
396 - fputs(diff_get_color(1, DIFF_RESET), stdout);
397 + fputs(diff_get_color(1, color), file);
398 + fwrite(ptr, len, 1, file);
399 + fputs(diff_get_color(1, DIFF_RESET), file);
400
401 if (eol) {
402 if (suppress_newline)
403 buffer->suppressed_newline = 1;
404 else
402 - putchar('\n');
405 + putc('\n', file);
406 }
407 }
408
@@ -409,20 +412,23 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
412
413 if (diff_words->minus.suppressed_newline) {
414 if (line[0] != '+')
412 - putchar('\n');
415 + putc('\n', diff_words->file);
416 diff_words->minus.suppressed_newline = 0;
417 }
418
419 len--;
420 switch (line[0]) {
421 case '-':
419 - print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
422 + print_word(diff_words->file,
423 + &diff_words->minus, len, DIFF_FILE_OLD, 1);
424 break;
425 case '+':
422 - print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
426 + print_word(diff_words->file,
427 + &diff_words->plus, len, DIFF_FILE_NEW, 0);
428 break;
429 case ' ':
425 - print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
430 + print_word(diff_words->file,
431 + &diff_words->plus, len, DIFF_PLAIN, 0);
432 diff_words->minus.current += len;
433 break;
434 }
@@ -466,7 +472,7 @@ static void diff_words_show(struct diff_words_data *diff_words)
472 diff_words->minus.text.size = diff_words->plus.text.size = 0;
473
474 if (diff_words->minus.suppressed_newline) {
469 - putchar('\n');
475 + putc('\n', diff_words->file);
476 diff_words->minus.suppressed_newline = 0;
477 }
478 }
@@ -481,6 +487,7 @@ struct emit_callback {
487 const char **label_path;
488 struct diff_words_data *diff_words;
489 int *found_changesp;
490 + FILE *file;
491 };
492
493 static void free_diff_words_data(struct emit_callback *ecbdata)
@@ -505,11 +512,11 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
512 return "";
513 }
514
508 -static void emit_line(const char *set, const char *reset, const char *line, int len)
515 +static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
516 {
510 - fputs(set, stdout);
511 - fwrite(line, len, 1, stdout);
512 - fputs(reset, stdout);
517 + fputs(set, file);
518 + fwrite(line, len, 1, file);
519 + fputs(reset, file);
520 }
521
522 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
@@ -518,13 +525,13 @@ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, cons
525 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
526
527 if (!*ws)
521 - emit_line(set, reset, line, len);
528 + emit_line(ecbdata->file, set, reset, line, len);
529 else {
530 /* Emit just the prefix, then the rest. */
524 - emit_line(set, reset, line, ecbdata->nparents);
531 + emit_line(ecbdata->file, set, reset, line, ecbdata->nparents);
532 (void)check_and_emit_line(line + ecbdata->nparents,
533 len - ecbdata->nparents, ecbdata->ws_rule,
527 - stdout, set, reset, ws);
534 + ecbdata->file, set, reset, ws);
535 }
536 }
537
@@ -563,10 +570,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
570 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
571 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
572
566 - printf("%s--- %s%s%s\n",
567 - meta, ecbdata->label_path[0], reset, name_a_tab);
568 - printf("%s+++ %s%s%s\n",
569 - meta, ecbdata->label_path[1], reset, name_b_tab);
573 + fprintf(ecbdata->file, "%s--- %s%s%s\n",
574 + meta, ecbdata->label_path[0], reset, name_a_tab);
575 + fprintf(ecbdata->file, "%s+++ %s%s%s\n",
576 + meta, ecbdata->label_path[1], reset, name_b_tab);
577 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
578 }
579
@@ -578,15 +585,16 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
585 if (2 <= i && i < len && line[i] == ' ') {
586 ecbdata->nparents = i - 1;
587 len = sane_truncate_line(ecbdata, line, len);
581 - emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
588 + emit_line(ecbdata->file,
589 + diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
590 reset, line, len);
591 if (line[len-1] != '\n')
584 - putchar('\n');
592 + putc('\n', ecbdata->file);
593 return;
594 }
595
596 if (len < ecbdata->nparents) {
589 - emit_line(reset, reset, line, len);
597 + emit_line(ecbdata->file, reset, reset, line, len);
598 return;
599 }
600
@@ -609,7 +617,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
617 diff_words_show(ecbdata->diff_words);
618 line++;
619 len--;
612 - emit_line(plain, reset, line, len);
620 + emit_line(ecbdata->file, plain, reset, line, len);
621 return;
622 }
623 for (i = 0; i < ecbdata->nparents && len; i++) {
@@ -620,7 +628,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
628 }
629
630 if (color != DIFF_FILE_NEW) {
623 - emit_line(diff_get_color(ecbdata->color_diff, color),
631 + emit_line(ecbdata->file,
632 + diff_get_color(ecbdata->color_diff, color),
633 reset, line, len);
634 return;
635 }
@@ -759,20 +768,21 @@ static int scale_linear(int it, int width, int max_change)
768 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
769 }
770
762 -static void show_name(const char *prefix, const char *name, int len,
771 +static void show_name(FILE *file,
772 + const char *prefix, const char *name, int len,
773 const char *reset, const char *set)
774 {
765 - printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
775 + fprintf(file, " %s%s%-*s%s |", set, prefix, len, name, reset);
776 }
777
768 -static void show_graph(char ch, int cnt, const char *set, const char *reset)
778 +static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
779 {
780 if (cnt <= 0)
781 return;
772 - printf("%s", set);
782 + fprintf(file, "%s", set);
783 while (cnt--)
774 - putchar(ch);
775 - printf("%s", reset);
784 + putc(ch, file);
785 + fprintf(file, "%s", reset);
786 }
787
788 static void fill_print_name(struct diffstat_file *file)
@@ -877,18 +887,18 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
887 }
888
889 if (data->files[i]->is_binary) {
880 - show_name(prefix, name, len, reset, set);
881 - printf(" Bin ");
882 - printf("%s%d%s", del_c, deleted, reset);
883 - printf(" -> ");
884 - printf("%s%d%s", add_c, added, reset);
885 - printf(" bytes");
886 - printf("\n");
890 + show_name(options->file, prefix, name, len, reset, set);
891 + fprintf(options->file, " Bin ");
892 + fprintf(options->file, "%s%d%s", del_c, deleted, reset);
893 + fprintf(options->file, " -> ");
894 + fprintf(options->file, "%s%d%s", add_c, added, reset);
895 + fprintf(options->file, " bytes");
896 + fprintf(options->file, "\n");
897 continue;
898 }
899 else if (data->files[i]->is_unmerged) {
890 - show_name(prefix, name, len, reset, set);
891 - printf(" Unmerged\n");
900 + show_name(options->file, prefix, name, len, reset, set);
901 + fprintf(options->file, " Unmerged\n");
902 continue;
903 }
904 else if (!data->files[i]->is_renamed &&
@@ -911,17 +921,18 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
921 del = scale_linear(del, width, max_change);
922 total = add + del;
923 }
914 - show_name(prefix, name, len, reset, set);
915 - printf("%5d ", added + deleted);
916 - show_graph('+', add, add_c, reset);
917 - show_graph('-', del, del_c, reset);
918 - putchar('\n');
919 - }
920 - printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
924 + show_name(options->file, prefix, name, len, reset, set);
925 + fprintf(options->file, "%5d ", added + deleted);
926 + show_graph(options->file, '+', add, add_c, reset);
927 + show_graph(options->file, '-', del, del_c, reset);
928 + fprintf(options->file, "\n");
929 + }
930 + fprintf(options->file,
931 + "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
932 set, total_files, adds, dels, reset);
933 }
934
924 -static void show_shortstats(struct diffstat_t* data)
935 +static void show_shortstats(struct diffstat_t* data, struct diff_options *options)
936 {
937 int i, adds = 0, dels = 0, total_files = data->nr;
938
@@ -942,7 +953,7 @@ static void show_shortstats(struct diffstat_t* data)
953 }
954 }
955 }
945 - printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
956 + fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
957 total_files, adds, dels);
958 }
959
@@ -957,24 +968,25 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)
968 struct diffstat_file *file = data->files[i];
969
970 if (file->is_binary)
960 - printf("-\t-\t");
971 + fprintf(options->file, "-\t-\t");
972 else
962 - printf("%d\t%d\t", file->added, file->deleted);
973 + fprintf(options->file,
974 + "%d\t%d\t", file->added, file->deleted);
975 if (options->line_termination) {
976 fill_print_name(file);
977 if (!file->is_renamed)
966 - write_name_quoted(file->name, stdout,
978 + write_name_quoted(file->name, options->file,
979 options->line_termination);
980 else {
969 - fputs(file->print_name, stdout);
970 - putchar(options->line_termination);
981 + fputs(file->print_name, options->file);
982 + putc(options->line_termination, options->file);
983 }
984 } else {
985 if (file->is_renamed) {
974 - putchar('\0');
975 - write_name_quoted(file->from_name, stdout, '\0');
986 + putc('\0', options->file);
987 + write_name_quoted(file->from_name, options->file, '\0');
988 }
977 - write_name_quoted(file->name, stdout, '\0');
989 + write_name_quoted(file->name, options->file, '\0');
990 }
991 }
992 }
@@ -984,7 +996,7 @@ struct diffstat_dir {
996 int nr, percent, cumulative;
997 };
998
987 -static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, const char *base, int baselen)
999 +static long gather_dirstat(FILE *file, struct diffstat_dir *dir, unsigned long changed, const char *base, int baselen)
1000 {
1001 unsigned long this_dir = 0;
1002 unsigned int sources = 0;
@@ -1002,7 +1014,7 @@ static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, cons
1014 slash = strchr(f->name + baselen, '/');
1015 if (slash) {
1016 int newbaselen = slash + 1 - f->name;
1005 - this = gather_dirstat(dir, changed, f->name, newbaselen);
1017 + this = gather_dirstat(file, dir, changed, f->name, newbaselen);
1018 sources++;
1019 } else {
1020 if (f->is_unmerged || f->is_binary)
@@ -1027,7 +1039,7 @@ static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, cons
1039 if (permille) {
1040 int percent = permille / 10;
1041 if (percent >= dir->percent) {
1030 - printf("%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1042 + fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1043 if (!dir->cumulative)
1044 return 0;
1045 }
@@ -1060,7 +1072,7 @@ static void show_dirstat(struct diffstat_t *data, struct diff_options *options)
1072 dir.nr = data->nr;
1073 dir.percent = options->dirstat_percent;
1074 dir.cumulative = options->output_format & DIFF_FORMAT_CUMULATIVE;
1063 - gather_dirstat(&dir, changed, "", 0);
1075 + gather_dirstat(options->file, &dir, changed, "", 0);
1076 }
1077
1078 static void free_diffstat_info(struct diffstat_t *diffstat)
@@ -1083,6 +1095,7 @@ struct checkdiff_t {
1095 int lineno, color_diff;
1096 unsigned ws_rule;
1097 unsigned status;
1098 + FILE *file;
1099 };
1100
1101 static void checkdiff_consume(void *priv, char *line, unsigned long len)
@@ -1100,11 +1113,11 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1113 if (!data->status)
1114 return;
1115 err = whitespace_error_string(data->status);
1103 - printf("%s:%d: %s.\n", data->filename, data->lineno, err);
1116 + fprintf(data->file, "%s:%d: %s.\n", data->filename, data->lineno, err);
1117 free(err);
1105 - emit_line(set, reset, line, 1);
1118 + emit_line(data->file, set, reset, line, 1);
1119 (void)check_and_emit_line(line + 1, len - 1, data->ws_rule,
1107 - stdout, set, reset, ws);
1120 + data->file, set, reset, ws);
1121 } else if (line[0] == ' ')
1122 data->lineno++;
1123 else if (line[0] == '@') {
@@ -1140,7 +1153,7 @@ static unsigned char *deflate_it(char *data,
1153 return deflated;
1154 }
1155
1143 -static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1156 +static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1157 {
1158 void *cp;
1159 void *delta;
@@ -1169,13 +1182,13 @@ static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1182 }
1183
1184 if (delta && delta_size < deflate_size) {
1172 - printf("delta %lu\n", orig_size);
1185 + fprintf(file, "delta %lu\n", orig_size);
1186 free(deflated);
1187 data = delta;
1188 data_size = delta_size;
1189 }
1190 else {
1178 - printf("literal %lu\n", two->size);
1191 + fprintf(file, "literal %lu\n", two->size);
1192 free(delta);
1193 data = deflated;
1194 data_size = deflate_size;
@@ -1193,17 +1206,18 @@ static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1206 line[0] = bytes - 26 + 'a' - 1;
1207 encode_85(line + 1, cp, bytes);
1208 cp = (char *) cp + bytes;
1196 - puts(line);
1209 + fputs(line, file);
1210 + fputc('\n', file);
1211 }
1198 - printf("\n");
1212 + fprintf(file, "\n");
1213 free(data);
1214 }
1215
1202 -static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1216 +static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
1217 {
1204 - printf("GIT binary patch\n");
1205 - emit_binary_diff_body(one, two);
1206 - emit_binary_diff_body(two, one);
1218 + fprintf(file, "GIT binary patch\n");
1219 + emit_binary_diff_body(file, one, two);
1220 + emit_binary_diff_body(file, two, one);
1221 }
1222
1223 static void setup_diff_attr_check(struct git_attr_check *check)
@@ -1334,25 +1348,25 @@ static void builtin_diff(const char *name_a,
1348 b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
1349 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1350 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1337 - printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1351 + fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1352 if (lbl[0][0] == '/') {
1353 /* /dev/null */
1340 - printf("%snew file mode %06o%s\n", set, two->mode, reset);
1354 + fprintf(o->file, "%snew file mode %06o%s\n", set, two->mode, reset);
1355 if (xfrm_msg && xfrm_msg[0])
1342 - printf("%s%s%s\n", set, xfrm_msg, reset);
1356 + fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1357 }
1358 else if (lbl[1][0] == '/') {
1345 - printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1359 + fprintf(o->file, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1360 if (xfrm_msg && xfrm_msg[0])
1347 - printf("%s%s%s\n", set, xfrm_msg, reset);
1361 + fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1362 }
1363 else {
1364 if (one->mode != two->mode) {
1351 - printf("%sold mode %06o%s\n", set, one->mode, reset);
1352 - printf("%snew mode %06o%s\n", set, two->mode, reset);
1365 + fprintf(o->file, "%sold mode %06o%s\n", set, one->mode, reset);
1366 + fprintf(o->file, "%snew mode %06o%s\n", set, two->mode, reset);
1367 }
1368 if (xfrm_msg && xfrm_msg[0])
1355 - printf("%s%s%s\n", set, xfrm_msg, reset);
1369 + fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1370 /*
1371 * we do not run diff between different kind
1372 * of objects.
@@ -1376,10 +1390,10 @@ static void builtin_diff(const char *name_a,
1390 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1391 goto free_ab_and_return;
1392 if (DIFF_OPT_TST(o, BINARY))
1379 - emit_binary_diff(&mf1, &mf2);
1393 + emit_binary_diff(o->file, &mf1, &mf2);
1394 else
1381 - printf("Binary files %s and %s differ\n",
1382 - lbl[0], lbl[1]);
1395 + fprintf(o->file, "Binary files %s and %s differ\n",
1396 + lbl[0], lbl[1]);
1397 o->found_changes = 1;
1398 }
1399 else {
@@ -1401,6 +1415,7 @@ static void builtin_diff(const char *name_a,
1415 ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1416 ecbdata.found_changesp = &o->found_changes;
1417 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1418 + ecbdata.file = o->file;
1419 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1420 xecfg.ctxlen = o->context;
1421 xecfg.flags = XDL_EMIT_FUNCNAMES;
@@ -1415,9 +1430,11 @@ static void builtin_diff(const char *name_a,
1430 ecb.outf = xdiff_outf;
1431 ecb.priv = &ecbdata;
1432 ecbdata.xm.consume = fn_out_consume;
1418 - if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1433 + if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1434 ecbdata.diff_words =
1435 xcalloc(1, sizeof(struct diff_words_data));
1436 + ecbdata.diff_words->file = o->file;
1437 + }
1438 xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1439 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1440 free_diff_words_data(&ecbdata);
@@ -1496,6 +1513,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1513 data.lineno = 0;
1514 data.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1515 data.ws_rule = whitespace_rule(attr_path);
1516 + data.file = o->file;
1517
1518 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1519 die("unable to read files to diff");
@@ -1966,7 +1984,7 @@ static void run_diff_cmd(const char *pgm,
1984 builtin_diff(name, other ? other : name,
1985 one, two, xfrm_msg, o, complete_rewrite);
1986 else
1969 - printf("* Unmerged path %s\n", name);
1987 + fprintf(o->file, "* Unmerged path %s\n", name);
1988 }
1989
1990 static void diff_fill_sha1_info(struct diff_filespec *one)
@@ -2157,6 +2175,9 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2175 void diff_setup(struct diff_options *options)
2176 {
2177 memset(options, 0, sizeof(*options));
2178 +
2179 + options->file = stdout;
2180 +
2181 options->line_termination = '\n';
2182 options->break_opt = -1;
2183 options->rename_limit = -1;
@@ -2470,7 +2491,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2491 options->b_prefix = arg + 13;
2492 else if (!strcmp(arg, "--no-prefix"))
2493 options->a_prefix = options->b_prefix = "";
2473 - else
2494 + else if (!prefixcmp(arg, "--output=")) {
2495 + options->file = fopen(arg + strlen("--output="), "w");
2496 + options->close_file = 1;
2497 + } else
2498 return 0;
2499 return 1;
2500 }
@@ -2599,15 +2623,15 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2623 int inter_name_termination = line_termination ? '\t' : '\0';
2624
2625 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
2602 - printf(":%06o %06o %s ", p->one->mode, p->two->mode,
2603 - diff_unique_abbrev(p->one->sha1, opt->abbrev));
2604 - printf("%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2626 + fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
2627 + diff_unique_abbrev(p->one->sha1, opt->abbrev));
2628 + fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2629 }
2630 if (p->score) {
2607 - printf("%c%03d%c", p->status, similarity_index(p),
2608 - inter_name_termination);
2631 + fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
2632 + inter_name_termination);
2633 } else {
2610 - printf("%c%c", p->status, inter_name_termination);
2634 + fprintf(opt->file, "%c%c", p->status, inter_name_termination);
2635 }
2636
2637 if (p->status == DIFF_STATUS_COPIED ||
@@ -2616,14 +2640,14 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2640 name_a = p->one->path;
2641 name_b = p->two->path;
2642 strip_prefix(opt->prefix_length, &name_a, &name_b);
2619 - write_name_quoted(name_a, stdout, inter_name_termination);
2620 - write_name_quoted(name_b, stdout, line_termination);
2643 + write_name_quoted(name_a, opt->file, inter_name_termination);
2644 + write_name_quoted(name_b, opt->file, line_termination);
2645 } else {
2646 const char *name_a, *name_b;
2647 name_a = p->one->mode ? p->one->path : p->two->path;
2648 name_b = NULL;
2649 strip_prefix(opt->prefix_length, &name_a, &name_b);
2626 - write_name_quoted(name_a, stdout, line_termination);
2650 + write_name_quoted(name_a, opt->file, line_termination);
2651 }
2652 }
2653
@@ -2825,62 +2849,62 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2849 name_a = p->two->path;
2850 name_b = NULL;
2851 strip_prefix(opt->prefix_length, &name_a, &name_b);
2828 - write_name_quoted(name_a, stdout, opt->line_termination);
2852 + write_name_quoted(name_a, opt->file, opt->line_termination);
2853 }
2854 }
2855
2832 -static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2856 +static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
2857 {
2858 if (fs->mode)
2835 - printf(" %s mode %06o ", newdelete, fs->mode);
2859 + fprintf(file, " %s mode %06o ", newdelete, fs->mode);
2860 else
2837 - printf(" %s ", newdelete);
2838 - write_name_quoted(fs->path, stdout, '\n');
2861 + fprintf(file, " %s ", newdelete);
2862 + write_name_quoted(fs->path, file, '\n');
2863 }
2864
2865
2842 -static void show_mode_change(struct diff_filepair *p, int show_name)
2866 +static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
2867 {
2868 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2845 - printf(" mode change %06o => %06o%c", p->one->mode, p->two->mode,
2869 + fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
2870 show_name ? ' ' : '\n');
2871 if (show_name) {
2848 - write_name_quoted(p->two->path, stdout, '\n');
2872 + write_name_quoted(p->two->path, file, '\n');
2873 }
2874 }
2875 }
2876
2853 -static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2877 +static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
2878 {
2879 char *names = pprint_rename(p->one->path, p->two->path);
2880
2857 - printf(" %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2881 + fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2882 free(names);
2859 - show_mode_change(p, 0);
2883 + show_mode_change(file, p, 0);
2884 }
2885
2862 -static void diff_summary(struct diff_filepair *p)
2886 +static void diff_summary(FILE *file, struct diff_filepair *p)
2887 {
2888 switch(p->status) {
2889 case DIFF_STATUS_DELETED:
2866 - show_file_mode_name("delete", p->one);
2890 + show_file_mode_name(file, "delete", p->one);
2891 break;
2892 case DIFF_STATUS_ADDED:
2869 - show_file_mode_name("create", p->two);
2893 + show_file_mode_name(file, "create", p->two);
2894 break;
2895 case DIFF_STATUS_COPIED:
2872 - show_rename_copy("copy", p);
2896 + show_rename_copy(file, "copy", p);
2897 break;
2898 case DIFF_STATUS_RENAMED:
2875 - show_rename_copy("rename", p);
2899 + show_rename_copy(file, "rename", p);
2900 break;
2901 default:
2902 if (p->score) {
2879 - fputs(" rewrite ", stdout);
2880 - write_name_quoted(p->two->path, stdout, ' ');
2881 - printf("(%d%%)\n", similarity_index(p));
2903 + fputs(" rewrite ", file);
2904 + write_name_quoted(p->two->path, file, ' ');
2905 + fprintf(file, "(%d%%)\n", similarity_index(p));
2906 }
2883 - show_mode_change(p, !p->score);
2907 + show_mode_change(file, p, !p->score);
2908 break;
2909 }
2910 }
@@ -3088,14 +3112,14 @@ void diff_flush(struct diff_options *options)
3112 if (output_format & DIFF_FORMAT_DIFFSTAT)
3113 show_stats(&diffstat, options);
3114 if (output_format & DIFF_FORMAT_SHORTSTAT)
3091 - show_shortstats(&diffstat);
3115 + show_shortstats(&diffstat, options);
3116 free_diffstat_info(&diffstat);
3117 separator++;
3118 }
3119
3120 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
3121 for (i = 0; i < q->nr; i++)
3098 - diff_summary(q->queue[i]);
3122 + diff_summary(options->file, q->queue[i]);
3123 separator++;
3124 }
3125
@@ -3103,9 +3127,9 @@ void diff_flush(struct diff_options *options)
3127 if (separator) {
3128 if (options->stat_sep) {
3129 /* attach patch instead of inline */
3106 - fputs(options->stat_sep, stdout);
3130 + fputs(options->stat_sep, options->file);
3131 } else {
3108 - putchar(options->line_termination);
3132 + putc(options->line_termination, options->file);
3133 }
3134 }
3135
@@ -3125,6 +3149,8 @@ free_queue:
3149 free(q->queue);
3150 q->queue = NULL;
3151 q->nr = q->alloc = 0;
3152 + if (options->close_file)
3153 + fclose(options->file);
3154 }
3155
3156 static void diffcore_apply_filter(const char *filter)
diff.h
+3
@@ -98,6 +98,9 @@ struct diff_options {
98 /* this is set by diffcore for DIFF_FORMAT_PATCH */
99 int found_changes;
100
101 + FILE *file;
102 + int close_file;
103 +
104 int nr_paths;
105 const char **paths;
106 int *pathlens;