cat-file: handle --batch format with missing type/size

Commit 98e2092 taught cat-file to stream blobs with --batch, which requires that we look up the object type before loading it into memory. As a result, we now print the object header from information in sha1_object_info, and the actual contents from the read_sha1_file. We double-check that the information we printed in the header matches the content we are about to show. Later, commit 93d2a60 allowed custom header lines for --batch, and commit 5b08640 made type lookups optional. As a result, specifying a header line without the type or size means that we will not look up those items at all. This causes our double-checking to erroneously die with an error; we think the type or size has changed, when in fact it was simply left at "0". For the size, we can fix this by only doing the consistency double-check when we have retrieved the size via sha1_object_info. In the case that we have not retrieved the value, that means we also did not print it, so there is nothing for us to check that we are consistent with. We could do the same for the type. However, besides our consistency check, we also care about the type in deciding whether to stream or not. So instead of handling the case where we do not know the type, this patch instead makes sure that we always trigger a type lookup when we are printing, so that even a format without the type will stream as we would in the normal case. Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 12, 2013 at 07:15 UTC 6554dfa97af92a159231d94e60403bfe0d6e8e61
2 files changed +32 -1
builtin/cat-file.c
+10 -1
@@ -197,6 +197,8 @@ static void print_object_or_die(int fd, struct expand_data *data)
197 {
198 const unsigned char *sha1 = data->sha1;
199
200 + assert(data->info.typep);
201 +
202 if (data->type == OBJ_BLOB) {
203 if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
204 die("unable to stream %s to stdout", sha1_to_hex(sha1));
@@ -211,7 +213,7 @@ static void print_object_or_die(int fd, struct expand_data *data)
213 die("object %s disappeared", sha1_to_hex(sha1));
214 if (type != data->type)
215 die("object %s changed type!?", sha1_to_hex(sha1));
214 - if (size != data->size)
216 + if (data->info.sizep && size != data->size)
217 die("object %s changed size!?", sha1_to_hex(sha1));
218
219 write_or_die(fd, contents, size);
@@ -275,6 +277,13 @@ static int batch_objects(struct batch_options *opt)
277 strbuf_expand(&buf, opt->format, expand_format, &data);
278 data.mark_query = 0;
279
280 + /*
281 + * If we are printing out the object, then always fill in the type,
282 + * since we will want to decide whether or not to stream.
283 + */
284 + if (opt->print_contents)
285 + data.info.typep = &data.type;
286 +
287 /*
288 * We are going to call get_sha1 on a potentially very large number of
289 * objects. In most large cases, these will be actual object sha1s. The
t/t1006-cat-file.sh
+22
@@ -85,6 +85,28 @@ $content"
85 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
86 test_cmp expect actual
87 '
88 +
89 + test -z "$content" ||
90 + test_expect_success "--batch without type ($type)" '
91 + {
92 + echo "$size" &&
93 + maybe_remove_timestamp "$content" $no_ts
94 + } >expect &&
95 + echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
96 + maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
97 + test_cmp expect actual
98 + '
99 +
100 + test -z "$content" ||
101 + test_expect_success "--batch without size ($type)" '
102 + {
103 + echo "$type" &&
104 + maybe_remove_timestamp "$content" $no_ts
105 + } >expect &&
106 + echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
107 + maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
108 + test_cmp expect actual
109 + '
110 }
111
112 hello_content="Hello World"