supermario/bin/flatten
2019-04-04 22:51:37 +08:00

43 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
from os import path, getcwd, makedirs, listdir, remove
from subprocess import run
def folder(default_search, user_input):
if path.sep in user_input:
return user_input
else:
the_path = path.dirname(path.dirname(path.abspath(__file__)))
the_path = path.join(the_path, default_search, user_input)
return the_path
parser = argparse.ArgumentParser(description='''
Shrink a git changelog into a patchset (based on git format-patch)
''')
parser.add_argument('worktree', metavar='WORKTREE', nargs='?', action='store', default=getcwd(), type=lambda x: folder('worktree', x), help='Worktree (default is cwd, or assumed in ../worktree/)')
parser.add_argument('patchset', metavar='PATCHSET', action='store', type=lambda x: folder('patchset', x), help='Destination patchset (assumed in ../patchset/)')
args = parser.parse_args()
args.patchset = path.abspath(args.patchset)
assert not path.exists(path.join(args.patchset, '.git')) # protect against argument swap
assert path.exists(path.join(args.worktree, '.git'))
makedirs(args.patchset, exist_ok=True)
for do_delete in listdir(args.patchset):
do_delete = path.join(args.patchset, do_delete)
if path.splitext(do_delete)[1].lower() == '.patch':
remove(do_delete)
run(['git', 'format-patch', '-o', args.patchset, '--anchored=;', 'master-patchset-base..HEAD'], cwd=args.worktree, check=True)