Skip to content

Instantly share code, notes, and snippets.

@willkg
Created May 14, 2012 15:39
Show Gist options
  • Save willkg/2694627 to your computer and use it in GitHub Desktop.
Save willkg/2694627 to your computer and use it in GitHub Desktop.
git-branchls
#!/usr/bin/env python
# Lists all the local branches, short sha, and how many days ago the last
# commit on the tip of that branch was.
#
# This makes it easier to identify and clean up branches that have landed.
import subprocess
import sys
import datetime
GITCMD = '/usr/bin/git'
def get_details(ref):
last_commit = subprocess.check_output(
[GITCMD, 'show', '--format=%h::%at', ref])
last_commit = last_commit.splitlines()[0].split('::')
datestamp = datetime.datetime.fromtimestamp(int(last_commit[1]))
age = datetime.datetime.now() - datestamp
return (ref, last_commit[0], age, datestamp)
def main(argv):
output = []
branches = subprocess.check_output([GITCMD, 'branch'])
for mem in branches.splitlines():
mem = mem.lstrip('*').strip()
output.append(get_details(mem))
output.sort(key=lambda mem: -mem[2])
for mem in output:
print '%-30s %-8s %s (%s)' % mem
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment