chaZm
chaZm

Reputation: 404

Get git commits by branch name\id with git api

I am working on automated service to work with Git hub repositories. And I am having problem on my side - I can't find a way to get all commit in particular branch by its hash\name.

My code is an automated tool to make code reviewes. So I've added a feature to ignore particular branch in my review process (ex. a testing branch or something like that). So in my service I am marking branch as ignored. Once I get commits from git hub api - there is no information there about which branch is current commit belongs to.

I started thinking that my overall github idea is wrong - since commit-branch link is pretty obvious thing so there should be something that made the API developers to ignore that in the GetCommits method

So my question is - Is there a way to find out which branch commit (using v3 api json result) belongs to in github api (v3 - GET /repos/:owner/:repo/commits/:sha).

Thanks

Upvotes: 13

Views: 11965

Answers (3)

joydeba
joydeba

Reputation: 1225

In Python with PyGitHub Library (https://pygithub.readthedocs.io/en/latest/index.html)

You can do

g = Github(accesskey) # PyGitHub object
repo = g.get_repo(repository) # Repository
commits = repo.get_commits(sha='stable-2.9') # Commits by a branch

Upvotes: 2

user2707671
user2707671

Reputation: 1704

If you just want to check if your commit is for example on branch test123, do:

https://api.github.com/repos/golang/go/compare/test123...001a75a74c4a27901b0b536efe1be581612c52a9

and check the status.

If it's 'identical' or 'behind', then the commit is part of the branch. If it is different, it's not.

Upvotes: 9

Ivan Zuzak
Ivan Zuzak

Reputation: 18762

There is currently no way in GitHub's API to ask if a commit is in the history of a specific branch. The only thing you can do is fetch all the commits for a specific branch, and then iterate through the commits to see if a specific commit is in the list. To do that, make a request to /repos/:owner/:repo/commits?sha=branchname, where branchname is the name of the branch you want to fetch commits for e.g. https://api.github.com/repos/izuzak/pmrpc/commits?sha=master.

Upvotes: 14

Related Questions