Merge branch 'en/rev-parse-invalid-range'
[git] / commit-graph.c
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 #include "object-store.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
42 char *get_commit_graph_filename(const char *obj_dir)
43 {
44         return xstrfmt("%s/info/commit-graph", obj_dir);
45 }
46
47 static 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
55 struct 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
176 cleanup_fail:
177         munmap(graph_map, graph_size);
178         close(fd);
179         exit(1);
180 }
181
182 /* global storage */
183 static struct commit_graph *commit_graph = NULL;
184
185 static 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
198 static int prepare_commit_graph_run_once = 0;
199 static 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(the_repository);
211         for (alt = the_repository->objects->alt_odb_list;
212              !commit_graph && alt;
213              alt = alt->next)
214                 prepare_commit_graph_one(alt->path);
215 }
216
217 static void close_commit_graph(void)
218 {
219         if (!commit_graph)
220                 return;
221
222         if (commit_graph->graph_fd >= 0) {
223                 munmap((void *)commit_graph->data, commit_graph->data_len);
224                 commit_graph->data = NULL;
225                 close(commit_graph->graph_fd);
226         }
227
228         FREE_AND_NULL(commit_graph);
229 }
230
231 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
232 {
233         return bsearch_hash(oid->hash, g->chunk_oid_fanout,
234                             g->chunk_oid_lookup, g->hash_len, pos);
235 }
236
237 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
238                                                  uint64_t pos,
239                                                  struct commit_list **pptr)
240 {
241         struct commit *c;
242         struct object_id oid;
243         hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
244         c = lookup_commit(&oid);
245         if (!c)
246                 die("could not find commit %s", oid_to_hex(&oid));
247         c->graph_pos = pos;
248         return &commit_list_insert(c, pptr)->next;
249 }
250
251 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
252 {
253         uint32_t edge_value;
254         uint32_t *parent_data_ptr;
255         uint64_t date_low, date_high;
256         struct commit_list **pptr;
257         const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
258
259         item->object.parsed = 1;
260         item->graph_pos = pos;
261
262         item->maybe_tree = NULL;
263
264         date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
265         date_low = get_be32(commit_data + g->hash_len + 12);
266         item->date = (timestamp_t)((date_high << 32) | date_low);
267
268         pptr = &item->parents;
269
270         edge_value = get_be32(commit_data + g->hash_len);
271         if (edge_value == GRAPH_PARENT_NONE)
272                 return 1;
273         pptr = insert_parent_or_die(g, edge_value, pptr);
274
275         edge_value = get_be32(commit_data + g->hash_len + 4);
276         if (edge_value == GRAPH_PARENT_NONE)
277                 return 1;
278         if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
279                 pptr = insert_parent_or_die(g, edge_value, pptr);
280                 return 1;
281         }
282
283         parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
284                           4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
285         do {
286                 edge_value = get_be32(parent_data_ptr);
287                 pptr = insert_parent_or_die(g,
288                                             edge_value & GRAPH_EDGE_LAST_MASK,
289                                             pptr);
290                 parent_data_ptr++;
291         } while (!(edge_value & GRAPH_LAST_EDGE));
292
293         return 1;
294 }
295
296 int parse_commit_in_graph(struct commit *item)
297 {
298         if (!core_commit_graph)
299                 return 0;
300         if (item->object.parsed)
301                 return 1;
302
303         prepare_commit_graph();
304         if (commit_graph) {
305                 uint32_t pos;
306                 int found;
307                 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
308                         pos = item->graph_pos;
309                         found = 1;
310                 } else {
311                         found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
312                 }
313
314                 if (found)
315                         return fill_commit_in_graph(item, commit_graph, pos);
316         }
317
318         return 0;
319 }
320
321 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
322 {
323         struct object_id oid;
324         const unsigned char *commit_data = g->chunk_commit_data +
325                                            GRAPH_DATA_WIDTH * (c->graph_pos);
326
327         hashcpy(oid.hash, commit_data);
328         c->maybe_tree = lookup_tree(&oid);
329
330         return c->maybe_tree;
331 }
332
333 struct tree *get_commit_tree_in_graph(const struct commit *c)
334 {
335         if (c->maybe_tree)
336                 return c->maybe_tree;
337         if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
338                 BUG("get_commit_tree_in_graph called from non-commit-graph commit");
339
340         return load_tree_for_commit(commit_graph, (struct commit *)c);
341 }
342
343 static void write_graph_chunk_fanout(struct hashfile *f,
344                                      struct commit **commits,
345                                      int nr_commits)
346 {
347         int i, count = 0;
348         struct commit **list = commits;
349
350         /*
351          * Write the first-level table (the list is sorted,
352          * but we use a 256-entry lookup to be able to avoid
353          * having to do eight extra binary search iterations).
354          */
355         for (i = 0; i < 256; i++) {
356                 while (count < nr_commits) {
357                         if ((*list)->object.oid.hash[0] != i)
358                                 break;
359                         count++;
360                         list++;
361                 }
362
363                 hashwrite_be32(f, count);
364         }
365 }
366
367 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
368                                    struct commit **commits, int nr_commits)
369 {
370         struct commit **list = commits;
371         int count;
372         for (count = 0; count < nr_commits; count++, list++)
373                 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
374 }
375
376 static const unsigned char *commit_to_sha1(size_t index, void *table)
377 {
378         struct commit **commits = table;
379         return commits[index]->object.oid.hash;
380 }
381
382 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
383                                    struct commit **commits, int nr_commits)
384 {
385         struct commit **list = commits;
386         struct commit **last = commits + nr_commits;
387         uint32_t num_extra_edges = 0;
388
389         while (list < last) {
390                 struct commit_list *parent;
391                 int edge_value;
392                 uint32_t packedDate[2];
393
394                 parse_commit(*list);
395                 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
396
397                 parent = (*list)->parents;
398
399                 if (!parent)
400                         edge_value = GRAPH_PARENT_NONE;
401                 else {
402                         edge_value = sha1_pos(parent->item->object.oid.hash,
403                                               commits,
404                                               nr_commits,
405                                               commit_to_sha1);
406
407                         if (edge_value < 0)
408                                 edge_value = GRAPH_PARENT_MISSING;
409                 }
410
411                 hashwrite_be32(f, edge_value);
412
413                 if (parent)
414                         parent = parent->next;
415
416                 if (!parent)
417                         edge_value = GRAPH_PARENT_NONE;
418                 else if (parent->next)
419                         edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
420                 else {
421                         edge_value = sha1_pos(parent->item->object.oid.hash,
422                                               commits,
423                                               nr_commits,
424                                               commit_to_sha1);
425                         if (edge_value < 0)
426                                 edge_value = GRAPH_PARENT_MISSING;
427                 }
428
429                 hashwrite_be32(f, edge_value);
430
431                 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
432                         do {
433                                 num_extra_edges++;
434                                 parent = parent->next;
435                         } while (parent);
436                 }
437
438                 if (sizeof((*list)->date) > 4)
439                         packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
440                 else
441                         packedDate[0] = 0;
442
443                 packedDate[1] = htonl((*list)->date);
444                 hashwrite(f, packedDate, 8);
445
446                 list++;
447         }
448 }
449
450 static void write_graph_chunk_large_edges(struct hashfile *f,
451                                           struct commit **commits,
452                                           int nr_commits)
453 {
454         struct commit **list = commits;
455         struct commit **last = commits + nr_commits;
456         struct commit_list *parent;
457
458         while (list < last) {
459                 int num_parents = 0;
460                 for (parent = (*list)->parents; num_parents < 3 && parent;
461                      parent = parent->next)
462                         num_parents++;
463
464                 if (num_parents <= 2) {
465                         list++;
466                         continue;
467                 }
468
469                 /* Since num_parents > 2, this initializer is safe. */
470                 for (parent = (*list)->parents->next; parent; parent = parent->next) {
471                         int edge_value = sha1_pos(parent->item->object.oid.hash,
472                                                   commits,
473                                                   nr_commits,
474                                                   commit_to_sha1);
475
476                         if (edge_value < 0)
477                                 edge_value = GRAPH_PARENT_MISSING;
478                         else if (!parent->next)
479                                 edge_value |= GRAPH_LAST_EDGE;
480
481                         hashwrite_be32(f, edge_value);
482                 }
483
484                 list++;
485         }
486 }
487
488 static int commit_compare(const void *_a, const void *_b)
489 {
490         const struct object_id *a = (const struct object_id *)_a;
491         const struct object_id *b = (const struct object_id *)_b;
492         return oidcmp(a, b);
493 }
494
495 struct packed_commit_list {
496         struct commit **list;
497         int nr;
498         int alloc;
499 };
500
501 struct packed_oid_list {
502         struct object_id *list;
503         int nr;
504         int alloc;
505 };
506
507 static int add_packed_commits(const struct object_id *oid,
508                               struct packed_git *pack,
509                               uint32_t pos,
510                               void *data)
511 {
512         struct packed_oid_list *list = (struct packed_oid_list*)data;
513         enum object_type type;
514         off_t offset = nth_packed_object_offset(pack, pos);
515         struct object_info oi = OBJECT_INFO_INIT;
516
517         oi.typep = &type;
518         if (packed_object_info(the_repository, pack, offset, &oi) < 0)
519                 die("unable to get type of object %s", oid_to_hex(oid));
520
521         if (type != OBJ_COMMIT)
522                 return 0;
523
524         ALLOC_GROW(list->list, list->nr + 1, list->alloc);
525         oidcpy(&(list->list[list->nr]), oid);
526         list->nr++;
527
528         return 0;
529 }
530
531 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
532 {
533         struct commit_list *parent;
534         for (parent = commit->parents; parent; parent = parent->next) {
535                 if (!(parent->item->object.flags & UNINTERESTING)) {
536                         ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
537                         oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
538                         oids->nr++;
539                         parent->item->object.flags |= UNINTERESTING;
540                 }
541         }
542 }
543
544 static void close_reachable(struct packed_oid_list *oids)
545 {
546         int i;
547         struct commit *commit;
548
549         for (i = 0; i < oids->nr; i++) {
550                 commit = lookup_commit(&oids->list[i]);
551                 if (commit)
552                         commit->object.flags |= UNINTERESTING;
553         }
554
555         /*
556          * As this loop runs, oids->nr may grow, but not more
557          * than the number of missing commits in the reachable
558          * closure.
559          */
560         for (i = 0; i < oids->nr; i++) {
561                 commit = lookup_commit(&oids->list[i]);
562
563                 if (commit && !parse_commit(commit))
564                         add_missing_parents(oids, commit);
565         }
566
567         for (i = 0; i < oids->nr; i++) {
568                 commit = lookup_commit(&oids->list[i]);
569
570                 if (commit)
571                         commit->object.flags &= ~UNINTERESTING;
572         }
573 }
574
575 void write_commit_graph(const char *obj_dir,
576                         const char **pack_indexes,
577                         int nr_packs,
578                         const char **commit_hex,
579                         int nr_commits,
580                         int append)
581 {
582         struct packed_oid_list oids;
583         struct packed_commit_list commits;
584         struct hashfile *f;
585         uint32_t i, count_distinct = 0;
586         char *graph_name;
587         int fd;
588         struct lock_file lk = LOCK_INIT;
589         uint32_t chunk_ids[5];
590         uint64_t chunk_offsets[5];
591         int num_chunks;
592         int num_extra_edges;
593         struct commit_list *parent;
594
595         oids.nr = 0;
596         oids.alloc = approximate_object_count() / 4;
597
598         if (append) {
599                 prepare_commit_graph_one(obj_dir);
600                 if (commit_graph)
601                         oids.alloc += commit_graph->num_commits;
602         }
603
604         if (oids.alloc < 1024)
605                 oids.alloc = 1024;
606         ALLOC_ARRAY(oids.list, oids.alloc);
607
608         if (append && commit_graph) {
609                 for (i = 0; i < commit_graph->num_commits; i++) {
610                         const unsigned char *hash = commit_graph->chunk_oid_lookup +
611                                 commit_graph->hash_len * i;
612                         hashcpy(oids.list[oids.nr++].hash, hash);
613                 }
614         }
615
616         if (pack_indexes) {
617                 struct strbuf packname = STRBUF_INIT;
618                 int dirlen;
619                 strbuf_addf(&packname, "%s/pack/", obj_dir);
620                 dirlen = packname.len;
621                 for (i = 0; i < nr_packs; i++) {
622                         struct packed_git *p;
623                         strbuf_setlen(&packname, dirlen);
624                         strbuf_addstr(&packname, pack_indexes[i]);
625                         p = add_packed_git(packname.buf, packname.len, 1);
626                         if (!p)
627                                 die("error adding pack %s", packname.buf);
628                         if (open_pack_index(p))
629                                 die("error opening index for %s", packname.buf);
630                         for_each_object_in_pack(p, add_packed_commits, &oids);
631                         close_pack(p);
632                 }
633                 strbuf_release(&packname);
634         }
635
636         if (commit_hex) {
637                 for (i = 0; i < nr_commits; i++) {
638                         const char *end;
639                         struct object_id oid;
640                         struct commit *result;
641
642                         if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
643                                 continue;
644
645                         result = lookup_commit_reference_gently(&oid, 1);
646
647                         if (result) {
648                                 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
649                                 oidcpy(&oids.list[oids.nr], &(result->object.oid));
650                                 oids.nr++;
651                         }
652                 }
653         }
654
655         if (!pack_indexes && !commit_hex)
656                 for_each_packed_object(add_packed_commits, &oids, 0);
657
658         close_reachable(&oids);
659
660         QSORT(oids.list, oids.nr, commit_compare);
661
662         count_distinct = 1;
663         for (i = 1; i < oids.nr; i++) {
664                 if (oidcmp(&oids.list[i-1], &oids.list[i]))
665                         count_distinct++;
666         }
667
668         if (count_distinct >= GRAPH_PARENT_MISSING)
669                 die(_("the commit graph format cannot write %d commits"), count_distinct);
670
671         commits.nr = 0;
672         commits.alloc = count_distinct;
673         ALLOC_ARRAY(commits.list, commits.alloc);
674
675         num_extra_edges = 0;
676         for (i = 0; i < oids.nr; i++) {
677                 int num_parents = 0;
678                 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
679                         continue;
680
681                 commits.list[commits.nr] = lookup_commit(&oids.list[i]);
682                 parse_commit(commits.list[commits.nr]);
683
684                 for (parent = commits.list[commits.nr]->parents;
685                      parent; parent = parent->next)
686                         num_parents++;
687
688                 if (num_parents > 2)
689                         num_extra_edges += num_parents - 1;
690
691                 commits.nr++;
692         }
693         num_chunks = num_extra_edges ? 4 : 3;
694
695         if (commits.nr >= GRAPH_PARENT_MISSING)
696                 die(_("too many commits to write graph"));
697
698         graph_name = get_commit_graph_filename(obj_dir);
699         fd = hold_lock_file_for_update(&lk, graph_name, 0);
700
701         if (fd < 0) {
702                 struct strbuf folder = STRBUF_INIT;
703                 strbuf_addstr(&folder, graph_name);
704                 strbuf_setlen(&folder, strrchr(folder.buf, '/') - folder.buf);
705
706                 if (mkdir(folder.buf, 0777) < 0)
707                         die_errno(_("cannot mkdir %s"), folder.buf);
708                 strbuf_release(&folder);
709
710                 fd = hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
711
712                 if (fd < 0)
713                         die_errno("unable to create '%s'", graph_name);
714         }
715
716         f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
717
718         hashwrite_be32(f, GRAPH_SIGNATURE);
719
720         hashwrite_u8(f, GRAPH_VERSION);
721         hashwrite_u8(f, GRAPH_OID_VERSION);
722         hashwrite_u8(f, num_chunks);
723         hashwrite_u8(f, 0); /* unused padding byte */
724
725         chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
726         chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
727         chunk_ids[2] = GRAPH_CHUNKID_DATA;
728         if (num_extra_edges)
729                 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
730         else
731                 chunk_ids[3] = 0;
732         chunk_ids[4] = 0;
733
734         chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
735         chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
736         chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
737         chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
738         chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
739
740         for (i = 0; i <= num_chunks; i++) {
741                 uint32_t chunk_write[3];
742
743                 chunk_write[0] = htonl(chunk_ids[i]);
744                 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
745                 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
746                 hashwrite(f, chunk_write, 12);
747         }
748
749         write_graph_chunk_fanout(f, commits.list, commits.nr);
750         write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
751         write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
752         write_graph_chunk_large_edges(f, commits.list, commits.nr);
753
754         close_commit_graph();
755         finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
756         commit_lock_file(&lk);
757
758         free(oids.list);
759         oids.alloc = 0;
760         oids.nr = 0;
761 }