]> git.ipfire.org Git - thirdparty/git.git/blobdiff - midx.c
midx: preliminary support for `--refs-snapshot`
[thirdparty/git.git] / midx.c
diff --git a/midx.c b/midx.c
index 5533361997b1dc5aacbafa210fdebafdfbe969ce..18a4f25aa35364f73018610a61f0f0617e597c3c 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -983,7 +983,43 @@ static void bitmap_show_commit(struct commit *commit, void *_data)
        data->commits[data->commits_nr++] = commit;
 }
 
+static int read_refs_snapshot(const char *refs_snapshot,
+                             struct rev_info *revs)
+{
+       struct strbuf buf = STRBUF_INIT;
+       struct object_id oid;
+       FILE *f = xfopen(refs_snapshot, "r");
+
+       while (strbuf_getline(&buf, f) != EOF) {
+               struct object *object;
+               int preferred = 0;
+               char *hex = buf.buf;
+               const char *end = NULL;
+
+               if (buf.len && *buf.buf == '+') {
+                       preferred = 1;
+                       hex = &buf.buf[1];
+               }
+
+               if (parse_oid_hex(hex, &oid, &end) < 0)
+                       die(_("could not parse line: %s"), buf.buf);
+               if (*end)
+                       die(_("malformed line: %s"), buf.buf);
+
+               object = parse_object_or_die(&oid, NULL);
+               if (preferred)
+                       object->flags |= NEEDS_BITMAP;
+
+               add_pending_object(revs, object, "");
+       }
+
+       fclose(f);
+       strbuf_release(&buf);
+       return 0;
+}
+
 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
+                                                   const char *refs_snapshot,
                                                    struct write_midx_context *ctx)
 {
        struct rev_info revs;
@@ -992,8 +1028,12 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
        cb.ctx = ctx;
 
        repo_init_revisions(the_repository, &revs, NULL);
-       setup_revisions(0, NULL, &revs, NULL);
-       for_each_ref(add_ref_to_pending, &revs);
+       if (refs_snapshot) {
+               read_refs_snapshot(refs_snapshot, &revs);
+       } else {
+               setup_revisions(0, NULL, &revs, NULL);
+               for_each_ref(add_ref_to_pending, &revs);
+       }
 
        /*
         * Skipping promisor objects here is intentional, since it only excludes
@@ -1022,6 +1062,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 
 static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
                             struct write_midx_context *ctx,
+                            const char *refs_snapshot,
                             unsigned flags)
 {
        struct packing_data pdata;
@@ -1033,7 +1074,7 @@ static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
 
        prepare_midx_packing_data(&pdata, ctx);
 
-       commits = find_commits_for_midx_bitmap(&commits_nr, ctx);
+       commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, ctx);
 
        /*
         * Build the MIDX-order index based on pdata.objects (which is already
@@ -1081,6 +1122,7 @@ static int write_midx_internal(const char *object_dir,
                               struct string_list *packs_to_include,
                               struct string_list *packs_to_drop,
                               const char *preferred_pack_name,
+                              const char *refs_snapshot,
                               unsigned flags)
 {
        char *midx_name;
@@ -1374,7 +1416,8 @@ static int write_midx_internal(const char *object_dir,
        if (flags & MIDX_WRITE_REV_INDEX)
                write_midx_reverse_index(midx_name, midx_hash, &ctx);
        if (flags & MIDX_WRITE_BITMAP) {
-               if (write_midx_bitmap(midx_name, midx_hash, &ctx, flags) < 0) {
+               if (write_midx_bitmap(midx_name, midx_hash, &ctx,
+                                     refs_snapshot, flags) < 0) {
                        error(_("could not write multi-pack bitmap"));
                        result = 1;
                        goto cleanup;
@@ -1409,19 +1452,21 @@ cleanup:
 
 int write_midx_file(const char *object_dir,
                    const char *preferred_pack_name,
+                   const char *refs_snapshot,
                    unsigned flags)
 {
        return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
-                                  flags);
+                                  refs_snapshot, flags);
 }
 
 int write_midx_file_only(const char *object_dir,
                         struct string_list *packs_to_include,
                         const char *preferred_pack_name,
+                        const char *refs_snapshot,
                         unsigned flags)
 {
        return write_midx_internal(object_dir, packs_to_include, NULL,
-                                  preferred_pack_name, flags);
+                                  preferred_pack_name, refs_snapshot, flags);
 }
 
 struct clear_midx_data {
@@ -1701,7 +1746,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
        free(count);
 
        if (packs_to_drop.nr) {
-               result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, flags);
+               result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
                m = NULL;
        }
 
@@ -1892,7 +1937,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
                goto cleanup;
        }
 
-       result = write_midx_internal(object_dir, NULL, NULL, NULL, flags);
+       result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
        m = NULL;
 
 cleanup: