Created
April 7, 2018 16:23
-
-
Save NomenSvyat/c50e40b136bf9871559cc733bd23b16d to your computer and use it in GitHub Desktop.
This file contains 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
import subprocess | |
import sys | |
def isInList(listToCheck, c_value): | |
for value in listToCheck: | |
if value in c_value.lower(): | |
return True | |
return False | |
class DeleteStaleBranchesTask: | |
BRANCHES_TO_EXCLUDE = ['develop', 'head', 'master', '->'] | |
def __init__(self, contextPath=None): | |
self.contextPath = contextPath | |
def run(self): | |
process = subprocess.Popen('git branch -l -r --merged origin/develop'.split(), | |
cwd=self.contextPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
output, error = process.communicate() | |
branchList = output.decode('utf-8').split() | |
branchesToDel = {branch.replace('origin/', '', 1) for branch in branchList if not isInList( | |
self.BRANCHES_TO_EXCLUDE, branch)} | |
if len(branchesToDel) == 0: | |
print("No branches to delete") | |
return | |
branchesToDel = sorted(branchesToDel) | |
print("Branches to be deleted: \n{}".format("\n".join(branchesToDel))) | |
answer = input( | |
"Would you like do delete {} branches? (Y/N) ".format(len(branchesToDel))) | |
if answer.lower() == "y": | |
self.deleteRemoteBraches(branchesToDel) | |
else: | |
pass | |
def deleteRemoteBraches(self, branches): | |
deleteCmd = 'git push -d -v origin {}'.format(' '.join(branches)) | |
print(deleteCmd) | |
process = subprocess.Popen(deleteCmd.split(), | |
cwd=self.contextPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
output, error = process.communicate() | |
print(output) | |
contextPath = None | |
if len(sys.argv) == 2: | |
contextPath = sys.argv[1] | |
print("Running for {}".format(sys.argv[1])) | |
else: | |
print("Running for current dir") | |
task = DeleteStaleBranchesTask(contextPath=contextPath) | |
task.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment