summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@vercel.com>2023-02-23 13:56:00 +0000
committerEdward Thomson <ethomson@vercel.com>2023-02-23 13:56:00 +0000
commit7afc57c6527bbeda0a5b66b40f733f176e85edb4 (patch)
treed2cdae31e644e86695c0e067cc1d5b53941d3e85
parentc9387a61656cd666d76a0491f6839c1a32835ca0 (diff)
downloadlibgit2-ethomson/sha256_fix.tar.gz
-rw-r--r--include/git2/odb_backend.h2
-rw-r--r--include/git2/sys/odb_backend.h5
-rw-r--r--src/libgit2/odb.c10
-rw-r--r--src/libgit2/odb.h2
-rw-r--r--src/libgit2/odb_loose.c8
-rw-r--r--src/libgit2/odb_mempack.c3
-rw-r--r--src/libgit2/odb_pack.c4
-rw-r--r--tests/libgit2/odb/backend/backend_helpers.c3
-rw-r--r--tests/libgit2/odb/sorting.c5
9 files changed, 35 insertions, 7 deletions
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index 4d0559ec0..699f96fb8 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -171,8 +171,6 @@ struct git_odb_stream {
unsigned int mode;
void *hash_ctx;
- git_oid_t oid_type;
-
git_object_size_t declared_size;
git_object_size_t received_bytes;
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index c42abd370..2d6458bf6 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -26,6 +26,11 @@ GIT_BEGIN_DECL
*/
struct git_odb_backend {
unsigned int version;
+
+#ifdef GIT_EXPERIMENTAL_SHA256
+ git_oid_t oid_type;
+#endif
+
git_odb *odb;
/* read and read_prefix each return to libgit2 a buffer which
diff --git a/src/libgit2/odb.c b/src/libgit2/odb.c
index edf4f001f..d50c7df9b 100644
--- a/src/libgit2/odb.c
+++ b/src/libgit2/odb.c
@@ -587,6 +587,13 @@ static int add_backend_internal(
GIT_ERROR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
+#ifdef GIT_EXPERIMENTAL_SHA256
+ if (!backend->oid_type) {
+ git_error_set(GIT_ERROR_ODB, "backend oid type is not set");
+ return -1;
+ }
+#endif
+
/* Check if the backend is already owned by another ODB */
GIT_ASSERT(!backend->odb || backend->odb == odb);
@@ -1704,7 +1711,6 @@ int git_odb_open_wstream(
(error = hash_header(ctx, size, type)) < 0)
goto done;
- (*stream)->oid_type = db->options.oid_type;
(*stream)->hash_ctx = ctx;
(*stream)->declared_size = size;
(*stream)->received_bytes = 0;
@@ -1750,7 +1756,7 @@ int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
git_hash_final(out->id, stream->hash_ctx);
#ifdef GIT_EXPERIMENTAL_SHA256
- out->type = stream->oid_type;
+ out->type = stream->backend->oid_type;
#endif
if (git_odb__freshen(stream->backend->odb, out))
diff --git a/src/libgit2/odb.h b/src/libgit2/odb.h
index 7a712e202..6bdacd384 100644
--- a/src/libgit2/odb.h
+++ b/src/libgit2/odb.h
@@ -180,7 +180,7 @@ int git_odb__hashfile(
git_object_t object_type,
git_oid_t oid_type);
-GIT_EXTERN(int) git_odb__backend_loose(
+int git_odb__backend_loose(
git_odb_backend **out,
const char *objects_dir,
git_odb_backend_loose_options *opts);
diff --git a/src/libgit2/odb_loose.c b/src/libgit2/odb_loose.c
index d1abbe233..306cbf9ad 100644
--- a/src/libgit2/odb_loose.c
+++ b/src/libgit2/odb_loose.c
@@ -1179,14 +1179,20 @@ int git_odb__backend_loose(
backend = git__calloc(1, alloclen);
GIT_ERROR_CHECK_ALLOC(backend);
+ normalize_options(&backend->options, opts);
+
backend->parent.version = GIT_ODB_BACKEND_VERSION;
+
+#ifdef GIT_EXPERIMENTAL_SHA256
+ backend->parent.oid_type = backend->options.oid_type;
+#endif
+
backend->objects_dirlen = objects_dirlen;
memcpy(backend->objects_dir, objects_dir, objects_dirlen);
if (backend->objects_dir[backend->objects_dirlen - 1] != '/')
backend->objects_dir[backend->objects_dirlen++] = '/';
- normalize_options(&backend->options, opts);
backend->oid_hexsize = git_oid_hexsize(backend->options.oid_type);
backend->parent.read = &loose_backend__read;
diff --git a/src/libgit2/odb_mempack.c b/src/libgit2/odb_mempack.c
index 6f27f45f8..8c83fcee6 100644
--- a/src/libgit2/odb_mempack.c
+++ b/src/libgit2/odb_mempack.c
@@ -178,6 +178,9 @@ int git_mempack_new(git_odb_backend **out)
return -1;
db->parent.version = GIT_ODB_BACKEND_VERSION;
+#ifdef GIT_EXPERIMENTAL_SHA256
+ db->parent.oid_type = GIT_OID_SHA1;
+#endif
db->parent.read = &impl__read;
db->parent.write = &impl__write;
db->parent.read_header = &impl__read_header;
diff --git a/src/libgit2/odb_pack.c b/src/libgit2/odb_pack.c
index 1b1d122b0..b0a6ae097 100644
--- a/src/libgit2/odb_pack.c
+++ b/src/libgit2/odb_pack.c
@@ -883,6 +883,10 @@ static int pack_backend__alloc(
backend->parent.version = GIT_ODB_BACKEND_VERSION;
+#ifdef GIT_EXPERIMENTAL_SHA256
+ backend->parent.oid_type = backend->opts.oid_type;
+#endif
+
backend->parent.read = &pack_backend__read;
backend->parent.read_prefix = &pack_backend__read_prefix;
backend->parent.read_header = &pack_backend__read_header;
diff --git a/tests/libgit2/odb/backend/backend_helpers.c b/tests/libgit2/odb/backend/backend_helpers.c
index c1a0070d6..3a113da85 100644
--- a/tests/libgit2/odb/backend/backend_helpers.c
+++ b/tests/libgit2/odb/backend/backend_helpers.c
@@ -155,6 +155,9 @@ int build_fake_backend(
GIT_ERROR_CHECK_ALLOC(backend);
backend->parent.version = GIT_ODB_BACKEND_VERSION;
+#ifdef GIT_EXPERIMENTAL_SHA256
+ backend->parent.oid_type = GIT_OID_SHA1;
+#endif
backend->objects = objects;
diff --git a/tests/libgit2/odb/sorting.c b/tests/libgit2/odb/sorting.c
index ec4e3696b..a26b9404e 100644
--- a/tests/libgit2/odb/sorting.c
+++ b/tests/libgit2/odb/sorting.c
@@ -15,8 +15,11 @@ static git_odb_backend *new_backend(size_t position)
if (b == NULL)
return NULL;
- b->base.free = (void (*)(git_odb_backend *)) git__free;
b->base.version = GIT_ODB_BACKEND_VERSION;
+#ifdef GIT_EXPERIMENTAL_SHA256
+ b->base.oid_type = GIT_OID_SHA1;
+#endif
+ b->base.free = (void (*)(git_odb_backend *)) git__free;
b->position = position;
return (git_odb_backend *)b;
}