Thread (103 messages) flat view 103 messages, 6 authors, 2021-04-22
STALE1975d

Revision v2 of 2 in this series.

Revisions (2)
  1. v1 [diff vs current]
  2. v2 current

[PATCH v2 06/10] tag.c: use type_from_string_gently() when parsing tags

From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2021-04-20 13:00:40
Subsystem: the rest · Maintainer: Linus Torvalds

Change a series of strcmp() to instead use type_from_string_gently()
to get the integer type early, and then use that for comparison.

Signed-off-by: Ævar Arnfjörð Bjarmason <redacted>
---
 tag.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/tag.c b/tag.c
index ed7037256e..1bd81bf1d1 100644
--- a/tag.c
+++ b/tag.c
@@ -140,7 +140,7 @@ void release_tag_memory(struct tag *t)
 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
 {
 	struct object_id oid;
-	char type[20];
+	enum object_type type;
 	const char *bufptr = data;
 	const char *tail = bufptr + size;
 	const char *nl;
@@ -167,23 +167,24 @@ int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, u
 		return -1;
 	bufptr += 5;
 	nl = memchr(bufptr, '\n', tail - bufptr);
-	if (!nl || sizeof(type) <= (nl - bufptr))
+	if (!nl)
+		return -1;
+	type = type_from_string_gently(bufptr, nl - bufptr);
+	if (type < 0)
 		return -1;
-	memcpy(type, bufptr, nl - bufptr);
-	type[nl - bufptr] = '\0';
 	bufptr = nl + 1;
 
-	if (!strcmp(type, blob_type)) {
+	if (type == OBJ_BLOB) {
 		item->tagged = (struct object *)lookup_blob(r, &oid);
-	} else if (!strcmp(type, tree_type)) {
+	} else if (type == OBJ_TREE) {
 		item->tagged = (struct object *)lookup_tree(r, &oid);
-	} else if (!strcmp(type, commit_type)) {
+	} else if (type == OBJ_COMMIT) {
 		item->tagged = (struct object *)lookup_commit(r, &oid);
-	} else if (!strcmp(type, tag_type)) {
+	} else if (type == OBJ_TAG) {
 		item->tagged = (struct object *)lookup_tag(r, &oid);
 	} else {
 		return error("unknown tag type '%s' in %s",
-			     type, oid_to_hex(&item->object.oid));
+			     type_name(type), oid_to_hex(&item->object.oid));
 	}
 
 	if (!item->tagged)
-- 
2.31.1.723.ga5d7868e4a
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help