Skip to content

Commit

Permalink
hello to git delete-squashed-and-merged-branches
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Jan 27, 2017
1 parent e852f88 commit 85342f1
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions bin/git-delete-squashed-and-merged-branches
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# prereqs:
#
# pip install pygithub
#

# usage:
# git delete-squashed-and-merged-branches --dry-run # won't actually delete shit
# git delete-squashed-and-merged-branches # probably will delete shit


# thanks!
# this script is a bit of a combo from:
# danvk https://gist.github.com/danvk/a715357444ff3c1a05c4cff730eea8e1
# larsks https://github.com/larsks/github-tools/blob/master/git-is-merged
# paulirish

from glob import glob
import os.path

import sys
import github
import subprocess

yellow = '\033[93m'
reset = '\033[0m'
DRY_RUN = len(sys.argv) > 1 and sys.argv[1] == '--dry-run'

token = '30f38aa82734da7ebb3edcdf896c8a08e467ce2e'

def get_branch_heads():
'''Returns a map from branch name --> SHA for local branches.'''
branch_to_sha = {}
for path in glob('.git/refs/heads/*'):
branch = os.path.basename(path)
if branch == 'master': continue
branch_to_sha[branch] = open(path).read().strip()
return branch_to_sha


def main():
G = github.Github(login_or_token=token)

retval = 0

for branch, sha in get_branch_heads().items():
res = G.search_issues(sha)
shortsha = sha[0:7]
issue_states = [issue.state == 'closed' for issue in res]

if not issue_states:
msg = '{}{}{}:\t No PRs found. (sha {})'
retval = 4
elif all(issue_states):
msg = '{}{}{}:\t 🗑!!! All PRs are closed! (sha {})'
retval = 0
elif any(issue_states):
msg = '{}{}{}:\t Some (BUT not all) PRs are closed. (sha {})'
retval = 1
else:
msg = '{}{}{}:\t All PRs still open. (sha {})'
retval = 2

print (msg.format(yellow, branch, reset, shortsha))

if retval == 0:
if DRY_RUN:
print('Would delete local branch %s' % branch)
else:
subprocess.check_call(['git', 'branch', '-D', branch])

sys.exit(retval)

if __name__ == '__main__':
main()

0 comments on commit 85342f1

Please sign in to comment.