Created
March 7, 2017 04:37
-
-
Save w4-hojin/c19af35bfc104fb527d9f293cd54e70f 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
#!/usr/local/bin/python3 | |
import argparse | |
import os | |
import os.path as osp | |
from git import Repo | |
from git.exc import GitCommandError | |
class ReadableDir(argparse.Action): | |
def __call__(self, parser, namespace, values, option_string=None): | |
prospective_dir = values | |
if not os.path.isdir(prospective_dir): | |
raise argparse.ArgumentTypeError( | |
"readable_dir:{0} is not a valid path".format(prospective_dir)) | |
if os.access(prospective_dir, os.R_OK): | |
setattr(namespace, self.dest, prospective_dir) | |
else: | |
raise argparse.ArgumentTypeError( | |
"readable_dir:{0} is not a readable dir".format(prospective_dir)) | |
parser = argparse.ArgumentParser(description='Branch cleaner') | |
parser.add_argument('path', metavar='path', default='.', help='git working tree dir') | |
args = parser.parse_args() | |
join = osp.join | |
repo = Repo(args.path) | |
git = repo.git | |
head_commit = repo.head.commit | |
index = repo.index | |
branches = repo.branches | |
head = repo.head | |
master = repo.branches['master'] | |
print(master.log()[-1]) | |
print('Now:', repo.head.ref) | |
print('Checkout to master...') | |
master.checkout() | |
print('Try to git pull...') | |
git.pull() | |
print('Now:', repo.head.ref) | |
assert len(head_commit.diff()) == 0, 'Please commit all changes before processing.' | |
assert len(repo.untracked_files) == 0, 'Please commit all changes before processing.' | |
for branch in repo.branches: | |
if not branch.name.startswith('#') or branch.name == 'master': | |
continue | |
print('# Checkout to branch:', branch.name) | |
branch.checkout() | |
print(' Try to rebase') | |
try: | |
git.rebase('origin/master') | |
except GitCommandError: | |
print(' Conflicted. Abort rebase') | |
git.rebase('--abort') | |
continue | |
if len(branch.commit.diff('origin/master')) != 0: | |
print(' Don`t delete branch.') | |
else: | |
print(' Delete branch.') | |
master.checkout() | |
repo.delete_head(branch) | |
print('Clean up completed. Checkout to master and pull...') | |
master.checkout() | |
git.pull() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#으로 시작하는 branch들을 clean up 합니다.