Skip to content

Instantly share code, notes, and snippets.

@matchu
Created November 8, 2018 01:17
Show Gist options
  • Save matchu/d32321c742df154c52dac0e40ef26458 to your computer and use it in GitHub Desktop.
Save matchu/d32321c742df154c52dac0e40ef26458 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import subprocess
CHECKOUT_RE = re.compile(
r'[0-9a-f]+ HEAD@\{[0-9]+\}: checkout: moving from .+? to (.+)')
def main():
reflog = subprocess.check_output(['git', 'reflog', '-n', '1000'])
lines = reflog.splitlines()
checkout_matches = [CHECKOUT_RE.match(l) for l in lines]
checkouts = filter(None, checkout_matches)
checkout_destinations_with_dupes = [match.group(1) for match in checkouts]
checkout_destinations = []
checkout_destinations_set = set()
for branch in checkout_destinations_with_dupes:
if branch not in checkout_destinations_set:
checkout_destinations.append(branch)
checkout_destinations_set.add(branch)
if len(checkout_destinations) >= 10:
break
for index, branch in enumerate(checkout_destinations):
print '[{}] {}'.format(index, branch)
chosen_index_str = raw_input('Navigate to: ')
try:
chosen_index = int(chosen_index_str)
except ValueError:
return
try:
chosen_branch = checkout_destinations[chosen_index]
except IndexError:
return
subprocess.check_call(['git', 'checkout', chosen_branch])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment