Skip to content

Instantly share code, notes, and snippets.

@w4-hojin
Created March 7, 2017 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w4-hojin/c19af35bfc104fb527d9f293cd54e70f to your computer and use it in GitHub Desktop.
Save w4-hojin/c19af35bfc104fb527d9f293cd54e70f to your computer and use it in GitHub Desktop.
#!/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()
@w4-hojin
Copy link
Author

w4-hojin commented Mar 7, 2017

#으로 시작하는 branch들을 clean up 합니다.

@w4-hojin
Copy link
Author

w4-hojin commented Mar 7, 2017

pip install gitpython 가 필요합니다.
https://github.com/gitpython-developers/GitPython

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment