log: prepare log/log-tree to reuse the diffopt.close_file attribute

We are about to teach the log-tree machinery to reuse the diffopt.file field to output to a file stream other than stdout, in line with the diff machinery already writing to diffopt.file. However, we might want to write something after the diff in log_tree_commit() (e.g. with the --show-linear-break option), therefore we must not let the diff machinery close the file (as per diffopt.close_file. This means that log_tree_commit() itself must override the diffopt.close_file flag and close the file, and if log_tree_commit() is called in a loop, the caller is responsible to do the same. Note: format-patch has an `--output-directory` option. Due to the fact that format-patch's options are parsed first, and that the parse-options machinery accepts uniquely abbreviated options, the diff options `--output` (and `-o`) are shadowed. Therefore close_file is not set to 1 so that cmd_format_patch() does *not* need to handle the close_file flag differently, even if it calls log_tree_commit() in a loop. 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 6ea57703f660afd00159b67bf8749d90881df3b4
2 files changed +16 -4
builtin/log.c
+12 -3
@@ -243,9 +243,10 @@ static struct itimerval early_output_timer;
243
244 static void log_show_early(struct rev_info *revs, struct commit_list *list)
245 {
246 - int i = revs->early_output;
246 + int i = revs->early_output, close_file = revs->diffopt.close_file;
247 int show_header = 1;
248
249 + revs->diffopt.close_file = 0;
250 sort_in_topological_order(&list, revs->sort_order);
251 while (list && i) {
252 struct commit *commit = list->item;
@@ -262,14 +263,19 @@ static void log_show_early(struct rev_info *revs, struct commit_list *list)
263 case commit_ignore:
264 break;
265 case commit_error:
266 + if (close_file)
267 + fclose(revs->diffopt.file);
268 return;
269 }
270 list = list->next;
271 }
272
273 /* Did we already get enough commits for the early output? */
271 - if (!i)
274 + if (!i) {
275 + if (close_file)
276 + fclose(revs->diffopt.file);
277 return;
278 + }
279
280 /*
281 * ..if no, then repeat it twice a second until we
@@ -331,7 +337,7 @@ static int cmd_log_walk(struct rev_info *rev)
337 {
338 struct commit *commit;
339 int saved_nrl = 0;
334 - int saved_dcctc = 0;
340 + int saved_dcctc = 0, close_file = rev->diffopt.close_file;
341
342 if (rev->early_output)
343 setup_early_output(rev);
@@ -347,6 +353,7 @@ static int cmd_log_walk(struct rev_info *rev)
353 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
354 * retain that state information if replacing rev->diffopt in this loop
355 */
356 + rev->diffopt.close_file = 0;
357 while ((commit = get_revision(rev)) != NULL) {
358 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
359 /*
@@ -367,6 +374,8 @@ static int cmd_log_walk(struct rev_info *rev)
374 }
375 rev->diffopt.degraded_cc_to_c = saved_dcctc;
376 rev->diffopt.needed_rename_limit = saved_nrl;
377 + if (close_file)
378 + fclose(rev->diffopt.file);
379
380 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
381 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
log-tree.c
+4 -1
@@ -862,11 +862,12 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
862 int log_tree_commit(struct rev_info *opt, struct commit *commit)
863 {
864 struct log_info log;
865 - int shown;
865 + int shown, close_file = opt->diffopt.close_file;
866
867 log.commit = commit;
868 log.parent = NULL;
869 opt->loginfo = &log;
870 + opt->diffopt.close_file = 0;
871
872 if (opt->line_level_traverse)
873 return line_log_print(opt, commit);
@@ -883,5 +884,7 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
884 printf("\n%s\n", opt->break_bar);
885 opt->loginfo = NULL;
886 maybe_flush_or_die(stdout, "stdout");
887 + if (close_file)
888 + fclose(opt->diffopt.file);
889 return shown;
890 }