3 #include "git-compat-util.h"
10 #include "sha1-lookup.h"
11 #include "commit-graph.h"
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" */
19 #define GRAPH_DATA_WIDTH 36
21 #define GRAPH_VERSION_1 0x1
22 #define GRAPH_VERSION GRAPH_VERSION_1
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
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
34 #define GRAPH_LAST_EDGE 0x80000000
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 + \
42 static char *get_commit_graph_filename(const char *obj_dir)
44 return xstrfmt("%s/info/commit-graph", obj_dir);
47 static void write_graph_chunk_fanout(struct hashfile *f,
48 struct commit **commits,
52 struct commit **list = commits;
55 * Write the first-level table (the list is sorted,
56 * but we use a 256-entry lookup to be able to avoid
57 * having to do eight extra binary search iterations).
59 for (i = 0; i < 256; i++) {
60 while (count < nr_commits) {
61 if ((*list)->object.oid.hash[0] != i)
67 hashwrite_be32(f, count);
71 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
72 struct commit **commits, int nr_commits)
74 struct commit **list = commits;
76 for (count = 0; count < nr_commits; count++, list++)
77 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
80 static const unsigned char *commit_to_sha1(size_t index, void *table)
82 struct commit **commits = table;
83 return commits[index]->object.oid.hash;
86 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
87 struct commit **commits, int nr_commits)
89 struct commit **list = commits;
90 struct commit **last = commits + nr_commits;
91 uint32_t num_extra_edges = 0;
94 struct commit_list *parent;
96 uint32_t packedDate[2];
99 hashwrite(f, (*list)->tree->object.oid.hash, hash_len);
101 parent = (*list)->parents;
104 edge_value = GRAPH_PARENT_NONE;
106 edge_value = sha1_pos(parent->item->object.oid.hash,
112 edge_value = GRAPH_PARENT_MISSING;
115 hashwrite_be32(f, edge_value);
118 parent = parent->next;
121 edge_value = GRAPH_PARENT_NONE;
122 else if (parent->next)
123 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
125 edge_value = sha1_pos(parent->item->object.oid.hash,
130 edge_value = GRAPH_PARENT_MISSING;
133 hashwrite_be32(f, edge_value);
135 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
138 parent = parent->next;
142 if (sizeof((*list)->date) > 4)
143 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
147 packedDate[1] = htonl((*list)->date);
148 hashwrite(f, packedDate, 8);
154 static void write_graph_chunk_large_edges(struct hashfile *f,
155 struct commit **commits,
158 struct commit **list = commits;
159 struct commit **last = commits + nr_commits;
160 struct commit_list *parent;
162 while (list < last) {
164 for (parent = (*list)->parents; num_parents < 3 && parent;
165 parent = parent->next)
168 if (num_parents <= 2) {
173 /* Since num_parents > 2, this initializer is safe. */
174 for (parent = (*list)->parents->next; parent; parent = parent->next) {
175 int edge_value = sha1_pos(parent->item->object.oid.hash,
181 edge_value = GRAPH_PARENT_MISSING;
182 else if (!parent->next)
183 edge_value |= GRAPH_LAST_EDGE;
185 hashwrite_be32(f, edge_value);
192 static int commit_compare(const void *_a, const void *_b)
194 const struct object_id *a = (const struct object_id *)_a;
195 const struct object_id *b = (const struct object_id *)_b;
199 struct packed_commit_list {
200 struct commit **list;
205 struct packed_oid_list {
206 struct object_id *list;
211 static int add_packed_commits(const struct object_id *oid,
212 struct packed_git *pack,
216 struct packed_oid_list *list = (struct packed_oid_list*)data;
217 enum object_type type;
218 off_t offset = nth_packed_object_offset(pack, pos);
219 struct object_info oi = OBJECT_INFO_INIT;
222 if (packed_object_info(pack, offset, &oi) < 0)
223 die("unable to get type of object %s", oid_to_hex(oid));
225 if (type != OBJ_COMMIT)
228 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
229 oidcpy(&(list->list[list->nr]), oid);
235 void write_commit_graph(const char *obj_dir)
237 struct packed_oid_list oids;
238 struct packed_commit_list commits;
240 uint32_t i, count_distinct = 0;
243 struct lock_file lk = LOCK_INIT;
244 uint32_t chunk_ids[5];
245 uint64_t chunk_offsets[5];
248 struct commit_list *parent;
251 oids.alloc = approximate_object_count() / 4;
253 if (oids.alloc < 1024)
255 ALLOC_ARRAY(oids.list, oids.alloc);
257 for_each_packed_object(add_packed_commits, &oids, 0);
259 QSORT(oids.list, oids.nr, commit_compare);
262 for (i = 1; i < oids.nr; i++) {
263 if (oidcmp(&oids.list[i-1], &oids.list[i]))
267 if (count_distinct >= GRAPH_PARENT_MISSING)
268 die(_("the commit graph format cannot write %d commits"), count_distinct);
271 commits.alloc = count_distinct;
272 ALLOC_ARRAY(commits.list, commits.alloc);
275 for (i = 0; i < oids.nr; i++) {
277 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
280 commits.list[commits.nr] = lookup_commit(&oids.list[i]);
281 parse_commit(commits.list[commits.nr]);
283 for (parent = commits.list[commits.nr]->parents;
284 parent; parent = parent->next)
288 num_extra_edges += num_parents - 1;
292 num_chunks = num_extra_edges ? 4 : 3;
294 if (commits.nr >= GRAPH_PARENT_MISSING)
295 die(_("too many commits to write graph"));
297 graph_name = get_commit_graph_filename(obj_dir);
298 fd = hold_lock_file_for_update(&lk, graph_name, 0);
301 struct strbuf folder = STRBUF_INIT;
302 strbuf_addstr(&folder, graph_name);
303 strbuf_setlen(&folder, strrchr(folder.buf, '/') - folder.buf);
305 if (mkdir(folder.buf, 0777) < 0)
306 die_errno(_("cannot mkdir %s"), folder.buf);
307 strbuf_release(&folder);
309 fd = hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
312 die_errno("unable to create '%s'", graph_name);
315 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
317 hashwrite_be32(f, GRAPH_SIGNATURE);
319 hashwrite_u8(f, GRAPH_VERSION);
320 hashwrite_u8(f, GRAPH_OID_VERSION);
321 hashwrite_u8(f, num_chunks);
322 hashwrite_u8(f, 0); /* unused padding byte */
324 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
325 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
326 chunk_ids[2] = GRAPH_CHUNKID_DATA;
328 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
333 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
334 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
335 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
336 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
337 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
339 for (i = 0; i <= num_chunks; i++) {
340 uint32_t chunk_write[3];
342 chunk_write[0] = htonl(chunk_ids[i]);
343 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
344 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
345 hashwrite(f, chunk_write, 12);
348 write_graph_chunk_fanout(f, commits.list, commits.nr);
349 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
350 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
351 write_graph_chunk_large_edges(f, commits.list, commits.nr);
353 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
354 commit_lock_file(&lk);