Skip to content

Commit 91969fd

Browse files
pks-tgitster
authored andcommitted
odb: return newly created in-memory sources
Callers have no trivial way to obtain the newly created object database source when adding it to the in-memory list of alternates. While not yet needed anywhere, a subsequent commit will want to obtain that pointer. Refactor the function to return the source to make it easily accessible. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 870ed81 commit 91969fd

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

odb.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,22 @@ static void read_info_alternates(struct object_database *odb,
139139
const char *relative_base,
140140
int depth);
141141

142-
static int link_alt_odb_entry(struct object_database *odb,
143-
const struct strbuf *entry,
144-
const char *relative_base,
145-
int depth,
146-
const char *normalized_objdir)
142+
static struct odb_source *link_alt_odb_entry(struct object_database *odb,
143+
const char *entry,
144+
const char *relative_base,
145+
int depth,
146+
const char *normalized_objdir)
147147
{
148-
struct odb_source *alternate;
148+
struct odb_source *alternate = NULL;
149149
struct strbuf pathbuf = STRBUF_INIT;
150150
struct strbuf tmp = STRBUF_INIT;
151151
khiter_t pos;
152-
int ret = -1;
153152

154-
if (!is_absolute_path(entry->buf) && relative_base) {
153+
if (!is_absolute_path(entry) && relative_base) {
155154
strbuf_realpath(&pathbuf, relative_base, 1);
156155
strbuf_addch(&pathbuf, '/');
157156
}
158-
strbuf_addbuf(&pathbuf, entry);
157+
strbuf_addstr(&pathbuf, entry);
159158

160159
if (!strbuf_realpath(&tmp, pathbuf.buf, 0)) {
161160
error(_("unable to normalize alternate object path: %s"),
@@ -189,11 +188,11 @@ static int link_alt_odb_entry(struct object_database *odb,
189188

190189
/* recursively add alternates */
191190
read_info_alternates(odb, alternate->path, depth + 1);
192-
ret = 0;
191+
193192
error:
194193
strbuf_release(&tmp);
195194
strbuf_release(&pathbuf);
196-
return ret;
195+
return alternate;
197196
}
198197

199198
static const char *parse_alt_odb_entry(const char *string,
@@ -246,7 +245,7 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
246245
alt = parse_alt_odb_entry(alt, sep, &entry);
247246
if (!entry.len)
248247
continue;
249-
link_alt_odb_entry(odb, &entry,
248+
link_alt_odb_entry(odb, entry.buf,
250249
relative_base, depth, objdirbuf.buf);
251250
}
252251
strbuf_release(&entry);
@@ -316,17 +315,23 @@ void odb_add_to_alternates_file(struct object_database *odb,
316315
free(alts);
317316
}
318317

319-
void odb_add_to_alternates_memory(struct object_database *odb,
320-
const char *reference)
318+
struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
319+
const char *reference)
321320
{
321+
struct odb_source *alternate;
322+
char *objdir;
323+
322324
/*
323325
* Make sure alternates are initialized, or else our entry may be
324326
* overwritten when they are.
325327
*/
326328
odb_prepare_alternates(odb);
327329

328-
link_alt_odb_entries(odb, reference,
329-
'\n', NULL, 0);
330+
objdir = real_pathdup(odb->sources->path, 1);
331+
alternate = link_alt_odb_entry(odb, reference, NULL, 0, objdir);
332+
333+
free(objdir);
334+
return alternate;
330335
}
331336

332337
struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,

odb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ void odb_add_to_alternates_file(struct object_database *odb,
265265
* recursive alternates it points to), but do not modify the on-disk alternates
266266
* file.
267267
*/
268-
void odb_add_to_alternates_memory(struct object_database *odb,
269-
const char *dir);
268+
struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
269+
const char *dir);
270270

271271
/*
272272
* Read an object from the database. Returns the object data and assigns object

0 commit comments

Comments
 (0)