diff mbox series

[v2,06/10] blob.c: remove parse_blob_buffer()

Message ID patch-06.10-02c8d2a9ba-20210420T124428Z-avarab@gmail.com (mailing list archive)
State New
Headers show
Series object.c et al: tests, small bug fixes etc. | expand

Commit Message

Ævar Arnfjörð Bjarmason April 20, 2021, 12:50 p.m. UTC
As noted in the comment introduced in 837d395a5c0 (Replace
parse_blob() with an explanatory comment, 2010-01-18) the old
parse_blob() function and the parse_blob_buffer() existed to provide
consistency in the API.

See bd2c39f58f9 ([PATCH] don't load and decompress objects twice with
parse_object(), 2005-05-06) for the introduction of
parse_blob_buffer().

We're not going to parse blobs like we "parse" commits, trees or
tags. So we should not have the parse_blob_buffer() take arguments
that pretends that we do. Its only use is to set the "parsed" flag.

So let's entirely remove the function, and use our newly created
create_blob() for the allocation. We can then set the "parsed" flag
directly in parse_object_buffer() and parse_object() instead.

At this point I could move the comment added in 837d395a5c0 to one or
both of those object.c function, but let's just delete it instead. I
think it's obvious from the flow of the code what's going on
here. Setting the parsed flag no longer happens at a distance, so why
we're doing it isn't unclear anymore.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 blob.c   |  8 +-------
 blob.h   | 12 +-----------
 object.c | 11 +++++++----
 3 files changed, 9 insertions(+), 22 deletions(-)

Comments

Junio C Hamano April 29, 2021, 4:51 a.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> -int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
> -{
> -	item->object.parsed = 1;
> -	return 0;
> -}

Understandable.

> diff --git a/object.c b/object.c
> index 78343781ae..f4e419e5c3 100644
> --- a/object.c
> +++ b/object.c
> @@ -195,8 +195,7 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
>  	if (type == OBJ_BLOB) {
>  		struct blob *blob = lookup_blob(r, oid);
>  		if (blob) {
> -			if (parse_blob_buffer(blob, buffer, size))
> -				return NULL;
> +			blob->object.parsed = 1;
>  			obj = &blob->object;
>  		}

Understandable, too.

>  	} else if (type == OBJ_TREE) {
> @@ -262,12 +261,16 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
>  	if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
>  	    (!obj && repo_has_object_file(r, oid) &&
>  	     oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
> +		if (!obj) {
> +			struct blob *blob = create_blob(r, oid);
> +			obj = &blob->object;
> +		}

I do not recall this change explained or justified in the log
message.  What am I missing?

>  		if (check_object_signature(r, repl, NULL, 0, NULL) < 0) {
>  			error(_("hash mismatch %s"), oid_to_hex(oid));
>  			return NULL;
>  		}
> -		parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
> -		return lookup_object(r, oid);
> +		obj->parsed = 1;
> +		return obj;

Likewise.  Why isn't it just a call to lookup_blob() followed by
setting of its .parsed bit?  

In other words, it is not clear why we need to expose create_blob().

>  	}
>  
>  	buffer = repo_read_object_file(r, oid, &type, &size);
diff mbox series

Patch

diff --git a/blob.c b/blob.c
index d98b6badc7..1308299eab 100644
--- a/blob.c
+++ b/blob.c
@@ -5,7 +5,7 @@ 
 
 const char *blob_type = "blob";
 
-static struct blob *create_blob(struct repository *r, const struct object_id *oid)
+struct blob *create_blob(struct repository *r, const struct object_id *oid)
 {
 	return create_object(r, oid, alloc_blob_node(r));
 }
@@ -17,9 +17,3 @@  struct blob *lookup_blob(struct repository *r, const struct object_id *oid)
 		return create_blob(r, oid);
 	return object_as_type(obj, OBJ_BLOB, 0);
 }
-
-int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
-{
-	item->object.parsed = 1;
-	return 0;
-}
diff --git a/blob.h b/blob.h
index 1664872055..6e6b23a769 100644
--- a/blob.h
+++ b/blob.h
@@ -9,17 +9,7 @@  struct blob {
 	struct object object;
 };
 
+struct blob *create_blob(struct repository *r, const struct object_id *oid);
 struct blob *lookup_blob(struct repository *r, const struct object_id *oid);
 
-int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size);
-
-/**
- * Blobs do not contain references to other objects and do not have
- * structured data that needs parsing. However, code may use the
- * "parsed" bit in the struct object for a blob to determine whether
- * its content has been found to actually be available, so
- * parse_blob_buffer() is used (by object.c) to flag that the object
- * has been read successfully from the database.
- **/
-
 #endif /* BLOB_H */
diff --git a/object.c b/object.c
index 78343781ae..f4e419e5c3 100644
--- a/object.c
+++ b/object.c
@@ -195,8 +195,7 @@  struct object *parse_object_buffer(struct repository *r, const struct object_id
 	if (type == OBJ_BLOB) {
 		struct blob *blob = lookup_blob(r, oid);
 		if (blob) {
-			if (parse_blob_buffer(blob, buffer, size))
-				return NULL;
+			blob->object.parsed = 1;
 			obj = &blob->object;
 		}
 	} else if (type == OBJ_TREE) {
@@ -262,12 +261,16 @@  struct object *parse_object(struct repository *r, const struct object_id *oid)
 	if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
 	    (!obj && repo_has_object_file(r, oid) &&
 	     oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
+		if (!obj) {
+			struct blob *blob = create_blob(r, oid);
+			obj = &blob->object;
+		}
 		if (check_object_signature(r, repl, NULL, 0, NULL) < 0) {
 			error(_("hash mismatch %s"), oid_to_hex(oid));
 			return NULL;
 		}
-		parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
-		return lookup_object(r, oid);
+		obj->parsed = 1;
+		return obj;
 	}
 
 	buffer = repo_read_object_file(r, oid, &type, &size);