mirror of
https://git.proxmox.com/git/libgit2
synced 2026-01-06 04:46:34 +00:00
Convert commit->parent_ids to git_array_t
This converts the array of parent SHAs from a git_vector where each SHA has to be separately allocated to a git_array_t where all the SHAs can be kept in one block. Since the two collections have almost identical APIs, there isn't much involved in making the change. I did add an API to git_array_t so that it could be allocated at a precise initial size.
This commit is contained in:
parent
bc6f0839eb
commit
9abc78ae61
@ -30,6 +30,9 @@
|
||||
#define git_array_init(a) \
|
||||
do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
|
||||
|
||||
#define git_array_init_to_size(a, desired) \
|
||||
do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0)
|
||||
|
||||
#define git_array_clear(a) \
|
||||
do { git__free((a).ptr); git_array_init(a); } while (0)
|
||||
|
||||
|
||||
29
src/commit.c
29
src/commit.c
@ -19,24 +19,11 @@
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
static void clear_parents(git_commit *commit)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < commit->parent_ids.length; ++i) {
|
||||
git_oid *parent = git_vector_get(&commit->parent_ids, i);
|
||||
git__free(parent);
|
||||
}
|
||||
|
||||
git_vector_clear(&commit->parent_ids);
|
||||
}
|
||||
|
||||
void git_commit__free(void *_commit)
|
||||
{
|
||||
git_commit *commit = _commit;
|
||||
|
||||
clear_parents(commit);
|
||||
git_vector_free(&commit->parent_ids);
|
||||
git_array_clear(commit->parent_ids);
|
||||
|
||||
git_signature_free(commit->author);
|
||||
git_signature_free(commit->committer);
|
||||
@ -44,6 +31,7 @@ void git_commit__free(void *_commit)
|
||||
git__free(commit->raw_header);
|
||||
git__free(commit->message);
|
||||
git__free(commit->message_encoding);
|
||||
|
||||
git__free(commit);
|
||||
}
|
||||
|
||||
@ -198,8 +186,8 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
|
||||
if (parent_count < 1)
|
||||
parent_count = 1;
|
||||
|
||||
if (git_vector_init(&commit->parent_ids, parent_count, NULL) < 0)
|
||||
return -1;
|
||||
git_array_init_to_size(commit->parent_ids, parent_count);
|
||||
GITERR_CHECK_ARRAY(commit->parent_ids);
|
||||
|
||||
if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
|
||||
goto bad_buffer;
|
||||
@ -209,13 +197,10 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
|
||||
*/
|
||||
|
||||
while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) {
|
||||
git_oid *new_id = git__malloc(sizeof(git_oid));
|
||||
git_oid *new_id = git_array_alloc(commit->parent_ids);
|
||||
GITERR_CHECK_ALLOC(new_id);
|
||||
|
||||
git_oid_cpy(new_id, &parent_id);
|
||||
|
||||
if (git_vector_insert(&commit->parent_ids, new_id) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
commit->author = git__malloc(sizeof(git_signature));
|
||||
@ -284,7 +269,7 @@ GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
|
||||
GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header)
|
||||
GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time)
|
||||
GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
|
||||
GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)commit->parent_ids.length)
|
||||
GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids))
|
||||
GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id);
|
||||
|
||||
int git_commit_tree(git_tree **tree_out, const git_commit *commit)
|
||||
@ -298,7 +283,7 @@ const git_oid *git_commit_parent_id(
|
||||
{
|
||||
assert(commit);
|
||||
|
||||
return git_vector_get(&commit->parent_ids, n);
|
||||
return git_array_get(commit->parent_ids, n);
|
||||
}
|
||||
|
||||
int git_commit_parent(
|
||||
|
||||
@ -10,14 +10,14 @@
|
||||
#include "git2/commit.h"
|
||||
#include "tree.h"
|
||||
#include "repository.h"
|
||||
#include "vector.h"
|
||||
#include "array.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
struct git_commit {
|
||||
git_object object;
|
||||
|
||||
git_vector parent_ids;
|
||||
git_array_t(git_oid) parent_ids;
|
||||
git_oid tree_id;
|
||||
|
||||
git_signature *author;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user