Skip to content

Commit c16a464

Browse files
committed
Merge branch 'ps/cat-file-null-output' into next
"git cat-file --batch" and friends learned "-Z" that uses NUL delimiter for both input and output. * ps/cat-file-null-output: cat-file: add option '-Z' that delimits input and output with NUL cat-file: simplify reading from standard input strbuf: provide CRLF-aware helper to read until a specified delimiter t1006: modernize test style to use `test_cmp` t1006: don't strip timestamps from expected results
2 parents 26c3a34 + f79e188 commit c16a464

File tree

5 files changed

+232
-138
lines changed

5 files changed

+232
-138
lines changed

Documentation/git-cat-file.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
'git cat-file' (-t | -s) [--allow-unknown-type] <object>
1515
'git cat-file' (--batch | --batch-check | --batch-command) [--batch-all-objects]
1616
[--buffer] [--follow-symlinks] [--unordered]
17-
[--textconv | --filters] [-z]
17+
[--textconv | --filters] [-Z]
1818
'git cat-file' (--textconv | --filters)
1919
[<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]
2020

@@ -243,10 +243,16 @@ respectively print:
243243
/etc/passwd
244244
--
245245

246+
-Z::
247+
Only meaningful with `--batch`, `--batch-check`, or
248+
`--batch-command`; input and output is NUL-delimited instead of
249+
newline-delimited.
250+
246251
-z::
247252
Only meaningful with `--batch`, `--batch-check`, or
248253
`--batch-command`; input is NUL-delimited instead of
249-
newline-delimited.
254+
newline-delimited. This option is deprecated in favor of
255+
`-Z` as the output can otherwise be ambiguous.
250256

251257

252258
OUTPUT
@@ -384,6 +390,11 @@ notdir SP <size> LF
384390
is printed when, during symlink resolution, a file is used as a
385391
directory name.
386392

393+
Alternatively, when `-Z` is passed, the line feeds in any of the above examples
394+
are replaced with NUL terminators. This ensures that output will be parsable if
395+
the output itself would contain a linefeed and is thus recommended for
396+
scripting purposes.
397+
387398
CAVEATS
388399
-------
389400

builtin/cat-file.c

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

strbuf.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,23 @@ static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
722722
return 0;
723723
}
724724

725-
int strbuf_getline(struct strbuf *sb, FILE *fp)
725+
int strbuf_getdelim_strip_crlf(struct strbuf *sb, FILE *fp, int term)
726726
{
727-
if (strbuf_getwholeline(sb, fp, '\n'))
727+
if (strbuf_getwholeline(sb, fp, term))
728728
return EOF;
729-
if (sb->buf[sb->len - 1] == '\n') {
729+
if (term == '\n' && sb->buf[sb->len - 1] == '\n') {
730730
strbuf_setlen(sb, sb->len - 1);
731731
if (sb->len && sb->buf[sb->len - 1] == '\r')
732732
strbuf_setlen(sb, sb->len - 1);
733733
}
734734
return 0;
735735
}
736736

737+
int strbuf_getline(struct strbuf *sb, FILE *fp)
738+
{
739+
return strbuf_getdelim_strip_crlf(sb, fp, '\n');
740+
}
741+
737742
int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
738743
{
739744
return strbuf_getdelim(sb, fp, '\n');

strbuf.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
471471
*/
472472
ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
473473

474+
/**
475+
* Read from a FILE * until the specified terminator is encountered,
476+
* overwriting the existing contents of the strbuf.
477+
*
478+
* Reading stops after the terminator or at EOF. The terminator is
479+
* removed from the buffer before returning. If the terminator is LF
480+
* and if it is preceded by a CR, then the whole CRLF is stripped.
481+
* Returns 0 unless there was nothing left before EOF, in which case
482+
* it returns `EOF`.
483+
*/
484+
int strbuf_getdelim_strip_crlf(struct strbuf *sb, FILE *fp, int term);
485+
474486
/**
475487
* Read a line from a FILE *, overwriting the existing contents of
476488
* the strbuf. The strbuf_getline*() family of functions share

0 commit comments

Comments
 (0)