Skip to content

Instantly share code, notes, and snippets.

@scottrogowski
Created March 26, 2015 15:44
Show Gist options
  • Select an option

  • Save scottrogowski/9218e2425a45b1b1ed9b to your computer and use it in GitHub Desktop.

Select an option

Save scottrogowski/9218e2425a45b1b1ed9b to your computer and use it in GitHub Desktop.
Better git branch
#!/usr/bin/env python
# This is a better way to do git branch. This orders branches by last modified
# date and prints the branch notes along with the branch.
# Branches with '+' and '_' in front come first
import subprocess
import sys
from dateutil.parser import parse
import os
rows, columns = os.popen('stty size', 'r').read().split()
def execute(cmd):
try:
return subprocess.check_output(cmd.split())
except:
return ''
def sort_branch(b):
if b['branch'][0] == '_':
typ = 0
elif b['branch'][0] == '+':
typ = 1
else:
typ = 2
return typ, b['dt']
if __name__ == '__main__':
branches = execute("git branch").split('\n')
branches = [b.strip() for b in branches]
branches = filter(None, branches)
max_len = max([len(b) for b in branches]) + 1
formatted = []
for branch in branches:
clean = branch[2:] if branch.startswith('* ') else branch
note = execute("git config branch.%s.note" % clean)
last_modified = execute('git log %s -1 --format="%%cd" --date=short' % clean).strip()[1:-1]
dt = parse(last_modified)
formatted.append({'branch': branch, 'dt': dt, 'note': note})
formatted.sort(key=sort_branch)
format_str = '{:%d} {}' % (int(columns) - 13)
for branch in formatted:
print format_str.format(branch['branch'], branch['dt'].strftime("%b %d, %Y"))
if branch['note']:
print ' ', branch['note'].strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment