odb/source-inmemory: implement `write_object()` callback

Implement the `write_object()` callback function for the in-memory
source.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Patrick Steinhardt 2026-04-10 14:12:36 +02:00 committed by Junio C Hamano
parent 8d9c1e421c
commit f611f4ba41
2 changed files with 27 additions and 14 deletions

16
odb.c
View File

@ -733,24 +733,12 @@ int odb_pretend_object(struct object_database *odb,
void *buf, unsigned long len, enum object_type type,
struct object_id *oid)
{
struct cached_object_entry *co;
char *co_buf;

hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
if (odb_has_object(odb, oid, 0))
return 0;

ALLOC_GROW(odb->inmemory_objects->objects,
odb->inmemory_objects->objects_nr + 1,
odb->inmemory_objects->objects_alloc);
co = &odb->inmemory_objects->objects[odb->inmemory_objects->objects_nr++];
co->value.size = len;
co->value.type = type;
co_buf = xmalloc(len);
memcpy(co_buf, buf, len);
co->value.buf = co_buf;
oidcpy(&co->oid, oid);
return 0;
return odb_source_write_object(&odb->inmemory_objects->base,
buf, len, type, oid, NULL, 0);
}

void *odb_read_object(struct object_database *odb,

View File

@ -1,4 +1,5 @@
#include "git-compat-util.h"
#include "object-file.h"
#include "odb.h"
#include "odb/source-inmemory.h"
#include "odb/streaming.h"
@ -104,6 +105,29 @@ static int odb_source_inmemory_read_object_stream(struct odb_read_stream **out,
return 0;
}

static int odb_source_inmemory_write_object(struct odb_source *source,
const void *buf, unsigned long len,
enum object_type type,
struct object_id *oid,
struct object_id *compat_oid UNUSED,
enum odb_write_object_flags flags UNUSED)
{
struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
struct cached_object_entry *object;

hash_object_file(source->odb->repo->hash_algo, buf, len, type, oid);

ALLOC_GROW(inmemory->objects, inmemory->objects_nr + 1,
inmemory->objects_alloc);
object = &inmemory->objects[inmemory->objects_nr++];
object->value.size = len;
object->value.type = type;
object->value.buf = xmemdupz(buf, len);
oidcpy(&object->oid, oid);

return 0;
}

static void odb_source_inmemory_free(struct odb_source *source)
{
struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
@ -124,6 +148,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
source->base.free = odb_source_inmemory_free;
source->base.read_object_info = odb_source_inmemory_read_object_info;
source->base.read_object_stream = odb_source_inmemory_read_object_stream;
source->base.write_object = odb_source_inmemory_write_object;

return source;
}