15

How do I get the Root :tree_sha of a GitHub repository via the GitHub API?

The GitHib API help pages don't seem to explain this critical piece of information:

http://develop.github.com/p/object.html

Can get the contents of a tree by tree SHA

tree/show/:user/:repo/:tree_sha

To get a listing of the root tree for the facebox project from our commit listing, we can call this:

$ curl http://github.com/api/v2/yaml/tree/show/defunkt/facebox/a47803c9ba26213ff194f042ab686a7749b17476

Chris Jacob
  • 11,878
  • 7
  • 47
  • 42

3 Answers3

13

Each commit contains the sha of the entire tree as of that commit. Use the API to get a JSON object representing the master branch.

https://api.github.com/repos/:owner/:repo/branches/master

That branch's last commit includes the tree's sha that I think you're asking for.

This bit of code demonstrates how to get the head_tree_sha in Python.

import requests
token = '0...f'
key = {'Authorization':'token '+token}
master = requests.get('https://api.github.com/repos/'+owner+'/' + repo '/branches/master', headers=key)
master = master.json()
head_tree_sha = master['commit']['commit']['tree']['sha']

https://developer.github.com/v3/git/commits/

Bennett Brown
  • 5,234
  • 1
  • 27
  • 35
7

http://develop.github.com/p/commits.html

The commit tells you its tree sha.

[EDIT]
If you want the tree sha of a subfolder cd into the parent folder of the one you're interested in and run:
git ls-tree HEAD

If you want Root tree sha:
git show HEAD --format=raw
1st line has commit sha
2nd line has tree sha

Chris Jacob
  • 11,878
  • 7
  • 47
  • 42
1

I'm not sure about the GitHub API — however if you want just the hash you can use this command in your clone:

git show HEAD --format=%T | head -1

Or use %t for the abbreviated hash.

Adam Sharp
  • 3,618
  • 25
  • 29