36d765e10a95708029ad7a003bf67cd0d9231838
   1#include "cache.h"
   2#include "config.h"
   3#include "git-compat-util.h"
   4#include "lockfile.h"
   5#include "pack.h"
   6#include "packfile.h"
   7#include "commit.h"
   8#include "object.h"
   9#include "revision.h"
  10#include "sha1-lookup.h"
  11#include "commit-graph.h"
  12
  13#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
  14#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
  15#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
  16#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
  17#define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
  18
  19#define GRAPH_DATA_WIDTH 36
  20
  21#define GRAPH_VERSION_1 0x1
  22#define GRAPH_VERSION GRAPH_VERSION_1
  23
  24#define GRAPH_OID_VERSION_SHA1 1
  25#define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
  26#define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
  27#define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
  28
  29#define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
  30#define GRAPH_PARENT_MISSING 0x7fffffff
  31#define GRAPH_EDGE_LAST_MASK 0x7fffffff
  32#define GRAPH_PARENT_NONE 0x70000000
  33
  34#define GRAPH_LAST_EDGE 0x80000000
  35
  36#define GRAPH_FANOUT_SIZE (4 * 256)
  37#define GRAPH_CHUNKLOOKUP_WIDTH 12
  38#define GRAPH_MIN_SIZE (5 * GRAPH_CHUNKLOOKUP_WIDTH + GRAPH_FANOUT_SIZE + \
  39                        GRAPH_OID_LEN + 8)
  40
  41char *get_commit_graph_filename(const char *obj_dir)
  42{
  43        return xstrfmt("%s/info/commit-graph", obj_dir);
  44}
  45
  46static struct commit_graph *alloc_commit_graph(void)
  47{
  48        struct commit_graph *g = xcalloc(1, sizeof(*g));
  49        g->graph_fd = -1;
  50
  51        return g;
  52}
  53
  54struct commit_graph *load_commit_graph_one(const char *graph_file)
  55{
  56        void *graph_map;
  57        const unsigned char *data, *chunk_lookup;
  58        size_t graph_size;
  59        struct stat st;
  60        uint32_t i;
  61        struct commit_graph *graph;
  62        int fd = git_open(graph_file);
  63        uint64_t last_chunk_offset;
  64        uint32_t last_chunk_id;
  65        uint32_t graph_signature;
  66        unsigned char graph_version, hash_version;
  67
  68        if (fd < 0)
  69                return NULL;
  70        if (fstat(fd, &st)) {
  71                close(fd);
  72                return NULL;
  73        }
  74        graph_size = xsize_t(st.st_size);
  75
  76        if (graph_size < GRAPH_MIN_SIZE) {
  77                close(fd);
  78                die("graph file %s is too small", graph_file);
  79        }
  80        graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
  81        data = (const unsigned char *)graph_map;
  82
  83        graph_signature = get_be32(data);
  84        if (graph_signature != GRAPH_SIGNATURE) {
  85                error("graph signature %X does not match signature %X",
  86                      graph_signature, GRAPH_SIGNATURE);
  87                goto cleanup_fail;
  88        }
  89
  90        graph_version = *(unsigned char*)(data + 4);
  91        if (graph_version != GRAPH_VERSION) {
  92                error("graph version %X does not match version %X",
  93                      graph_version, GRAPH_VERSION);
  94                goto cleanup_fail;
  95        }
  96
  97        hash_version = *(unsigned char*)(data + 5);
  98        if (hash_version != GRAPH_OID_VERSION) {
  99                error("hash version %X does not match version %X",
 100                      hash_version, GRAPH_OID_VERSION);
 101                goto cleanup_fail;
 102        }
 103
 104        graph = alloc_commit_graph();
 105
 106        graph->hash_len = GRAPH_OID_LEN;
 107        graph->num_chunks = *(unsigned char*)(data + 6);
 108        graph->graph_fd = fd;
 109        graph->data = graph_map;
 110        graph->data_len = graph_size;
 111
 112        last_chunk_id = 0;
 113        last_chunk_offset = 8;
 114        chunk_lookup = data + 8;
 115        for (i = 0; i < graph->num_chunks; i++) {
 116                uint32_t chunk_id = get_be32(chunk_lookup + 0);
 117                uint64_t chunk_offset = get_be64(chunk_lookup + 4);
 118                int chunk_repeated = 0;
 119
 120                chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
 121
 122                if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
 123                        error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset >> 32),
 124                              (uint32_t)chunk_offset);
 125                        goto cleanup_fail;
 126                }
 127
 128                switch (chunk_id) {
 129                case GRAPH_CHUNKID_OIDFANOUT:
 130                        if (graph->chunk_oid_fanout)
 131                                chunk_repeated = 1;
 132                        else
 133                                graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
 134                        break;
 135
 136                case GRAPH_CHUNKID_OIDLOOKUP:
 137                        if (graph->chunk_oid_lookup)
 138                                chunk_repeated = 1;
 139                        else
 140                                graph->chunk_oid_lookup = data + chunk_offset;
 141                        break;
 142
 143                case GRAPH_CHUNKID_DATA:
 144                        if (graph->chunk_commit_data)
 145                                chunk_repeated = 1;
 146                        else
 147                                graph->chunk_commit_data = data + chunk_offset;
 148                        break;
 149
 150                case GRAPH_CHUNKID_LARGEEDGES:
 151                        if (graph->chunk_large_edges)
 152                                chunk_repeated = 1;
 153                        else
 154                                graph->chunk_large_edges = data + chunk_offset;
 155                        break;
 156                }
 157
 158                if (chunk_repeated) {
 159                        error("chunk id %08x appears multiple times", chunk_id);
 160                        goto cleanup_fail;
 161                }
 162
 163                if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
 164                {
 165                        graph->num_commits = (chunk_offset - last_chunk_offset)
 166                                             / graph->hash_len;
 167                }
 168
 169                last_chunk_id = chunk_id;
 170                last_chunk_offset = chunk_offset;
 171        }
 172
 173        return graph;
 174
 175cleanup_fail:
 176        munmap(graph_map, graph_size);
 177        close(fd);
 178        exit(1);
 179}
 180
 181/* global storage */
 182static struct commit_graph *commit_graph = NULL;
 183
 184static void prepare_commit_graph_one(const char *obj_dir)
 185{
 186        char *graph_name;
 187
 188        if (commit_graph)
 189                return;
 190
 191        graph_name = get_commit_graph_filename(obj_dir);
 192        commit_graph = load_commit_graph_one(graph_name);
 193
 194        FREE_AND_NULL(graph_name);
 195}
 196
 197static int prepare_commit_graph_run_once = 0;
 198static void prepare_commit_graph(void)
 199{
 200        struct alternate_object_database *alt;
 201        char *obj_dir;
 202
 203        if (prepare_commit_graph_run_once)
 204                return;
 205        prepare_commit_graph_run_once = 1;
 206
 207        obj_dir = get_object_directory();
 208        prepare_commit_graph_one(obj_dir);
 209        prepare_alt_odb();
 210        for (alt = alt_odb_list; !commit_graph && alt; alt = alt->next)
 211                prepare_commit_graph_one(alt->path);
 212}
 213
 214static void close_commit_graph(void)
 215{
 216        if (!commit_graph)
 217                return;
 218
 219        if (commit_graph->graph_fd >= 0) {
 220                munmap((void *)commit_graph->data, commit_graph->data_len);
 221                commit_graph->data = NULL;
 222                close(commit_graph->graph_fd);
 223        }
 224
 225        FREE_AND_NULL(commit_graph);
 226}
 227
 228static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
 229{
 230        return bsearch_hash(oid->hash, g->chunk_oid_fanout,
 231                            g->chunk_oid_lookup, g->hash_len, pos);
 232}
 233
 234static struct commit_list **insert_parent_or_die(struct commit_graph *g,
 235                                                 uint64_t pos,
 236                                                 struct commit_list **pptr)
 237{
 238        struct commit *c;
 239        struct object_id oid;
 240        hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
 241        c = lookup_commit(&oid);
 242        if (!c)
 243                die("could not find commit %s", oid_to_hex(&oid));
 244        c->graph_pos = pos;
 245        return &commit_list_insert(c, pptr)->next;
 246}
 247
 248static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
 249{
 250        uint32_t edge_value;
 251        uint32_t *parent_data_ptr;
 252        uint64_t date_low, date_high;
 253        struct commit_list **pptr;
 254        const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
 255
 256        item->object.parsed = 1;
 257        item->graph_pos = pos;
 258
 259        item->maybe_tree = NULL;
 260
 261        date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
 262        date_low = get_be32(commit_data + g->hash_len + 12);
 263        item->date = (timestamp_t)((date_high << 32) | date_low);
 264
 265        item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
 266
 267        pptr = &item->parents;
 268
 269        edge_value = get_be32(commit_data + g->hash_len);
 270        if (edge_value == GRAPH_PARENT_NONE)
 271                return 1;
 272        pptr = insert_parent_or_die(g, edge_value, pptr);
 273
 274        edge_value = get_be32(commit_data + g->hash_len + 4);
 275        if (edge_value == GRAPH_PARENT_NONE)
 276                return 1;
 277        if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
 278                pptr = insert_parent_or_die(g, edge_value, pptr);
 279                return 1;
 280        }
 281
 282        parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
 283                          4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
 284        do {
 285                edge_value = get_be32(parent_data_ptr);
 286                pptr = insert_parent_or_die(g,
 287                                            edge_value & GRAPH_EDGE_LAST_MASK,
 288                                            pptr);
 289                parent_data_ptr++;
 290        } while (!(edge_value & GRAPH_LAST_EDGE));
 291
 292        return 1;
 293}
 294
 295int parse_commit_in_graph(struct commit *item)
 296{
 297        if (!core_commit_graph)
 298                return 0;
 299        if (item->object.parsed)
 300                return 1;
 301
 302        prepare_commit_graph();
 303        if (commit_graph) {
 304                uint32_t pos;
 305                int found;
 306                if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
 307                        pos = item->graph_pos;
 308                        found = 1;
 309                } else {
 310                        found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
 311                }
 312
 313                if (found)
 314                        return fill_commit_in_graph(item, commit_graph, pos);
 315        }
 316
 317        return 0;
 318}
 319
 320static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
 321{
 322        struct object_id oid;
 323        const unsigned char *commit_data = g->chunk_commit_data +
 324                                           GRAPH_DATA_WIDTH * (c->graph_pos);
 325
 326        hashcpy(oid.hash, commit_data);
 327        c->maybe_tree = lookup_tree(&oid);
 328
 329        return c->maybe_tree;
 330}
 331
 332struct tree *get_commit_tree_in_graph(const struct commit *c)
 333{
 334        if (c->maybe_tree)
 335                return c->maybe_tree;
 336        if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
 337                BUG("get_commit_tree_in_graph called from non-commit-graph commit");
 338
 339        return load_tree_for_commit(commit_graph, (struct commit *)c);
 340}
 341
 342static void write_graph_chunk_fanout(struct hashfile *f,
 343                                     struct commit **commits,
 344                                     int nr_commits)
 345{
 346        int i, count = 0;
 347        struct commit **list = commits;
 348
 349        /*
 350         * Write the first-level table (the list is sorted,
 351         * but we use a 256-entry lookup to be able to avoid
 352         * having to do eight extra binary search iterations).
 353         */
 354        for (i = 0; i < 256; i++) {
 355                while (count < nr_commits) {
 356                        if ((*list)->object.oid.hash[0] != i)
 357                                break;
 358                        count++;
 359                        list++;
 360                }
 361
 362                hashwrite_be32(f, count);
 363        }
 364}
 365
 366static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
 367                                   struct commit **commits, int nr_commits)
 368{
 369        struct commit **list = commits;
 370        int count;
 371        for (count = 0; count < nr_commits; count++, list++)
 372                hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
 373}
 374
 375static const unsigned char *commit_to_sha1(size_t index, void *table)
 376{
 377        struct commit **commits = table;
 378        return commits[index]->object.oid.hash;
 379}
 380
 381static void write_graph_chunk_data(struct hashfile *f, int hash_len,
 382                                   struct commit **commits, int nr_commits)
 383{
 384        struct commit **list = commits;
 385        struct commit **last = commits + nr_commits;
 386        uint32_t num_extra_edges = 0;
 387
 388        while (list < last) {
 389                struct commit_list *parent;
 390                int edge_value;
 391                uint32_t packedDate[2];
 392
 393                parse_commit(*list);
 394                hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
 395
 396                parent = (*list)->parents;
 397
 398                if (!parent)
 399                        edge_value = GRAPH_PARENT_NONE;
 400                else {
 401                        edge_value = sha1_pos(parent->item->object.oid.hash,
 402                                              commits,
 403                                              nr_commits,
 404                                              commit_to_sha1);
 405
 406                        if (edge_value < 0)
 407                                edge_value = GRAPH_PARENT_MISSING;
 408                }
 409
 410                hashwrite_be32(f, edge_value);
 411
 412                if (parent)
 413                        parent = parent->next;
 414
 415                if (!parent)
 416                        edge_value = GRAPH_PARENT_NONE;
 417                else if (parent->next)
 418                        edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
 419                else {
 420                        edge_value = sha1_pos(parent->item->object.oid.hash,
 421                                              commits,
 422                                              nr_commits,
 423                                              commit_to_sha1);
 424                        if (edge_value < 0)
 425                                edge_value = GRAPH_PARENT_MISSING;
 426                }
 427
 428                hashwrite_be32(f, edge_value);
 429
 430                if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
 431                        do {
 432                                num_extra_edges++;
 433                                parent = parent->next;
 434                        } while (parent);
 435                }
 436
 437                if (sizeof((*list)->date) > 4)
 438                        packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
 439                else
 440                        packedDate[0] = 0;
 441
 442                packedDate[0] |= htonl((*list)->generation << 2);
 443
 444                packedDate[1] = htonl((*list)->date);
 445                hashwrite(f, packedDate, 8);
 446
 447                list++;
 448        }
 449}
 450
 451static void write_graph_chunk_large_edges(struct hashfile *f,
 452                                          struct commit **commits,
 453                                          int nr_commits)
 454{
 455        struct commit **list = commits;
 456        struct commit **last = commits + nr_commits;
 457        struct commit_list *parent;
 458
 459        while (list < last) {
 460                int num_parents = 0;
 461                for (parent = (*list)->parents; num_parents < 3 && parent;
 462                     parent = parent->next)
 463                        num_parents++;
 464
 465                if (num_parents <= 2) {
 466                        list++;
 467                        continue;
 468                }
 469
 470                /* Since num_parents > 2, this initializer is safe. */
 471                for (parent = (*list)->parents->next; parent; parent = parent->next) {
 472                        int edge_value = sha1_pos(parent->item->object.oid.hash,
 473                                                  commits,
 474                                                  nr_commits,
 475                                                  commit_to_sha1);
 476
 477                        if (edge_value < 0)
 478                                edge_value = GRAPH_PARENT_MISSING;
 479                        else if (!parent->next)
 480                                edge_value |= GRAPH_LAST_EDGE;
 481
 482                        hashwrite_be32(f, edge_value);
 483                }
 484
 485                list++;
 486        }
 487}
 488
 489static int commit_compare(const void *_a, const void *_b)
 490{
 491        const struct object_id *a = (const struct object_id *)_a;
 492        const struct object_id *b = (const struct object_id *)_b;
 493        return oidcmp(a, b);
 494}
 495
 496struct packed_commit_list {
 497        struct commit **list;
 498        int nr;
 499        int alloc;
 500};
 501
 502struct packed_oid_list {
 503        struct object_id *list;
 504        int nr;
 505        int alloc;
 506};
 507
 508static int add_packed_commits(const struct object_id *oid,
 509                              struct packed_git *pack,
 510                              uint32_t pos,
 511                              void *data)
 512{
 513        struct packed_oid_list *list = (struct packed_oid_list*)data;
 514        enum object_type type;
 515        off_t offset = nth_packed_object_offset(pack, pos);
 516        struct object_info oi = OBJECT_INFO_INIT;
 517
 518        oi.typep = &type;
 519        if (packed_object_info(pack, offset, &oi) < 0)
 520                die("unable to get type of object %s", oid_to_hex(oid));
 521
 522        if (type != OBJ_COMMIT)
 523                return 0;
 524
 525        ALLOC_GROW(list->list, list->nr + 1, list->alloc);
 526        oidcpy(&(list->list[list->nr]), oid);
 527        list->nr++;
 528
 529        return 0;
 530}
 531
 532static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
 533{
 534        struct commit_list *parent;
 535        for (parent = commit->parents; parent; parent = parent->next) {
 536                if (!(parent->item->object.flags & UNINTERESTING)) {
 537                        ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
 538                        oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
 539                        oids->nr++;
 540                        parent->item->object.flags |= UNINTERESTING;
 541                }
 542        }
 543}
 544
 545static void close_reachable(struct packed_oid_list *oids)
 546{
 547        int i;
 548        struct commit *commit;
 549
 550        for (i = 0; i < oids->nr; i++) {
 551                commit = lookup_commit(&oids->list[i]);
 552                if (commit)
 553                        commit->object.flags |= UNINTERESTING;
 554        }
 555
 556        /*
 557         * As this loop runs, oids->nr may grow, but not more
 558         * than the number of missing commits in the reachable
 559         * closure.
 560         */
 561        for (i = 0; i < oids->nr; i++) {
 562                commit = lookup_commit(&oids->list[i]);
 563
 564                if (commit && !parse_commit(commit))
 565                        add_missing_parents(oids, commit);
 566        }
 567
 568        for (i = 0; i < oids->nr; i++) {
 569                commit = lookup_commit(&oids->list[i]);
 570
 571                if (commit)
 572                        commit->object.flags &= ~UNINTERESTING;
 573        }
 574}
 575
 576static void compute_generation_numbers(struct packed_commit_list* commits)
 577{
 578        int i;
 579        struct commit_list *list = NULL;
 580
 581        for (i = 0; i < commits->nr; i++) {
 582                if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
 583                    commits->list[i]->generation != GENERATION_NUMBER_ZERO)
 584                        continue;
 585
 586                commit_list_insert(commits->list[i], &list);
 587                while (list) {
 588                        struct commit *current = list->item;
 589                        struct commit_list *parent;
 590                        int all_parents_computed = 1;
 591                        uint32_t max_generation = 0;
 592
 593                        for (parent = current->parents; parent; parent = parent->next) {
 594                                if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
 595                                    parent->item->generation == GENERATION_NUMBER_ZERO) {
 596                                        all_parents_computed = 0;
 597                                        commit_list_insert(parent->item, &list);
 598                                        break;
 599                                } else if (parent->item->generation > max_generation) {
 600                                        max_generation = parent->item->generation;
 601                                }
 602                        }
 603
 604                        if (all_parents_computed) {
 605                                current->generation = max_generation + 1;
 606                                pop_commit(&list);
 607
 608                                if (current->generation > GENERATION_NUMBER_MAX)
 609                                        current->generation = GENERATION_NUMBER_MAX;
 610                        }
 611                }
 612        }
 613}
 614
 615void write_commit_graph(const char *obj_dir,
 616                        const char **pack_indexes,
 617                        int nr_packs,
 618                        const char **commit_hex,
 619                        int nr_commits,
 620                        int append)
 621{
 622        struct packed_oid_list oids;
 623        struct packed_commit_list commits;
 624        struct hashfile *f;
 625        uint32_t i, count_distinct = 0;
 626        char *graph_name;
 627        int fd;
 628        struct lock_file lk = LOCK_INIT;
 629        uint32_t chunk_ids[5];
 630        uint64_t chunk_offsets[5];
 631        int num_chunks;
 632        int num_extra_edges;
 633        struct commit_list *parent;
 634
 635        oids.nr = 0;
 636        oids.alloc = approximate_object_count() / 4;
 637
 638        if (append) {
 639                prepare_commit_graph_one(obj_dir);
 640                if (commit_graph)
 641                        oids.alloc += commit_graph->num_commits;
 642        }
 643
 644        if (oids.alloc < 1024)
 645                oids.alloc = 1024;
 646        ALLOC_ARRAY(oids.list, oids.alloc);
 647
 648        if (append && commit_graph) {
 649                for (i = 0; i < commit_graph->num_commits; i++) {
 650                        const unsigned char *hash = commit_graph->chunk_oid_lookup +
 651                                commit_graph->hash_len * i;
 652                        hashcpy(oids.list[oids.nr++].hash, hash);
 653                }
 654        }
 655
 656        if (pack_indexes) {
 657                struct strbuf packname = STRBUF_INIT;
 658                int dirlen;
 659                strbuf_addf(&packname, "%s/pack/", obj_dir);
 660                dirlen = packname.len;
 661                for (i = 0; i < nr_packs; i++) {
 662                        struct packed_git *p;
 663                        strbuf_setlen(&packname, dirlen);
 664                        strbuf_addstr(&packname, pack_indexes[i]);
 665                        p = add_packed_git(packname.buf, packname.len, 1);
 666                        if (!p)
 667                                die("error adding pack %s", packname.buf);
 668                        if (open_pack_index(p))
 669                                die("error opening index for %s", packname.buf);
 670                        for_each_object_in_pack(p, add_packed_commits, &oids);
 671                        close_pack(p);
 672                }
 673                strbuf_release(&packname);
 674        }
 675
 676        if (commit_hex) {
 677                for (i = 0; i < nr_commits; i++) {
 678                        const char *end;
 679                        struct object_id oid;
 680                        struct commit *result;
 681
 682                        if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
 683                                continue;
 684
 685                        result = lookup_commit_reference_gently(&oid, 1);
 686
 687                        if (result) {
 688                                ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
 689                                oidcpy(&oids.list[oids.nr], &(result->object.oid));
 690                                oids.nr++;
 691                        }
 692                }
 693        }
 694
 695        if (!pack_indexes && !commit_hex)
 696                for_each_packed_object(add_packed_commits, &oids, 0);
 697
 698        close_reachable(&oids);
 699
 700        QSORT(oids.list, oids.nr, commit_compare);
 701
 702        count_distinct = 1;
 703        for (i = 1; i < oids.nr; i++) {
 704                if (oidcmp(&oids.list[i-1], &oids.list[i]))
 705                        count_distinct++;
 706        }
 707
 708        if (count_distinct >= GRAPH_PARENT_MISSING)
 709                die(_("the commit graph format cannot write %d commits"), count_distinct);
 710
 711        commits.nr = 0;
 712        commits.alloc = count_distinct;
 713        ALLOC_ARRAY(commits.list, commits.alloc);
 714
 715        num_extra_edges = 0;
 716        for (i = 0; i < oids.nr; i++) {
 717                int num_parents = 0;
 718                if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
 719                        continue;
 720
 721                commits.list[commits.nr] = lookup_commit(&oids.list[i]);
 722                parse_commit(commits.list[commits.nr]);
 723
 724                for (parent = commits.list[commits.nr]->parents;
 725                     parent; parent = parent->next)
 726                        num_parents++;
 727
 728                if (num_parents > 2)
 729                        num_extra_edges += num_parents - 1;
 730
 731                commits.nr++;
 732        }
 733        num_chunks = num_extra_edges ? 4 : 3;
 734
 735        if (commits.nr >= GRAPH_PARENT_MISSING)
 736                die(_("too many commits to write graph"));
 737
 738        compute_generation_numbers(&commits);
 739
 740        graph_name = get_commit_graph_filename(obj_dir);
 741        fd = hold_lock_file_for_update(&lk, graph_name, 0);
 742
 743        if (fd < 0) {
 744                struct strbuf folder = STRBUF_INIT;
 745                strbuf_addstr(&folder, graph_name);
 746                strbuf_setlen(&folder, strrchr(folder.buf, '/') - folder.buf);
 747
 748                if (mkdir(folder.buf, 0777) < 0)
 749                        die_errno(_("cannot mkdir %s"), folder.buf);
 750                strbuf_release(&folder);
 751
 752                fd = hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
 753
 754                if (fd < 0)
 755                        die_errno("unable to create '%s'", graph_name);
 756        }
 757
 758        f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
 759
 760        hashwrite_be32(f, GRAPH_SIGNATURE);
 761
 762        hashwrite_u8(f, GRAPH_VERSION);
 763        hashwrite_u8(f, GRAPH_OID_VERSION);
 764        hashwrite_u8(f, num_chunks);
 765        hashwrite_u8(f, 0); /* unused padding byte */
 766
 767        chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
 768        chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
 769        chunk_ids[2] = GRAPH_CHUNKID_DATA;
 770        if (num_extra_edges)
 771                chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
 772        else
 773                chunk_ids[3] = 0;
 774        chunk_ids[4] = 0;
 775
 776        chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
 777        chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
 778        chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
 779        chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
 780        chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
 781
 782        for (i = 0; i <= num_chunks; i++) {
 783                uint32_t chunk_write[3];
 784
 785                chunk_write[0] = htonl(chunk_ids[i]);
 786                chunk_write[1] = htonl(chunk_offsets[i] >> 32);
 787                chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
 788                hashwrite(f, chunk_write, 12);
 789        }
 790
 791        write_graph_chunk_fanout(f, commits.list, commits.nr);
 792        write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
 793        write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
 794        write_graph_chunk_large_edges(f, commits.list, commits.nr);
 795
 796        close_commit_graph();
 797        finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
 798        commit_lock_file(&lk);
 799
 800        free(oids.list);
 801        oids.alloc = 0;
 802        oids.nr = 0;
 803}