Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hello to git delete-squashed-and-merged-branches
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |