Skip to content

Instantly share code, notes, and snippets.

@Grayson
Created October 11, 2008 14:43
Show Gist options
  • Save Grayson/16274 to your computer and use it in GitHub Desktop.
Save Grayson/16274 to your computer and use it in GitHub Desktop.
Prints a git hash based on commit number.
#!/usr/bin/env python
# Grayson Hansard, 2008
# This is a simple python script that I wrote as part of a system to
# use the number of git commits on a project as an incremental build number system.
import os, sys
if len(sys.argv) < 2:
print "Usage git hash-from-commit-number <commit-number>"
sys.exit()
f = os.popen('git rev-list --all --reverse')
number = int(sys.argv[1])-1 # Correction for 0-based list index
lines = f.readlines()
if number == 0 or number > len(lines):
print "Commit number out of range"
sys.exit()
print lines[number][:-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment