8000 Add example program to emulate 'git log' and create pathspec matching API by arrbee · Pull Request #1711 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Add example program to emulate 'git log' and create pathspec matching API #1711

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
arrbee committed Jul 8, 2013
commit 5865a7ab7ffb19e5e5eb485d0eea98225710574f
3 changes: 3 additions & 0 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 7 additions & 22 deletions src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@

#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);

git__free(commit->raw_header);
git__free(commit->message);
git__free(commit->message_encoding);

git__free(commit);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
0