git-branchp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. # vim: set noet sw=4 ts=4:
  3. import subprocess
  4. import sys
  5. import time
  6. force = len(sys.argv) == 2 and sys.argv[1] == '-f'
  7. output = subprocess.check_output(['git', 'branch', '--merged', 'origin/master', '-a'], universal_newlines=True)
  8. branches = []
  9. for line in output.split('\n'):
  10. if not line.startswith(' ') or ' -> ' in line:
  11. continue
  12. line = line[2:]
  13. if line.startswith('remotes/origin/'):
  14. line = line[8:]
  15. branches.append(line)
  16. day_seconds = 24 * 60 * 60
  17. now = time.time()
  18. cutoff = now - 30 * day_seconds
  19. for branch in branches:
  20. if branch == 'origin/master':
  21. continue
  22. output = subprocess.check_output(['git', 'show', branch, '-s', '--format=%at %ct'], universal_newlines=True)
  23. if not output.endswith('\n'):
  24. raise Exception()
  25. timestamp = max(map(int, output[:-1].split(' ', 2)))
  26. if timestamp > cutoff:
  27. print('ignoring', branch)
  28. continue
  29. print('deleting %s (%.1f days old)' % (branch, (now - timestamp) / day_seconds))
  30. if branch.startswith('origin/'):
  31. subprocess.check_call(['git', 'push', 'origin', ':' + branch[7:]])
  32. else:
  33. subprocess.check_call(['git', 'branch', '-d', branch])