Last active
October 19, 2016 13:54
-
-
Save exallium/7c2b54a2bc6e8dd366b9a95490b59eb5 to your computer and use it in GitHub Desktop.
Clean all merged branches and prune origin
This file contains hidden or 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
| # Allows us to call the script via git bp | |
| [alias] | |
| bp = !~/clean_merged_branches.py |
This file contains hidden or 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/python | |
| import subprocess | |
| # Add any branches you want to exclude here (ex. master, dev, etc) | |
| EXCLUDE = [] | |
| # Grabs all branches merged w.r.t. your current branch | |
| GET_MERGED_BRANCHES = ["git", "branch", "--merged"] | |
| # Command to delete a single branch | |
| DELETE_BRANCH = ["git", "branch", "--delete"] | |
| # Command to prune against your remote server | |
| PRUNE_ORIGIN = ["git", "remote", "prune", "origin"] | |
| # Runs the given process and returns output as pipe. Tuple(out, err) | |
| def runSubProc(proc): | |
| return subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() | |
| # Removes the branch we currently have checked out | |
| def removeCurrentBranch(branches): | |
| return filter(lambda b: not b.startswith('*') and len(b) != 0, branches) | |
| # Removes extra whitespace from the branch name | |
| def trimBranches(branches): | |
| return map(lambda b: b.strip(), branches) | |
| # Removes anything from our EXCLUDE list | |
| def removeExcludes(branches): | |
| return filter(lambda b: b not in EXCLUDE, branches) | |
| # Deletes the given branch locally | |
| def deleteBranch(branch): | |
| return runSubProc(sum([DELETE_BRANCH, [branch]], [])) | |
| # Grabs all our merged branches | |
| out, err = runSubProc(GET_MERGED_BRANCHES) | |
| # Process branches and delete the ones we want deleted | |
| print '\n'.join([deleteBranch(b)[0] for b in (removeExcludes(trimBranches(removeCurrentBranch(out.split('\n')))))]) | |
| # Prune origin | |
| print runSubProc(PRUNE_ORIGIN)[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment