@@ -238,16 +238,17 @@ static void show_early_header(struct rev_info *rev, const char *stage, int nr)
238
238
if (rev -> commit_format != CMIT_FMT_ONELINE )
239
239
putchar (rev -> diffopt .line_termination );
240
240
}
241
- printf ( _ ("Final output: %d %s\n" ), nr , stage );
241
+ fprintf ( rev -> diffopt . file , _ ("Final output: %d %s\n" ), nr , stage );
242
242
}
243
243
244
244
static struct itimerval early_output_timer ;
245
245
246
246
static void log_show_early (struct rev_info * revs , struct commit_list * list )
247
247
{
248
- int i = revs -> early_output ;
248
+ int i = revs -> early_output , close_file = revs -> diffopt . close_file ;
249
249
int show_header = 1 ;
250
250
251
+ revs -> diffopt .close_file = 0 ;
251
252
sort_in_topological_order (& list , revs -> sort_order );
252
253
while (list && i ) {
253
254
struct commit * commit = list -> item ;
@@ -264,14 +265,19 @@ static void log_show_early(struct rev_info *revs, struct commit_list *list)
264
265
case commit_ignore :
265
266
break ;
266
267
case commit_error :
268
+ if (close_file )
269
+ fclose (revs -> diffopt .file );
267
270
return ;
268
271
}
269
272
list = list -> next ;
270
273
}
271
274
272
275
/* Did we already get enough commits for the early output? */
273
- if (!i )
276
+ if (!i ) {
277
+ if (close_file )
278
+ fclose (revs -> diffopt .file );
274
279
return ;
280
+ }
275
281
276
282
/*
277
283
* ..if no, then repeat it twice a second until we
@@ -333,7 +339,7 @@ static int cmd_log_walk(struct rev_info *rev)
333
339
{
334
340
struct commit * commit ;
335
341
int saved_nrl = 0 ;
336
- int saved_dcctc = 0 ;
342
+ int saved_dcctc = 0 , close_file = rev -> diffopt . close_file ;
337
343
338
344
if (rev -> early_output )
339
345
setup_early_output (rev );
@@ -349,6 +355,7 @@ static int cmd_log_walk(struct rev_info *rev)
349
355
* and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
350
356
* retain that state information if replacing rev->diffopt in this loop
351
357
*/
358
+ rev -> diffopt .close_file = 0 ;
352
359
while ((commit = get_revision (rev )) != NULL ) {
353
360
if (!log_tree_commit (rev , commit ) && rev -> max_count >= 0 )
354
361
/*
@@ -369,6 +376,8 @@ static int cmd_log_walk(struct rev_info *rev)
369
376
}
370
377
rev -> diffopt .degraded_cc_to_c = saved_dcctc ;
371
378
rev -> diffopt .needed_rename_limit = saved_nrl ;
379
+ if (close_file )
380
+ fclose (rev -> diffopt .file );
372
381
373
382
if (rev -> diffopt .output_format & DIFF_FORMAT_CHECKDIFF &&
374
383
DIFF_OPT_TST (& rev -> diffopt , CHECK_FAILED )) {
@@ -451,7 +460,7 @@ static void show_tagger(char *buf, int len, struct rev_info *rev)
451
460
pp .fmt = rev -> commit_format ;
452
461
pp .date_mode = rev -> date_mode ;
453
462
pp_user_info (& pp , "Tagger" , & out , buf , get_log_output_encoding ());
454
- printf ( "%s" , out .buf );
463
+ fprintf ( rev -> diffopt . file , "%s" , out .buf );
455
464
strbuf_release (& out );
456
465
}
457
466
@@ -462,7 +471,7 @@ static int show_blob_object(const unsigned char *sha1, struct rev_info *rev, con
462
471
char * buf ;
463
472
unsigned long size ;
464
473
465
- fflush (stdout );
474
+ fflush (rev -> diffopt . file );
466
475
if (!DIFF_OPT_TOUCHED (& rev -> diffopt , ALLOW_TEXTCONV ) ||
467
476
!DIFF_OPT_TST (& rev -> diffopt , ALLOW_TEXTCONV ))
468
477
return stream_blob_to_fd (1 , sha1 , NULL , 0 );
@@ -502,7 +511,7 @@ static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
502
511
}
503
512
504
513
if (offset < size )
505
- fwrite (buf + offset , size - offset , 1 , stdout );
514
+ fwrite (buf + offset , size - offset , 1 , rev -> diffopt . file );
506
515
free (buf );
507
516
return 0 ;
508
517
}
@@ -511,7 +520,8 @@ static int show_tree_object(const unsigned char *sha1,
511
520
struct strbuf * base ,
512
521
const char * pathname , unsigned mode , int stage , void * context )
513
522
{
514
- printf ("%s%s\n" , pathname , S_ISDIR (mode ) ? "/" : "" );
523
+ FILE * file = context ;
524
+ fprintf (file , "%s%s\n" , pathname , S_ISDIR (mode ) ? "/" : "" );
515
525
return 0 ;
516
526
}
517
527
@@ -571,7 +581,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
571
581
572
582
if (rev .shown_one )
573
583
putchar ('\n' );
574
- printf ( "%stag %s%s\n" ,
584
+ fprintf ( rev . diffopt . file , "%stag %s%s\n" ,
575
585
diff_get_color_opt (& rev .diffopt , DIFF_COMMIT ),
576
586
t -> tag ,
577
587
diff_get_color_opt (& rev .diffopt , DIFF_RESET ));
@@ -590,12 +600,12 @@ int cmd_show(int argc, const char **argv, const char *prefix)
590
600
case OBJ_TREE :
591
601
if (rev .shown_one )
592
602
putchar ('\n' );
593
- printf ( "%stree %s%s\n\n" ,
603
+ fprintf ( rev . diffopt . file , "%stree %s%s\n\n" ,
594
604
diff_get_color_opt (& rev .diffopt , DIFF_COMMIT ),
595
605
name ,
596
606
diff_get_color_opt (& rev .diffopt , DIFF_RESET ));
597
607
read_tree_recursive ((struct tree * )o , "" , 0 , 0 , & match_all ,
598
- show_tree_object , NULL );
608
+ show_tree_object , rev . diffopt . file );
599
609
rev .shown_one = 1 ;
600
610
break ;
601
611
case OBJ_COMMIT :
@@ -801,11 +811,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
801
811
return git_log_config (var , value , cb );
802
812
}
803
813
804
- static FILE * realstdout = NULL ;
805
814
static const char * output_directory = NULL ;
806
815
static int outdir_offset ;
807
816
808
- static int reopen_stdout (struct commit * commit , const char * subject ,
817
+ static int open_next_file (struct commit * commit , const char * subject ,
809
818
struct rev_info * rev , int quiet )
810
819
{
811
820
struct strbuf filename = STRBUF_INIT ;
@@ -827,9 +836,9 @@ static int reopen_stdout(struct commit *commit, const char *subject,
827
836
fmt_output_subject (& filename , subject , rev );
828
837
829
838
if (!quiet )
830
- fprintf ( realstdout , "%s\n" , filename .buf + outdir_offset );
839
+ printf ( "%s\n" , filename .buf + outdir_offset );
831
840
832
- if (freopen ( filename .buf , "w" , stdout ) == NULL )
841
+ if (( rev -> diffopt . file = fopen ( filename .buf , "w" ) ) == NULL )
833
842
return error (_ ("Cannot open patch file %s" ), filename .buf );
834
843
835
844
strbuf_release (& filename );
@@ -888,15 +897,15 @@ static void gen_message_id(struct rev_info *info, char *base)
888
897
info -> message_id = strbuf_detach (& buf , NULL );
889
898
}
890
899
891
- static void print_signature (void )
900
+ static void print_signature (FILE * file )
892
901
{
893
902
if (!signature || !* signature )
894
903
return ;
895
904
896
- printf ( "-- \n%s" , signature );
905
+ fprintf ( file , "-- \n%s" , signature );
897
906
if (signature [strlen (signature )- 1 ] != '\n' )
898
- putchar ('\n' );
899
- putchar ('\n' );
907
+ putc ('\n' , file );
908
+ putc ('\n' , file );
900
909
}
901
910
902
911
static void add_branch_description (struct strbuf * buf , const char * branch_name )
@@ -965,7 +974,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
965
974
committer = git_committer_info (0 );
966
975
967
976
if (!use_stdout &&
968
- reopen_stdout (NULL , rev -> numbered_files ? NULL : "cover-letter" , rev , quiet ))
977
+ open_next_file (NULL , rev -> numbered_files ? NULL : "cover-letter" , rev , quiet ))
969
978
return ;
970
979
971
980
log_write_email_headers (rev , head , & pp .subject , & pp .after_subject ,
@@ -988,7 +997,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
988
997
pp_title_line (& pp , & msg , & sb , encoding , need_8bit_cte );
989
998
pp_remainder (& pp , & msg , & sb , 0 );
990
999
add_branch_description (& sb , branch_name );
991
- printf ( "%s\n" , sb .buf );
1000
+ fprintf ( rev -> diffopt . file , "%s\n" , sb .buf );
992
1001
993
1002
strbuf_release (& sb );
994
1003
@@ -997,6 +1006,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
997
1006
log .wrap = 72 ;
998
1007
log .in1 = 2 ;
999
1008
log .in2 = 4 ;
1009
+ log .file = rev -> diffopt .file ;
1000
1010
for (i = 0 ; i < nr ; i ++ )
1001
1011
shortlog_add_commit (& log , list [i ]);
1002
1012
@@ -1019,8 +1029,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1019
1029
diffcore_std (& opts );
1020
1030
diff_flush (& opts );
1021
1031
1022
- printf ( "\n" );
1023
- print_signature ();
1032
+ fprintf ( rev -> diffopt . file , "\n" );
1033
+ print_signature (rev -> diffopt . file );
1024
1034
}
1025
1035
1026
1036
static const char * clean_message_id (const char * msg_id )
@@ -1330,7 +1340,7 @@ static void prepare_bases(struct base_tree_info *bases,
1330
1340
}
1331
1341
}
1332
1342
1333
- static void print_bases (struct base_tree_info * bases )
1343
+ static void print_bases (struct base_tree_info * bases , FILE * file )
1334
1344
{
1335
1345
int i ;
1336
1346
@@ -1339,11 +1349,11 @@ static void print_bases(struct base_tree_info *bases)
1339
1349
return ;
1340
1350
1341
1351
/* Show the base commit */
1342
- printf ( "base-commit: %s\n" , oid_to_hex (& bases -> base_commit ));
1352
+ fprintf ( file , "base-commit: %s\n" , oid_to_hex (& bases -> base_commit ));
1343
1353
1344
1354
/* Show the prerequisite patches */
1345
1355
for (i = bases -> nr_patch_id - 1 ; i >= 0 ; i -- )
1346
- printf ( "prerequisite-patch-id: %s\n" , oid_to_hex (& bases -> patch_id [i ]));
1356
+ fprintf ( file , "prerequisite-patch-id: %s\n" , oid_to_hex (& bases -> patch_id [i ]));
1347
1357
1348
1358
free (bases -> patch_id );
1349
1359
bases -> nr_patch_id = 0 ;
@@ -1575,6 +1585,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1575
1585
setup_pager ();
1576
1586
1577
1587
if (output_directory ) {
1588
+ if (rev .diffopt .use_color != GIT_COLOR_ALWAYS )
1589
+ rev .diffopt .use_color = GIT_COLOR_NEVER ;
1578
1590
if (use_stdout )
1579
1591
die (_ ("standard output, or directory, which one?" ));
1580
1592
if (mkdir (output_directory , 0777 ) < 0 && errno != EEXIST )
@@ -1632,9 +1644,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1632
1644
get_patch_ids (& rev , & ids );
1633
1645
}
1634
1646
1635
- if (!use_stdout )
1636
- realstdout = xfdopen (xdup (1 ), "w" );
1637
-
1638
1647
if (prepare_revision_walk (& rev ))
1639
1648
die (_ ("revision walk setup failed" ));
1640
1649
rev .boundary = 1 ;
@@ -1699,7 +1708,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1699
1708
gen_message_id (& rev , "cover" );
1700
1709
make_cover_letter (& rev , use_stdout ,
1701
1710
origin , nr , list , branch_name , quiet );
1702
- print_bases (& bases );
1711
+ print_bases (& bases , rev . diffopt . file );
1703
1712
total ++ ;
1704
1713
start_number -- ;
1705
1714
}
@@ -1745,7 +1754,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1745
1754
}
1746
1755
1747
1756
if (!use_stdout &&
1748
- reopen_stdout (rev .numbered_files ? NULL : commit , NULL , & rev , quiet ))
1757
+ open_next_file (rev .numbered_files ? NULL : commit , NULL , & rev , quiet ))
1749
1758
die (_ ("Failed to create output files" ));
1750
1759
shown = log_tree_commit (& rev , commit );
1751
1760
free_commit_buffer (commit );
@@ -1760,15 +1769,15 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1760
1769
rev .shown_one = 0 ;
1761
1770
if (shown ) {
1762
1771
if (rev .mime_boundary )
1763
- printf ( "\n--%s%s--\n\n\n" ,
1772
+ fprintf ( rev . diffopt . file , "\n--%s%s--\n\n\n" ,
1764
1773
mime_boundary_leader ,
1765
1774
rev .mime_boundary );
1766
1775
else
1767
- print_signature ();
1768
- print_bases (& bases );
1776
+ print_signature (rev . diffopt . file );
1777
+ print_bases (& bases , rev . diffopt . file );
1769
1778
}
1770
1779
if (!use_stdout )
1771
- fclose (stdout );
1780
+ fclose (rev . diffopt . file );
1772
1781
}
1773
1782
free (list );
1774
1783
free (branch_name );
@@ -1800,15 +1809,15 @@ static const char * const cherry_usage[] = {
1800
1809
};
1801
1810
1802
1811
static void print_commit (char sign , struct commit * commit , int verbose ,
1803
- int abbrev )
1812
+ int abbrev , FILE * file )
1804
1813
{
1805
1814
if (!verbose ) {
1806
- printf ( "%c %s\n" , sign ,
1815
+ fprintf ( file , "%c %s\n" , sign ,
1807
1816
find_unique_abbrev (commit -> object .oid .hash , abbrev ));
1808
1817
} else {
1809
1818
struct strbuf buf = STRBUF_INIT ;
1810
1819
pp_commit_easy (CMIT_FMT_ONELINE , commit , & buf );
1811
- printf ( "%c %s %s\n" , sign ,
1820
+ fprintf ( file , "%c %s %s\n" , sign ,
1812
1821
find_unique_abbrev (commit -> object .oid .hash , abbrev ),
1813
1822
buf .buf );
1814
1823
strbuf_release (& buf );
@@ -1889,7 +1898,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
1889
1898
commit = list -> item ;
1890
1899
if (has_commit_patch_id (commit , & ids ))
1891
1900
sign = '-' ;
1892
- print_commit (sign , commit , verbose , abbrev );
1901
+ print_commit (sign , commit , verbose , abbrev , revs . diffopt . file );
1893
1902
list = list -> next ;
1894
1903
}
1895
1904
0 commit comments