Merge branch 'commitparents' of https://github.com/JustinLove/libgit2 into JustinLove-commitparents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/src/commit.c b/src/commit.c
index 6fcbfd7..8af92bf 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -257,6 +257,35 @@ time_t git_commit_time(git_commit *commit)
return commit->commit_time;
}
+unsigned int git_commit_parentcount(git_commit *commit)
+{
+ git_commit_parents *parent;
+ unsigned int count = 0;
+
+ assert(commit);
+ CHECK_FULL_PARSE();
+
+ for (parent = commit->parents; parent != NULL; parent = parent->next) {
+ count++;
+ }
+
+ return count;
+}
+
+git_commit * git_commit_parent(git_commit *commit, unsigned int n)
+{
+ git_commit_parents *parent;
+
+ assert(commit);
+ CHECK_FULL_PARSE();
+
+ for (parent = commit->parents; parent != NULL && n > 0; parent = parent->next) {
+ n--;
+ }
+
+ return parent ? parent->commit : NULL;
+}
+
void git_commit_set_tree(git_commit *commit, git_tree *tree)
{
assert(commit && tree);
diff --git a/src/git/commit.h b/src/git/commit.h
index 8cb3401..70b08e9 100644
--- a/src/git/commit.h
+++ b/src/git/commit.h
@@ -94,6 +94,22 @@ GIT_EXTERN(const git_person *) git_commit_author(git_commit *commit);
GIT_EXTERN(const git_tree *) git_commit_tree(git_commit *commit);
/**
+ * Get the number of parents of this commit
+ *
+ * @param commit a previously loaded commit.
+ * @return integer of count of parents
+ */
+GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit);
+
+/**
+ * Get the specified parent of the commit.
+ * @param commit a previously loaded commit.
+ * @param n the position of the entry
+ * @return a pointer to the commit; NULL if out of bounds
+ */
+GIT_EXTERN(git_commit *) git_commit_parent(git_commit *commit, unsigned int n);
+
+/**
* Add a new parent commit to an existing commit
* @param commit the commit object
* @param new_parent the new commit which will be a parent
diff --git a/tests/t0402-details.c b/tests/t0402-details.c
index 22d1f8e..a01a708 100644
--- a/tests/t0402-details.c
+++ b/tests/t0402-details.c
@@ -31,6 +31,8 @@ BEGIN_TEST(query_details_test)
const git_person *author, *committer;
const char *message, *message_short;
time_t commit_time;
+ unsigned int parents, p;
+ git_commit *parent;
git_oid_mkstr(&id, commit_ids[i]);
@@ -41,6 +43,7 @@ BEGIN_TEST(query_details_test)
author = git_commit_author(commit);
committer = git_commit_committer(commit);
commit_time = git_commit_time(commit);
+ parents = git_commit_parentcount(commit);
must_be_true(strcmp(author->name, "Scott Chacon") == 0);
must_be_true(strcmp(author->email, "schacon@gmail.com") == 0);
@@ -49,6 +52,13 @@ BEGIN_TEST(query_details_test)
must_be_true(strchr(message, '\n') != NULL);
must_be_true(strchr(message_short, '\n') == NULL);
must_be_true(commit_time > 0);
+ must_be_true(0 <= parents && parents <= 2);
+ for (p = 0;p < parents;p++) {
+ parent = git_commit_parent(commit, p);
+ must_be_true(parent != NULL);
+ must_be_true(git_commit_author(parent) != NULL); // is it really a commit?
+ }
+ must_be_true(git_commit_parent(commit, parents) == NULL);
}
git_repository_free(repo);