Skip to content

Commit 6ea5770

Browse files
dschogitster
authored andcommitted
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>
1 parent ab7797d commit 6ea5770

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

builtin/log.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ static struct itimerval early_output_timer;
243243

244244
static void log_show_early(struct rev_info *revs, struct commit_list *list)
245245
{
246-
int i = revs->early_output;
246+
int i = revs->early_output, close_file = revs->diffopt.close_file;
247247
int show_header = 1;
248248

249+
revs->diffopt.close_file = 0;
249250
sort_in_topological_order(&list, revs->sort_order);
250251
while (list && i) {
251252
struct commit *commit = list->item;
@@ -262,14 +263,19 @@ static void log_show_early(struct rev_info *revs, struct commit_list *list)
262263
case commit_ignore:
263264
break;
264265
case commit_error:
266+
if (close_file)
267+
fclose(revs->diffopt.file);
265268
return;
266269
}
267270
list = list->next;
268271
}
269272

270273
/* 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);
272277
return;
278+
}
273279

274280
/*
275281
* ..if no, then repeat it twice a second until we
@@ -331,7 +337,7 @@ static int cmd_log_walk(struct rev_info *rev)
331337
{
332338
struct commit *commit;
333339
int saved_nrl = 0;
334-
int saved_dcctc = 0;
340+
int saved_dcctc = 0, close_file = rev->diffopt.close_file;
335341

336342
if (rev->early_output)
337343
setup_early_output(rev);
@@ -347,6 +353,7 @@ static int cmd_log_walk(struct rev_info *rev)
347353
* and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
348354
* retain that state information if replacing rev->diffopt in this loop
349355
*/
356+
rev->diffopt.close_file = 0;
350357
while ((commit = get_revision(rev)) != NULL) {
351358
if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
352359
/*
@@ -367,6 +374,8 @@ static int cmd_log_walk(struct rev_info *rev)
367374
}
368375
rev->diffopt.degraded_cc_to_c = saved_dcctc;
369376
rev->diffopt.needed_rename_limit = saved_nrl;
377+
if (close_file)
378+
fclose(rev->diffopt.file);
370379

371380
if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
372381
DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {

log-tree.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,12 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
862862
int log_tree_commit(struct rev_info *opt, struct commit *commit)
863863
{
864864
struct log_info log;
865-
int shown;
865+
int shown, close_file = opt->diffopt.close_file;
866866

867867
log.commit = commit;
868868
log.parent = NULL;
869869
opt->loginfo = &log;
870+
opt->diffopt.close_file = 0;
870871

871872
if (opt->line_level_traverse)
872873
return line_log_print(opt, commit);
@@ -883,5 +884,7 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
883884
printf("\n%s\n", opt->break_bar);
884885
opt->loginfo = NULL;
885886
maybe_flush_or_die(stdout, "stdout");
887+
if (close_file)
888+
fclose(opt->diffopt.file);
886889
return shown;
887890
}

0 commit comments

Comments
 (0)