2

Are there any easy ways to grab the git repository (on GitHub) version hash with Python code? I want to use this to handle versioning of 'dev' releases of my software on github.

2
  • 1
    GutHub or Git repositories? stackoverflow.com/questions/5694389/… Commented Oct 10, 2012 at 19:20
  • Sorry it wasn't clear, I meant git repositories (I store them on github). Commented Oct 10, 2012 at 19:24

4 Answers 4

9
def git_version():
    from subprocess import Popen, PIPE
    gitproc = Popen(['git', 'rev-parse','HEAD'], stdout = PIPE)
    (stdout, _) = gitproc.communicate()
    return stdout.strip()
Sign up to request clarification or add additional context in comments.

Comments

1
from subprocess import Popen, PIPE

gitproc = Popen(['git', 'show-ref'], stdout = PIPE)
(stdout, stderr) = gitproc.communicate()

for row in stdout.split('\n'):
    if row.find('HEAD') != -1:
        hash = row.split()[0]
        break

print hash

Comments

1

Like this ?

import subprocess
ref = subprocess.check_output("""
    git 2>/dev/null show-ref | awk '/refs\/heads\/master/{print $1}'
""", shell=True)
print ref

Adapt it if you have something else than master

3 Comments

Via python. run the git command via Popen and parse the output in python.
No need to pipe it to awk. Just read stdout from the Popen object and parse the text in Python. Python is very good as string handling!
Sorry, had to go with tMC's answer. check_output is from 2.7, I'de like backwards compatibility for now.
-1

You can also use the GitHub API for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.