@@ -41,7 +41,8 @@ struct batch_options {
4141 int all_objects ;
4242 int unordered ;
4343 int transform_mode ; /* may be 'w' or 'c' for --filters or --textconv */
44- int nul_terminated ;
44+ char input_delim ;
45+ char output_delim ;
4546 const char * format ;
4647};
4748
@@ -436,11 +437,12 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
436437 }
437438}
438439
439- static void print_default_format (struct strbuf * scratch , struct expand_data * data )
440+ static void print_default_format (struct strbuf * scratch , struct expand_data * data ,
441+ struct batch_options * opt )
440442{
441- strbuf_addf (scratch , "%s %s %" PRIuMAX "\n " , oid_to_hex (& data -> oid ),
443+ strbuf_addf (scratch , "%s %s %" PRIuMAX "%c " , oid_to_hex (& data -> oid ),
442444 type_name (data -> type ),
443- (uintmax_t )data -> size );
445+ (uintmax_t )data -> size , opt -> output_delim );
444446}
445447
446448/*
@@ -469,8 +471,8 @@ static void batch_object_write(const char *obj_name,
469471 & data -> oid , & data -> info ,
470472 OBJECT_INFO_LOOKUP_REPLACE );
471473 if (ret < 0 ) {
472- printf ("%s missing\n " ,
473- obj_name ? obj_name : oid_to_hex (& data -> oid ));
474+ printf ("%s missing%c " ,
475+ obj_name ? obj_name : oid_to_hex (& data -> oid ), opt -> output_delim );
474476 fflush (stdout );
475477 return ;
476478 }
@@ -491,17 +493,17 @@ static void batch_object_write(const char *obj_name,
491493 strbuf_reset (scratch );
492494
493495 if (!opt -> format ) {
494- print_default_format (scratch , data );
496+ print_default_format (scratch , data , opt );
495497 } else {
496498 strbuf_expand (scratch , opt -> format , expand_format , data );
497- strbuf_addch (scratch , '\n' );
499+ strbuf_addch (scratch , opt -> output_delim );
498500 }
499501
500502 batch_write (opt , scratch -> buf , scratch -> len );
501503
502504 if (opt -> batch_mode == BATCH_MODE_CONTENTS ) {
503505 print_object_or_die (opt , data );
504- batch_write (opt , "\n" , 1 );
506+ batch_write (opt , & opt -> output_delim , 1 );
505507 }
506508}
507509
@@ -519,22 +521,25 @@ static void batch_one_object(const char *obj_name,
519521 if (result != FOUND ) {
520522 switch (result ) {
521523 case MISSING_OBJECT :
522- printf ("%s missing\n " , obj_name );
524+ printf ("%s missing%c " , obj_name , opt -> output_delim );
523525 break ;
524526 case SHORT_NAME_AMBIGUOUS :
525- printf ("%s ambiguous\n " , obj_name );
527+ printf ("%s ambiguous%c " , obj_name , opt -> output_delim );
526528 break ;
527529 case DANGLING_SYMLINK :
528- printf ("dangling %" PRIuMAX "\n%s\n" ,
529- (uintmax_t )strlen (obj_name ), obj_name );
530+ printf ("dangling %" PRIuMAX "%c%s%c" ,
531+ (uintmax_t )strlen (obj_name ),
532+ opt -> output_delim , obj_name , opt -> output_delim );
530533 break ;
531534 case SYMLINK_LOOP :
532- printf ("loop %" PRIuMAX "\n%s\n" ,
533- (uintmax_t )strlen (obj_name ), obj_name );
535+ printf ("loop %" PRIuMAX "%c%s%c" ,
536+ (uintmax_t )strlen (obj_name ),
537+ opt -> output_delim , obj_name , opt -> output_delim );
534538 break ;
535539 case NOT_DIR :
536- printf ("notdir %" PRIuMAX "\n%s\n" ,
537- (uintmax_t )strlen (obj_name ), obj_name );
540+ printf ("notdir %" PRIuMAX "%c%s%c" ,
541+ (uintmax_t )strlen (obj_name ),
542+ opt -> output_delim , obj_name , opt -> output_delim );
538543 break ;
539544 default :
540545 BUG ("unknown get_sha1_with_context result %d\n" ,
@@ -546,9 +551,9 @@ static void batch_one_object(const char *obj_name,
546551 }
547552
548553 if (ctx .mode == 0 ) {
549- printf ("symlink %" PRIuMAX "\n%s\n " ,
554+ printf ("symlink %" PRIuMAX "%c%s%c " ,
550555 (uintmax_t )ctx .symlink_path .len ,
551- ctx .symlink_path .buf );
556+ opt -> output_delim , ctx .symlink_path .buf , opt -> output_delim );
552557 fflush (stdout );
553558 return ;
554559 }
@@ -693,20 +698,12 @@ static void batch_objects_command(struct batch_options *opt,
693698 struct queued_cmd * queued_cmd = NULL ;
694699 size_t alloc = 0 , nr = 0 ;
695700
696- while (1 ) {
697- int i , ret ;
701+ while (strbuf_getdelim_strip_crlf ( & input , stdin , opt -> input_delim ) != EOF ) {
702+ int i ;
698703 const struct parse_cmd * cmd = NULL ;
699704 const char * p = NULL , * cmd_end ;
700705 struct queued_cmd call = {0 };
701706
702- if (opt -> nul_terminated )
703- ret = strbuf_getline_nul (& input , stdin );
704- else
705- ret = strbuf_getline (& input , stdin );
706-
707- if (ret )
708- break ;
709-
710707 if (!input .len )
711708 die (_ ("empty command in input" ));
712709 if (isspace (* input .buf ))
@@ -850,16 +847,7 @@ static int batch_objects(struct batch_options *opt)
850847 goto cleanup ;
851848 }
852849
853- while (1 ) {
854- int ret ;
855- if (opt -> nul_terminated )
856- ret = strbuf_getline_nul (& input , stdin );
857- else
858- ret = strbuf_getline (& input , stdin );
859-
860- if (ret == EOF )
861- break ;
862-
850+ while (strbuf_getdelim_strip_crlf (& input , stdin , opt -> input_delim ) != EOF ) {
863851 if (data .split_on_whitespace ) {
864852 /*
865853 * Split at first whitespace, tying off the beginning
@@ -928,14 +916,16 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
928916 const char * exp_type = NULL , * obj_name = NULL ;
929917 struct batch_options batch = {0 };
930918 int unknown_type = 0 ;
919+ int input_nul_terminated = 0 ;
920+ int nul_terminated = 0 ;
931921
932922 const char * const usage [] = {
933923 N_ ("git cat-file <type> <object>" ),
934924 N_ ("git cat-file (-e | -p) <object>" ),
935925 N_ ("git cat-file (-t | -s) [--allow-unknown-type] <object>" ),
936926 N_ ("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
937927 " [--buffer] [--follow-symlinks] [--unordered]\n"
938- " [--textconv | --filters] [-z ]" ),
928+ " [--textconv | --filters] [-Z ]" ),
939929 N_ ("git cat-file (--textconv | --filters)\n"
940930 " [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]" ),
941931 NULL
@@ -964,7 +954,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
964954 N_ ("like --batch, but don't emit <contents>" ),
965955 PARSE_OPT_OPTARG | PARSE_OPT_NONEG ,
966956 batch_option_callback ),
967- OPT_BOOL ('z' , NULL , & batch .nul_terminated , N_ ("stdin is NUL-terminated" )),
957+ OPT_BOOL_F ('z' , NULL , & input_nul_terminated , N_ ("stdin is NUL-terminated" ),
958+ PARSE_OPT_HIDDEN ),
959+ OPT_BOOL ('Z' , NULL , & nul_terminated , N_ ("stdin and stdout is NUL-terminated" )),
968960 OPT_CALLBACK_F (0 , "batch-command" , & batch , N_ ("format" ),
969961 N_ ("read commands from stdin" ),
970962 PARSE_OPT_OPTARG | PARSE_OPT_NONEG ,
@@ -1023,9 +1015,18 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
10231015 else if (batch .all_objects )
10241016 usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
10251017 "--batch-all-objects" );
1026- else if (batch . nul_terminated )
1018+ else if (input_nul_terminated )
10271019 usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
10281020 "-z" );
1021+ else if (nul_terminated )
1022+ usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
1023+ "-Z" );
1024+
1025+ batch .input_delim = batch .output_delim = '\n' ;
1026+ if (input_nul_terminated )
1027+ batch .input_delim = '\0' ;
1028+ if (nul_terminated )
1029+ batch .input_delim = batch .output_delim = '\0' ;
10291030
10301031 /* Batch defaults */
10311032 if (batch .buffer_output < 0 )
0 commit comments