odb: write alternates via sources

Refactor writing of alternates so that the actual business logic is structured around the object database source we want to write the alternate to. Same as with the preceding commit, this will eventually allow us to have different logic for writing alternates depending on the backend used. Note that after the refactoring we start to call `odb_add_alternate_recursively()` unconditionally. This is fine though as we know to skip adding sources that are tracked already. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 11, 2025 at 10:30 UTC 221a877d4785030e07d20977418609257fd606d8
1 file changed +35 -16
odb.c
+35 -16
@@ -271,25 +271,28 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
271 return alternate;
272 }
273
274 -void odb_add_to_alternates_file(struct object_database *odb,
275 - const char *dir)
274 +static int odb_source_write_alternate(struct odb_source *source,
275 + const char *alternate)
276 {
277 struct lock_file lock = LOCK_INIT;
278 - char *alts = repo_git_path(odb->repo, "objects/info/alternates");
278 + char *path = xstrfmt("%s/%s", source->path, "info/alternates");
279 FILE *in, *out;
280 int found = 0;
281 + int ret;
282
282 - hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
283 + hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR);
284 out = fdopen_lock_file(&lock, "w");
284 - if (!out)
285 - die_errno(_("unable to fdopen alternates lockfile"));
285 + if (!out) {
286 + ret = error_errno(_("unable to fdopen alternates lockfile"));
287 + goto out;
288 + }
289
287 - in = fopen(alts, "r");
290 + in = fopen(path, "r");
291 if (in) {
292 struct strbuf line = STRBUF_INIT;
293
294 while (strbuf_getline(&line, in) != EOF) {
292 - if (!strcmp(dir, line.buf)) {
295 + if (!strcmp(alternate, line.buf)) {
296 found = 1;
297 break;
298 }
@@ -298,20 +301,36 @@ void odb_add_to_alternates_file(struct object_database *odb,
301
302 strbuf_release(&line);
303 fclose(in);
304 + } else if (errno != ENOENT) {
305 + ret = error_errno(_("unable to read alternates file"));
306 + goto out;
307 }
302 - else if (errno != ENOENT)
303 - die_errno(_("unable to read alternates file"));
308
309 if (found) {
310 rollback_lock_file(&lock);
311 } else {
308 - fprintf_or_die(out, "%s\n", dir);
309 - if (commit_lock_file(&lock))
310 - die_errno(_("unable to move new alternates file into place"));
311 - if (odb->loaded_alternates)
312 - odb_add_alternate_recursively(odb, dir, 0);
312 + fprintf_or_die(out, "%s\n", alternate);
313 + if (commit_lock_file(&lock)) {
314 + ret = error_errno(_("unable to move new alternates file into place"));
315 + goto out;
316 + }
317 }
314 - free(alts);
318 +
319 + ret = 0;
320 +
321 +out:
322 + free(path);
323 + return ret;
324 +}
325 +
326 +void odb_add_to_alternates_file(struct object_database *odb,
327 + const char *dir)
328 +{
329 + int ret = odb_source_write_alternate(odb->sources, dir);
330 + if (ret < 0)
331 + die(NULL);
332 + if (odb->loaded_alternates)
333 + odb_add_alternate_recursively(odb, dir, 0);
334 }
335
336 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,