Subject: Re: [PATCH 01/15] sha1-file: allow hashing objects literally with any algorithm

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Fri, 16 Apr 2021 17:04:52 +0200

On Sat, Apr 10 2021, brian m. carlson wrote:

> In order to perform suitable testing with multiple algorithms and
> interoperability, we'll need the ability to hash an object with a given
> algorithm. Introduce this capability for now only for objects which are
> hashed literally by adding a function which does this and changing a
> static function to accept an algorithm pointer.
>
> Signed-off-by: brian m. carlson <[email-masked]>
> ---
>  object-file.c  | 16 ++++++++++++++--
>  object-store.h |  3 +++
>  2 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/object-file.c b/object-file.c
> index 624af408cd..f5847ee20f 100644
> --- a/object-file.c
> +++ b/object-file.c
> @@ -1957,6 +1957,15 @@ int write_object_file(const void *buf, unsigned long len, const char *type,
>  int hash_object_file_literally(const void *buf, unsigned long len,
>  			       const char *type, struct object_id *oid,
>  			       unsigned flags)
> +{
> +	return hash_object_file_literally_algop(buf, len, type, oid, flags,
> +						the_hash_algo);
> +}
> +
> +int hash_object_file_literally_algop(const void *buf, unsigned long len,
> +				     const char *type, struct object_id *oid,
> +				     unsigned flags,
> +				     const struct git_hash_algo *algo)
>  {
>  	char *header;
>  	int hdrlen, status = 0;
> @@ -1964,11 +1973,14 @@ int hash_object_file_literally(const void *buf, unsigned long len,
>  	/* type string, SP, %lu of the length plus NUL must fit this */
>  	hdrlen = strlen(type) + MAX_HEADER_LEN;
>  	header = xmalloc(hdrlen);
> -	write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
> -				  &hdrlen);
> +	write_object_file_prepare(algo, buf, len, type, oid, header, &hdrlen);
>  
>  	if (!(flags & HASH_WRITE_OBJECT))
>  		goto cleanup;
> +	if (algo->format_id != the_hash_algo->format_id) {
> +		status = -1;
> +		goto cleanup;
> +	}
>  	if (freshen_packed_object(oid) || freshen_loose_object(oid))
>  		goto cleanup;
>  	status = write_loose_object(oid, header, hdrlen, buf, len, 0);
> diff --git a/object-store.h b/object-store.h
> index ec32c23dcb..f95d03a7f5 100644
> --- a/object-store.h
> +++ b/object-store.h
> @@ -221,6 +221,9 @@ int hash_object_file_literally(const void *buf, unsigned long len,
>  			       const char *type, struct object_id *oid,
>  			       unsigned flags);
>  
> +int hash_object_file_literally_algop(const void *buf, unsigned long len,
> +				     const char *type, struct object_id *oid,
> +				     unsigned flags, const struct git_hash_algo *algo);
>  /*
>   * Add an object file to the in-memory object store, without writing it
>   * to disk.

We only have one user of hash_object_file_literally(),
builtin/hash-object.c, let's just change the signature of
hash_object_file_literally() instead of adding a new function. This
leaves the tree with no direct user of hash_object_file_literally().