Skip to content

Instantly share code, notes, and snippets.

@willkg
Created December 13, 2012 23:25
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 willkg/4281067 to your computer and use it in GitHub Desktop.
Save willkg/4281067 to your computer and use it in GitHub Desktop.
git-branchls: command for showing you list of local branches, shas and dates you last touched them sorted by age
#!/usr/bin/env python
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:]))
@mythmon
Copy link

mythmon commented Aug 8, 2013

Not sure if you care about this, but this script fails when you are in detached head state:

$ git branchls
fatal: ambiguous argument '(detached from rlr/l10n-metrics-889928)': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Traceback (most recent call last):
  File "/home/mythmon/bin/git-branchls", line 34, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/home/mythmon/bin/git-branchls", line 25, in main
    output.append(get_details(mem))
  File "/home/mythmon/bin/git-branchls", line 13, in get_details
    [GITCMD, 'show', '--format=%h::%at', ref])
  File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['/usr/bin/git', 'show', '--format=%h::%at', '(detached from rlr/l10n-metrics-889928)']' returned non-zero exit status 128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment