Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 44 lines (32 sloc) 1.39 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This scripts helps removing Git branches.
# Run it from a git repo dir. It will open the Git branches list
# in your favorite editor. Delete some branches from the list.
# Save and close the file. The script applies the changes to the repo.
from subprocess import call, Popen, PIPE
from os import getenv
import re
# configure commands used in this script
editor_command = getenv('EDITOR')
branch_command = 'git branch'
mktemp_command = 'mktemp'
# takes 'git branch' output and returns a set of branches
def branches_to_list(branches):
return set(re.sub('\* ', '', branches, flags=re.M).split())
# create a temp file
tmp_file = Popen(mktemp_command, stdout=PIPE).communicate()[0].decode().rstrip()
# write git branches to the temp file
call('%s > %s' % (branch_command, tmp_file), shell=True)
# get original branches
original_branches = branches_to_list(open(tmp_file, 'r').read())
# edit the temp file with the editor command
call('%s %s' % (editor_command, tmp_file), shell=True)
# get new branches
new_branches = branches_to_list(open(tmp_file, 'r').read())
# adding and renaming branches is not supported
exit('Aborting! Do not add or rename branches, just remove them.')
# remove branches with Git
for branch in original_branches.difference(new_branches):
call('git branch -D %s' % branch, shell=True)