Merge branch 'ms/submodule-foreach-fix'
[gitweb.git] / commit-graph.c
index 02e5f8c65155ac864b3dc369e666525fe68dff71..8cc1d1d6c3aff0842c42ecda5cc545c9eb085802 100644 (file)
@@ -267,8 +267,10 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
                last_chunk_offset = chunk_offset;
        }
 
-       if (verify_commit_graph_lite(graph))
+       if (verify_commit_graph_lite(graph)) {
+               free(graph);
                return NULL;
+       }
 
        return graph;
 }
@@ -359,10 +361,10 @@ int generation_numbers_enabled(struct repository *r)
        return !!first_generation;
 }
 
-void close_commit_graph(struct repository *r)
+void close_commit_graph(struct raw_object_store *o)
 {
-       free_commit_graph(r->objects->commit_graph);
-       r->objects->commit_graph = NULL;
+       free_commit_graph(o->commit_graph);
+       o->commit_graph = NULL;
 }
 
 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
@@ -397,6 +399,11 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
        item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
 }
 
+static inline void set_commit_tree(struct commit *c, struct tree *t)
+{
+       c->maybe_tree = t;
+}
+
 static int fill_commit_in_graph(struct repository *r,
                                struct commit *item,
                                struct commit_graph *g, uint32_t pos)
@@ -410,7 +417,7 @@ static int fill_commit_in_graph(struct repository *r,
        item->object.parsed = 1;
        item->graph_pos = pos;
 
-       item->maybe_tree = NULL;
+       set_commit_tree(item, NULL);
 
        date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
        date_low = get_be32(commit_data + g->hash_len + 12);
@@ -496,7 +503,7 @@ static struct tree *load_tree_for_commit(struct repository *r,
                                           GRAPH_DATA_WIDTH * (c->graph_pos);
 
        hashcpy(oid.hash, commit_data);
-       c->maybe_tree = lookup_tree(r, &oid);
+       set_commit_tree(c, lookup_tree(r, &oid));
 
        return c->maybe_tree;
 }
@@ -912,105 +919,60 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
        return 0;
 }
 
