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