Skip to content

Commit 7ae2363

Browse files
pks-tgitster
authored andcommitted
odb/source: make read_alternates() 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 fc7fb0e commit 7ae2363

4 files changed

Lines changed: 59 additions & 22 deletions

File tree

odb.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ static bool odb_is_source_usable(struct object_database *o, const char *path)
131131
return usable;
132132
}
133133

134-
static void parse_alternates(const char *string,
135-
int sep,
136-
const char *relative_base,
137-
struct strvec *out)
134+
void parse_alternates(const char *string,
135+
int sep,
136+
const char *relative_base,
137+
struct strvec *out)
138138
{
139139
struct strbuf pathbuf = STRBUF_INIT;
140140
struct strbuf buf = STRBUF_INIT;
@@ -198,24 +198,6 @@ static void parse_alternates(const char *string,
198198
strbuf_release(&buf);
199199
}
200200

201-
static void odb_source_read_alternates(struct odb_source *source,
202-
struct strvec *out)
203-
{
204-
struct strbuf buf = STRBUF_INIT;
205-
char *path;
206-
207-
path = xstrfmt("%s/info/alternates", source->path);
208-
if (strbuf_read_file(&buf, path, 1024) < 0) {
209-
warn_on_fopen_errors(path);
210-
free(path);
211-
return;
212-
}
213-
parse_alternates(buf.buf, '\n', source->path, out);
214-
215-
strbuf_release(&buf);
216-
free(path);
217-
}
218-
219201
static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
220202
const char *source,
221203
int depth)

odb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,9 @@ int odb_write_object_stream(struct object_database *odb,
500500
struct odb_write_stream *stream, size_t len,
501501
struct object_id *oid);
502502

503+
void parse_alternates(const char *string,
504+
int sep,
505+
const char *relative_base,
506+
struct strvec *out);
507+
503508
#endif /* ODB_H */

odb/source-files.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#include "abspath.h"
33
#include "chdir-notify.h"
44
#include "object-file.h"
5+
#include "odb.h"
56
#include "odb/source.h"
67
#include "odb/source-files.h"
78
#include "packfile.h"
9+
#include "strbuf.h"
810

911
static void odb_source_files_reparent(const char *name UNUSED,
1012
const char *old_cwd,
@@ -117,6 +119,25 @@ static int odb_source_files_write_object_stream(struct odb_source *source,
117119
return odb_source_loose_write_stream(source, stream, len, oid);
118120
}
119121

122+
static int odb_source_files_read_alternates(struct odb_source *source,
123+
struct strvec *out)
124+
{
125+
struct strbuf buf = STRBUF_INIT;
126+
char *path;
127+
128+
path = xstrfmt("%s/info/alternates", source->path);
129+
if (strbuf_read_file(&buf, path, 1024) < 0) {
130+
warn_on_fopen_errors(path);
131+
free(path);
132+
return 0;
133+
}
134+
parse_alternates(buf.buf, '\n', source->path, out);
135+
136+
strbuf_release(&buf);
137+
free(path);
138+
return 0;
139+
}
140+
120141
struct odb_source_files *odb_source_files_new(struct object_database *odb,
121142
const char *path,
122143
bool local)
@@ -137,6 +158,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
137158
files->base.freshen_object = odb_source_files_freshen_object;
138159
files->base.write_object = odb_source_files_write_object;
139160
files->base.write_object_stream = odb_source_files_write_object_stream;
161+
files->base.read_alternates = odb_source_files_read_alternates;
140162

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

odb/source.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct object_id;
5454
struct object_info;
5555
struct odb_read_stream;
5656
struct odb_write_stream;
57+
struct strvec;
5758

5859
/*
5960
* A callback function that can be used to iterate through objects. If given,
@@ -231,6 +232,19 @@ struct odb_source {
231232
int (*write_object_stream)(struct odb_source *source,
232233
struct odb_write_stream *stream, size_t len,
233234
struct object_id *oid);
235+
236+
/*
237+
* This callback is expected to read the list of alternate object
238+
* database sources connected to it and write them into the `strvec`.
239+
*
240+
* The result is expected to be paths to the alternates. All paths must
241+
* be resolved to absolute paths.
242+
*
243+
* The callback is expected to return 0 on success, a negative error
244+
* code otherwise.
245+
*/
246+
int (*read_alternates)(struct odb_source *source,
247+
struct strvec *out);
234248
};
235249

236250
/*
@@ -384,4 +398,18 @@ static inline int odb_source_write_object_stream(struct odb_source *source,
384398
return source->write_object_stream(source, stream, len, oid);
385399
}
386400

401+
/*
402+
* Read the list of alternative object database sources from the given backend
403+
* and populate the `strvec` with them. The listing is not recursive -- that
404+
* is, if any of the yielded alternate sources has alternates itself, those
405+
* will not be yielded as part of this function call.
406+
*
407+
* Return 0 on success, a negative error code otherwise.
408+
*/
409+
static inline int odb_source_read_alternates(struct odb_source *source,
410+
struct strvec *out)
411+
{
412+
return source->read_alternates(source, out);
413+
}
414+
387415
#endif

0 commit comments

Comments
 (0)