Skip to content

Commit 46ce10f

Browse files
authored
Merge pull request libgit2#6354 from libgit2/ethomson/sha256_experimental
sha256: indirection for experimental functions
2 parents 433a133 + b43567d commit 46ce10f

File tree

188 files changed

+1212
-958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+1212
-958
lines changed

examples/general.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ static void oid_parsing(git_oid *oid)
142142
* this throughout the example for storing the value of the current SHA
143143
* key we're working with.
144144
*/
145+
#ifdef GIT_EXPERIMENTAL_SHA256
145146
git_oid_fromstr(oid, hex, GIT_OID_SHA1);
147+
#else
148+
git_oid_fromstr(oid, hex);
149+
#endif
146150

147151
/*
148152
* Once we've converted the string into the oid value, we can get the raw
@@ -287,9 +291,14 @@ static void commit_writing(git_repository *repo)
287291
* parents. Here we're creating oid objects to create the commit with,
288292
* but you can also use
289293
*/
294+
#ifdef GIT_EXPERIMENTAL_SHA256
290295
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
291-
git_tree_lookup(&tree, repo, &tree_id);
292296
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
297+
#else
298+
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
299+
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
300+
#endif
301+
git_tree_lookup(&tree, repo, &tree_id);
293302
git_commit_lookup(&parent, repo, &parent_id);
294303

295304
/**
@@ -353,7 +362,11 @@ static void commit_parsing(git_repository *repo)
353362

354363
printf("\n*Commit Parsing*\n");
355364

365+
#ifdef GIT_EXPERIMENTAL_SHA256
356366
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1);
367+
#else
368+
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
369+
#endif
357370

358371
error = git_commit_lookup(&commit, repo, &oid);
359372
check_error(error, "looking up commit");
@@ -422,7 +435,11 @@ static void tag_parsing(git_repository *repo)
422435
* We create an oid for the tag object if we know the SHA and look it up
423436
* the same way that we would a commit (or any other object).
424437
*/
438+
#ifdef GIT_EXPERIMENTAL_SHA256
425439
git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OID_SHA1);
440+
#else
441+
git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
442+
#endif
426443

427444
error = git_tag_lookup(&tag, repo, &oid);
428445
check_error(error, "looking up tag");
@@ -470,7 +487,11 @@ static void tree_parsing(git_repository *repo)
470487
/**
471488
* Create the oid and lookup the tree object just like the other objects.
472489
*/
490+
#ifdef GIT_EXPERIMENTAL_SHA256
473491
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
492+
#else
493+
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
494+
#endif
474495
git_tree_lookup(&tree, repo, &oid);
475496

476497
/**
@@ -524,7 +545,11 @@ static void blob_parsing(git_repository *repo)
524545

525546
printf("\n*Blob Parsing*\n");
526547

548+
#ifdef GIT_EXPERIMENTAL_SHA256
527549
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1);
550+
#else
551+
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
552+
#endif
528553
git_blob_lookup(&blob, repo, &oid);
529554

530555
/**
@@ -566,7 +591,11 @@ static void revwalking(git_repository *repo)
566591

567592
printf("\n*Revwalking*\n");
568593

594+
#ifdef GIT_EXPERIMENTAL_SHA256
569595
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
596+
#else
597+
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
598+
#endif
570599

571600
/**
572601
* To use the revwalker, create a new walker, tell it how you want to sort

examples/rev-list.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,14 @@ static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct ar
140140
if (push_spec(repo, walk, curr, hide) == 0)
141141
continue;
142142

143+
#ifdef GIT_EXPERIMENTAL_SHA256
143144
if ((error = git_oid_fromstr(&oid, curr, GIT_OID_SHA1)))
144145
return error;
146+
#else
147+
if ((error = git_oid_fromstr(&oid, curr)))
148+
return error;
149+
#endif
150+
145151
if ((error = push_commit(walk, &oid, hide)))
146152
return error;
147153
}

fuzzers/packfile_fuzzer.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
3636
fprintf(stderr, "Failed to limit maximum pack object count\n");
3737
abort();
3838
}
39+
40+
#ifdef GIT_EXPERIMENTAL_SHA256
3941
if (git_odb_new(&odb, NULL) < 0) {
4042
fprintf(stderr, "Failed to create the odb\n");
4143
abort();
4244
}
45+
#else
46+
if (git_odb_new(&odb) < 0) {
47+
fprintf(stderr, "Failed to create the odb\n");
48+
abort();
49+
}
50+
#endif
51+
4352
if (git_mempack_new(&mempack) < 0) {
4453
fprintf(stderr, "Failed to create the mempack\n");
4554
abort();
@@ -90,10 +99,18 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9099
if (git_indexer_append(indexer, data, size, &stats) < 0)
91100
goto cleanup;
92101
if (append_hash) {
102+
#ifdef GIT_EXPERIMENTAL_SHA256
93103
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) {
94104
fprintf(stderr, "Failed to compute the SHA1 hash\n");
95105
abort();
96106
}
107+
#else
108+
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
109+
fprintf(stderr, "Failed to compute the SHA1 hash\n");
110+
abort();
111+
}
112+
#endif
113+
97114
if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) {
98115
goto cleanup;
99116
}

include/git2/odb.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ typedef struct {
6868
* @param opts the options for this object database or NULL for defaults
6969
* @return 0 or an error code
7070
*/
71+
#ifdef GIT_EXPERIMENTAL_SHA256
7172
GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
73+
#else
74+
GIT_EXTERN(int) git_odb_new(git_odb **out);
75+
#endif
7276

