Skip to content

Commit 00c8fd4

Browse files
pcloudsgitster
authored andcommitted
cat-file: use streaming API to print blobs
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d41489a commit 00c8fd4

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

builtin/cat-file.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "parse-options.h"
1212
#include "diff.h"
1313
#include "userdiff.h"
14+
#include "streaming.h"
1415

1516
#define BATCH 1
1617
#define BATCH_CHECK 2
@@ -127,6 +128,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
127128
return cmd_ls_tree(2, ls_args, NULL);
128129
}
129130

131+
if (type == OBJ_BLOB)
132+
return stream_blob_to_fd(1, sha1, NULL, 0);
130133
buf = read_sha1_file(sha1, &type, &size);
131134
if (!buf)
132135
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)
149152
break;
150153

151154
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+
}
152177
buf = read_object_with_reference(sha1, exp_type, &size, NULL);
153178
break;
154179

t/t1050-large.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ test_expect_success 'hash-object' '
116116
git hash-object large1
117117
'
118118

119-
test_expect_failure 'cat-file a large file' '
119+
test_expect_success 'cat-file a large file' '
120120
git cat-file blob :large1 >/dev/null
121121
'
122122

123-
test_expect_failure 'cat-file a large file from a tag' '
123+
test_expect_success 'cat-file a large file from a tag' '
124124
git tag -m largefile largefiletag :large1 &&
125125
git cat-file blob largefiletag >/dev/null
126126
'

0 commit comments

Comments
 (0)