Skip to content

Instantly share code, notes, and snippets.

@joequery
Created August 14, 2013 14:19
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 joequery/6231493 to your computer and use it in GitHub Desktop.
Save joequery/6231493 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment