Skip to content

Instantly share code, notes, and snippets.

@jkeyes
Created April 15, 2010 23:53
Show Gist options
  • Save jkeyes/367811 to your computer and use it in GitHub Desktop.
Save jkeyes/367811 to your computer and use it in GitHub Desktop.
def get_revision(path=None):
"""
Return the revision identifier of the last commit in a git or svn source pool.
"""
if path is None:
path = "."
import os, re, subprocess
revision = None
os.chdir(path)
if os.path.exists('.git'):
# get git revision
output = subprocess.Popen(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE).communicate()[0]
revision = output.strip()
elif os.path.exists('.svn'):
# get svn revision
REVISION_RE = re.compile('(\d+).*', re.M)
subprocess.Popen(["svn", "up", "-q"], stdout=None)
output = subprocess.Popen(["svnversion"], stdout=subprocess.PIPE).communicate()[0]
match = REVISION_RE.search(output)
if match:
revision = match.group(1)
return revision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment