8000 sha256: indirection for experimental functions by ethomson · Pull Request #6354 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

sha256: indirection for experimental functions #6354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading 8000
Diff view
Diff view
31 changes: 30 additions & 1 deletion examples/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ static void oid_parsing(git_oid *oid)
* this throughout the example for storing the value of the current SHA
* key we're working with.
*/
#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(oid, hex, GIT_OID_SHA1);
#else
git_oid_fromstr(oid, hex);
#endif

/*
* Once we've converted the string into the oid value, we can get the raw
Expand Down Expand Up @@ -287,9 +291,14 @@ static void commit_writing(git_repository *repo)
* parents. Here we're creating oid objects to create the commit with,
* but you can also use
*/
#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
git_tree_lookup(&tree, repo, &tree_id);
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
#else
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
#endif
git_tree_lookup(&tree, repo, &tree_id);
git_commit_lookup(&parent, repo, &parent_id);

/**
Expand Down Expand Up @@ -353,7 +362,11 @@ static void commit_parsing(git_repository *repo)

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

#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1);
#else
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
#endif

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

error = git_tag_lookup(&tag, repo, &oid);
check_error(error, "looking up tag");
Expand Down Expand Up @@ -470,7 +487,11 @@ static void tree_parsing(git_repository *repo)
/**
* Create the oid and lookup the tree object just like the other objects.
*/
#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1", GIT_OID_SHA1);
#else
git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
#endif
git_tree_lookup(&tree, repo, &oid);

/**
Expand Down Expand Up @@ -524,7 +545,11 @@ static void blob_parsing(git_repository *repo)

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

#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1);
#else
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
#endif
git_blob_lookup(&blob, repo, &oid);

/**
Expand Down Expand Up @@ -566,7 +591,11 @@ static void revwalking(git_repository *repo)

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

#ifdef GIT_EXPERIMENTAL_SHA256
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
#else
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
#endif

/**
* To use the revwalker, create a new walker, tell it how you want to sort
Expand Down
6 changes: 6 additions & 0 deletions examples/rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct ar
if (push_spec(repo, walk, curr, hide) == 0)
continue;

#ifdef GIT_EXPERIMENTAL_SHA256
if ((error = git_oid_fromstr(&oid, curr, GIT_OID_SHA1)))
return error;
#else
if ((error = git_oid_fromstr(&oid, curr)))
return error;
#endif

if ((error = push_commit(walk, &oid, hide)))
return error;
}
Expand Down
17 changes: 17 additions & 0 deletions fuzzers/packfile_fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
fprintf(stderr, "Failed to limit maximum pack object count\n");
abort();
}

#ifdef GIT_EXPERIMENTAL_SHA256
if (git_odb_new(&odb, NULL) < 0) {
fprintf(stderr, "Failed to create the odb\n");
abort();
}
#else
if (git_odb_new(&odb) < 0) {
fprintf(stderr, "Failed to create the odb\n");
abort();
}
#endif

if (git_mempack_new(&mempack) < 0) {
fprintf(stderr, "Failed to create the mempack\n");
abort();
Expand Down Expand Up @@ -90,10 +99,18 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (git_indexer_append(indexer, data, size, &stats) < 0)
goto cleanup;
if (append_hash) {
#ifdef GIT_EXPERIMENTAL_SHA256
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
abort();
}
#else
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
abort();
}
#endif

if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) {
goto cleanup;
}
Expand Down
16 changes: 16 additions & 0 deletions include/git2/odb.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ typedef struct {
* @param opts the options for this object database or NULL for defaults
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
#else
GIT_EXTERN(int) git_odb_new(git_odb **out);
#endif

/**
* Create a new object database and automatically add
Expand All @@ -87,10 +91,14 @@ GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
* @param opts the options for this object database or NULL for defaults
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_odb_open(
git_odb **out,
const char *objects_dir,
const git_odb_options *opts);
#else
GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
#endif

/**
* Add an on-disk alternate to an existing Object DB.
Expand Down Expand Up @@ -471,12 +479,16 @@ GIT_EXTERN(int) git_odb_write_multi_pack_index(
* @param oid_type the oid type to hash to
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_odb_hash(
git_oid *out,
const void *data,
size_t len,
git_object_t object_type,
git_oid_t oid_type);
#else
GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
#endif

/**
* Read a file from disk and fill a git_oid with the object id
Expand All @@ -492,11 +504,15 @@ GIT_EXTERN(int) git_odb_hash(
* @param oid_type the oid type to hash to
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_odb_hashfile(
git_oid *out,
const char *path,
git_object_t object_type,
git_oid_t oid_type);
#else
GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type);
#endif

/**
* Create a copy of an odb_object
Expand Down
10 changes: 10 additions & 0 deletions include/git2/odb_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,20 @@ typedef struct {
*
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_odb_backend_loose(
git_odb_backend **out,
const char *objects_dir,
git_odb_backend_loose_options *opts);
#else
GIT_EXTERN(int) git_odb_backend_loose(
git_odb_backend **out,
const char *objects_dir,
int compression_level,
int do_fsync,
unsigned int dir_mode,
unsigned int file_mode);
#endif

/**
* Create a backend out of a single packfile
Expand Down
16 changes: 16 additions & 0 deletions include/git2/oid.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ typedef struct git_oid {
* @param type the type of object id
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
#else
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
#endif

/**
* Parse a hex formatted NUL-terminated string into a git_oid.
Expand All @@ -132,7 +136,11 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
* @param type the type of object id
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
#else
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
#endif

/**
* Parse N characters of a hex formatted object id into a git_oid.
Expand All @@ -146,7 +154,11 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
* @param type the type of object id
* @return 0 or an error code
*/
#ifdef GIT_EXPERIMENTAL_SHA256
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type);
#else
GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
#endif

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

