Skip to content

Commit 0be0712

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: create new version parameter
In anticipation of a new commit-graph file format version, create a parameter for the write_commit_graph() and write_commit_graph_reachable() methods to take a version number. When the given version is zero, the implementation selects a default value. Currently, the only valid value is 1. The file format will change the header information, so place the existing header logic inside a switch statement with only one case. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7d9995a commit 0be0712

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

builtin/commit-graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static int graph_write(int argc, const char **argv)
173173
read_replace_refs = 0;
174174

175175
if (opts.reachable)
176-
return write_commit_graph_reachable(opts.obj_dir, flags);
176+
return write_commit_graph_reachable(opts.obj_dir, flags, 0);
177177

178178
string_list_init(&lines, 0);
179179
if (opts.stdin_packs || opts.stdin_commits) {
@@ -193,7 +193,7 @@ static int graph_write(int argc, const char **argv)
193193
result = write_commit_graph(opts.obj_dir,
194194
pack_indexes,
195195
commit_hex,
196-
flags);
196+
flags, 0);
197197

198198
UNLEAK(lines);
199199
return result;

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16701670
"not exceeded, and then \"git reset HEAD\" to recover."));
16711671

16721672
if (git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
1673-
write_commit_graph_reachable(get_object_directory(), 0))
1673+
write_commit_graph_reachable(get_object_directory(), 0, 0))
16741674
return 1;
16751675

16761676
repo_rerere(the_repository, 0);

builtin/gc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
666666

667667
if (gc_write_commit_graph &&
668668
write_commit_graph_reachable(get_object_directory(),
669-
!quiet && !daemonized ? COMMIT_GRAPH_PROGRESS : 0))
669+
!quiet && !daemonized ? COMMIT_GRAPH_PROGRESS : 0,
670+
0))
670671
return 1;
671672

672673
if (auto_gc && too_many_loose_objects())

commit-graph.c

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
2727

28-
#define GRAPH_VERSION_1 0x1
29-
#define GRAPH_VERSION GRAPH_VERSION_1
30-
3128
#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
3229
#define GRAPH_EDGE_LAST_MASK 0x7fffffff
3330
#define GRAPH_PARENT_NONE 0x70000000
@@ -173,30 +170,35 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
173170
}
174171

175172
graph_version = *(unsigned char*)(data + 4);
176-
if (graph_version != GRAPH_VERSION) {
173+
if (graph_version != 1) {
177174
error(_("commit-graph version %X does not match version %X"),
178-
graph_version, GRAPH_VERSION);
179-
return NULL;
180-
}
181-
182-
hash_version = *(unsigned char*)(data + 5);
183-
if (hash_version != oid_version()) {
184-
error(_("commit-graph hash version %X does not match version %X"),
185-
hash_version, oid_version());
175+
graph_version, 1);
186176
return NULL;
187177
}
188178

189179
graph = alloc_commit_graph();
190180

181+
switch (graph_version) {
182+
case 1:
183+
hash_version = *(unsigned char*)(data + 5);
184+
if (hash_version != oid_version()) {
185+
error(_("commit-graph hash version %X does not match version %X"),
186+
hash_version, oid_version());
187+
return NULL;
188+
}
189+
190+
graph->num_chunks = *(unsigned char*)(data + 6);
191+
chunk_lookup = data + 8;
192+
break;
193+
}
194+
191195
graph->hash_len = the_hash_algo->rawsz;
192-
graph->num_chunks = *(unsigned char*)(data + 6);
193196
graph->graph_fd = fd;
194197
graph->data = graph_map;
195198
graph->data_len = graph_size;
196199

197200
last_chunk_id = 0;
198201
last_chunk_offset = 8;
199-
chunk_lookup = data + 8;
200202
for (i = 0; i < graph->num_chunks; i++) {
201203
uint32_t chunk_id;
202204
uint64_t chunk_offset;
@@ -851,14 +853,15 @@ static int add_ref_to_list(const char *refname,
851853
return 0;
852854
}
853855

854-
int write_commit_graph_reachable(const char *obj_dir, unsigned int flags)
856+
int write_commit_graph_reachable(const char *obj_dir, unsigned int flags,
857+
unsigned char version)
855858
{
856859
struct string_list list = STRING_LIST_INIT_DUP;
857860
int result;
858861

859862
for_each_ref(add_ref_to_list, &list);
860863
result = write_commit_graph(obj_dir, NULL, &list,
861-
flags);
864+
flags, version);
862865

863866
string_list_clear(&list, 0);
864867
return result;
@@ -867,7 +870,8 @@ int write_commit_graph_reachable(const char *obj_dir, unsigned int flags)
867870
int write_commit_graph(const char *obj_dir,
868871
struct string_list *pack_indexes,
869872
struct string_list *commit_hex,
870-
unsigned int flags)
873+
unsigned int flags,
874+
unsigned char version)
871875
{
872876
struct packed_oid_list oids;
873877
struct packed_commit_list commits;
@@ -888,10 +892,19 @@ int write_commit_graph(const char *obj_dir,
888892
int res = 0;
889893
int append = flags & COMMIT_GRAPH_APPEND;
890894
int report_progress = flags & COMMIT_GRAPH_PROGRESS;
895+
int header_size = 0;
891896

892897
if (!commit_graph_compatible(the_repository))
893898
return 0;
894899

900+
if (!version)
901+
version = 1;
902+
if (version != 1) {
903+
error(_("unsupported commit-graph version %d"),
904+
version);
905+
return 1;
906+
}
907+
895908
oids.nr = 0;
896909
approx_nr_objects = approximate_object_count();
897910
oids.alloc = approx_nr_objects / 32;
@@ -1076,10 +1089,16 @@ int write_commit_graph(const char *obj_dir,
10761089

10771090
hashwrite_be32(f, GRAPH_SIGNATURE);
10781091

1079-
hashwrite_u8(f, GRAPH_VERSION);
1080-
hashwrite_u8(f, oid_version());
1081-
hashwrite_u8(f, num_chunks);
1082-
hashwrite_u8(f, 0); /* unused padding byte */
1092+
hashwrite_u8(f, version);
1093+
1094+
switch (version) {
1095+
case 1:
1096+
hashwrite_u8(f, oid_version());
1097+
hashwrite_u8(f, num_chunks);
1098+
hashwrite_u8(f, 0); /* unused padding byte */
1099+
header_size = 8;
1100+
break;
1101+
}
10831102

10841103
chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
10851104
chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
@@ -1090,7 +1109,7 @@ int write_commit_graph(const char *obj_dir,
10901109
chunk_ids[3] = 0;
10911110
chunk_ids[4] = 0;
10921111

1093-
chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1112+
chunk_offsets[0] = header_size + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
10941113
chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
10951114
chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
10961115
chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;

commit-graph.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ int generation_numbers_enabled(struct repository *r);
6868
#define COMMIT_GRAPH_APPEND (1 << 0)
6969
#define COMMIT_GRAPH_PROGRESS (1 << 1)
7070

71-
int write_commit_graph_reachable(const char *obj_dir, unsigned int flags);
71+
int write_commit_graph_reachable(const char *obj_dir, unsigned int flags,
72+
unsigned char version);
7273
int write_commit_graph(const char *obj_dir,
7374
struct string_list *pack_indexes,
7475
struct string_list *commit_hex,
75-
unsigned int flags);
76+
unsigned int flags,
77+
unsigned char version);
7678

7779
int verify_commit_graph(struct repository *r, struct commit_graph *g);
7880

0 commit comments

Comments
 (0)