summaryrefslogtreecommitdiff
path: root/builtin/cat-file.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2018-10-30 23:23:38 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-10-31 04:05:26 (GMT)
commit98f425b453870cdb20b4bf8daa29f52af8e59866 (patch)
treee9ec3be96b91298e849218623f36f56ba7c13c94 /builtin/cat-file.c
parentccdc4819d56202d49e35fbfec2960e221576f862 (diff)
downloadgit-98f425b453870cdb20b4bf8daa29f52af8e59866.zip
git-98f425b453870cdb20b4bf8daa29f52af8e59866.tar.gz
git-98f425b453870cdb20b4bf8daa29f52af8e59866.tar.bz2
cat-file: handle streaming failures consistently
There are three ways to convince cat-file to stream a blob: - cat-file -p $blob - cat-file blob $blob - echo $batch | cat-file --batch In the first two, we simply exit with the error code of streaw_blob_to_fd(). That means that an error will cause us to exit with "-1" (which we try to avoid) without printing any kind of error message (which is confusing to the user). Instead, let's match the third case, which calls die() on an error. Unfortunately we cannot be more specific, as stream_blob_to_fd() does not tell us whether the problem was on reading (e.g., a corrupt object) or on writing (e.g., ENOSPC). That might be an opportunity for future work, but for now we will at least exit with a sane message and exit code. 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.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 30383e9..f117328 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -45,6 +45,13 @@ static int filter_object(const char *path, unsigned mode,
return 0;
}
+static int stream_blob(const struct object_id *oid)
+{
+ if (stream_blob_to_fd(1, oid, NULL, 0))
+ die("unable to stream %s to stdout", oid_to_hex(oid));
+ return 0;
+}
+
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
int unknown_type)
{
@@ -124,7 +131,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
}
if (type == OBJ_BLOB)
- return stream_blob_to_fd(1, &oid, NULL, 0);
+ return stream_blob(&oid);
buf = read_sha1_file(oid.hash, &type, &size);
if (!buf)
die("Cannot read object %s", obj_name);
@@ -146,7 +153,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
oidcpy(&blob_oid, &oid);
if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
- return stream_blob_to_fd(1, &blob_oid, NULL, 0);
+ return stream_blob(&blob_oid);
/*
* we attempted to dereference a tag to a blob
* and failed; there may be new dereference
@@ -306,8 +313,9 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
die("BUG: invalid cmdmode: %c", opt->cmdmode);
batch_write(opt, contents, size);
free(contents);
- } else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
- die("unable to stream %s to stdout", oid_to_hex(oid));
+ } else {
+ stream_blob(oid);
+ }
}
else {
enum object_type type;