Joachim Breitner
Joachim Breitner

Reputation: 25763

Github API: Check if a branch or repository contains a commit

Can I use the Github API to check if a certain repository contains a certain commit?

At first glance, it seems that the get a single commit API call should work, returning 404 if there is no such commit in the repository. But that is not true: It seems that this call will run successful on commits that are present in forked repositories (possibly due to a pull request). (This effect can also be observed in the regular web interface; this particular commit has not been pulled into that repository yet.)

Upvotes: 15

Views: 4339

Answers (1)

nmanh
nmanh

Reputation: 335

Api GitHub search

For searching other repositories one can use the api, which finds commits via various criteria. (This method returns up to 100 results per page.):

Api usage

Example parameters for q

  • hash:124a9a0ee1d8f1e15e833aff432fbb3b02632105 Matches commits with the hash 124a9a0ee1d8f1e15e833aff432fbb3b02632105.
  • parent:124a9a0ee1d8f1e15e833aff432fbb3b02632105 Matches children of 124a9a0ee1d8f1e15e833aff432fbb3b02632105.

further parameters, like sorting, ordering can be found in the documentation above.

Usage example per hash:

  • example call https://api.github.com/search/commits?q=<searchterm>+<searchterm2>
  • specific call: https://api.github.com/search/commits?q=repo:adejoux/kitchen-wpar+hash:0a3a228e5b250daf06f933b35b3f0eafc715be4f

You need to add an special header, because the api is available for developers to preview

header to add: application/vnd.github.cloak-preview

Upvotes: 4

Related Questions