summaryrefslogtreecommitdiff
path: root/src/libgit2/commit.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2022-09-21 05:09:46 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2023-02-12 22:02:00 +0000
commitfe2ee3a018286b04cd0c64f84d437c7317c8f138 (patch)
tree196a29fc72ef0a27e0277b4d142cc93c28fa4cb7 /src/libgit2/commit.c
parent6204499242101d0f93cd7e56e259c900828e41e1 (diff)
downloadlibgit2-fe2ee3a018286b04cd0c64f84d437c7317c8f138.tar.gz
object: lookup sha256 objects
This is much of the plumbing for the object database to support SHA256, and for objects to be able to parse SHA256 versions of themselves.
Diffstat (limited to 'src/libgit2/commit.c')
-rw-r--r--src/libgit2/commit.c57
1 files changed, 43 insertions, 14 deletions
diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c
index 114ae6fc9..d85fefb3d 100644
--- a/src/libgit2/commit.c
+++ b/src/libgit2/commit.c
@@ -390,7 +390,11 @@ int git_commit_amend(
return error;
}
-static int commit_parse(git_commit *commit, const char *data, size_t size, unsigned int flags)
+static int commit_parse(
+ git_commit *commit,
+ const char *data,
+ size_t size,
+ git_commit__parse_options *opts)
{
const char *buffer_start = data, *buffer;
const char *buffer_end = buffer_start + size;
@@ -401,6 +405,7 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
GIT_ASSERT_ARG(commit);
GIT_ASSERT_ARG(data);
+ GIT_ASSERT_ARG(opts);
buffer = buffer_start;
@@ -409,13 +414,14 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
GIT_ERROR_CHECK_ARRAY(commit->parent_ids);
/* The tree is always the first field */
- if (!(flags & GIT_COMMIT_PARSE_QUICK)) {
+ if (!(opts->flags & GIT_COMMIT_PARSE_QUICK)) {
if (git_object__parse_oid_header(&commit->tree_id,
&buffer, buffer_end, "tree ",
- GIT_OID_SHA1) < 0)
+ opts->oid_type) < 0)
goto bad_buffer;
} else {
- size_t tree_len = strlen("tree ") + GIT_OID_SHA1_HEXSIZE + 1;
+ size_t tree_len = strlen("tree ") + git_oid_hexsize(opts->oid_type) + 1;
+
if (buffer + tree_len > buffer_end)
goto bad_buffer;
buffer += tree_len;
@@ -427,14 +433,14 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
while (git_object__parse_oid_header(&parent_id,
&buffer, buffer_end, "parent ",
- GIT_OID_SHA1) == 0) {
+ opts->oid_type) == 0) {
git_oid *new_id = git_array_alloc(commit->parent_ids);
GIT_ERROR_CHECK_ALLOC(new_id);
git_oid_cpy(new_id, &parent_id);
}
- if (!(flags & GIT_COMMIT_PARSE_QUICK)) {
+ if (!opts || !(opts->flags & GIT_COMMIT_PARSE_QUICK)) {
commit->author = git__malloc(sizeof(git_signature));
GIT_ERROR_CHECK_ALLOC(commit->author);
@@ -458,7 +464,7 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
if ((error = git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n')) < 0)
return error;
- if (flags & GIT_COMMIT_PARSE_QUICK)
+ if (opts && opts->flags & GIT_COMMIT_PARSE_QUICK)
return 0;
/* Parse add'l header entries */
@@ -503,19 +509,39 @@ bad_buffer:
return GIT_EINVALID;
}
-int git_commit__parse_raw(void *commit, const char *data, size_t size)
+int git_commit__parse(
+ void *commit,
+ git_odb_object *odb_obj,
+ git_oid_t oid_type)
{
- return commit_parse(commit, data, size, 0);
+ git_commit__parse_options parse_options = {0};
+ parse_options.oid_type = oid_type;
+
+ return git_commit__parse_ext(commit, odb_obj, &parse_options);
}
-int git_commit__parse_ext(git_commit *commit, git_odb_object *odb_obj, unsigned int flags)
+int git_commit__parse_raw(
+ void *commit,
+ const char *data,
+ size_t size,
+ git_oid_t oid_type)
{
- return commit_parse(commit, git_odb_object_data(odb_obj), git_odb_object_size(odb_obj), flags);
+ git_commit__parse_options parse_options = {0};
+ parse_options.oid_type = oid_type;
+
+ return commit_parse(commit, data, size, &parse_options);
}
-int git_commit__parse(void *_commit, git_odb_object *odb_obj)
+int git_commit__parse_ext(
+ git_commit *commit,
+ git_odb_object *odb_obj,
+ git_commit__parse_options *parse_opts)
{
- return git_commit__parse_ext(_commit, odb_obj, 0);
+ return commit_parse(
+ commit,
+ git_odb_object_data(odb_obj),
+ git_odb_object_size(odb_obj),
+ parse_opts);
}
#define GIT_COMMIT_GETTER(_rvalue, _name, _return, _invalid) \
@@ -985,11 +1011,14 @@ int git_commit_create_with_signature(
git_str commit = GIT_STR_INIT;
git_commit *parsed;
git_array_oid_t parents = GIT_ARRAY_INIT;
+ git_commit__parse_options parse_opts = {0};
+
+ parse_opts.oid_type = repo->oid_type;
/* The first step is to verify that all the tree and parents exist */
parsed = git__calloc(1, sizeof(git_commit));
GIT_ERROR_CHECK_ALLOC(parsed);
- if (commit_parse(parsed, commit_content, strlen(commit_content), 0) < 0) {
+ if (commit_parse(parsed, commit_content, strlen(commit_content), &parse_opts) < 0) {
error = -1;
goto cleanup;
}