diff mbox series

[5/7] commit.c: don't use deref_tag() -> object_as_type()

Message ID patch-5.7-754d5ae267-20210409T083436Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series object.c: add and use "is expected" utility function + object_as_type() use | expand

Commit Message

Ævar Arnfjörð Bjarmason April 9, 2021, 8:50 a.m. UTC
Change a use of the object_as_type() function introduced in
8ff226a9d5e (add object_as_type helper for casting objects,
2014-07-13) to instead assume that we're not dealing with OBJ_NONE (or
OBJ_BAD) from deref_tag().

This makes this code easier to read, as the reader isn't wondering why
the function would need to deal with that. We're simply doing a check
of OBJ_{COMMIT,TREE,BLOB,TAG} here, not the bare-bones initialization
object_as_type() might be called on to do.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
 commit.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
diff mbox series


diff --git a/commit.c b/commit.c
index b370100367..437a8b8548 100644
--- a/commit.c
+++ b/commit.c
@@ -31,13 +31,19 @@  const char *commit_type = "commit";
 struct commit *lookup_commit_reference_gently(struct repository *r,
 		const struct object_id *oid, int quiet)
-	struct object *obj = deref_tag(r,
-				       parse_object(r, oid),
-				       NULL, 0);
+	struct object *tmp = parse_object(r, oid);
+	struct object *obj = deref_tag(r, tmp, NULL, 0);
 	if (!obj)
 		return NULL;
-	return object_as_type(obj, OBJ_COMMIT, quiet);
+	if (obj->type != OBJ_COMMIT) {
+		enum object_type want = OBJ_COMMIT;
+		if (!quiet)
+			oid_is_type_or_error(oid, OBJ_COMMIT, &want);
+		return NULL;
+	}
+	return (struct commit *)obj;
 struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)