Skip to content

Commit 98f425b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ccdc481 commit 98f425b

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

builtin/cat-file.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ static int filter_object(const char *path, unsigned mode,
4545
return 0;
4646
}
4747

48+
static int stream_blob(const struct object_id *oid)
49+
{
50+
if (stream_blob_to_fd(1, oid, NULL, 0))
51+
die("unable to stream %s to stdout", oid_to_hex(oid));
52+
return 0;
53+
}
54+
4855
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
4956
int unknown_type)
5057
{
@@ -124,7 +131,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
124131
}
125132

126133
if (type == OBJ_BLOB)
127-
return stream_blob_to_fd(1, &oid, NULL, 0);
134+
return stream_blob(&oid);
128135
buf = read_sha1_file(oid.hash, &type, &size);
129136
if (!buf)
130137
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,
146153
oidcpy(&blob_oid, &oid);
147154

148155
if (sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
149-
return stream_blob_to_fd(1, &blob_oid, NULL, 0);
156+
return stream_blob(&blob_oid);
150157
/*
151158
* we attempted to dereference a tag to a blob
152159
* 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
306313
die("BUG: invalid cmdmode: %c", opt->cmdmode);
307314
batch_write(opt, contents, size);
308315
free(contents);
309-
} else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
310-
die("unable to stream %s to stdout", oid_to_hex(oid));
316+
} else {
317+
stream_blob(oid);
318+
}
311319
}
312320
else {
313321
enum object_type type;

0 commit comments

Comments
 (0)