7377
/**
7478
* Create a new object database and automatically add
@@ -87,10 +91,14 @@ GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
8791
* @param opts the options for this object database or NULL for defaults
8892
* @return 0 or an error code
8993
*/
94+
#ifdef GIT_EXPERIMENTAL_SHA256
9095
GIT_EXTERN(int) git_odb_open(
9196
git_odb **out,
9297
const char *objects_dir,
9398
const git_odb_options *opts);
99+
#else
100+
GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
101+
#endif
94102

95103
/**
96104
* Add an on-disk alternate to an existing Object DB.
@@ -471,12 +479,16 @@ GIT_EXTERN(int) git_odb_write_multi_pack_index(
471479
* @param oid_type the oid type to hash to
472480
* @return 0 or an error code
473481
*/
482+
#ifdef GIT_EXPERIMENTAL_SHA256
474483
GIT_EXTERN(int) git_odb_hash(
475484
git_oid *out,
476485
const void *data,
477486
size_t len,
478487
git_object_t object_type,
479488
git_oid_t oid_type);
489+
#else
490+
GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
491+
#endif
480492

481493
/**
482494
* Read a file from disk and fill a git_oid with the object id
@@ -492,11 +504,15 @@ GIT_EXTERN(int) git_odb_hash(
492504
* @param oid_type the oid type to hash to
493505
* @return 0 or an error code
494506
*/
507+
#ifdef GIT_EXPERIMENTAL_SHA256
495508
GIT_EXTERN(int) git_odb_hashfile(
496509
git_oid *out,
497510
const char *path,
498511
git_object_t object_type,
499512
git_oid_t oid_type);
513+
#else
514+
GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type);
515+
#endif
500516

501517
/**
502518
* Create a copy of an odb_object

include/git2/odb_backend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,20 @@ typedef struct {
8585
*
8686
* @return 0 or an error code
8787
*/
88+
#ifdef GIT_EXPERIMENTAL_SHA256
8889
GIT_EXTERN(int) git_odb_backend_loose(
8990
git_odb_backend **out,
9091
const char *objects_dir,
9192
git_odb_backend_loose_options *opts);
93+
#else
94+
GIT_EXTERN(int) git_odb_backend_loose(
95+
git_odb_backend **out,
96+
const char *objects_dir,
97+
int compression_level,
98+
int do_fsync,
99+
unsigned int dir_mode,
100+
unsigned int file_mode);
101+
#endif
92102

93103
/**
94104
* Create a backend out of a single packfile

include/git2/oid.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ typedef struct git_oid {
122122
* @param type the type of object id
123123
* @return 0 or an error code
124124
*/
125+
#ifdef GIT_EXPERIMENTAL_SHA256
125126
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
127+
#else
128+
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
129+
#endif
126130