-int write_commit_graph(const char *obj_dir,
-                      struct string_list *pack_indexes,
-                      struct string_list *commit_hex,
-                      unsigned int flags)
+static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
+                                     struct string_list *commit_hex)
 {
-       struct write_commit_graph_context *ctx;
-       struct hashfile *f;
-       uint32_t i, count_distinct = 0;
-       char *graph_name = NULL;
-       struct lock_file lk = LOCK_INIT;
-       uint32_t chunk_ids[5];
-       uint64_t chunk_offsets[5];
-       int num_chunks;
-       struct commit_list *parent;
-       const unsigned hashsz = the_hash_algo->rawsz;
+       uint32_t i;
        struct strbuf progress_title = STRBUF_INIT;
-       int res = 0;
 
-       if (!commit_graph_compatible(the_repository))
-               return 0;
-
-       ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
-       ctx->r = the_repository;
-       ctx->obj_dir = obj_dir;
-       ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
-       ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
-
-       ctx->approx_nr_objects = approximate_object_count();
-       ctx->oids.alloc = ctx->approx_nr_objects / 32;
-
-       if (ctx->append) {
-               prepare_commit_graph_one(ctx->r, ctx->obj_dir);
-               if (ctx->r->objects->commit_graph)
-                       ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
+       if (ctx->report_progress) {
+               strbuf_addf(&progress_title,
+                           Q_("Finding commits for commit graph from %d ref",
+                              "Finding commits for commit graph from %d refs",
+                              commit_hex->nr),
+                           commit_hex->nr);
+               ctx->progress = start_delayed_progress(
+                                       progress_title.buf,
+                                       commit_hex->nr);
        }
+       for (i = 0; i < commit_hex->nr; i++) {
+               const char *end;
+               struct object_id oid;
+               struct commit *result;
 
-       if (ctx->oids.alloc < 1024)
-               ctx->oids.alloc = 1024;
-       ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
-
-       if (ctx->append && ctx->r->objects->commit_graph) {
-               struct commit_graph *g = ctx->r->objects->commit_graph;
-               for (i = 0; i < g->num_commits; i++) {
-                       const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
-                       hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
-               }
-       }
+               display_progress(ctx->progress, i + 1);
+               if (commit_hex->items[i].string &&
+                   parse_oid_hex(commit_hex->items[i].string, &oid, &end))
+                       continue;
 
-       if (pack_indexes) {
-               if ((res = fill_oids_from_packs(ctx, pack_indexes)))
-                       goto cleanup;
-       }
+               result = lookup_commit_reference_gently(ctx->r, &oid, 1);
 
-       if (commit_hex) {
-               if (ctx->report_progress) {
-                       strbuf_addf(&progress_title,
-                                   Q_("Finding commits for commit graph from %d ref",
-                                      "Finding commits for commit graph from %d refs",
-                                      commit_hex->nr),
-                                   commit_hex->nr);
-                       ctx->progress = start_delayed_progress(
-                                               progress_title.buf,
-                                               commit_hex->nr);
-               }
-               for (i = 0; i < commit_hex->nr; i++) {
-                       const char *end;
-                       struct object_id oid;
-                       struct commit *result;
-
-                       display_progress(ctx->progress, i + 1);
-                       if (commit_hex->items[i].string &&
-                           parse_oid_hex(commit_hex->items[i].string, &oid, &end))
-                               continue;
-
-                       result = lookup_commit_reference_gently(ctx->r, &oid, 1);
-
-                       if (result) {
-                               ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
-                               oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
-                               ctx->oids.nr++;
-                       }
+               if (result) {
+                       ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
+                       oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
+                       ctx->oids.nr++;
                }
-               stop_progress(&ctx->progress);
-               strbuf_reset(&progress_title);
        }
+       stop_progress(&ctx->progress);
+       strbuf_release(&progress_title);
+}
 
-       if (!pack_indexes && !commit_hex) {
-               if (ctx->report_progress)
-                       ctx->progress = start_delayed_progress(
-                               _("Finding commits for commit graph among packed objects"),
-                               ctx->approx_nr_objects);
-               for_each_packed_object(add_packed_commits, ctx,
-                                      FOR_EACH_OBJECT_PACK_ORDER);
-               if (ctx->progress_done < ctx->approx_nr_objects)
-                       display_progress(ctx->progress, ctx->approx_nr_objects);
-               stop_progress(&ctx->progress);
-       }
+static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
+{
+       if (ctx->report_progress)
+               ctx->progress = start_delayed_progress(
+                       _("Finding commits for commit graph among packed objects"),
+                       ctx->approx_nr_objects);
+       for_each_packed_object(add_packed_commits, ctx,
+                              FOR_EACH_OBJECT_PACK_ORDER);
+       if (ctx->progress_done < ctx->approx_nr_objects)
+               display_progress(ctx->progress, ctx->approx_nr_objects);
+       stop_progress(&ctx->progress);
+}
 
-       close_reachable(ctx);
+static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
+{
+       uint32_t i, count_distinct = 1;
 
        if (ctx->report_progress)
                ctx->progress = start_delayed_progress(
@@ -1018,7 +980,7 @@ int write_commit_graph(const char *obj_dir,
                        ctx->oids.nr);
        display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
        QSORT(ctx->oids.list, ctx->oids.nr, commit_compare);
-       count_distinct = 1;
+
        for (i = 1; i < ctx->oids.nr; i++) {
                display_progress(ctx->progress, i + 1);
                if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
@@ -1026,14 +988,13 @@ int write_commit_graph(const char *obj_dir,
        }
        stop_progress(&ctx->progress);
 
-       if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
-               error(_("the commit graph format cannot write %d commits"), count_distinct);
-               res = -1;
-               goto cleanup;
-       }
+       return count_distinct;
+}
 
-       ctx->commits.alloc = count_distinct;
-       ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
+static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
+{
+       uint32_t i;
+       struct commit_list *parent;
 
        ctx->num_extra_edges = 0;
        if (ctx->report_progress)
@@ -1059,24 +1020,25 @@ int write_commit_graph(const char *obj_dir,
                ctx->commits.nr++;
        }
        stop_progress(&ctx->progress);
+}
 
-       if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
-               error(_("too many commits to write graph"));
-               res = -1;
-               goto cleanup;
-       }
-
-       compute_generation_numbers(ctx);
-
-       num_chunks = ctx->num_extra_edges ? 4 : 3;
+static int write_commit_graph_file(struct write_commit_graph_context *ctx)
+{
+       uint32_t i;
+       struct hashfile *f;
+       struct lock_file lk = LOCK_INIT;
+       uint32_t chunk_ids[5];
+       uint64_t chunk_offsets[5];
+       const unsigned hashsz = the_hash_algo->rawsz;
+       struct strbuf progress_title = STRBUF_INIT;
+       int num_chunks = ctx->num_extra_edges ? 4 : 3;
 
        ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
        if (safe_create_leading_directories(ctx->graph_name)) {
                UNLEAK(ctx->graph_name);
                error(_("unable to create leading directories of %s"),
                        ctx->graph_name);
-               res = -1;
-               goto cleanup;
+               return -1;
        }
 
        hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
@@ -1131,12 +1093,90 @@ int write_commit_graph(const char *obj_dir,
        stop_progress(&ctx->progress);
        strbuf_release(&progress_title);
 
-       close_commit_graph(ctx->r);
+       close_commit_graph(ctx->r->objects);
        finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
        commit_lock_file(&lk);
 
+       return 0;
+}
+
+int write_commit_graph(const char *obj_dir,
+                      struct string_list *pack_indexes,
+                      struct string_list *commit_hex,
+                      unsigned int flags)
+{
+       struct write_commit_graph_context *ctx;
+       uint32_t i, count_distinct = 0;
+       int res = 0;
+
+       if (!commit_graph_compatible(the_repository))
+               return 0;
+
+       ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
+       ctx->r = the_repository;
+       ctx->obj_dir = obj_dir;
+       ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
+       ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
+
+       ctx->approx_nr_objects = approximate_object_count();
+       ctx->oids.alloc = ctx->approx_nr_objects / 32;
+
+       if (ctx->append) {
+               prepare_commit_graph_one(ctx->r, ctx->obj_dir);
+               if (ctx->r->objects->commit_graph)
+                       ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
+       }
+
+       if (ctx->oids.alloc < 1024)
+               ctx->oids.alloc = 1024;
+       ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
+
+       if (ctx->append && ctx->r->objects->commit_graph) {
+               struct commit_graph *g = ctx->r->objects->commit_graph;
+               for (i = 0; i < g->num_commits; i++) {
+                       const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
+                       hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
+               }
+       }
+
+       if (pack_indexes) {
+               if ((res = fill_oids_from_packs(ctx, pack_indexes)))
+                       goto cleanup;
+       }
+
+       if (commit_hex)
+               fill_oids_from_commit_hex(ctx, commit_hex);
+
+       if (!pack_indexes && !commit_hex)
+               fill_oids_from_all_packs(ctx);
+
+       close_reachable(ctx);
+
+       count_distinct = count_distinct_commits(ctx);
+
+       if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
+               error(_("the commit graph format cannot write %d commits"), count_distinct);
+               res = -1;
+               goto cleanup;
+       }
+
+       ctx->commits.alloc = count_distinct;
+       ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
+
+       copy_oids_to_commits(ctx);
+
+       if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
+               error(_("too many commits to write graph"));
+               res = -1;
+               goto cleanup;
+       }
+
+       compute_generation_numbers(ctx);
+
+       res = write_commit_graph_file(ctx);
+
 cleanup:
-       free(graph_name);
+       free(ctx->graph_name);
        free(ctx->commits.list);
        free(ctx->oids.list);
        free(ctx);
@@ -1239,7 +1279,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
 
                graph_commit = lookup_commit(r, &cur_oid);
-               odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
+               odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
                if (parse_commit_internal(odb_commit, 0, 0)) {
                        graph_report(_("failed to parse commit %s from object database for commit-graph"),
                                     oid_to_hex(&cur_oid));