Skip to content

Commit 6e76c3a

Browse files
pks-tgitster
authored andcommitted
odb/source: make write_object() function pluggable
Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6a38b13 commit 6e76c3a

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

odb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,8 @@ int odb_write_object_ext(struct object_database *odb,
10051005
struct object_id *compat_oid,
10061006
unsigned flags)
10071007
{
1008-
return odb_source_loose_write_object(odb->sources, buf, len, type,
1009-
oid, compat_oid, flags);
1008+
return odb_source_write_object(odb->sources, buf, len, type,
1009+
oid, compat_oid, flags);
10101010
}
10111011

10121012
int odb_write_object_stream(struct object_database *odb,

odb/source-files.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ static int odb_source_files_freshen_object(struct odb_source *source,
9898
return 0;
9999
}
100100

101+
static int odb_source_files_write_object(struct odb_source *source,
102+
const void *buf, unsigned long len,
103+
enum object_type type,
104+
struct object_id *oid,
105+
struct object_id *compat_oid,
106+
unsigned flags)
107+
{
108+
return odb_source_loose_write_object(source, buf, len, type,
109+
oid, compat_oid, flags);
110+
}
111+
101112
struct odb_source_files *odb_source_files_new(struct object_database *odb,
102113
const char *path,
103114
bool local)
@@ -116,6 +127,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
116127
files->base.read_object_stream = odb_source_files_read_object_stream;
117128
files->base.for_each_object = odb_source_files_for_each_object;
118129
files->base.freshen_object = odb_source_files_freshen_object;
130+
files->base.write_object = odb_source_files_write_object;
119131

120132
/*
121133
* Ideally, we would only ever store absolute paths in the source. This

odb/source.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ODB_SOURCE_H
22
#define ODB_SOURCE_H
33

4+
#include "object.h"
5+
46
enum odb_source_type {
57
/*
68
* The "unknown" type, which should never be in use. This type mostly
@@ -198,6 +200,24 @@ struct odb_source {
198200
*/
199201
int (*freshen_object)(struct odb_source *source,
200202
const struct object_id *oid);
203+
204+
/*
205+
* This callback is expected to persist the given object into the
206+
* object source. In case the object already exists it shall be
207+
* freshened.
208+
*
209+
* The flags field is a combination of `WRITE_OBJECT` flags.
210+
*
211+
* The resulting object ID (and optionally the compatibility object ID)
212+
* shall be written into the out pointers. The callback is expected to
213+
* return 0 on success, a negative error code otherwise.
214+
*/
215+
int (*write_object)(struct odb_source *source,
216+
const void *buf, unsigned long len,
217+
enum object_type type,
218+
struct object_id *oid,
219+
struct object_id *compat_oid,
220+
unsigned flags);
201221
};
202222

203223
/*
@@ -320,4 +340,20 @@ static inline int odb_source_freshen_object(struct odb_source *source,
320340
return source->freshen_object(source, oid);
321341
}
322342

343+
/*
344+
* Write an object into the object database source. Returns 0 on success, a
345+
* negative error code otherwise. Populates the given out pointers for the
346+
* object ID and the compatibility object ID, if non-NULL.
347+
*/
348+
static inline int odb_source_write_object(struct odb_source *source,
349+
const void *buf, unsigned long len,
350+
enum object_type type,
351+
struct object_id *oid,
352+
struct object_id *compat_oid,
353+
unsigned flags)
354+
{
355+
return source->write_object(source, buf, len, type, oid,
356+
compat_oid, flags);
357+
}
358+
323359
#endif

0 commit comments

Comments
 (0)