Skip to content

Instantly share code, notes, and snippets.

@wbbradley
Last active September 2, 2015 21:40
Show Gist options
  • Save wbbradley/a88e537fe109bd3b35d1 to your computer and use it in GitHub Desktop.
Save wbbradley/a88e537fe109bd3b35d1 to your computer and use it in GitHub Desktop.
A script to show a git reflog with names for any branches that are recognized
#! /usr/local/bin/python
import subprocess
def get_refs():
refs = {}
proc = subprocess.Popen([
'git', 'show-ref', '--heads', '--tags', '--dereference'
], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
# the real code does filtering here
sha, ref = line.rstrip().split(' ')
if sha in refs:
if len(refs[sha]) > len(ref):
refs[sha] = ref
else:
refs[sha] = ref
else:
break
return refs
def get_reflog():
shas = []
proc = subprocess.Popen([
'git', 'reflog', '--no-color',
], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
sha_short = line.rstrip().split(' ')[0]
if sha_short not in shas:
shas = shas + [sha_short]
else:
break
if len(shas) > 19:
break
return shas
refs = get_refs()
shas = get_reflog()
for sha_short in shas:
found_ref = sha_short
for sha, ref in refs.iteritems():
if sha_short in sha:
found_ref = ref
break
print found_ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment