8000 Merge pull request #6192 from libgit2/ethomson/sha256_preparation · libgit2/libgit2@d9863fc · GitHub
[go: up one dir, main page]

Skip to content

Commit d9863fc

Browse files
authored
Merge pull request #6192 from libgit2/ethomson/sha256_preparation
SHA256: early preparation
2 parents 4efd656 + 70d9bfa commit d9863fc

24 files changed

+228
-131
lines changed

examples/index-pack.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ int lg2_index_pack(git_repository *repo, int argc, char **argv)
1717
git_indexer *idx;
1818
git_indexer_progress stats = {0, 0};
1919
int error;
20-
char hash[GIT_OID_HEXSZ + 1] = {0};
2120
int fd;
2221
ssize_t read_bytes;
2322
char buf[512];
@@ -61,8 +60,7 @@ int lg2_index_pack(git_repository *repo, int argc, char **argv)
6160

6261
printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects);
6362

64-
git_oid_fmt(hash, git_indexer_hash(idx));
65-
puts(hash);
63+
puts(git_indexer_name(idx));
6664

6765
cleanup:
6866
close(fd);

fuzzers/packfile_fuzzer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
101101
if (git_indexer_commit(indexer, &stats) < 0)
102102
goto cleanup;
103103

104-
if (git_str_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
104+
if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0)
105105
goto cleanup;
106106
p_unlink(git_str_cstr(&path));
107107

108108
git_str_clear(&path);
109109

110-
if (git_str_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
110+
if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0)
111111
goto cleanup;
112112
p_unlink(git_str_cstr(&path));
113113

include/git2/index.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,20 @@ GIT_EXTERN(int) git_index_write(git_index *index);
296296
*/
297297
GIT_EXTERN(const char *) git_index_path(const git_index *index);
298298

299+
#ifndef GIT_DEPRECATE_HARD
299300
/**
300301
* Get the checksum of the index
301302
*
302303
* This checksum is the SHA-1 hash over the index file (except the
303304
* last 20 bytes which are the checksum itself). In cases where the
304305
* index does not exist on-disk, it will be zeroed out.
305306
*
307+
* @deprecated this function is deprecated with no replacement
306308
* @param index an existing index object
307309
* @return a pointer to the checksum of the index
308 F438 310
*/
309311
GIT_EXTERN(const git_oid *) git_index_checksum(git_index *index);
312+
#endif
310313

311314
/**
312315
* Read a tree into the index file with stats

include/git2/indexer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,30 @@ GIT_EXTERN(int) git_indexer_append(git_indexer *idx, const void *data, size_t si
129129
*/
130130
GIT_EXTERN(int) git_indexer_commit(git_indexer *idx, git_indexer_progress *stats);
131131

132+
#ifndef GIT_DEPRECATE_HARD
132133
/**
133134
* Get the packfile's hash
134135
*
135136
* A packfile's name is derived from the sorted hashing of all object
136137
* names. This is only correct after the index has been finalized.
137138
*
139+
* @deprecated use git_indexer_name
138140
* @param idx the indexer instance
139141
* @return the packfile's hash
140142
*/
141143
GIT_EXTERN(const git_oid *) git_indexer_hash(const git_indexer *idx);
144+
#endif
145+
146+
/**
147+
* Get the unique name for the resulting packfile.
148+
*
149+
* The packfile's name is derived from the packfile's content.
150+
* This is only correct after the index has been finalized.
151+
*
152+
* @param idx the indexer instance
153+
* @return a NUL terminated string for the packfile name
154+
*/
155+
GIT_EXTERN(const char *) git_indexer_name(const git_indexer *idx);
142156

143157
/**
144158
* Free the indexer and its resources

include/git2/pack.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,30 @@ GIT_EXTERN(int) git_packbuilder_write(
170170
git_indexer_progress_cb progress_cb,
171171
void *progress_cb_payload);
172172

173+
#ifndef GIT_DEPRECATE_HARD
173174
/**
174-
* Get the packfile's hash
175-
*
176-
* A packfile's name is derived from the sorted hashing of all object
177-
* names. This is only correct after the packfile has been written.
178-
*
179-
* @param pb The packbuilder object
175+
* Get the packfile's hash
176+
*
177+
* A packfile's name is derived from the sorted hashing of all object
178+
* names. This is only correct after the packfile has been written.
179+
*
180+
* @deprecated use git_packbuilder_name
181+
* @param pb The packbuilder object
180182
* @return 0 or an error code
181-
*/
183+
*/
182184
GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
185+
#endif
186+
187+
/**
188+
* Get the unique name for the resulting packfile.
189+
*
190+
* The packfile's name is derived from the packfile's content.
191+
* This is only correct after the packfile has been written.
192+
*
193+
* @param pb the packbuilder instance
194+
* @return a NUL terminated string for the packfile name
195+
*/
196+
GIT_EXTERN(const char *) git_packbuilder_name(git_packbuilder *pb);
183197

184198
/**
185199
* Callback used to iterate over packed objects

src/branch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ int git_branch_create(
123123
const git_commit *commit,
124124
int force)
125125
{
126-
return create_branch(ref_out, repository, branch_name, commit, git_oid_tostr_s(git_commit_id(commit)), force);
126+
char commit_id[GIT_OID_HEXSZ + 1];
127+
128+
git_oid_tostr(commit_id, GIT_OID_HEXSZ + 1, git_commit_id(commit));
129+
return create_branch(ref_out, repository, branch_name, commit, commit_id, force);
127130
}
128131

129132
int git_branch_create_from_annotated(

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
/**

src/config_file.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
#include "regexp.h"
2020
#include "sysdir.h"
2121
#include "wildmatch.h"
22+
#include "hash.h"
2223

2324
/* Max depth for [include] directives */
2425
#define MAX_INCLUDE_DEPTH 10
2526

2627
typedef struct config_file {
2728
git_futils_filestamp stamp;
28-
git_oid checksum;
29+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
2930
char *path;
3031
git_array_t(struct config_file) includes;
3132
} config_file;
@@ -132,7 +133,7 @@ static int config_file_is_modified(int *modified, config_file *file)
132133
{
133134
config_file *include;
134135
git_str buf = GIT_STR_INIT;
135-
git_oid hash;
136+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
136137
uint32_t i;
137138
int error = 0;
138139

@@ -144,10 +145,10 @@ static int config_file_is_modified(int *modified, config_file *file)
144145
if ((error = git_futils_readbuffer(&buf, file->path)) < 0)
145146
goto out;
146147

147-
if ((error = git_hash_buf(hash.id, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
148+
if ((error = git_hash_buf(checksum, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
148149
goto out;
149150

150-
if (!git_oid_equal(&hash, &file->checksum)) {
151+
if (memcmp(checksum, file->checksum, GIT_HASH_SHA1_SIZE) != 0) {
151152
*modified = 1;
152153
goto out;
153154
}
@@ -880,7 +881,7 @@ static int config_file_read(
880881
goto out;
881882

882883
git_futils_filestamp_set_from_stat(&file->stamp, &st);
883-
if ((error = git_hash_buf(file->checksum.id, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
884+
if ((error = git_hash_buf(file->checksum, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
884885
goto out;
885886

886887
if ((error = config_file_read_buffer(entries, repo, file, level, depth,

src/hash.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,19 @@ int git_hash_vec(
124124

125125
return error;
126126
}
127+
128+
int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len)
129+
{
130+
static char hex[] = "0123456789abcdef";
131+
char *str = out;
132+
size_t i;
133+
134+
for (i = 0; i < hash_len; i++) {
135+
*str++ = hex[hash[i] >> 4];
136+
*str++ = hex[hash[i] & 0x0f];
137+
}
138+
139+
*str++ = '\0';
140+
141+
return 0;
142+
}

src/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ int git_hash_final(unsigned char *out, git_hash_ctx *c);
4141
int git_hash_buf(unsigned char *out, const void *data, size_t len, git_hash_algorithm_t algorithm);
4242
int git_hash_vec(unsigned char *out, git_str_vec *vec, size_t n, git_hash_algorithm_t algorithm);
4343

44+
int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len);
45+
4446
#endif

0 commit comments

Comments
 (0)
0