/**
* Format a git_oid into a hex string.
Expand Down
5 changes: 5 additions & 0 deletions src/cli/cmd_hash_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ static int hash_buf(git_odb *odb, git_str *buf, git_object_t type)
if (git_odb_write(&oid, odb, buf->ptr, buf->size, type) < 0)
return cli_error_git();
} else {
#ifdef GIT_EXPERIMENTAL_SHA256
if (git_odb_hash(&oid, buf->ptr, buf->size, type, GIT_OID_SHA1) < 0)
return cli_error_git();
#else
if (git_odb_hash(&oid, buf->ptr, buf->size, type) < 0)
return cli_error_git();
#endif
}

if (printf("%s\n", git_oid_tostr_s(&oid)) < 0)
Expand Down
4 changes: 2 additions & 2 deletions src/libgit2/commit_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ static int git_commit_graph_entry_get_byindex(
}

commit_data = file->commit_data + pos * (GIT_OID_SHA1_SIZE + 4 * sizeof(uint32_t));
git_oid_fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1);
git_oid__fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1);
e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE)));
e->parent_indices[1] = ntohl(
*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + sizeof(uint32_t))));
Expand Down Expand Up @@ -471,7 +471,7 @@ static int git_commit_graph_entry_get_byindex(
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/libgit2/diff_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int git_diff_file_content__init_from_src(
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
} else {
int error;
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0)
if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0)
return error;
fc->file->size = src->buflen;
fc->file->id_abbrev = GIT_OID_SHA1_HEXSIZE;
Expand Down Expand Up @@ -411,7 +411,7 @@ static int diff_file_content_load_workdir(

/* once data is loaded, update OID if we didn't have it previously */
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
error = git_odb_hash(
error = git_odb__hash(
&fc->file->id, fc->map.data, fc->map.len,
GIT_OBJECT_BLOB, GIT_OID_SHA1);
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
Expand Down
3 changes: 2 additions & 1 deletion src/libgit2/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "git2/transport.h"
#include "git2/sys/remote.h"

#include "oid.h"
#include "remote.h"
#include "refspec.h"
#include "pack.h"
Expand Down Expand Up @@ -75,7 +76,7 @@ static int maybe_want_oid(git_remote *remote, git_refspec *spec)
oid_head = git__calloc(1, sizeof(git_remote_head));
GIT_ERROR_CHECK_ALLOC(oid_head);

git_oid_fromstr(&oid_head->oid, spec->src, GIT_OID_SHA1);
git_oid__fromstr(&oid_head->oid, spec->src, GIT_OID_SHA1);

if (spec->dst) {
oid_head->name = git__strdup(spec->dst);
Expand Down
2 changes: 1 addition & 1 deletion src/libgit2/fetchhead.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static int fetchhead_ref_parse(
return -1;
}

if (git_oid_fromstr(oid, oid_str, GIT_OID_SHA1) < 0) {
if (git_oid__fromstr(oid, oid_str, GIT_OID_SHA1) < 0) {
const git_error *oid_err = git_error_last();
const char *err_msg = oid_err ? oid_err->message : "invalid object ID";

Expand Down
4 changes: 2 additions & 2 deletions src/libgit2/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ static int read_reuc(git_index *index, const char *buffer, size_t size)
return index_error_invalid("reading reuc entry oid");
}

if (git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer, GIT_OID_SHA1) < 0)
if (git_oid__fromraw(&lost->oid[i], (const unsigned char *) buffer, GIT_OID_SHA1) < 0)
return -1;

size -= GIT_OID_SHA1_SIZE;
Expand Down Expand Up @@ -2484,7 +2484,7 @@ static int read_entry(
entry.file_size = ntohl(source.file_size);
entry.flags = ntohs(source.flags);

if (git_oid_fromraw(&entry.id, source.oid, GIT_OID_SHA1) < 0)
if (git_oid__fromraw(&entry.id, source.oid, GIT_OID_SHA1) < 0)
return -1;

if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) {
Expand Down
2 changes: 1 addition & 1 deletion src/libgit2/indexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ static int fix_thin_pack(git_indexer *idx, git_indexer_progress *stats)
return -1;
}

git_oid_fromraw(&base, base_info, GIT_OID_SHA1);
git_oid__fromraw(&base, base_info, GIT_OID_SHA1);
git_mwindow_close(&w);

if (has_entry(idx, &base))
Expand Down
2 changes: 1 addition & 1 deletion src/libgit2/iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ static int filesystem_iterator_entry_hash(

if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) &&
!(error = git_path_validate_str_length(iter->base.repo, &fullpath)))
error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, GIT_OID_SHA1);
error = git_odb__hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, GIT_OID_SHA1);

git_str_dispose(&fullpath);
return error;
Expand Down
2 changes: 1 addition & 1 deletion src/libgit2/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ int git_repository_mergehead_foreach(
goto cleanup;
}

if ((error = git_oid_fromstr(&oid, line, GIT_OID_SHA1)) < 0)
if ((error = git_oid__fromstr(&oid, line, GIT_OID_SHA1)) < 0)
goto cleanup;

if ((error = cb(&oid, payload)) != 0) {
Expand Down
Loading
0