Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 6845e8a

Browse files
committed
Merge branch 'jk/cat-file-regression-fix' into maint
"git cat-file --batch=", an admittedly useless command, did not behave very well. * jk/cat-file-regression-fix: cat-file: handle --batch format with missing type/size cat-file: pass expand_data to print_object_or_die
2 parents ebba6c0 + 6554dfa commit 6845e8a

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

builtin/cat-file.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,28 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
193193
return end - start + 1;
194194
}
195195

196-
static void print_object_or_die(int fd, const unsigned char *sha1,
197-
enum object_type type, unsigned long size)
196+
static void print_object_or_die(int fd, struct expand_data *data)
198197
{
199-
if (type == OBJ_BLOB) {
198+
const unsigned char *sha1 = data->sha1;
199+
200+
assert(data->info.typep);
201+
202+
if (data->type == OBJ_BLOB) {
200203
if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
201204
die("unable to stream %s to stdout", sha1_to_hex(sha1));
202205
}
203206
else {
204-
enum object_type rtype;
205-
unsigned long rsize;
207+
enum object_type type;
208+
unsigned long size;
206209
void *contents;
207210

208-
contents = read_sha1_file(sha1, &rtype, &rsize);
211+
contents = read_sha1_file(sha1, &type, &size);
209212
if (!contents)
210213
die("object %s disappeared", sha1_to_hex(sha1));
211-
if (rtype != type)
214+
if (type != data->type)
212215
die("object %s changed type!?", sha1_to_hex(sha1));
213-
if (rsize != size)
214-
die("object %s change size!?", sha1_to_hex(sha1));
216+
if (data->info.sizep && size != data->size)
217+
die("object %s changed size!?", sha1_to_hex(sha1));
215218

216219
write_or_die(fd, contents, size);
217220
free(contents);
@@ -250,7 +253,7 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
250253
strbuf_release(&buf);
251254

252255
if (opt->print_contents) {
253-
print_object_or_die(1, data->sha1, data->type, data->size);
256+
print_object_or_die(1, data);
254257
write_or_die(1, "\n", 1);
255258
}
256259
return 0;
@@ -274,6 +277,13 @@ static int batch_objects(struct batch_options *opt)
274277
strbuf_expand(&buf, opt->format, expand_format, &data);
275278
data.mark_query = 0;
276279

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+
277287
/*
278288
* We are going to call get_sha1 on a potentially very large number of
279289
* objects. In most large cases, these will be actual object sha1s. The

t/t1006-cat-file.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ $content"
8585
git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
8686
test_cmp expect actual
8787
'
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+
'
88110
}
89111

90112
hello_content="Hello World"

0 commit comments

Comments
 (0)