Commit 923364c58eaca0c54f09def3292dff848975906c

Edward Thomson 2021-09-30T08:40:29

futils: use a particular hash not a git_oid In `git_futils_readbuffer_updated`, always take a particular hash instead of a `git_oid`. This lets us change the checksum algorithm independently of `git_oid` usage.

diff --git a/src/futils.c b/src/futils.c
index 138fc2c..d28b231 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -177,13 +177,16 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
 }
 
 int git_futils_readbuffer_updated(
-	git_buf *out, const char *path, git_oid *checksum, int *updated)
+	git_buf *out,
+	const char *path,
+	unsigned char checksum[GIT_HASH_SHA1_SIZE],
+	int *updated)
 {
 	int error;
 	git_file fd;
 	struct stat st;
 	git_buf buf = GIT_BUF_INIT;
-	git_oid checksum_new;
+	unsigned char checksum_new[GIT_HASH_SHA1_SIZE];
 
 	GIT_ASSERT_ARG(out);
 	GIT_ASSERT_ARG(path && *path);
@@ -216,7 +219,7 @@ int git_futils_readbuffer_updated(
 	p_close(fd);
 
 	if (checksum) {
-		if ((error = git_hash_buf(checksum_new.id, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
+		if ((error = git_hash_buf(checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
 			git_buf_dispose(&buf);
 			return error;
 		}
@@ -224,7 +227,7 @@ int git_futils_readbuffer_updated(
 		/*
 		 * If we were given a checksum, we only want to use it if it's different
 		 */
-		if (!git_oid__cmp(checksum, &checksum_new)) {
+		if (!memcmp(checksum, checksum_new, GIT_HASH_SHA1_SIZE)) {
 			git_buf_dispose(&buf);
 			if (updated)
 				*updated = 0;
@@ -232,7 +235,7 @@ int git_futils_readbuffer_updated(
 			return 0;
 		}
 
-		git_oid_cpy(checksum, &checksum_new);
+		memcpy(checksum, checksum_new, GIT_HASH_SHA1_SIZE);
 	}
 
 	/*
diff --git a/src/futils.h b/src/futils.h
index 5893612..373cc30 100644
--- a/src/futils.h
+++ b/src/futils.h
@@ -14,7 +14,7 @@
 #include "path.h"
 #include "pool.h"
 #include "strmap.h"
-#include "oid.h"
+#include "hash.h"
 
 /**
  * Filebuffer methods
@@ -23,7 +23,10 @@
  */
 extern int git_futils_readbuffer(git_buf *obj, const char *path);
 extern int git_futils_readbuffer_updated(
-	git_buf *obj, const char *path, git_oid *checksum, int *updated);
+	git_buf *obj,
+	const char *path,
+	unsigned char checksum[GIT_HASH_SHA1_SIZE],
+	int *updated);
 extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len);
 
 /* Additional constants for `git_futils_writebuffer`'s `open_flags`.  We