alshabib | b6ba508 | 2015-07-09 14:57:23 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from subprocess import check_output |
| 4 | import sys |
| 5 | |
| 6 | def get_merged_branches_by_change_id(): |
| 7 | '''a list of merged branches, by change id excluding support branches and master''' |
| 8 | raw_changeIds = check_output('git log origin/master | grep -i change-id | awk {\' print $2 \'}', shell=True) |
| 9 | changeIds = [b.strip() for b in raw_changeIds.split('\n') if b.strip()] |
| 10 | raw_branches = check_output('git branch -a', shell=True) |
| 11 | branches = [b.strip() for b in raw_branches.split('\n') |
| 12 | if b.strip() and not b.startswith('*') and \ |
| 13 | not b.strip().startswith('onos') and not b.strip().startswith('remotes') and b.strip() != 'master'] |
| 14 | to_delete = [] |
| 15 | for branch in branches: |
Brian O'Connor | 8494262 | 2016-10-14 17:48:19 -0700 | [diff] [blame] | 16 | raw_local_change_ids = check_output('git show %s -- | grep -i change-id | awk {\' print $2 \'}' % branch, shell=True) |
alshabib | b6ba508 | 2015-07-09 14:57:23 -0700 | [diff] [blame] | 17 | local_change_ids = [ b.strip() for b in raw_local_change_ids.split('\n') if b.strip() ] |
| 18 | for local_change_id in local_change_ids: |
| 19 | if local_change_id in changeIds and branch not in to_delete: |
| 20 | to_delete.append(branch) |
| 21 | |
| 22 | return to_delete |
| 23 | |
| 24 | |
| 25 | def delete_branch(branch): |
| 26 | return check_output('git branch -D %s' % branch, shell=True).strip() |
| 27 | |
| 28 | |
| 29 | if __name__ == '__main__': |
| 30 | dry_run = '--confirm' not in sys.argv |
| 31 | one_by_one = '--one-by-one' in sys.argv |
| 32 | to_delete = get_merged_branches_by_change_id() |
| 33 | if len(to_delete) == 0: |
| 34 | print "Nothing to clean" |
| 35 | sys.exit(0) |
| 36 | for branch in to_delete: |
| 37 | if dry_run: |
| 38 | print branch |
| 39 | else: |
| 40 | if one_by_one: |
| 41 | print 'Do you want to delete branch %s [y/N]' % branch |
| 42 | ans = raw_input() |
| 43 | if ans == 'y' or ans == 'Y': |
| 44 | print delete_branch(branch) |
| 45 | else: |
| 46 | print delete_branch(branch) |
| 47 | |
| 48 | if dry_run: |
| 49 | print '*****************************************************************' |
| 50 | print 'Did not actually delete anything yet, pass in --confirm to delete' |
| 51 | print |