diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 253cfb07fbd..d9082831bb2 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -217,8 +217,8 @@ static int mark_link(struct object *obj, int type, void *data, struct fsck_optio
if (!obj)
return -1;
- if (type != OBJ_ANY && obj->type != type)
- die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
+ if (type != OBJ_ANY)
+ oid_is_type_or_die(&obj->oid, obj->type, &type);
obj->flags |= FLAG_LINK;
return 0;@@ -240,10 +240,7 @@ static unsigned check_object(struct object *obj)
if (type == OBJ_BAD)
die(_("did not receive expected object %s"),
oid_to_hex(&obj->oid));
- if (type != obj->type)
- die(_("object %s: expected type %s, found %s"),
- oid_to_hex(&obj->oid),
- type_name(obj->type), type_name(type));
+ oid_is_type_or_die(&obj->oid, obj->type, &type);
obj->flags |= FLAG_CHECKED;
return 1;
}diff --git a/commit.c b/commit.c
index 6ccd774841c..54627b546c3 100644
--- a/commit.c
+++ b/commit.c
@@ -299,9 +299,7 @@ const void *repo_get_commit_buffer(struct repository *r,
if (!ret)
die("cannot read commit object %s",
oid_to_hex(&commit->object.oid));
- if (type != OBJ_COMMIT)
- die("expected commit for %s, got %s",
- oid_to_hex(&commit->object.oid), type_name(type));
+ oid_is_type_or_die(&commit->object.oid, OBJ_COMMIT, &type);
if (sizep)
*sizep = size;
}@@ -489,10 +487,10 @@ int repo_parse_commit_internal(struct repository *r,
return quiet_on_missing ? -1 :
error("Could not read %s",
oid_to_hex(&item->object.oid));
- if (type != OBJ_COMMIT) {
+ ret = oid_is_type_or_error(&item->object.oid, OBJ_COMMIT, &type);
+ if (ret) {
free(buffer);
- return error("Object %s not a commit",
- oid_to_hex(&item->object.oid));
+ return ret;
}
ret = parse_commit_buffer(r, item, buffer, size, 0);diff --git a/object.c b/object.c
index eebacc28847..819ee0faa26 100644
--- a/object.c
+++ b/object.c
@@ -28,6 +28,8 @@ static const char *object_type_strings[] = {
"tag", /* OBJ_TAG = 4 */
};
+static const char *oid_is_a_X_not_a_Y = N_("object %s is a %s, not a %s");
+
const char *type_name(unsigned int type)
{
if (type >= ARRAY_SIZE(object_type_strings))@@ -159,6 +161,36 @@ void *create_object(struct repository *r, const struct object_id *oid, void *o)
return obj;
}
+static int oid_is_type_or(const struct object_id *oid,
+ enum object_type want,
+ enum object_type type,
+ int err)
+{
+ if (want == type)
+ return 0;
+ if (err)
+ return error(_(oid_is_a_X_not_a_Y),
+ oid_to_hex(oid), type_name(type),
+ type_name(want));
+ else
+ die(_(oid_is_a_X_not_a_Y), oid_to_hex(oid),
+ type_name(type), type_name(want));
+}
+
+void oid_is_type_or_die(const struct object_id *oid,
+ enum object_type want,
+ enum object_type *type)
+{
+ oid_is_type_or(oid, want, *type, 0);
+}
+
+int oid_is_type_or_error(const struct object_id *oid,
+ enum object_type want,
+ enum object_type *type)
+{
+ return oid_is_type_or(oid, want, *type, 1);
+}
+
void *object_as_type(struct object *obj, enum object_type type, int quiet)
{
if (obj->type == type)@@ -172,7 +204,7 @@ void *object_as_type(struct object *obj, enum object_type type, int quiet)
}
else {
if (!quiet)
- error(_("object %s is a %s, not a %s"),
+ error(_(oid_is_a_X_not_a_Y),
oid_to_hex(&obj->oid),
type_name(obj->type), type_name(type));
return NULL;diff --git a/object.h b/object.h
index 5e7a523e858..d2d4a236d0e 100644
--- a/object.h
+++ b/object.h
@@ -124,6 +124,11 @@ void *create_object(struct repository *r, const struct object_id *oid, void *obj
void *object_as_type(struct object *obj, enum object_type type, int quiet);
+void oid_is_type_or_die(const struct object_id *oid, enum object_type want,
+ enum object_type *type);
+int oid_is_type_or_error(const struct object_id *oid, enum object_type want,
+ enum object_type *type);
+
/*
* Returns the object, having parsed it to find out what it is.
*
diff --git a/tree.c b/tree.c
index 4820d66a10c..d9b1c70b28a 100644
--- a/tree.c
+++ b/tree.c
@@ -219,6 +219,7 @@ int parse_tree_gently(struct tree *item, int quiet_on_missing)
enum object_type type;
void *buffer;
unsigned long size;
+ int ret;
if (item->object.parsed)
return 0;
@@ -227,10 +228,10 @@ int parse_tree_gently(struct tree *item, int quiet_on_missing)
return quiet_on_missing ? -1 :
error("Could not read %s",
oid_to_hex(&item->object.oid));
- if (type != OBJ_TREE) {
+ ret = oid_is_type_or_error(&item->object.oid, OBJ_TREE, &type);
+ if (ret) {
free(buffer);
- return error("Object %s not a tree",
- oid_to_hex(&item->object.oid));
+ return ret;
}
return parse_tree_buffer(item, buffer, size);
}--
2.31.0.rc1.210.g0f8085a843c