]> git.ipfire.org Git - thirdparty/git.git/blobdiff - sha1_file.c
sha1_file: allow prepare_alt_odb to handle arbitrary repositories
[thirdparty/git.git] / sha1_file.c
index 097c372d030e42da75f40643fc74efcfc7fe2a4e..04118f331c78ee0904b44bcc3496a2ead851e399 100644 (file)
@@ -390,9 +390,11 @@ static int alt_odb_usable(struct raw_object_store *o,
  * SHA1, an extra slash for the first level indirection, and the
  * terminating NUL.
  */
-static void read_info_alternates(const char * relative_base, int depth);
-static int link_alt_odb_entry(const char *entry, const char *relative_base,
-       int depth, const char *normalized_objdir)
+static void read_info_alternates(struct repository *r,
+                                const char *relative_base,
+                                int depth);
+static int link_alt_odb_entry(struct repository *r, const char *entry,
+       const char *relative_base, int depth, const char *normalized_objdir)
 {
        struct alternate_object_database *ent;
        struct strbuf pathbuf = STRBUF_INIT;
@@ -417,7 +419,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
        while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
                strbuf_setlen(&pathbuf, pathbuf.len - 1);
 
-       if (!alt_odb_usable(the_repository->objects, &pathbuf, normalized_objdir)) {
+       if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
                strbuf_release(&pathbuf);
                return -1;
        }
@@ -425,12 +427,12 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
        ent = alloc_alt_odb(pathbuf.buf);
 
        /* add the alternate entry */
-       *the_repository->objects->alt_odb_tail = ent;
-       the_repository->objects->alt_odb_tail = &(ent->next);
+       *r->objects->alt_odb_tail = ent;
+       r->objects->alt_odb_tail = &(ent->next);
        ent->next = NULL;
 
        /* recursively add alternates */
-       read_info_alternates(pathbuf.buf, depth + 1);
+       read_info_alternates(r, pathbuf.buf, depth + 1);
 
        strbuf_release(&pathbuf);
        return 0;
@@ -465,8 +467,8 @@ static const char *parse_alt_odb_entry(const char *string,
        return end;
 }
 
-static void link_alt_odb_entries(const char *alt, int sep,
-                                const char *relative_base, int depth)
+static void link_alt_odb_entries(struct repository *r, const char *alt,
+                                int sep, const char *relative_base, int depth)
 {
        struct strbuf objdirbuf = STRBUF_INIT;
        struct strbuf entry = STRBUF_INIT;
@@ -480,7 +482,7 @@ static void link_alt_odb_entries(const char *alt, int sep,
                return;
        }
 
-       strbuf_add_absolute_path(&objdirbuf, get_object_directory());
+       strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir);
        if (strbuf_normalize_path(&objdirbuf) < 0)
                die("unable to normalize object directory: %s",
                    objdirbuf.buf);
@@ -489,13 +491,16 @@ static void link_alt_odb_entries(const char *alt, int sep,
                alt = parse_alt_odb_entry(alt, sep, &entry);
                if (!entry.len)
                        continue;
-               link_alt_odb_entry(entry.buf, relative_base, depth, objdirbuf.buf);
+               link_alt_odb_entry(r, entry.buf,
+                                  relative_base, depth, objdirbuf.buf);
        }
        strbuf_release(&entry);
        strbuf_release(&objdirbuf);
 }
 
-static void read_info_alternates(const char * relative_base, int depth)
+static void read_info_alternates(struct repository *r,
+                                const char *relative_base,
+                                int depth)
 {
        char *path;
        struct strbuf buf = STRBUF_INIT;
@@ -507,7 +512,7 @@ static void read_info_alternates(const char * relative_base, int depth)
                return;
        }
 
-       link_alt_odb_entries(buf.buf, '\n', relative_base, depth);
+       link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
        strbuf_release(&buf);
        free(path);
 }
@@ -561,7 +566,8 @@ void add_to_alternates_file(const char *reference)
                if (commit_lock_file(&lock))
                        die_errno("unable to move new alternates file into place");
                if (the_repository->objects->alt_odb_tail)
-                       link_alt_odb_entries(reference, '\n', NULL, 0);
+                       link_alt_odb_entries(the_repository, reference,
+                                            '\n', NULL, 0);
        }
        free(alts);
 }
@@ -572,9 +578,10 @@ void add_to_alternates_memory(const char *reference)
         * Make sure alternates are initialized, or else our entry may be
         * overwritten when they are.
         */
-       prepare_alt_odb();
+       prepare_alt_odb(the_repository);
 
-       link_alt_odb_entries(reference, '\n', NULL, 0);
+       link_alt_odb_entries(the_repository, reference,
+                            '\n', NULL, 0);
 }
 
 /*
@@ -657,7 +664,7 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
        struct alternate_object_database *ent;
        int r = 0;
 
-       prepare_alt_odb();
+       prepare_alt_odb(the_repository);
        for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) {
                r = fn(ent, cb);
                if (r)
@@ -666,17 +673,15 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
        return r;
 }
 
-void prepare_alt_odb(void)
+void prepare_alt_odb(struct repository *r)
 {
-       if (the_repository->objects->alt_odb_tail)
+       if (r->objects->alt_odb_tail)
                return;
 
-       the_repository->objects->alt_odb_tail =
-                       &the_repository->objects->alt_odb_list;
-       link_alt_odb_entries(the_repository->objects->alternate_db,
-                            PATH_SEP, NULL, 0);
+       r->objects->alt_odb_tail = &r->objects->alt_odb_list;
+       link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
 
-       read_info_alternates(get_object_directory(), 0);
+       read_info_alternates(r, r->objects->objectdir, 0);
 }
 
 /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
@@ -716,7 +721,7 @@ static int check_and_freshen_local(const unsigned char *sha1, int freshen)
 static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
 {
        struct alternate_object_database *alt;
-       prepare_alt_odb();
+       prepare_alt_odb(the_repository);
        for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
                const char *path = alt_sha1_path(alt, sha1);
                if (check_and_freshen_file(path, freshen))
@@ -875,7 +880,7 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
        if (!lstat(*path, st))
                return 0;
 
-       prepare_alt_odb();
+       prepare_alt_odb(the_repository);
        errno = ENOENT;
        for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
                *path = alt_sha1_path(alt, sha1);
@@ -906,7 +911,7 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
                return fd;
        most_interesting_errno = errno;
 
-       prepare_alt_odb();
+       prepare_alt_odb(the_repository);
        for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
                *path = alt_sha1_path(alt, sha1);
                fd = git_open(*path);