Skip to content

Commit c6e1f2b

Browse files
committed
commit_graph: use a byte array for checksum
1 parent b7429e1 commit c6e1f2b

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/commit_graph.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ int git_commit_graph_file_parse(
201201
struct git_commit_graph_chunk *last_chunk;
202202
uint32_t i;
203203
off64_t last_chunk_offset, chunk_offset, trailer_offset;
204-
git_oid cgraph_checksum = {{0}};
204+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
205+
size_t checksum_size;
205206
int error;
206207
struct git_commit_graph_chunk chunk_oid_fanout = {0}, chunk_oid_lookup = {0},
207208
chunk_commit_data = {0}, chunk_extra_edge_list = {0},
@@ -227,13 +228,15 @@ int git_commit_graph_file_parse(
227228
*/
228229
last_chunk_offset = sizeof(struct git_commit_graph_header) + (1 + hdr->chunks) * 12;
229230
trailer_offset = size - GIT_OID_RAWSZ;
231+
checksum_size = GIT_HASH_SHA1_SIZE;
232+
230233
if (trailer_offset < last_chunk_offset)
231234
return commit_graph_error("wrong commit-graph size");
232-
git_oid_cpy(&file->checksum, (git_oid *)(data + trailer_offset));
235+
memcpy(file->checksum, (data + trailer_offset), checksum_size);
233236

234-
if (git_hash_buf(cgraph_checksum.id, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
237+
if (git_hash_buf(checksum, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
235238
return commit_graph_error("could not calculate signature");
236-
if (!git_oid_equal(&cgraph_checksum, &file->checksum))
239+
if (memcmp(checksum, file->checksum, checksum_size) != 0)
237240
return commit_graph_error("index signature mismatch");
238241

239242
chunk_hdr = data + sizeof(struct git_commit_graph_header);
@@ -476,7 +479,8 @@ bool git_commit_graph_file_needs_refresh(const git_commit_graph_file *file, cons
476479
git_file fd = -1;
477480
struct stat st;
478481
ssize_t bytes_read;
479-
git_oid cgraph_checksum = {{0}};
482+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
483+
size_t checksum_size = GIT_HASH_SHA1_SIZE;
480484

481485
/* TODO: properly open the file without access time using O_NOATIME */
482486
fd = git_futils_open_ro(path);
@@ -494,12 +498,12 @@ bool git_commit_graph_file_needs_refresh(const git_commit_graph_file *file, cons
494498
return true;
495499
}
496500

497-
bytes_read = p_pread(fd, cgraph_checksum.id, GIT_OID_RAWSZ, st.st_size - GIT_OID_RAWSZ);
501+
bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - checksum_size);
498502
p_close(fd);
499-
if (bytes_read != GIT_OID_RAWSZ)
503+
if (bytes_read != (ssize_t)checksum_size)
500504
return true;
501505

502-
return !git_oid_equal(&cgraph_checksum, &file->checksum);
506+
return (memcmp(checksum, file->checksum, checksum_size) != 0);
503507
}
504508

505509
int git_commit_graph_entry_find(
@@ -974,7 +978,8 @@ static int commit_graph_write(
974978
off64_t offset;
975979
git_str oid_lookup = GIT_STR_INIT, commit_data = GIT_STR_INIT,
976980
extra_edge_list = GIT_STR_INIT;
977-
git_oid cgraph_checksum = {{0}};
981+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
982+
size_t checksum_size;
978983
git_hash_ctx ctx;
979984
struct commit_graph_write_hash_context hash_cb_data = {0};
980985

@@ -987,6 +992,7 @@ static int commit_graph_write(
987992
hash_cb_data.cb_data = cb_data;
988993
hash_cb_data.ctx = &ctx;
989994

995+
checksum_size = GIT_HASH_SHA1_SIZE;
990996
error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1);
991997
if (error < 0)
992998
return error;
@@ -1133,10 +1139,10 @@ static int commit_graph_write(
11331139
goto cleanup;
11341140

11351141
/* Finalize the checksum and write the trailer. */
1136-
error = git_hash_final(cgraph_checksum.id, &ctx);
1142+
error = git_hash_final(checksum, &ctx);
11371143
if (error < 0)
11381144
goto cleanup;
1139-
error = write_cb((const char *)&cgraph_checksum, sizeof(cgraph_checksum), cb_data);
1145+
error = write_cb((char *)checksum, checksum_size, cb_data);
11401146
if (error < 0)
11411147
goto cleanup;
11421148

src/commit_graph.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "map.h"
1717
#include "vector.h"
18+
#include "oid.h"
19+
#include "hash.h"
1820

1921
/**
2022
* A commit-graph file.
@@ -55,7 +57,7 @@ typedef struct git_commit_graph_file {
5557
size_t num_extra_edge_list;
5658

5759
/* The trailer of the file. Contains the SHA1-checksum of the whole file. */
58-
git_oid checksum;
60+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
5961
} git_commit_graph_file;
6062

6163
/**

0 commit comments

Comments
 (0)