limp_chimp
limp_chimp

Reputation: 15153

Get the hash of the current HEAD of a repository using only HTTP

I know that if I have a Git repository checked out, I can use the git CLI to get the current commit hash. But is there a way to do this just over HTTP, without cloning the repository?

For example, let's say that I want to get the current hash of https://github.com/jashkenas/coffeescript. Manually, I can go to the page, press Y on my keyboard, and see that the hash is (at the time of this writing) 1f197fcc1bf1a15bc45a6b23fb3f706ffcb77025. Which means that if at some later point I wanted to download the CoffeeScript source as it is at this moment, I could download https://github.com/jashkenas/coffeescript/archive/1f197fcc1bf1a15bc45a6b23fb3f706ffcb77025.zip, and it would give me the source.

Is there a way to do this just using HTTP (e.g. curl)? Can I hit some endpoint which will tell me what the current hash is of the default branch? Generalizing a bit, are there endpoints which will tell me the hash of

Upvotes: 2

Views: 2622

Answers (4)

user149341
user149341

Reputation:

If you wanted this information on GitHub specifically, you could retrieve it using the GitHub API, which will return a somewhat large JSON structure with exhaustive information on the state of that branch:

https://api.github.com/repos/jashkenas/coffeescript/branches/master

(Or remove the branch name to get a more terse listing of all branches and their heads.)

For Git repositories hosted on other web services, it's possible to fetch the resource /info/refs under the repository (e.g, http://example.com/repo.git/info/refs) to get a listing of all branches, e.g.:

2aae6c35c94fcfb415dbe95f408b9ce91ee846ed    refs/heads/master

However, this is not available on GitHub, because GitHub disabled the "dumb" HTTP transport in 2011 for performance reasons. The replacement "smart" transport, while far superior for Git's own use, isn't usable outside that context.

Upvotes: 3

jmargolisvt
jmargolisvt

Reputation: 6088

According to GitHub API documentation, GET /repos/:owner/:repo/git/commits/:sha should get you the current SHA-1 hash and more.

You can get a tag with this: GET /repos/:owner/:repo/git/tags/:sha

Upvotes: 0

WanderingPetriDish
WanderingPetriDish

Reputation: 21

This command appears to be what you are looking for.

git ls-remote https://github.com/jashkenas/coffeescript

You can parse the SHA-1 hash out from the line that ends with HEAD and carriage return.

E.g. using grep to show you the line I am talking about:

git ls-remote https://github.com/jashkenas/coffeescript | grep "HEAD$"

Upvotes: 1

VonC
VonC

Reputation: 1323223

I would rather use the GitHub API Reference to query the SHA1 of:

In each case, you get a JSON answer similar to (for a given tag for instance)

{
  "ref": "refs/tags/v1.7.0",
  "url": "https://api.github.com/repos/git/git/git/refs/tags/v1.7.0",
  "object": {
    "sha": "15c6c83c4950d80e6bbabc5186c90b35629ce4f7",
    "type": "tag",
    "url": "https://api.github.com/repos/git/git/git/tags/15c6c83c4950d80e6bbabc5186c90b35629ce4f7"
  }
}

Note: to get the HEAD (in GitHub, called "default branch"), see "How do I find the default branch for a repository using the Github v3 API" (and repos get)

Make a call to /repos/:owner/:repo and read the master_branch property value - this is the name of the default branch.

Example: https://api.github.com/repos/git/git

"default_branch": "master",

I have implemented a short Go program which does just that (find the SHA1 of a GitHub repo reference)

See VonC/ghref

  • to find the default branch of a GitHub repo

    vonc@voncm:~/prog/git/ghref$ bin/ghref VonC/ghref
    master
    
  • to find the SHA1 of a branch of a GitHub repo

    vonc@voncm:~/prog/git/ghref$ bin/ghref VonC/ghref master
    c024c28ff3e17cd6864c6d692845860b4c3d8003
    
  • it works with tags too:

    vonc@voncm:~/prog/git/ghref$ bin/ghref VonC/ghref v1.1.0
    59a4be1888c2e8d9f7c83892c4c16b92cc333328         (^^^^^^)
    
  • to find the SHA1 of the default branch of a GitHub repo:

    vonc@voncm:~/prog/git/ghref$ bin/ghref VonC/ghref | xargs bin/ghref VonC/ghref
    c024c28ff3e17cd6864c6d692845860b4c3d8003
    

Upvotes: 3

Related Questions