From b1aca6eae084ebe59c5a092314e94019c59ecec6 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Wed, 11 Jul 2012 16:14:12 +0200 Subject: commit: introduce git_commit_nth_gen_ancestor() --- src/commit.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/commit.c') diff --git a/src/commit.c b/src/commit.c index 5acbbc39b..32c47944b 100644 --- a/src/commit.c +++ b/src/commit.c @@ -255,3 +255,37 @@ int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n) return git_commit_lookup(parent, commit->object.repo, parent_oid); } + +int git_commit_nth_gen_ancestor( + git_commit **ancestor, + const git_commit *commit, + unsigned int n) +{ + git_commit *current, *parent; + int error; + + assert(ancestor && commit); + + current = (git_commit *)commit; + + if (n == 0) + return git_commit_lookup( + ancestor, + commit->object.repo, + git_object_id((const git_object *)commit)); + + while (n--) { + error = git_commit_parent(&parent, (git_commit *)current, 0); + + if (current != commit) + git_commit_free(current); + + if (error < 0) + return error; + + current = parent; + } + + *ancestor = parent; + return 0; +} -- cgit v1.2.1