13

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

chaZm
  • 404
  • 1
  • 3
  • 11
  • 2
    How are you getting commits from GitHub? Are you using repository hooks (http://developer.github.com/v3/repos/hooks/) or are you fetching commits manually (http://developer.github.com/v3/repos/commits/)? If you are using hooks, then you will be notified which branch was pushed to -- http://developer.github.com/v3/activity/events/types/#pushevent. – Ivan Zuzak May 13 '13 at 08:47
  • I am using both actually. First one informs me that there was a commit added and then I use second to gets its data. But thank you anyway - I will try to use that information. – chaZm May 13 '13 at 10:59
  • I will need to get all the commits in the branch - not the last one in the push. So I need either a way to get all commit by branch or find out a branch in the list of all comments – chaZm May 13 '13 at 11:15
  • Each commit object has the parent(s)'s sha(s). If you keep track of the HEAD of each branch, you can likely figure it out. If it matches none of the existing heads, that means the chances are it's a branch from an earlier commit, but then you just request all the commits on the new branch (which you'll have from the Push Event) – Ian Stapleton Cordasco May 13 '13 at 23:23

3 Answers3

13

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.

Ivan Zuzak
  • 18,068
  • 3
  • 69
  • 61
  • 2
    I would also strongly advise that @chaZm store the ETag information on those listings so that they can on the next pass through only receive the commits they haven't already seen (assuming they're storing the old information somewhere already). – Ian Stapleton Cordasco May 13 '13 at 19:32
9

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.

nathancahill
  • 10,452
  • 9
  • 51
  • 91
user2707671
  • 1,694
  • 13
  • 12
2

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
joydeba
  • 778
  • 1
  • 9
  • 24