diff mbox series

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

Message ID 20210410152140.3525040-2-sandals@crustytoothpaste.net (mailing list archive)
State Superseded
Headers show
Series SHA-256 / SHA-1 interop, part 1 | expand

Commit Message

brian m. carlson April 10, 2021, 3:21 p.m. UTC
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 <sandals@crustytoothpaste.net>
---
 object-file.c  | 16 ++++++++++++++--
 object-store.h |  3 +++
 2 files changed, 17 insertions(+), 2 deletions(-)

Comments

Denton Liu April 15, 2021, 8:55 a.m. UTC | #1
Hi brian,

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

s/sha1-file/object-file/

I can see that you've waited a while to send this series ;)

On Sat, Apr 10, 2021 at 03:21:26PM +0000, 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 <sandals@crustytoothpaste.net>
> ---
>  object-file.c  | 16 ++++++++++++++--
>  object-store.h |  3 +++
>  2 files changed, 17 insertions(+), 2 deletions(-)
brian m. carlson April 15, 2021, 11:03 p.m. UTC | #2
On 2021-04-15 at 08:55:52, Denton Liu wrote:
> Hi brian,
> 
> > Subject: [PATCH 01/15] sha1-file: allow hashing objects literally with any algorithm
> 
> s/sha1-file/object-file/

Good point.  Will fix.

> I can see that you've waited a while to send this series ;)

Yes, I started writing this series after I finished the origin SHA-256
series.  That had been completed for some time, but it took time to send
out all the patches, so it's been sitting in my repository for a while.

I had hoped that things would be a little simpler than they had been and
I could have been finished by now, but I still have some 150 tests to
fix, so I decided to send out some initial patches to keep things
moving.
Ævar Arnfjörð Bjarmason April 16, 2021, 3:04 p.m. UTC | #3
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 <sandals@crustytoothpaste.net>
> ---
>  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().
Junio C Hamano April 16, 2021, 6:55 p.m. UTC | #4
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> 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().

Makes sense.
diff mbox series

Patch

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.