cat-file: use streaming API to print blobs

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 7, 2012 at 17:54 UTC 00c8fd493afbd1620febf2b895fb2365f76d5875
2 files changed +27 -2
builtin/cat-file.c
+25
@@ -11,6 +11,7 @@
11 #include "parse-options.h"
12 #include "diff.h"
13 #include "userdiff.h"
14 +#include "streaming.h"
15
16 #define BATCH 1
17 #define BATCH_CHECK 2
@@ -127,6 +128,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
128 return cmd_ls_tree(2, ls_args, NULL);
129 }
130
131 + if (type == OBJ_BLOB)
132 + return stream_blob_to_fd(1, sha1, NULL, 0);
133 buf = read_sha1_file(sha1, &type, &size);
134 if (!buf)
135 die("Cannot read object %s", obj_name);
@@ -149,6 +152,28 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
152 break;
153
154 case 0:
155 + if (type_from_string(exp_type) == OBJ_BLOB) {
156 + unsigned char blob_sha1[20];
157 + if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
158 + enum object_type type;
159 + unsigned long size;
160 + char *buffer = read_sha1_file(sha1, &type, &size);
161 + if (memcmp(buffer, "object ", 7) ||
162 + get_sha1_hex(buffer + 7, blob_sha1))
163 + die("%s not a valid tag", sha1_to_hex(sha1));
164 + free(buffer);
165 + } else
166 + hashcpy(blob_sha1, sha1);
167 +
168 + if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
169 + return stream_blob_to_fd(1, blob_sha1, NULL, 0);
170 + /*
171 + * we attempted to dereference a tag to a blob
172 + * and failed; there may be new dereference
173 + * mechanisms this code is not aware of.
174 + * fall-back to the usual case.
175 + */
176 + }
177 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
178 break;
179
t/t1050-large.sh
+2 -2
@@ -116,11 +116,11 @@ test_expect_success 'hash-object' '
116 git hash-object large1
117 '
118
119 -test_expect_failure 'cat-file a large file' '
119 +test_expect_success 'cat-file a large file' '
120 git cat-file blob :large1 >/dev/null
121 '
122
123 -test_expect_failure 'cat-file a large file from a tag' '
123 +test_expect_success 'cat-file a large file from a tag' '
124 git tag -m largefile largefiletag :large1 &&
125 git cat-file blob largefiletag >/dev/null
126 '