{"thread":{"id":"12612","subject":"[PATCH] Write diff output to a file in struct diff_options","startedAt":"2008-03-10T02:43:39Z","lastAt":"2008-03-10T17:58:26Z","messageCount":6,"participants":["Daniel Barkalow","Junio C Hamano","Johannes Schindelin","Jakub Narebski","Kristian Høgsberg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"71548","messageId":"alpine.LNX.1.00.0803092235150.19665@iabervon.org","threadId":"12612","inReplyTo":null,"subject":"[PATCH] Write diff output to a file in struct diff_options","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-03-10T02:43:39Z","receivedAt":"2008-03-10T02:43:39Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\nI remember there being some expectation that this would be difficult, but \nit was a pretty straightforward conversion, so I'm wondering if I've \nmissed something. In any case, this is missing tests and documentation for \n--output=, but I really want it to use directly for rebase-interactive, \nand I only did the command line option so I could do informal testing.\n\n diff.c |  306 +++++++++++++++++++++++++++++++++++-----------------------------\n diff.h |    3 +\n 2 files changed, 169 insertions(+), 140 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex 00e1590..3a0b077 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -256,40 +256,41 @@ static int count_lines(const char *data, int size)\n \treturn count;\n }\n \n-static void print_line_count(int count)\n+static void print_line_count(FILE *file, int count)\n {\n \tswitch (count) {\n \tcase 0:\n-\t\tprintf(\"0,0\");\n+\t\tfprintf(file, \"0,0\");\n \t\tbreak;\n \tcase 1:\n-\t\tprintf(\"1\");\n+\t\tfprintf(file, \"1\");\n \t\tbreak;\n \tdefault:\n-\t\tprintf(\"1,%d\", count);\n+\t\tfprintf(file, \"1,%d\", count);\n \t\tbreak;\n \t}\n }\n \n-static void copy_file_with_prefix(int prefix, const char *data, int size,\n+static void copy_file_with_prefix(FILE *file,\n+\t\t\t\t  int prefix, const char *data, int size,\n \t\t\t\t  const char *set, const char *reset)\n {\n \tint ch, nl_just_seen = 1;\n \twhile (0 < size--) {\n \t\tch = *data++;\n \t\tif (nl_just_seen) {\n-\t\t\tfputs(set, stdout);\n-\t\t\tputchar(prefix);\n+\t\t\tfputs(set, file);\n+\t\t\tputc(prefix, file);\n \t\t}\n \t\tif (ch == '\\n') {\n \t\t\tnl_just_seen = 1;\n-\t\t\tfputs(reset, stdout);\n+\t\t\tfputs(reset, file);\n \t\t} else\n \t\t\tnl_just_seen = 0;\n-\t\tputchar(ch);\n+\t\tputc(ch, file);\n \t}\n \tif (!nl_just_seen)\n-\t\tprintf(\"%s\\n\\\\ No newline at end of file\\n\", reset);\n+\t\tfprintf(file, \"%s\\n\\\\ No newline at end of file\\n\", reset);\n }\n \n static void emit_rewrite_diff(const char *name_a,\n@@ -322,17 +323,18 @@ static void emit_rewrite_diff(const char *name_a,\n \tdiff_populate_filespec(two, 0);\n \tlc_a = count_lines(one->data, one->size);\n \tlc_b = count_lines(two->data, two->size);\n-\tprintf(\"%s--- %s%s%s\\n%s+++ %s%s%s\\n%s@@ -\",\n-\t       metainfo, a_name.buf, name_a_tab, reset,\n-\t       metainfo, b_name.buf, name_b_tab, reset, fraginfo);\n-\tprint_line_count(lc_a);\n-\tprintf(\" +\");\n-\tprint_line_count(lc_b);\n-\tprintf(\" @@%s\\n\", reset);\n+\tfprintf(o->file,\n+\t\t\"%s--- %s%s%s\\n%s+++ %s%s%s\\n%s@@ -\",\n+\t\tmetainfo, a_name.buf, name_a_tab, reset,\n+\t\tmetainfo, b_name.buf, name_b_tab, reset, fraginfo);\n+\tprint_line_count(o->file, lc_a);\n+\tfprintf(o->file, \" +\");\n+\tprint_line_count(o->file, lc_b);\n+\tfprintf(o->file, \" @@%s\\n\", reset);\n \tif (lc_a)\n-\t\tcopy_file_with_prefix('-', one->data, one->size, old, reset);\n+\t\tcopy_file_with_prefix(o->file, '-', one->data, one->size, old, reset);\n \tif (lc_b)\n-\t\tcopy_file_with_prefix('+', two->data, two->size, new, reset);\n+\t\tcopy_file_with_prefix(o->file, '+', two->data, two->size, new, reset);\n }\n \n static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)\n@@ -372,9 +374,10 @@ static void diff_words_append(char *line, unsigned long len,\n struct diff_words_data {\n \tstruct xdiff_emit_state xm;\n \tstruct diff_words_buffer minus, plus;\n+\tFILE *file;\n };\n \n-static void print_word(struct diff_words_buffer *buffer, int len, int color,\n+static void print_word(FILE *file, struct diff_words_buffer *buffer, int len, int color,\n \t\tint suppress_newline)\n {\n \tconst char *ptr;\n@@ -391,15 +394,15 @@ static void print_word(struct diff_words_buffer *buffer, int len, int color,\n \t\tlen--;\n \t}\n \n-\tfputs(diff_get_color(1, color), stdout);\n-\tfwrite(ptr, len, 1, stdout);\n-\tfputs(diff_get_color(1, DIFF_RESET), stdout);\n+\tfputs(diff_get_color(1, color), file);\n+\tfwrite(ptr, len, 1, file);\n+\tfputs(diff_get_color(1, DIFF_RESET), file);\n \n \tif (eol) {\n \t\tif (suppress_newline)\n \t\t\tbuffer->suppressed_newline = 1;\n \t\telse\n-\t\t\tputchar('\\n');\n+\t\t\tputc('\\n', file);\n \t}\n }\n \n@@ -409,20 +412,23 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)\n \n \tif (diff_words->minus.suppressed_newline) {\n \t\tif (line[0] != '+')\n-\t\t\tputchar('\\n');\n+\t\t\tputc('\\n', diff_words->file);\n \t\tdiff_words->minus.suppressed_newline = 0;\n \t}\n \n \tlen--;\n \tswitch (line[0]) {\n \t\tcase '-':\n-\t\t\tprint_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);\n+\t\t\tprint_word(diff_words->file,\n+\t\t\t\t   &diff_words->minus, len, DIFF_FILE_OLD, 1);\n \t\t\tbreak;\n \t\tcase '+':\n-\t\t\tprint_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);\n+\t\t\tprint_word(diff_words->file,\n+\t\t\t\t   &diff_words->plus, len, DIFF_FILE_NEW, 0);\n \t\t\tbreak;\n \t\tcase ' ':\n-\t\t\tprint_word(&diff_words->plus, len, DIFF_PLAIN, 0);\n+\t\t\tprint_word(diff_words->file,\n+\t\t\t\t   &diff_words->plus, len, DIFF_PLAIN, 0);\n \t\t\tdiff_words->minus.current += len;\n \t\t\tbreak;\n \t}\n@@ -466,7 +472,7 @@ static void diff_words_show(struct diff_words_data *diff_words)\n \tdiff_words->minus.text.size = diff_words->plus.text.size = 0;\n \n \tif (diff_words->minus.suppressed_newline) {\n-\t\tputchar('\\n');\n+\t\tputc('\\n', diff_words->file);\n \t\tdiff_words->minus.suppressed_newline = 0;\n \t}\n }\n@@ -481,6 +487,7 @@ struct emit_callback {\n \tconst char **label_path;\n \tstruct diff_words_data *diff_words;\n \tint *found_changesp;\n+\tFILE *file;\n };\n \n static void free_diff_words_data(struct emit_callback *ecbdata)\n@@ -505,11 +512,11 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)\n \treturn \"\";\n }\n \n-static void emit_line(const char *set, const char *reset, const char *line, int len)\n+static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)\n {\n-\tfputs(set, stdout);\n-\tfwrite(line, len, 1, stdout);\n-\tfputs(reset, stdout);\n+\tfputs(set, file);\n+\tfwrite(line, len, 1, file);\n+\tfputs(reset, file);\n }\n \n static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)\n@@ -518,13 +525,13 @@ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, cons\n \tconst char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);\n \n \tif (!*ws)\n-\t\temit_line(set, reset, line, len);\n+\t\temit_line(ecbdata->file, set, reset, line, len);\n \telse {\n \t\t/* Emit just the prefix, then the rest. */\n-\t\temit_line(set, reset, line, ecbdata->nparents);\n+\t\temit_line(ecbdata->file, set, reset, line, ecbdata->nparents);\n \t\t(void)check_and_emit_line(line + ecbdata->nparents,\n \t\t    len - ecbdata->nparents, ecbdata->ws_rule,\n-\t\t    stdout, set, reset, ws);\n+\t\t    ecbdata->file, set, reset, ws);\n \t}\n }\n \n@@ -563,10 +570,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)\n \t\tname_a_tab = strchr(ecbdata->label_path[0], ' ') ? \"\\t\" : \"\";\n \t\tname_b_tab = strchr(ecbdata->label_path[1], ' ') ? \"\\t\" : \"\";\n \n-\t\tprintf(\"%s--- %s%s%s\\n\",\n-\t\t       meta, ecbdata->label_path[0], reset, name_a_tab);\n-\t\tprintf(\"%s+++ %s%s%s\\n\",\n-\t\t       meta, ecbdata->label_path[1], reset, name_b_tab);\n+\t\tfprintf(ecbdata->file, \"%s--- %s%s%s\\n\",\n+\t\t\tmeta, ecbdata->label_path[0], reset, name_a_tab);\n+\t\tfprintf(ecbdata->file, \"%s+++ %s%s%s\\n\",\n+\t\t\tmeta, ecbdata->label_path[1], reset, name_b_tab);\n \t\tecbdata->label_path[0] = ecbdata->label_path[1] = NULL;\n \t}\n \n@@ -578,15 +585,16 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)\n \tif (2 <= i && i < len && line[i] == ' ') {\n \t\tecbdata->nparents = i - 1;\n \t\tlen = sane_truncate_line(ecbdata, line, len);\n-\t\temit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),\n+\t\temit_line(ecbdata->file, \n+\t\t\t  diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),\n \t\t\t  reset, line, len);\n \t\tif (line[len-1] != '\\n')\n-\t\t\tputchar('\\n');\n+\t\t\tputc('\\n', ecbdata->file);\n \t\treturn;\n \t}\n \n \tif (len < ecbdata->nparents) {\n-\t\temit_line(reset, reset, line, len);\n+\t\temit_line(ecbdata->file, reset, reset, line, len);\n \t\treturn;\n \t}\n \n@@ -609,7 +617,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)\n \t\t\tdiff_words_show(ecbdata->diff_words);\n \t\tline++;\n \t\tlen--;\n-\t\temit_line(plain, reset, line, len);\n+\t\temit_line(ecbdata->file, plain, reset, line, len);\n \t\treturn;\n \t}\n \tfor (i = 0; i < ecbdata->nparents && len; i++) {\n@@ -620,7 +628,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)\n \t}\n \n \tif (color != DIFF_FILE_NEW) {\n-\t\temit_line(diff_get_color(ecbdata->color_diff, color),\n+\t\temit_line(ecbdata->file,\n+\t\t\t  diff_get_color(ecbdata->color_diff, color),\n \t\t\t  reset, line, len);\n \t\treturn;\n \t}\n@@ -759,20 +768,21 @@ static int scale_linear(int it, int width, int max_change)\n \treturn ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);\n }\n \n-static void show_name(const char *prefix, const char *name, int len,\n+static void show_name(FILE *file,\n+\t\t      const char *prefix, const char *name, int len,\n \t\t      const char *reset, const char *set)\n {\n-\tprintf(\" %s%s%-*s%s |\", set, prefix, len, name, reset);\n+\tfprintf(file, \" %s%s%-*s%s |\", set, prefix, len, name, reset);\n }\n \n-static void show_graph(char ch, int cnt, const char *set, const char *reset)\n+static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)\n {\n \tif (cnt <= 0)\n \t\treturn;\n-\tprintf(\"%s\", set);\n+\tfprintf(file, \"%s\", set);\n \twhile (cnt--)\n-\t\tputchar(ch);\n-\tprintf(\"%s\", reset);\n+\t\tputc(ch, file);\n+\tfprintf(file, \"%s\", reset);\n }\n \n static void fill_print_name(struct diffstat_file *file)\n@@ -877,18 +887,18 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)\n \t\t}\n \n \t\tif (data->files[i]->is_binary) {\n-\t\t\tshow_name(prefix, name, len, reset, set);\n-\t\t\tprintf(\"  Bin \");\n-\t\t\tprintf(\"%s%d%s\", del_c, deleted, reset);\n-\t\t\tprintf(\" -> \");\n-\t\t\tprintf(\"%s%d%s\", add_c, added, reset);\n-\t\t\tprintf(\" bytes\");\n-\t\t\tprintf(\"\\n\");\n+\t\t\tshow_name(options->file, prefix, name, len, reset, set);\n+\t\t\tfprintf(options->file, \"  Bin \");\n+\t\t\tfprintf(options->file, \"%s%d%s\", del_c, deleted, reset);\n+\t\t\tfprintf(options->file, \" -> \");\n+\t\t\tfprintf(options->file, \"%s%d%s\", add_c, added, reset);\n+\t\t\tfprintf(options->file, \" bytes\");\n+\t\t\tfprintf(options->file, \"\\n\");\n \t\t\tcontinue;\n \t\t}\n \t\telse if (data->files[i]->is_unmerged) {\n-\t\t\tshow_name(prefix, name, len, reset, set);\n-\t\t\tprintf(\"  Unmerged\\n\");\n+\t\t\tshow_name(options->file, prefix, name, len, reset, set);\n+\t\t\tfprintf(options->file, \"  Unmerged\\n\");\n \t\t\tcontinue;\n \t\t}\n \t\telse if (!data->files[i]->is_renamed &&\n@@ -911,17 +921,18 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)\n \t\t\tdel = scale_linear(del, width, max_change);\n \t\t\ttotal = add + del;\n \t\t}\n-\t\tshow_name(prefix, name, len, reset, set);\n-\t\tprintf(\"%5d \", added + deleted);\n-\t\tshow_graph('+', add, add_c, reset);\n-\t\tshow_graph('-', del, del_c, reset);\n-\t\tputchar('\\n');\n-\t}\n-\tprintf(\"%s %d files changed, %d insertions(+), %d deletions(-)%s\\n\",\n+\t\tshow_name(options->file, prefix, name, len, reset, set);\n+\t\tfprintf(options->file, \"%5d \", added + deleted);\n+\t\tshow_graph(options->file, '+', add, add_c, reset);\n+\t\tshow_graph(options->file, '-', del, del_c, reset);\n+\t\tfprintf(options->file, \"\\n\");\n+\t}\n+\tfprintf(options->file,\n+\t       \"%s %d files changed, %d insertions(+), %d deletions(-)%s\\n\",\n \t       set, total_files, adds, dels, reset);\n }\n \n-static void show_shortstats(struct diffstat_t* data)\n+static void show_shortstats(struct diffstat_t* data, struct diff_options *options)\n {\n \tint i, adds = 0, dels = 0, total_files = data->nr;\n \n@@ -942,7 +953,7 @@ static void show_shortstats(struct diffstat_t* data)\n \t\t\t}\n \t\t}\n \t}\n-\tprintf(\" %d files changed, %d insertions(+), %d deletions(-)\\n\",\n+\tfprintf(options->file, \" %d files changed, %d insertions(+), %d deletions(-)\\n\",\n \t       total_files, adds, dels);\n }\n \n@@ -957,24 +968,25 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)\n \t\tstruct diffstat_file *file = data->files[i];\n \n \t\tif (file->is_binary)\n-\t\t\tprintf(\"-\\t-\\t\");\n+\t\t\tfprintf(options->file, \"-\\t-\\t\");\n \t\telse\n-\t\t\tprintf(\"%d\\t%d\\t\", file->added, file->deleted);\n+\t\t\tfprintf(options->file,\n+\t\t\t\t\"%d\\t%d\\t\", file->added, file->deleted);\n \t\tif (options->line_termination) {\n \t\t\tfill_print_name(file);\n \t\t\tif (!file->is_renamed)\n-\t\t\t\twrite_name_quoted(file->name, stdout,\n+\t\t\t\twrite_name_quoted(file->name, options->file,\n \t\t\t\t\t\t  options->line_termination);\n \t\t\telse {\n-\t\t\t\tfputs(file->print_name, stdout);\n-\t\t\t\tputchar(options->line_termination);\n+\t\t\t\tfputs(file->print_name, options->file);\n+\t\t\t\tputc(options->line_termination, options->file);\n \t\t\t}\n \t\t} else {\n \t\t\tif (file->is_renamed) {\n-\t\t\t\tputchar('\\0');\n-\t\t\t\twrite_name_quoted(file->from_name, stdout, '\\0');\n+\t\t\t\tputc('\\0', options->file);\n+\t\t\t\twrite_name_quoted(file->from_name, options->file, '\\0');\n \t\t\t}\n-\t\t\twrite_name_quoted(file->name, stdout, '\\0');\n+\t\t\twrite_name_quoted(file->name, options->file, '\\0');\n \t\t}\n \t}\n }\n@@ -984,7 +996,7 @@ struct diffstat_dir {\n \tint nr, percent, cumulative;\n };\n \n-static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, const char *base, int baselen)\n+static long gather_dirstat(FILE *file, struct diffstat_dir *dir, unsigned long changed, const char *base, int baselen)\n {\n \tunsigned long this_dir = 0;\n \tunsigned int sources = 0;\n@@ -1002,7 +1014,7 @@ static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, cons\n \t\tslash = strchr(f->name + baselen, '/');\n \t\tif (slash) {\n \t\t\tint newbaselen = slash + 1 - f->name;\n-\t\t\tthis = gather_dirstat(dir, changed, f->name, newbaselen);\n+\t\t\tthis = gather_dirstat(file, dir, changed, f->name, newbaselen);\n \t\t\tsources++;\n \t\t} else {\n \t\t\tif (f->is_unmerged || f->is_binary)\n@@ -1027,7 +1039,7 @@ static long gather_dirstat(struct diffstat_dir *dir, unsigned long changed, cons\n \t\tif (permille) {\n \t\t\tint percent = permille / 10;\n \t\t\tif (percent >= dir->percent) {\n-\t\t\t\tprintf(\"%4d.%01d%% %.*s\\n\", percent, permille % 10, baselen, base);\n+\t\t\t\tfprintf(file, \"%4d.%01d%% %.*s\\n\", percent, permille % 10, baselen, base);\n \t\t\t\tif (!dir->cumulative)\n \t\t\t\t\treturn 0;\n \t\t\t}\n@@ -1060,7 +1072,7 @@ static void show_dirstat(struct diffstat_t *data, struct diff_options *options)\n \tdir.nr = data->nr;\n \tdir.percent = options->dirstat_percent;\n \tdir.cumulative = options->output_format & DIFF_FORMAT_CUMULATIVE;\n-\tgather_dirstat(&dir, changed, \"\", 0);\n+\tgather_dirstat(options->file, &dir, changed, \"\", 0);\n }\n \n static void free_diffstat_info(struct diffstat_t *diffstat)\n@@ -1083,6 +1095,7 @@ struct checkdiff_t {\n \tint lineno, color_diff;\n \tunsigned ws_rule;\n \tunsigned status;\n+\tFILE *file;\n };\n \n static void checkdiff_consume(void *priv, char *line, unsigned long len)\n@@ -1100,11 +1113,11 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)\n \t\tif (!data->status)\n \t\t\treturn;\n \t\terr = whitespace_error_string(data->status);\n-\t\tprintf(\"%s:%d: %s.\\n\", data->filename, data->lineno, err);\n+\t\tfprintf(data->file, \"%s:%d: %s.\\n\", data->filename, data->lineno, err);\n \t\tfree(err);\n-\t\temit_line(set, reset, line, 1);\n+\t\temit_line(data->file, set, reset, line, 1);\n \t\t(void)check_and_emit_line(line + 1, len - 1, data->ws_rule,\n-\t\t    stdout, set, reset, ws);\n+\t\t    data->file, set, reset, ws);\n \t} else if (line[0] == ' ')\n \t\tdata->lineno++;\n \telse if (line[0] == '@') {\n@@ -1140,7 +1153,7 @@ static unsigned char *deflate_it(char *data,\n \treturn deflated;\n }\n \n-static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)\n+static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)\n {\n \tvoid *cp;\n \tvoid *delta;\n@@ -1169,13 +1182,13 @@ static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)\n \t}\n \n \tif (delta && delta_size < deflate_size) {\n-\t\tprintf(\"delta %lu\\n\", orig_size);\n+\t\tfprintf(file, \"delta %lu\\n\", orig_size);\n \t\tfree(deflated);\n \t\tdata = delta;\n \t\tdata_size = delta_size;\n \t}\n \telse {\n-\t\tprintf(\"literal %lu\\n\", two->size);\n+\t\tfprintf(file, \"literal %lu\\n\", two->size);\n \t\tfree(delta);\n \t\tdata = deflated;\n \t\tdata_size = deflate_size;\n@@ -1193,17 +1206,18 @@ static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)\n \t\t\tline[0] = bytes - 26 + 'a' - 1;\n \t\tencode_85(line + 1, cp, bytes);\n \t\tcp = (char *) cp + bytes;\n-\t\tputs(line);\n+\t\tfputs(line, file);\n+\t\tfputc('\\n', file);\n \t}\n-\tprintf(\"\\n\");\n+\tfprintf(file, \"\\n\");\n \tfree(data);\n }\n \n-static void emit_binary_diff(mmfile_t *one, mmfile_t *two)\n+static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)\n {\n-\tprintf(\"GIT binary patch\\n\");\n-\temit_binary_diff_body(one, two);\n-\temit_binary_diff_body(two, one);\n+\tfprintf(file, \"GIT binary patch\\n\");\n+\temit_binary_diff_body(file, one, two);\n+\temit_binary_diff_body(file, two, one);\n }\n \n static void setup_diff_attr_check(struct git_attr_check *check)\n@@ -1334,25 +1348,25 @@ static void builtin_diff(const char *name_a,\n \tb_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));\n \tlbl[0] = DIFF_FILE_VALID(one) ? a_one : \"/dev/null\";\n \tlbl[1] = DIFF_FILE_VALID(two) ? b_two : \"/dev/null\";\n-\tprintf(\"%sdiff --git %s %s%s\\n\", set, a_one, b_two, reset);\n+\tfprintf(o->file, \"%sdiff --git %s %s%s\\n\", set, a_one, b_two, reset);\n \tif (lbl[0][0] == '/') {\n \t\t/* /dev/null */\n-\t\tprintf(\"%snew file mode %06o%s\\n\", set, two->mode, reset);\n+\t\tfprintf(o->file, \"%snew file mode %06o%s\\n\", set, two->mode, reset);\n \t\tif (xfrm_msg && xfrm_msg[0])\n-\t\t\tprintf(\"%s%s%s\\n\", set, xfrm_msg, reset);\n+\t\t\tfprintf(o->file, \"%s%s%s\\n\", set, xfrm_msg, reset);\n \t}\n \telse if (lbl[1][0] == '/') {\n-\t\tprintf(\"%sdeleted file mode %06o%s\\n\", set, one->mode, reset);\n+\t\tfprintf(o->file, \"%sdeleted file mode %06o%s\\n\", set, one->mode, reset);\n \t\tif (xfrm_msg && xfrm_msg[0])\n-\t\t\tprintf(\"%s%s%s\\n\", set, xfrm_msg, reset);\n+\t\t\tfprintf(o->file, \"%s%s%s\\n\", set, xfrm_msg, reset);\n \t}\n \telse {\n \t\tif (one->mode != two->mode) {\n-\t\t\tprintf(\"%sold mode %06o%s\\n\", set, one->mode, reset);\n-\t\t\tprintf(\"%snew mode %06o%s\\n\", set, two->mode, reset);\n+\t\t\tfprintf(o->file, \"%sold mode %06o%s\\n\", set, one->mode, reset);\n+\t\t\tfprintf(o->file, \"%snew mode %06o%s\\n\", set, two->mode, reset);\n \t\t}\n \t\tif (xfrm_msg && xfrm_msg[0])\n-\t\t\tprintf(\"%s%s%s\\n\", set, xfrm_msg, reset);\n+\t\t\tfprintf(o->file, \"%s%s%s\\n\", set, xfrm_msg, reset);\n \t\t/*\n \t\t * we do not run diff between different kind\n \t\t * of objects.\n@@ -1376,10 +1390,10 @@ static void builtin_diff(const char *name_a,\n \t\t    !memcmp(mf1.ptr, mf2.ptr, mf1.size))\n \t\t\tgoto free_ab_and_return;\n \t\tif (DIFF_OPT_TST(o, BINARY))\n-\t\t\temit_binary_diff(&mf1, &mf2);\n+\t\t\temit_binary_diff(o->file, &mf1, &mf2);\n \t\telse\n-\t\t\tprintf(\"Binary files %s and %s differ\\n\",\n-\t\t\t       lbl[0], lbl[1]);\n+\t\t\tfprintf(o->file, \"Binary files %s and %s differ\\n\",\n+\t\t\t\tlbl[0], lbl[1]);\n \t\to->found_changes = 1;\n \t}\n \telse {\n@@ -1401,6 +1415,7 @@ static void builtin_diff(const char *name_a,\n \t\tecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);\n \t\tecbdata.found_changesp = &o->found_changes;\n \t\tecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);\n+\t\tecbdata.file = o->file;\n \t\txpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;\n \t\txecfg.ctxlen = o->context;\n \t\txecfg.flags = XDL_EMIT_FUNCNAMES;\n@@ -1415,9 +1430,11 @@ static void builtin_diff(const char *name_a,\n \t\tecb.outf = xdiff_outf;\n \t\tecb.priv = &ecbdata;\n \t\tecbdata.xm.consume = fn_out_consume;\n-\t\tif (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))\n+\t\tif (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {\n \t\t\tecbdata.diff_words =\n \t\t\t\txcalloc(1, sizeof(struct diff_words_data));\n+\t\t\tecbdata.diff_words->file = o->file;\n+\t\t}\n \t\txdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);\n \t\tif (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))\n \t\t\tfree_diff_words_data(&ecbdata);\n@@ -1496,6 +1513,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,\n \tdata.lineno = 0;\n \tdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);\n \tdata.ws_rule = whitespace_rule(attr_path);\n+\tdata.file = o->file;\n \n \tif (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)\n \t\tdie(\"unable to read files to diff\");\n@@ -1966,7 +1984,7 @@ static void run_diff_cmd(const char *pgm,\n \t\tbuiltin_diff(name, other ? other : name,\n \t\t\t     one, two, xfrm_msg, o, complete_rewrite);\n \telse\n-\t\tprintf(\"* Unmerged path %s\\n\", name);\n+\t\tfprintf(o->file, \"* Unmerged path %s\\n\", name);\n }\n \n static void diff_fill_sha1_info(struct diff_filespec *one)\n@@ -2157,6 +2175,9 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n void diff_setup(struct diff_options *options)\n {\n \tmemset(options, 0, sizeof(*options));\n+\n+\toptions->file = stdout;\n+\n \toptions->line_termination = '\\n';\n \toptions->break_opt = -1;\n \toptions->rename_limit = -1;\n@@ -2470,7 +2491,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)\n \t\toptions->b_prefix = arg + 13;\n \telse if (!strcmp(arg, \"--no-prefix\"))\n \t\toptions->a_prefix = options->b_prefix = \"\";\n-\telse\n+\telse if (!prefixcmp(arg, \"--output=\")) {\n+\t\toptions->file = fopen(arg + strlen(\"--output=\"), \"w\");\n+\t\toptions->close_file = 1;\n+\t} else\n \t\treturn 0;\n \treturn 1;\n }\n@@ -2599,15 +2623,15 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)\n \tint inter_name_termination = line_termination ? '\\t' : '\\0';\n \n \tif (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {\n-\t\tprintf(\":%06o %06o %s \", p->one->mode, p->two->mode,\n-\t\t       diff_unique_abbrev(p->one->sha1, opt->abbrev));\n-\t\tprintf(\"%s \", diff_unique_abbrev(p->two->sha1, opt->abbrev));\n+\t\tfprintf(opt->file, \":%06o %06o %s \", p->one->mode, p->two->mode,\n+\t\t\tdiff_unique_abbrev(p->one->sha1, opt->abbrev));\n+\t\tfprintf(opt->file, \"%s \", diff_unique_abbrev(p->two->sha1, opt->abbrev));\n \t}\n \tif (p->score) {\n-\t\tprintf(\"%c%03d%c\", p->status, similarity_index(p),\n-\t\t\t   inter_name_termination);\n+\t\tfprintf(opt->file, \"%c%03d%c\", p->status, similarity_index(p),\n+\t\t\tinter_name_termination);\n \t} else {\n-\t\tprintf(\"%c%c\", p->status, inter_name_termination);\n+\t\tfprintf(opt->file, \"%c%c\", p->status, inter_name_termination);\n \t}\n \n \tif (p->status == DIFF_STATUS_COPIED ||\n@@ -2616,14 +2640,14 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)\n \t\tname_a = p->one->path;\n \t\tname_b = p->two->path;\n \t\tstrip_prefix(opt->prefix_length, &name_a, &name_b);\n-\t\twrite_name_quoted(name_a, stdout, inter_name_termination);\n-\t\twrite_name_quoted(name_b, stdout, line_termination);\n+\t\twrite_name_quoted(name_a, opt->file, inter_name_termination);\n+\t\twrite_name_quoted(name_b, opt->file, line_termination);\n \t} else {\n \t\tconst char *name_a, *name_b;\n \t\tname_a = p->one->mode ? p->one->path : p->two->path;\n \t\tname_b = NULL;\n \t\tstrip_prefix(opt->prefix_length, &name_a, &name_b);\n-\t\twrite_name_quoted(name_a, stdout, line_termination);\n+\t\twrite_name_quoted(name_a, opt->file, line_termination);\n \t}\n }\n \n@@ -2825,62 +2849,62 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)\n \t\tname_a = p->two->path;\n \t\tname_b = NULL;\n \t\tstrip_prefix(opt->prefix_length, &name_a, &name_b);\n-\t\twrite_name_quoted(name_a, stdout, opt->line_termination);\n+\t\twrite_name_quoted(name_a, opt->file, opt->line_termination);\n \t}\n }\n \n-static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)\n+static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)\n {\n \tif (fs->mode)\n-\t\tprintf(\" %s mode %06o \", newdelete, fs->mode);\n+\t\tfprintf(file, \" %s mode %06o \", newdelete, fs->mode);\n \telse\n-\t\tprintf(\" %s \", newdelete);\n-\twrite_name_quoted(fs->path, stdout, '\\n');\n+\t\tfprintf(file, \" %s \", newdelete);\n+\twrite_name_quoted(fs->path, file, '\\n');\n }\n \n \n-static void show_mode_change(struct diff_filepair *p, int show_name)\n+static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)\n {\n \tif (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {\n-\t\tprintf(\" mode change %06o => %06o%c\", p->one->mode, p->two->mode,\n+\t\tfprintf(file, \" mode change %06o => %06o%c\", p->one->mode, p->two->mode,\n \t\t\tshow_name ? ' ' : '\\n');\n \t\tif (show_name) {\n-\t\t\twrite_name_quoted(p->two->path, stdout, '\\n');\n+\t\t\twrite_name_quoted(p->two->path, file, '\\n');\n \t\t}\n \t}\n }\n \n-static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)\n+static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)\n {\n \tchar *names = pprint_rename(p->one->path, p->two->path);\n \n-\tprintf(\" %s %s (%d%%)\\n\", renamecopy, names, similarity_index(p));\n+\tfprintf(file, \" %s %s (%d%%)\\n\", renamecopy, names, similarity_index(p));\n \tfree(names);\n-\tshow_mode_change(p, 0);\n+\tshow_mode_change(file, p, 0);\n }\n \n-static void diff_summary(struct diff_filepair *p)\n+static void diff_summary(FILE *file, struct diff_filepair *p)\n {\n \tswitch(p->status) {\n \tcase DIFF_STATUS_DELETED:\n-\t\tshow_file_mode_name(\"delete\", p->one);\n+\t\tshow_file_mode_name(file, \"delete\", p->one);\n \t\tbreak;\n \tcase DIFF_STATUS_ADDED:\n-\t\tshow_file_mode_name(\"create\", p->two);\n+\t\tshow_file_mode_name(file, \"create\", p->two);\n \t\tbreak;\n \tcase DIFF_STATUS_COPIED:\n-\t\tshow_rename_copy(\"copy\", p);\n+\t\tshow_rename_copy(file, \"copy\", p);\n \t\tbreak;\n \tcase DIFF_STATUS_RENAMED:\n-\t\tshow_rename_copy(\"rename\", p);\n+\t\tshow_rename_copy(file, \"rename\", p);\n \t\tbreak;\n \tdefault:\n \t\tif (p->score) {\n-\t\t\tfputs(\" rewrite \", stdout);\n-\t\t\twrite_name_quoted(p->two->path, stdout, ' ');\n-\t\t\tprintf(\"(%d%%)\\n\", similarity_index(p));\n+\t\t\tfputs(\" rewrite \", file);\n+\t\t\twrite_name_quoted(p->two->path, file, ' ');\n+\t\t\tfprintf(file, \"(%d%%)\\n\", similarity_index(p));\n \t\t}\n-\t\tshow_mode_change(p, !p->score);\n+\t\tshow_mode_change(file, p, !p->score);\n \t\tbreak;\n \t}\n }\n@@ -3088,14 +3112,14 @@ void diff_flush(struct diff_options *options)\n \t\tif (output_format & DIFF_FORMAT_DIFFSTAT)\n \t\t\tshow_stats(&diffstat, options);\n \t\tif (output_format & DIFF_FORMAT_SHORTSTAT)\n-\t\t\tshow_shortstats(&diffstat);\n+\t\t\tshow_shortstats(&diffstat, options);\n \t\tfree_diffstat_info(&diffstat);\n \t\tseparator++;\n \t}\n \n \tif (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {\n \t\tfor (i = 0; i < q->nr; i++)\n-\t\t\tdiff_summary(q->queue[i]);\n+\t\t\tdiff_summary(options->file, q->queue[i]);\n \t\tseparator++;\n \t}\n \n@@ -3103,9 +3127,9 @@ void diff_flush(struct diff_options *options)\n \t\tif (separator) {\n \t\t\tif (options->stat_sep) {\n \t\t\t\t/* attach patch instead of inline */\n-\t\t\t\tfputs(options->stat_sep, stdout);\n+\t\t\t\tfputs(options->stat_sep, options->file);\n \t\t\t} else {\n-\t\t\t\tputchar(options->line_termination);\n+\t\t\t\tputc(options->line_termination, options->file);\n \t\t\t}\n \t\t}\n \n@@ -3125,6 +3149,8 @@ free_queue:\n \tfree(q->queue);\n \tq->queue = NULL;\n \tq->nr = q->alloc = 0;\n+\tif (options->close_file)\n+\t\tfclose(options->file);\n }\n \n static void diffcore_apply_filter(const char *filter)\ndiff --git a/diff.h b/diff.h\nindex 9a652e7..f2c7739 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -98,6 +98,9 @@ struct diff_options {\n \t/* this is set by diffcore for DIFF_FORMAT_PATCH */\n \tint found_changes;\n \n+\tFILE *file;\n+\tint close_file;\n+\n \tint nr_paths;\n \tconst char **paths;\n \tint *pathlens;\n-- \n1.5.4.3.610.gea6cd\n"},{"id":"71554","messageId":"7v3aqzm7ac.fsf@gitster.siamese.dyndns.org","threadId":"12612","inReplyTo":"alpine.LNX.1.00.0803092235150.19665@iabervon.org","subject":"Re: [PATCH] Write diff output to a file in struct diff_options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-10T06:18:03Z","receivedAt":"2008-03-10T06:18:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> ---\n> I remember there being some expectation that this would be difficult, but \n> it was a pretty straightforward conversion, so I'm wondering if I've \n> missed something. In any case, this is missing tests and documentation for \n> --output=, but I really want it to use directly for rebase-interactive, \n> and I only did the command line option so I could do informal testing.\n\nLooks Ok at lesat to my cursory review.\n\nSome people however seemed to have had plans to take this even further and\noptionally emit into strbuf; I suspect that was the difficulty you were\nremembering about?\n"},{"id":"71572","messageId":"alpine.LSU.1.00.0803101204250.3975@racer.site","threadId":"12612","inReplyTo":"alpine.LNX.1.00.0803092235150.19665@iabervon.org","subject":"Re: [PATCH] Write diff output to a file in struct diff_options","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-03-10T11:06:06Z","receivedAt":"2008-03-10T11:06:06Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 9 Mar 2008, Daniel Barkalow wrote:\n\n> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> ---\n> I remember there being some expectation that this would be difficult, but \n> it was a pretty straightforward conversion, so I'm wondering if I've \n> missed something. In any case, this is missing tests and documentation for \n> --output=, but I really want it to use directly for rebase-interactive, \n> and I only did the command line option so I could do informal testing.\n\nI think I posted this link in that discussion already:\n\nhttp://repo.or.cz/w/git/dscho.git?a=commitdiff;h=899fac730a75a0a8e6dd426e61beea113e75ac4b\n\nBut without user, this commit is not useful.  Therefore, here another \nlink:\n\nhttp://repo.or.cz/w/git/dscho.git?a=commitdiff;h=b5a44da9ecd15391e06c85fe8a2bb04b86f4f8d1\n\nEventually, Hannes solved the problem differently, so I did not bother to \nsubmit this officially, since it is not needed.\n\nCiao,\nDscho\n"},{"id":"71577","messageId":"fr37ec$9k4$1@ger.gmane.org","threadId":"12612","inReplyTo":"alpine.LNX.1.00.0803092235150.19665@iabervon.org","subject":"Re: [PATCH] Write diff output to a file in struct diff_options","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2008-03-10T11:52:44Z","receivedAt":"2008-03-10T11:52:44Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Daniel Barkalow wrote:\n\n> I remember there being some expectation that this would be difficult, but \n> it was a pretty straightforward conversion, so I'm wondering if I've \n> missed something. In any case, this is missing tests and documentation for \n> --output=, but I really want it to use directly for rebase-interactive, \n> and I only did the command line option so I could do informal testing.\n> \n>  diff.c |  306 +++++++++++++++++++++++++++++++++++-----------------------------\n>  diff.h |    3 +\n>  2 files changed, 169 insertions(+), 140 deletions(-)\n\nErrr... where is the documentation?\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"71579","messageId":"alpine.LSU.1.00.0803101329200.3975@racer.site","threadId":"12612","inReplyTo":"fr37ec$9k4$1@ger.gmane.org","subject":"Re: [PATCH] Write diff output to a file in struct diff_options","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-03-10T12:29:39Z","receivedAt":"2008-03-10T12:29:39Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 10 Mar 2008, Jakub Narebski wrote:\n\n> Daniel Barkalow wrote:\n> \n> > I remember there being some expectation that this would be difficult, but \n> > it was a pretty straightforward conversion, so I'm wondering if I've \n> > missed something. In any case, this is missing tests and documentation for \n> > --output=, but I really want it to use directly for rebase-interactive, \n> > and I only did the command line option so I could do informal testing.\n> > \n> >  diff.c |  306 +++++++++++++++++++++++++++++++++++-----------------------------\n> >  diff.h |    3 +\n> >  2 files changed, 169 insertions(+), 140 deletions(-)\n> \n> Errr... where is the documentation?\n\nThere is no user for that feature yet, so where exactly should it be \ndocumented?\n\nCiao,\nDscho\n"},{"id":"71604","messageId":"1205171906.31046.27.camel@gaara.boston.redhat.com","threadId":"12612","inReplyTo":"alpine.LNX.1.00.0803092235150.19665@iabervon.org","subject":"Re: [PATCH] Write diff output to a file in struct diff_options","fromName":"Kristian Høgsberg","fromEmail":"krh@redhat.com","sentAt":"2008-03-10T17:58:26Z","receivedAt":"2008-03-10T17:58:26Z","isPatch":true,"sender":{"key":"krh@redhat.com","avatar":"https://gravatar.com/avatar/763dee6f9594ac474f725b137a39565792928e583ddf59b32befc2907409027e?d=mp&s=160"},"body":"On Sun, 2008-03-09 at 22:43 -0400, Daniel Barkalow wrote:\n> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> ---\n> I remember there being some expectation that this would be difficult, but \n> it was a pretty straightforward conversion, so I'm wondering if I've \n> missed something. In any case, this is missing tests and documentation for \n> --output=, but I really want it to use directly for rebase-interactive, \n> and I only did the command line option so I could do informal testing.\n\nI ended up just dup()-ing files around in wt-status.c, but it wasn't\nthat converting looked difficult, just that I was trying to finish the\nbuiltin-commit work and didn't want to undertake a new rewrite :)  With\nyour patch, we can now avoid the dup-dance.\n\ncheers,\nKristian\n\n\ndiff --git a/wt-status.c b/wt-status.c\nindex 32d780a..181821b 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -314,25 +314,13 @@ static void wt_status_print_verbose(struct wt_status *s)\n \tstruct rev_info rev;\n \tint saved_stdout;\n \n-\tfflush(s->fp);\n-\n-\t/* Sigh, the entire diff machinery is hardcoded to output to\n-\t * stdout.  Do the dup-dance...*/\n-\tsaved_stdout = dup(STDOUT_FILENO);\n-\tif (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)\n-\t\tdie(\"couldn't redirect stdout\\n\");\n-\n \tinit_revisions(&rev, NULL);\n \tsetup_revisions(0, NULL, &rev, s->reference);\n \trev.diffopt.output_format |= DIFF_FORMAT_PATCH;\n \trev.diffopt.detect_rename = 1;\n+\trev.diffopt.file = s->fp;\n+\trev.diffopt.close_file = 0;\n \trun_diff_index(&rev, 1);\n-\n-\tfflush(stdout);\n-\n-\tif (dup2(saved_stdout, STDOUT_FILENO) < 0)\n-\t\tdie(\"couldn't restore stdout\\n\");\n-\tclose(saved_stdout);\n }\n \n void wt_status_print(struct wt_status *s)\n"}]}