summaryrefslogtreecommitdiff
path: root/builtin/cat-file.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2013-07-10 11:38:24 (GMT)
committerJunio C Hamano <gitster@pobox.com>2013-07-11 17:37:16 (GMT)
commit98e2092b5027fde9dbe68cb49a196ad2184c02c3 (patch)
treeeac218bc047ba18cc65d4fee392f0f1d2c3d2fe3 /builtin/cat-file.c
parent03c893cbf9ad2e9a5be43382bcabdb2af2a49a4a (diff)
downloadgit-98e2092b5027fde9dbe68cb49a196ad2184c02c3.zip
git-98e2092b5027fde9dbe68cb49a196ad2184c02c3.tar.gz
git-98e2092b5027fde9dbe68cb49a196ad2184c02c3.tar.bz2
cat-file: teach --batch to stream blob objects
The regular "git cat-file -p" and "git cat-file blob" code paths already learned to stream large blobs. Let's do the same here. Note that this means we look up the type and size before making a decision of whether to load the object into memory or stream (just like the "-p" code path does). That can lead to extra work, but it should be dwarfed by the cost of actually accessing the object itself. In my measurements, there was a 1-2% slowdown when using "--batch" on a large number of objects. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/cat-file.c')
-rw-r--r--builtin/cat-file.c41
1 files changed, 28 insertions, 13 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 045cee7..70dd8c8 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -117,12 +117,36 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
return 0;
}
+static void print_object_or_die(int fd, const unsigned char *sha1,
+ enum object_type type, unsigned long size)
+{
+ if (type == OBJ_BLOB) {
+ if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
+ die("unable to stream %s to stdout", sha1_to_hex(sha1));
+ }
+ else {
+ enum object_type rtype;
+ unsigned long rsize;
+ void *contents;
+
+ contents = read_sha1_file(sha1, &rtype, &rsize);
+ if (!contents)
+ die("object %s disappeared", sha1_to_hex(sha1));
+ if (rtype != type)
+ die("object %s changed type!?", sha1_to_hex(sha1));
+ if (rsize != size)
+ die("object %s change size!?", sha1_to_hex(sha1));
+
+ write_or_die(fd, contents, size);
+ free(contents);
+ }
+}
+
static int batch_one_object(const char *obj_name, int print_contents)
{
unsigned char sha1[20];
enum object_type type = 0;
unsigned long size;
- void *contents = NULL;
if (!obj_name)
return 1;
@@ -133,16 +157,10 @@ static int batch_one_object(const char *obj_name, int print_contents)
return 0;
}
- if (print_contents == BATCH)
- contents = read_sha1_file(sha1, &type, &size);
- else
- type = sha1_object_info(sha1, &size);
-
+ type = sha1_object_info(sha1, &size);
if (type <= 0) {
printf("%s missing\n", obj_name);
fflush(stdout);
- if (print_contents == BATCH)
- free(contents);
return 0;
}
@@ -150,12 +168,9 @@ static int batch_one_object(const char *obj_name, int print_contents)
fflush(stdout);
if (print_contents == BATCH) {
- write_or_die(1, contents, size);
- printf("\n");
- fflush(stdout);
- free(contents);
+ print_object_or_die(1, sha1, type, size);
+ write_or_die(1, "\n", 1);
}
-
return 0;
}