Skip to content

Commit c166599

Browse files
bk2204gitster
authored andcommitted
commit-graph: convert to using the_hash_algo
Instead of using hard-coded constants for object sizes, use the_hash_algo to look them up. In addition, use a function call to look up the object ID version and produce the correct value. For now, we use version 1, which means to use the default algorithm used in the rest of the repository. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 37649b7 commit c166599

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

commit-graph.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020
#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
2121
#define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
2222

23-
#define GRAPH_DATA_WIDTH 36
23+
#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
2424

2525
#define GRAPH_VERSION_1 0x1
2626
#define GRAPH_VERSION GRAPH_VERSION_1
2727

28-
#define GRAPH_OID_VERSION_SHA1 1
29-
#define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
30-
#define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
31-
#define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
32-
3328
#define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
3429
#define GRAPH_PARENT_MISSING 0x7fffffff
3530
#define GRAPH_EDGE_LAST_MASK 0x7fffffff
@@ -41,13 +36,18 @@
4136
#define GRAPH_FANOUT_SIZE (4 * 256)
4237
#define GRAPH_CHUNKLOOKUP_WIDTH 12
4338
#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
44-
+ GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
39+
+ GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
4540

4641
char *get_commit_graph_filename(const char *obj_dir)
4742
{
4843
return xstrfmt("%s/info/commit-graph", obj_dir);
4944
}
5045

46+
static uint8_t oid_version(void)
47+
{
48+
return 1;
49+
}
50+
5151
static struct commit_graph *alloc_commit_graph(void)
5252
{
5353
struct commit_graph *g = xcalloc(1, sizeof(*g));
@@ -100,15 +100,15 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
100100
}
101101

102102
hash_version = *(unsigned char*)(data + 5);
103-
if (hash_version != GRAPH_OID_VERSION) {
103+
if (hash_version != oid_version()) {
104104
error(_("hash version %X does not match version %X"),
105-
hash_version, GRAPH_OID_VERSION);
105+
hash_version, oid_version());
106106
goto cleanup_fail;
107107
}
108108

109109
graph = alloc_commit_graph();
110110

111-
graph->hash_len = GRAPH_OID_LEN;
111+
graph->hash_len = the_hash_algo->rawsz;
112112
graph->num_chunks = *(unsigned char*)(data + 6);
113113
graph->graph_fd = fd;
114114
graph->data = graph_map;
@@ -124,7 +124,7 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
124124

125125
chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
126126

127-
if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
127+
if (chunk_offset > graph_size - the_hash_algo->rawsz) {
128128
error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
129129
(uint32_t)chunk_offset);
130130
goto cleanup_fail;
@@ -711,6 +711,7 @@ void write_commit_graph(const char *obj_dir,
711711
int num_chunks;
712712
int num_extra_edges;
713713
struct commit_list *parent;
714+
const unsigned hashsz = the_hash_algo->rawsz;
714715

715716
oids.nr = 0;
716717
oids.alloc = approximate_object_count() / 4;
@@ -831,7 +832,7 @@ void write_commit_graph(const char *obj_dir,
831832
hashwrite_be32(f, GRAPH_SIGNATURE);
832833

833834
hashwrite_u8(f, GRAPH_VERSION);
834-
hashwrite_u8(f, GRAPH_OID_VERSION);
835+
hashwrite_u8(f, oid_version());
835836
hashwrite_u8(f, num_chunks);
836837
hashwrite_u8(f, 0); /* unused padding byte */
837838

@@ -846,8 +847,8 @@ void write_commit_graph(const char *obj_dir,
846847

847848
chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
848849
chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
849-
chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
850-
chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
850+
chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
851+
chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
851852
chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
852853

853854
for (i = 0; i <= num_chunks; i++) {
@@ -860,8 +861,8 @@ void write_commit_graph(const char *obj_dir,
860861
}
861862

862863
write_graph_chunk_fanout(f, commits.list, commits.nr);
863-
write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
864-
write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
864+
write_graph_chunk_oids(f, hashsz, commits.list, commits.nr);
865+
write_graph_chunk_data(f, hashsz, commits.list, commits.nr);
865866
write_graph_chunk_large_edges(f, commits.list, commits.nr);
866867

867868
close_commit_graph();

0 commit comments

Comments
 (0)