cat-file: support --textconv/--filters in batch mode
With this patch, --batch can be combined with --textconv or --filters. For this to work, the input needs to have the form <object name><single white space><path> so that the filters can be chosen appropriately. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Sep 9, 2016 at 12:10 UTC
321459439e19517c412cab1cfbb64a2749f272c9
3 files changed
+67
-10
Documentation/git-cat-file.txt
+13
-5
@@ -10,7 +10,7 @@ SYNOPSIS
10
--------
11
[verse]
12
'git cat-file' (-t [--allow-unknown-type]| -s [--allow-unknown-type]| -e | -p | <type> | --textconv | --filters ) [--path=<path>] <object>
13
-'git cat-file' (--batch | --batch-check) [--follow-symlinks]
13
+'git cat-file' (--batch | --batch-check) [ --textconv | --filters ] [--follow-symlinks]
14
15
DESCRIPTION
16
-----------
@@ -20,7 +20,11 @@ object type, or `-s` is used to find the object size, or `--textconv` or
20
`--filters` is used (which imply type "blob").
21
22
In the second form, a list of objects (separated by linefeeds) is provided on
23
-stdin, and the SHA-1, type, and size of each object is printed on stdout.
23
+stdin, and the SHA-1, type, and size of each object is printed on stdout. The
24
+output format can be overridden using the optional `<format>` argument. If
25
+either `--textconv` or `--filters` was specified, the input is expected to
26
+list the object names followed by the path name, separated by a single white
27
+space, so that the appropriate drivers can be determined.
28
29
OPTIONS
30
-------
@@ -72,13 +76,17 @@ OPTIONS
76
--batch::
77
--batch=<format>::
78
Print object information and contents for each object provided
75
- on stdin. May not be combined with any other options or arguments.
76
- See the section `BATCH OUTPUT` below for details.
79
+ on stdin. May not be combined with any other options or arguments
80
+ except `--textconv` or `--filters`, in which case the input lines
81
+ also need to specify the path, separated by white space. See the
82
+ section `BATCH OUTPUT` below for details.
83
84
--batch-check::
85
--batch-check=<format>::
86
Print object information for each object provided on stdin. May
81
- not be combined with any other options or arguments. See the
87
+ not be combined with any other options or arguments except
88
+ `--textconv` or `--filters`, in which case the input lines also
89
+ need to specify the path, separated by white space. See the
90
section `BATCH OUTPUT` below for details.
91
92
--batch-all-objects::
builtin/cat-file.c
+44
-5
@@ -17,6 +17,7 @@ struct batch_options {
17
int print_contents;
18
int buffer_output;
19
int all_objects;
20
+ int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
21
const char *format;
22
};
23
@@ -280,7 +281,32 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
281
if (data->type == OBJ_BLOB) {
282
if (opt->buffer_output)
283
fflush(stdout);
283
- if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
284
+ if (opt->cmdmode) {
285
+ char *contents;
286
+ unsigned long size;
287
+
288
+ if (!data->rest)
289
+ die("missing path for '%s'", sha1_to_hex(sha1));
290
+
291
+ if (opt->cmdmode == 'w') {
292
+ if (filter_object(data->rest, 0100644, sha1,
293
+ &contents, &size))
294
+ die("could not convert '%s' %s",
295
+ sha1_to_hex(sha1), data->rest);
296
+ } else if (opt->cmdmode == 'c') {
297
+ enum object_type type;
298
+ if (!textconv_object(data->rest, 0100644, sha1,
299
+ 1, &contents, &size))
300
+ contents = read_sha1_file(sha1, &type,
301
+ &size);
302
+ if (!contents)
303
+ die("could not convert '%s' %s",
304
+ sha1_to_hex(sha1), data->rest);
305
+ } else
306
+ die("BUG: invalid cmdmode: %c", opt->cmdmode);
307
+ batch_write(opt, contents, size);
308
+ free(contents);
309
+ } else if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
310
die("unable to stream %s to stdout", sha1_to_hex(sha1));
311
}
312
else {
@@ -417,6 +443,8 @@ static int batch_objects(struct batch_options *opt)
443
data.mark_query = 1;
444
strbuf_expand(&buf, opt->format, expand_format, &data);
445
data.mark_query = 0;
446
+ if (opt->cmdmode)
447
+ data.split_on_whitespace = 1;
448
449
if (opt->all_objects) {
450
struct object_info empty;
@@ -482,7 +510,7 @@ static int batch_objects(struct batch_options *opt)
510
511
static const char * const cat_file_usage[] = {
512
N_("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv|--filters) [--path=<path>] <object>"),
485
- N_("git cat-file (--batch | --batch-check) [--follow-symlinks]"),
513
+ N_("git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv|--filters]"),
514
NULL
515
};
516
@@ -553,7 +581,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
581
argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
582
583
if (opt) {
556
- if (argc == 1)
584
+ if (batch.enabled && (opt == 'c' || opt == 'w'))
585
+ batch.cmdmode = opt;
586
+ else if (argc == 1)
587
obj_name = argv[0];
588
else
589
usage_with_options(cat_file_usage, options);
@@ -565,8 +595,12 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
595
} else
596
usage_with_options(cat_file_usage, options);
597
}
568
- if (batch.enabled && (opt || argc)) {
569
- usage_with_options(cat_file_usage, options);
598
+ if (batch.enabled) {
599
+ if (batch.cmdmode != opt || argc)
600
+ usage_with_options(cat_file_usage, options);
601
+ if (batch.cmdmode && batch.all_objects)
602
+ die("--batch-all-objects cannot be combined with "
603
+ "--textconv nor with --filters");
604
}
605
606
if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
@@ -578,6 +612,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
612
usage_with_options(cat_file_usage, options);
613
}
614
615
+ if (force_path && batch.enabled) {
616
+ error("--path=<path> incompatible with --batch");
617
+ usage_with_options(cat_file_usage, options);
618
+ }
619
+
620
if (batch.buffer_output < 0)
621
batch.buffer_output = batch.all_objects;
622
t/t8010-cat-file-filters.sh
+10
@@ -51,4 +51,14 @@ test_expect_success '--path=<path> complains without --textconv/--filters' '
51
grep "path.*needs.*filters" err
52
'
53
54
+test_expect_success 'cat-file --textconv --batch works' '
55
+ sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
56
+ test_config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <" &&
57
+ printf "%s hello.txt\n%s hello\n" $sha1 $sha1 |
58
+ git cat-file --textconv --batch >actual &&
59
+ printf "%s blob 6\nuryyb\r\n\n%s blob 6\nhello\n\n" \
60
+ $sha1 $sha1 >expect &&
61
+ test_cmp expect actual
62
+'
63
+
64
test_done