Created
December 13, 2012 23:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure if you care about this, but this script fails when you are in detached head state: