Committed by
Gerrit Code Review
adding a simple script to purge topic branches from your local repo.
It works by checking if your branchs' gerrit change id is present in master if yes it adds the branch to the list of branches to clean. You must run it with --confirm to delete otherwise only a dry-run will be done You can also run it with --one-by-one where it will ask you to confirm each branch deletion Change-Id: Iaabe1a6db72242fd31b14302fb3109e8545a0ad8
Showing
1 changed file
with
51 additions
and
0 deletions
tools/dev/bin/clean-branches.py
0 → 100755
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: | ||
16 | + raw_local_change_ids = check_output('git show %s | grep -i change-id | awk {\' print $2 \'}' % branch, shell=True) | ||
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 |
-
Please register or login to post a comment