shortlog: support outputting to streams other than stdout

This will be needed to avoid freopen() in `git format-patch`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 22, 2016 at 17:01 UTC 0a7b357737e3352ce1b0385313c1efc1b2564dbb
2 files changed +9 -5
builtin/shortlog.c
+8 -5
@@ -229,6 +229,7 @@ void shortlog_init(struct shortlog *log)
229 log->wrap = DEFAULT_WRAPLEN;
230 log->in1 = DEFAULT_INDENT1;
231 log->in2 = DEFAULT_INDENT2;
232 + log->file = stdout;
233 }
234
235 int cmd_shortlog(int argc, const char **argv, const char *prefix)
@@ -310,22 +311,24 @@ void shortlog_output(struct shortlog *log)
311 for (i = 0; i < log->list.nr; i++) {
312 const struct string_list_item *item = &log->list.items[i];
313 if (log->summary) {
313 - printf("%6d\t%s\n", (int)UTIL_TO_INT(item), item->string);
314 + fprintf(log->file, "%6d\t%s\n",
315 + (int)UTIL_TO_INT(item), item->string);
316 } else {
317 struct string_list *onelines = item->util;
316 - printf("%s (%d):\n", item->string, onelines->nr);
318 + fprintf(log->file, "%s (%d):\n",
319 + item->string, onelines->nr);
320 for (j = onelines->nr - 1; j >= 0; j--) {
321 const char *msg = onelines->items[j].string;
322
323 if (log->wrap_lines) {
324 strbuf_reset(&sb);
325 add_wrapped_shortlog_msg(&sb, msg, log);
323 - fwrite(sb.buf, sb.len, 1, stdout);
326 + fwrite(sb.buf, sb.len, 1, log->file);
327 }
328 else
326 - printf(" %s\n", msg);
329 + fprintf(log->file, " %s\n", msg);
330 }
328 - putchar('\n');
331 + putc('\n', log->file);
332 onelines->strdup_strings = 1;
333 string_list_clear(onelines, 0);
334 free(onelines);
shortlog.h
+1
@@ -17,6 +17,7 @@ struct shortlog {
17 char *common_repo_prefix;
18 int email;
19 struct string_list mailmap;
20 + FILE *file;
21 };
22
23 void shortlog_init(struct shortlog *log);