What is the equivalent of `git merge-base --is-ancestor` using API?

679 views
Skip to first unread message

Himanshu Gohel

unread,
Jun 15, 2018, 5:25:23 PM6/15/18
to GitHub Java API (http://github-api.kohsuke.org/)
Hi all,

I've been looking through the API docs in trying to work out the equivalent of this, described in git-merge-base man page:

if git merge-base --is-ancestor A B then ... A is an ancestor of B ... fi

How can this be done using the API? I've been experimenting with GHCompare class but not clear yet. Thanks,

--
Himanshu

Himanshu Gohel

unread,
Jun 18, 2018, 5:50:07 PM6/18/18
to GitHub Java API (http://github-api.kohsuke.org/)
Here's some code that worked for the limited cases I had to handle (caveats: setup code not shown, and this sample can be simplified):

def commitCompare = repo.getCompare(commit1, commit2)
if (commitCompare.getStatus() == GHCompare.Status.identical) {
println "Commits are identical: $commit1, $commit2"
return true;
}
def mergeBaseCommit = commitCompare.getMergeBaseCommit().getSHA1();
println "Merge-base commit for $commit1, $commit2 is $mergeBaseCommit"

def compareStatus = repo.getCompare(commit1, mergeBaseCommit).getStatus();
if ( compareStatus == GHCompare.Status.identical )
{
println "$commit1 is an ancestor of $commit2"
} else {
println "$commit1 is not an ancestor of $commit2"
}

Reply all
Reply to author
Forward
0 new messages