| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #!/usr/bin/env python3
- # vim: set noet sw=4 ts=4:
- import subprocess
- import sys
- def main():
- delete = len(sys.argv) == 2 and sys.argv[1] == '-d'
- remotes = all_remotes()
- to_delete = []
- print('will delete:')
- for local, remote in iter_locals():
- if remote not in remotes:
- to_delete.append(local)
- sys.stdout.buffer.write(b'\t' + local)
- print()
- if delete and len(to_delete) > 0:
- subprocess.run([b'git', b'branch', b'-D'] + to_delete, check=True)
- def all_remotes():
- refs = subprocess.run(['git', 'for-each-ref', '--format=%(refname:short)', 'refs/remotes'],
- capture_output=True, check=True)
- return frozenset(refs.stdout.rstrip(b'\n').split(b'\n'))
- def iter_locals():
- refs = subprocess.run(['git', 'for-each-ref', '--format=%(refname:short) %(upstream)', 'refs/heads'],
- capture_output=True, check=True)
- for line in refs.stdout.split(b'\n'):
- if not line:
- continue
- local, remote = line.split(b' ', 1)
- if not remote:
- continue
- assert remote.startswith(b'refs/remotes/')
- remote = remote.removeprefix(b'refs/remotes/')
- yield local, remote
- if __name__ == '__main__':
- main()
|