joequery
8/14/2013 - 2:19 PM

Git some information about a repo if possible.

Git some information about a repo if possible.

#!/usr/bin/env python
import subprocess
import os

F_DEVNULL = open(os.devnull, 'w')

p = subprocess.Popen(["git", "rev-parse", "--show-toplevel"],
        stdout=subprocess.PIPE, stderr=F_DEVNULL)
repo_root, err = p.communicate()
repo_root = repo_root.strip()


if not repo_root:
    print("(Non-git)")
    exit()


info = False
try:
    with open(os.path.join(repo_root, '.git', 'description')) as f:
        name = f.read()[:35].strip()

        p = subprocess.Popen(["git", "rev-parse", "--abbrev-ref", "HEAD"],
                stdout=subprocess.PIPE, stderr=F_DEVNULL)
        branch, err = p.communicate()
        branch = branch.strip()

        info = "%s (%s)" % (name, branch)
except IOError:
    pass

if info:
    print(info)