summaryrefslogtreecommitdiff
path: root/builtin/cat-file.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-08-05 22:52:14 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-08-05 22:52:14 (GMT)
commit1e92768aa1771d5d326990855e7a582da541ef9b (patch)
tree185d75918a1fe4f5cbae8a93e24b23f824e054f8 /builtin/cat-file.c
parent3a4d71f52f3a45d14d963c5700bd634e28d58687 (diff)
parentdb9d67f2e9c9ea389f5558d6a168460d51631769 (diff)
downloadgit-1e92768aa1771d5d326990855e7a582da541ef9b.zip
git-1e92768aa1771d5d326990855e7a582da541ef9b.tar.gz
git-1e92768aa1771d5d326990855e7a582da541ef9b.tar.bz2
Merge branch 'tb/cat-file-z'
Operating modes like "--batch" of "git cat-file" command learned to take NUL-terminated input, instead of one-item-per-line. * tb/cat-file-z: builtin/cat-file.c: support NUL-delimited input with `-z` t1006: extract --batch-command inputs to variables
Diffstat (limited to 'builtin/cat-file.c')
-rw-r--r--builtin/cat-file.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index cbccb55..989eee0 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -32,6 +32,7 @@ struct batch_options {
int all_objects;
int unordered;
int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
+ int nul_terminated;
const char *format;
};
@@ -650,12 +651,20 @@ static void batch_objects_command(struct batch_options *opt,
struct queued_cmd *queued_cmd = NULL;
size_t alloc = 0, nr = 0;
- while (!strbuf_getline(&input, stdin)) {
- int i;
+ while (1) {
+ int i, ret;
const struct parse_cmd *cmd = NULL;
const char *p = NULL, *cmd_end;
struct queued_cmd call = {0};
+ if (opt->nul_terminated)
+ ret = strbuf_getline_nul(&input, stdin);
+ else
+ ret = strbuf_getline(&input, stdin);
+
+ if (ret)
+ break;
+
if (!input.len)
die(_("empty command in input"));
if (isspace(*input.buf))
@@ -799,7 +808,16 @@ static int batch_objects(struct batch_options *opt)
goto cleanup;
}
- while (strbuf_getline(&input, stdin) != EOF) {
+ while (1) {
+ int ret;
+ if (opt->nul_terminated)
+ ret = strbuf_getline_nul(&input, stdin);
+ else
+ ret = strbuf_getline(&input, stdin);
+
+ if (ret == EOF)
+ break;
+
if (data.split_on_whitespace) {
/*
* Split at first whitespace, tying off the beginning
@@ -904,6 +922,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
N_("like --batch, but don't emit <contents>"),
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
batch_option_callback),
+ OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
N_("read commands from stdin"),
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -962,6 +981,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
else if (batch.all_objects)
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
"--batch-all-objects");
+ else if (batch.nul_terminated)
+ usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
+ "-z");
/* Batch defaults */
if (batch.buffer_output < 0)