127131
/**
128132
* Parse a hex formatted NUL-terminated string into a git_oid.
@@ -132,7 +136,11 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
132136
* @param type the type of object id
133137
* @return 0 or an error code
134138
*/
139+
#ifdef GIT_EXPERIMENTAL_SHA256
135140
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
141+
#else
142+
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
143+
#endif
136144

137145
/**
138146
* Parse N characters of a hex formatted object id into a git_oid.
@@ -146,7 +154,11 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
146154
* @param type the type of object id
147155
* @return 0 or an error code
148156
*/
157+
#ifdef GIT_EXPERIMENTAL_SHA256
149158
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type);
159+
#else
160+
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
161+
#endif
150162

151163
/**
152164
* Copy an already raw oid into a git_oid structure.
@@ -155,7 +167,11 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, g
155167
* @param raw the raw input bytes to be copied.
156168
* @return 0 on success or error code
157169
*/
170+
#ifdef GIT_EXPERIMENTAL_SHA256
158171
GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type);
172+
#else
173+
GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw);
174+
#endif
159175

160176
/**
161177
* Format a git_oid into a hex string.

src/cli/cmd_hash_object.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ static int hash_buf(git_odb *odb, git_str *buf, git_object_t type)
6464
if (git_odb_write(&oid, odb, buf->ptr, buf->size, type) < 0)
6565
return cli_error_git();
6666
} else {
67+
#ifdef GIT_EXPERIMENTAL_SHA256
6768
if (git_odb_hash(&oid, buf->ptr, buf->size, type, GIT_OID_SHA1) < 0)
6869
return cli_error_git();
70+
#else
71+
if (git_odb_hash(&oid, buf->ptr, buf->size, type) < 0)
72+
return cli_error_git();
73+
#endif
6974
}
7075

7176
if (printf("%s\n", git_oid_tostr_s(&oid)) < 0)

src/libgit2/commit_graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ static int git_commit_graph_entry_get_byindex(
437437
}
438438

439439
commit_data = file->commit_data + pos * (GIT_OID_SHA1_SIZE + 4 * sizeof(uint32_t));
440-
git_oid_fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1);
440+
git_oid__fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1);
441441
e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE)));
442442
e->parent_indices[1] = ntohl(
443443
*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + sizeof(uint32_t))));
@@ -471,7 +471,7 @@ static int git_commit_graph_entry_get_byindex(
471471
}
472472
}
473473

474-
git_oid_fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_SHA1_SIZE], GIT_OID_SHA1);
474+
git_oid__fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_SHA1_SIZE], GIT_OID_SHA1);
475475
return 0;
476476
}
477477

src/libgit2/diff_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int git_diff_file_content__init_from_src(
162162
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
163163
} else {
164164
int error;
165-
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0)
165+
if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0)
166166
return error;
167167
fc->file->size = src->buflen;
168168
fc->file->id_abbrev = GIT_OID_SHA1_HEXSIZE;
@@ -411,7 +411,7 @@ static int diff_file_content_load_workdir(
411411

412412
/* once data is loaded, update OID if we didn't have it previously */
413413
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
414-
error = git_odb_hash(
414+
error = git_odb__hash(
415415
&fc->file->id, fc->map.data, fc->map.len,
416416
GIT_OBJECT_BLOB, GIT_OID_SHA1);
417417
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;

src/libgit2/fetch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "git2/transport.h"
1414
#include "git2/sys/remote.h"
1515

16+
#include "oid.h"
1617
#include "remote.h"
1718
#include "refspec.h"
1819
#include "pack.h"
@@ -75,7 +76,7 @@ static int maybe_want_oid(git_remote *remote, git_refspec *spec)
7576
oid_head = git__calloc(1, sizeof(git_remote_head));
7677
GIT_ERROR_CHECK_ALLOC(oid_head);
7778

78-
git_oid_fromstr(&oid_head->oid, spec->src, GIT_OID_SHA1);
79+
git_oid__fromstr(&oid_head->oid, spec->src, GIT_OID_SHA1);
7980

8081
if (spec->dst) {
8182
oid_head->name = git__strdup(spec->dst);

0 commit comments

Comments
 (0)