]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: write alternates via sources
authorPatrick Steinhardt <ps@pks.im>
Thu, 11 Dec 2025 09:30:17 +0000 (10:30 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Dec 2025 09:39:37 +0000 (18:39 +0900)
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>
odb.c

diff --git a/odb.c b/odb.c
index c5ba26b85f20e175523f461590e264a9cb4c83f4..cc7f8324655e08d38283f2f737138a5ae90199b0 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -271,25 +271,28 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
        return alternate;
 }
 
-void odb_add_to_alternates_file(struct object_database *odb,
-                               const char *dir)
+static int odb_source_write_alternate(struct odb_source *source,
+                                     const char *alternate)
 {
        struct lock_file lock = LOCK_INIT;
-       char *alts = repo_git_path(odb->repo, "objects/info/alternates");
+       char *path = xstrfmt("%s/%s", source->path, "info/alternates");
        FILE *in, *out;
        int found = 0;
+       int ret;
 
-       hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
+       hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR);
        out = fdopen_lock_file(&lock, "w");
-       if (!out)
-               die_errno(_("unable to fdopen alternates lockfile"));
+       if (!out) {
+               ret = error_errno(_("unable to fdopen alternates lockfile"));
+               goto out;
+       }
 
-       in = fopen(alts, "r");
+       in = fopen(path, "r");
        if (in) {
                struct strbuf line = STRBUF_INIT;
 
                while (strbuf_getline(&line, in) != EOF) {
-                       if (!strcmp(dir, line.buf)) {
+                       if (!strcmp(alternate, line.buf)) {
                                found = 1;
                                break;
                        }
@@ -298,20 +301,36 @@ void odb_add_to_alternates_file(struct object_database *odb,
 
                strbuf_release(&line);
                fclose(in);
+       } else if (errno != ENOENT) {
+               ret = error_errno(_("unable to read alternates file"));
+               goto out;
        }
-       else if (errno != ENOENT)
-               die_errno(_("unable to read alternates file"));
 
        if (found) {
                rollback_lock_file(&lock);
        } else {
-               fprintf_or_die(out, "%s\n", dir);
-               if (commit_lock_file(&lock))
-                       die_errno(_("unable to move new alternates file into place"));
-               if (odb->loaded_alternates)
-                       odb_add_alternate_recursively(odb, dir, 0);
+               fprintf_or_die(out, "%s\n", alternate);
+               if (commit_lock_file(&lock)) {
+                       ret = error_errno(_("unable to move new alternates file into place"));
+                       goto out;
+               }
        }
-       free(alts);
+
+       ret = 0;
+
+out:
+       free(path);
+       return ret;
+}
+
+void odb_add_to_alternates_file(struct object_database *odb,
+                               const char *dir)
+{
+       int ret = odb_source_write_alternate(odb->sources, dir);
+       if (ret < 0)
+               die(NULL);
+       if (odb->loaded_alternates)
+               odb_add_alternate_recursively(odb, dir, 0);
 }
 
 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,