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