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