@@ -42,7 +42,8 @@ struct batch_options {
42
42
int all_objects ;
43
43
int unordered ;
44
44
int transform_mode ; /* may be 'w' or 'c' for --filters or --textconv */
45
- int nul_terminated ;
45
+ char input_delim ;
46
+ char output_delim ;
46
47
const char * format ;
47
48
};
48
49
@@ -437,11 +438,12 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
437
438
}
438
439
}
439
440
440
- static void print_default_format (struct strbuf * scratch , struct expand_data * data )
441
+ static void print_default_format (struct strbuf * scratch , struct expand_data * data ,
442
+ struct batch_options * opt )
441
443
{
442
- strbuf_addf (scratch , "%s %s %" PRIuMAX "\n " , oid_to_hex (& data -> oid ),
444
+ strbuf_addf (scratch , "%s %s %" PRIuMAX "%c " , oid_to_hex (& data -> oid ),
443
445
type_name (data -> type ),
444
- (uintmax_t )data -> size );
446
+ (uintmax_t )data -> size , opt -> output_delim );
445
447
}
446
448
447
449
/*
@@ -470,8 +472,8 @@ static void batch_object_write(const char *obj_name,
470
472
& data -> oid , & data -> info ,
471
473
OBJECT_INFO_LOOKUP_REPLACE );
472
474
if (ret < 0 ) {
473
- printf ("%s missing\n " ,
474
- obj_name ? obj_name : oid_to_hex (& data -> oid ));
475
+ printf ("%s missing%c " ,
476
+ obj_name ? obj_name : oid_to_hex (& data -> oid ), opt -> output_delim );
475
477
fflush (stdout );
476
478
return ;
477
479
}
@@ -492,17 +494,17 @@ static void batch_object_write(const char *obj_name,
492
494
strbuf_reset (scratch );
493
495
494
496
if (!opt -> format ) {
495
- print_default_format (scratch , data );
497
+ print_default_format (scratch , data , opt );
496
498
} else {
497
499
strbuf_expand (scratch , opt -> format , expand_format , data );
498
- strbuf_addch (scratch , '\n' );
500
+ strbuf_addch (scratch , opt -> output_delim );
499
501
}
500
502
501
503
batch_write (opt , scratch -> buf , scratch -> len );
502
504
503
505
if (opt -> batch_mode == BATCH_MODE_CONTENTS ) {
504
506
print_object_or_die (opt , data );
505
- batch_write (opt , "\n" , 1 );
507
+ batch_write (opt , & opt -> output_delim , 1 );
506
508
}
507
509
}
508
510
@@ -520,22 +522,25 @@ static void batch_one_object(const char *obj_name,
520
522
if (result != FOUND ) {
521
523
switch (result ) {
522
524
case MISSING_OBJECT :
523
- printf ("%s missing\n " , obj_name );
525
+ printf ("%s missing%c " , obj_name , opt -> output_delim );
524
526
break ;
525
527
case SHORT_NAME_AMBIGUOUS :
526
- printf ("%s ambiguous\n " , obj_name );
528
+ printf ("%s ambiguous%c " , obj_name , opt -> output_delim );
527
529
break ;
528
530
case DANGLING_SYMLINK :
529
- printf ("dangling %" PRIuMAX "\n%s\n" ,
530
- (uintmax_t )strlen (obj_name ), obj_name );
531
+ printf ("dangling %" PRIuMAX "%c%s%c" ,
532
+ (uintmax_t )strlen (obj_name ),
533
+ opt -> output_delim , obj_name , opt -> output_delim );
531
534
break ;
532
535
case SYMLINK_LOOP :
533
- printf ("loop %" PRIuMAX "\n%s\n" ,
534
- (uintmax_t )strlen (obj_name ), obj_name );
536
+ printf ("loop %" PRIuMAX "%c%s%c" ,
537
+ (uintmax_t )strlen (obj_name ),
538
+ opt -> output_delim , obj_name , opt -> output_delim );
535
539
break ;
536
540
case NOT_DIR :
537
- printf ("notdir %" PRIuMAX "\n%s\n" ,
538
- (uintmax_t )strlen (obj_name ), obj_name );
541
+ printf ("notdir %" PRIuMAX "%c%s%c" ,
542
+ (uintmax_t )strlen (obj_name ),
543
+ opt -> output_delim , obj_name , opt -> output_delim );
539
544
break ;
540
545
default :
541
546
BUG ("unknown get_sha1_with_context result %d\n" ,
@@ -547,9 +552,9 @@ static void batch_one_object(const char *obj_name,
547
552
}
548
553
549
554
if (ctx .mode == 0 ) {
550
- printf ("symlink %" PRIuMAX "\n%s\n " ,
555
+ printf ("symlink %" PRIuMAX "%c%s%c " ,
551
556
(uintmax_t )ctx .symlink_path .len ,
552
- ctx .symlink_path .buf );
557
+ opt -> output_delim , ctx .symlink_path .buf , opt -> output_delim );
553
558
fflush (stdout );
554
559
return ;
555
560
}
@@ -694,20 +699,12 @@ static void batch_objects_command(struct batch_options *opt,
694
699
struct queued_cmd * queued_cmd = NULL ;
695
700
size_t alloc = 0 , nr = 0 ;
696
701
697
- while (1 ) {
698
- int i , ret ;
702
+ while (strbuf_getdelim_strip_crlf ( & input , stdin , opt -> input_delim ) != EOF ) {
703
+ int i ;
699
704
const struct parse_cmd * cmd = NULL ;
700
705
const char * p = NULL , * cmd_end ;
701
706
struct queued_cmd call = {0 };
702
707
703
- if (opt -> nul_terminated )
704
- ret = strbuf_getline_nul (& input , stdin );
705
- else
706
- ret = strbuf_getline (& input , stdin );
707
-
708
- if (ret )
709
- break ;
710
-
711
708
if (!input .len )
712
709
die (_ ("empty command in input" ));
713
710
if (isspace (* input .buf ))
@@ -851,16 +848,7 @@ static int batch_objects(struct batch_options *opt)
851
848
goto cleanup ;
852
849
}
853
850
854
- while (1 ) {
855
- int ret ;
856
- if (opt -> nul_terminated )
857
- ret = strbuf_getline_nul (& input , stdin );
858
- else
859
- ret = strbuf_getline (& input , stdin );
860
-
861
- if (ret == EOF )
862
- break ;
863
-
851
+ while (strbuf_getdelim_strip_crlf (& input , stdin , opt -> input_delim ) != EOF ) {
864
852
if (data .split_on_whitespace ) {
865
853
/*
866
854
* Split at first whitespace, tying off the beginning
@@ -929,14 +917,16 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
929
917
const char * exp_type = NULL , * obj_name = NULL ;
930
918
struct batch_options batch = {0 };
931
919
int unknown_type = 0 ;
920
+ int input_nul_terminated = 0 ;
921
+ int nul_terminated = 0 ;
932
922
933
923
const char * const usage [] = {
934
924
N_ ("git cat-file <type> <object>" ),
935
925
N_ ("git cat-file (-e | -p) <object>" ),
936
926
N_ ("git cat-file (-t | -s) [--allow-unknown-type] <object>" ),
937
927
N_ ("git cat-file (--batch | --batch-check | --batch-command) [--batch-all-objects]\n"
938
928
" [--buffer] [--follow-symlinks] [--unordered]\n"
939
- " [--textconv | --filters] [-z ]" ),
929
+ " [--textconv | --filters] [-Z ]" ),
940
930
N_ ("git cat-file (--textconv | --filters)\n"
941
931
" [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]" ),
942
932
NULL
@@ -965,7 +955,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
965
955
N_ ("like --batch, but don't emit <contents>" ),
966
956
PARSE_OPT_OPTARG | PARSE_OPT_NONEG ,
967
957
batch_option_callback ),
968
- OPT_BOOL ('z' , NULL , & batch .nul_terminated , N_ ("stdin is NUL-terminated" )),
958
+ OPT_BOOL_F ('z' , NULL , & input_nul_terminated , N_ ("stdin is NUL-terminated" ),
959
+ PARSE_OPT_HIDDEN ),
960
+ OPT_BOOL ('Z' , NULL , & nul_terminated , N_ ("stdin and stdout is NUL-terminated" )),
969
961
OPT_CALLBACK_F (0 , "batch-command" , & batch , N_ ("format" ),
970
962
N_ ("read commands from stdin" ),
971
963
PARSE_OPT_OPTARG | PARSE_OPT_NONEG ,
@@ -1024,9 +1016,18 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
1024
1016
else if (batch .all_objects )
1025
1017
usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
1026
1018
"--batch-all-objects" );
1027
- else if (batch . nul_terminated )
1019
+ else if (input_nul_terminated )
1028
1020
usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
1029
1021
"-z" );
1022
+ else if (nul_terminated )
1023
+ usage_msg_optf (_ ("'%s' requires a batch mode" ), usage , options ,
1024
+ "-Z" );
1025
+
1026
+ batch .input_delim = batch .output_delim = '\n' ;
1027
+ if (input_nul_terminated )
1028
+ batch .input_delim = '\0' ;
1029
+ if (nul_terminated )
1030
+ batch .input_delim = batch .output_delim = '\0' ;
1030
1031
1031
1032
/* Batch defaults */
1032
1033
if (batch .buffer_output < 0 )
0 commit comments