commit: convert register_commit_graft to handle arbitrary repositories
[gitweb.git] / commit.c
index 2cd5b8a0b96fa01b64b64f98db5bb43b60f84fa7..8a2ab53fc67355ac8631b427e85f617cd90261ba 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -104,38 +104,38 @@ static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
        return commit_graft_table[index]->oid.hash;
 }
 
-#define commit_graft_pos(r, s) commit_graft_pos_##r(s)
-static int commit_graft_pos_the_repository(const unsigned char *sha1)
+static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
 {
-       return sha1_pos(sha1, the_repository->parsed_objects->grafts,
-                       the_repository->parsed_objects->grafts_nr,
+       return sha1_pos(sha1, r->parsed_objects->grafts,
+                       r->parsed_objects->grafts_nr,
                        commit_graft_sha1_access);
 }
 
-int register_commit_graft(struct commit_graft *graft, int ignore_dups)
+int register_commit_graft(struct repository *r, struct commit_graft *graft,
+                         int ignore_dups)
 {
-       int pos = commit_graft_pos(the_repository, graft->oid.hash);
+       int pos = commit_graft_pos(r, graft->oid.hash);
 
        if (0 <= pos) {
                if (ignore_dups)
                        free(graft);
                else {
-                       free(the_repository->parsed_objects->grafts[pos]);
-                       the_repository->parsed_objects->grafts[pos] = graft;
+                       free(r->parsed_objects->grafts[pos]);
+                       r->parsed_objects->grafts[pos] = graft;
                }
                return 1;
        }
        pos = -pos - 1;
-       ALLOC_GROW(the_repository->parsed_objects->grafts,
-                  the_repository->parsed_objects->grafts_nr + 1,
-                  the_repository->parsed_objects->grafts_alloc);
-       the_repository->parsed_objects->grafts_nr++;
-       if (pos < the_repository->parsed_objects->grafts_nr)
-               memmove(the_repository->parsed_objects->grafts + pos + 1,
-                       the_repository->parsed_objects->grafts + pos,
-                       (the_repository->parsed_objects->grafts_nr - pos - 1) *
-                       sizeof(*the_repository->parsed_objects->grafts));
-       the_repository->parsed_objects->grafts[pos] = graft;
+       ALLOC_GROW(r->parsed_objects->grafts,
+                  r->parsed_objects->grafts_nr + 1,
+                  r->parsed_objects->grafts_alloc);
+       r->parsed_objects->grafts_nr++;
+       if (pos < r->parsed_objects->grafts_nr)
+               memmove(r->parsed_objects->grafts + pos + 1,
+                       r->parsed_objects->grafts + pos,
+                       (r->parsed_objects->grafts_nr - pos - 1) *
+                       sizeof(*r->parsed_objects->grafts));
+       r->parsed_objects->grafts[pos] = graft;
        return 0;
 }
 
@@ -177,7 +177,8 @@ struct commit_graft *read_graft_line(struct strbuf *line)
        return NULL;
 }
 
-static int read_graft_file(const char *graft_file)
+#define read_graft_file(r, f) read_graft_file_##r(f)
+static int read_graft_file_the_repository(const char *graft_file)
 {
        FILE *fp = fopen_or_warn(graft_file, "r");
        struct strbuf buf = STRBUF_INIT;
@@ -188,7 +189,7 @@ static int read_graft_file(const char *graft_file)
                struct commit_graft *graft = read_graft_line(&buf);
                if (!graft)
                        continue;
-               if (register_commit_graft(graft, 1))
+               if (register_commit_graft(the_repository, graft, 1))
                        error("duplicate graft data: %s", buf.buf);
        }
        fclose(fp);
@@ -196,7 +197,8 @@ static int read_graft_file(const char *graft_file)
        return 0;
 }
 
-static void prepare_commit_graft(void)
+#define prepare_commit_graft(r) prepare_commit_graft_##r()
+static void prepare_commit_graft_the_repository(void)
 {
        static int commit_graft_prepared;
        char *graft_file;
@@ -204,16 +206,16 @@ static void prepare_commit_graft(void)
        if (commit_graft_prepared)
                return;
        graft_file = get_graft_file();
-       read_graft_file(graft_file);
+       read_graft_file(the_repository, graft_file);
        /* make sure shallows are read */
-       is_repository_shallow();
+       is_repository_shallow(the_repository);
        commit_graft_prepared = 1;
 }
 
-struct commit_graft *lookup_commit_graft(const struct object_id *oid)
+struct commit_graft *lookup_commit_graft_the_repository(const struct object_id *oid)
 {
        int pos;
-       prepare_commit_graft();
+       prepare_commit_graft(the_repository);
        pos = commit_graft_pos(the_repository, oid->hash);
        if (pos < 0)
                return NULL;
@@ -357,7 +359,7 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
        bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
        pptr = &item->parents;
 
-       graft = lookup_commit_graft(&item->object.oid);
+       graft = lookup_commit_graft(the_repository, &item->object.oid);
        while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
                struct commit *new_parent;