Skip to content

Instantly share code, notes, and snippets.

@Fred-Barclay
Created January 15, 2019 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fred-Barclay/336b0dd6cf78fbc85f35c33af7118272 to your computer and use it in GitHub Desktop.
Save Fred-Barclay/336b0dd6cf78fbc85f35c33af7118272 to your computer and use it in GitHub Desktop.
Find the checksum of the most recent commit in a GitHub repository
#!/usr/bin/env python3
"""
Get the sha sum of the last commit on the master branch
of a github repository.
Suitable for using in shell scripts: i.e.
last_commit=`python3 ~/bin/get_last_commit.py $url` will
assign the sha sum of the last commit found at $url
to $last_commit. In this case (for bash at least)
print(shasum) will not actually print the sha sum to the
console.
"""
import json
import requests
import sys
main_url = sys.argv[1]
protocol, path = main_url.split('//')
components = path.split('/')
api_path = 'api.' + components[0] + '/repos/' + '/'.join(components[1:]) + '/commits/master'
api_url = protocol + '//' + api_path
commit = requests.get(api_url)
if commit.ok:
shasum = json.loads(commit.text)['sha']
print(shasum)
sys.exit(0)
else:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment