2019-04-02 16:12:47 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from os import path, makedirs, walk, listdir, remove
|
|
|
|
from shutil import copy, rmtree
|
|
|
|
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='''
|
|
|
|
Expand a patchset into a git repository (based on git-am)
|
|
|
|
''')
|
|
|
|
|
|
|
|
parser.add_argument('patchset', metavar='PATCHSET', action='store', type=lambda x: folder('patchset', x), help='Patchset (assumed in ../patchset/)')
|
|
|
|
parser.add_argument('worktree', metavar='WORKTREE', action='store', type=lambda x: folder('worktree', x), help='Destination worktree (assumed in ../worktree/)')
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument('--base', metavar='SRC', action='store', default='SuperMarioProj.1994-02-09', type=lambda x: folder('base', x), help='Base source tree (default=SuperMarioProj.1994-02-09, assumed in ../base/)')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
assert path.exists(args.patchset)
|
|
|
|
assert not path.exists(path.join(args.patchset, '.git')) # protect against argument swap
|
|
|
|
|
|
|
|
|
2019-04-04 14:33:33 +00:00
|
|
|
horst = ['-c', 'user.name=Horst Beepmanh', '-c', 'user.email=<>']
|
2019-06-29 14:13:18 +00:00
|
|
|
def git(*gitargs):
|
|
|
|
run(['git', *horst, *gitargs], cwd=args.worktree, check=True)
|
2019-04-04 14:33:33 +00:00
|
|
|
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
try:
|
|
|
|
rmtree(args.worktree)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
makedirs(args.worktree)
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
with open(path.join(args.worktree, '.gitignore'), 'a') as f:
|
|
|
|
f.write('.*\n*.dmg\nBuild*/\n')
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
git('init')
|
|
|
|
git('add', '-f', '.gitignore')
|
|
|
|
git('commit', '-m', 'Useful non-source things')
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
for walk_base, walk_dirs, walk_files in walk(args.base):
|
|
|
|
walk_dirs[:] = [x for x in walk_dirs if not x.startswith('.')]
|
|
|
|
walk_files[:] = [x for x in walk_files if not x.startswith('.')]
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
other_dir = path.join(args.worktree, path.relpath(walk_base, args.base))
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
makedirs(other_dir, exist_ok=True)
|
2019-04-02 16:12:47 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
for this_file in walk_files:
|
|
|
|
copy(path.join(walk_base, this_file), other_dir)
|
2019-04-04 15:01:41 +00:00
|
|
|
|
2019-06-29 14:13:18 +00:00
|
|
|
git('add', '.')
|
|
|
|
git('commit', '-m', path.basename(args.base))
|
|
|
|
git('tag', 'patchset-base')
|
2019-04-02 16:12:47 +00:00
|
|
|
|
|
|
|
patchfiles = [path.join(args.patchset, x) for x in sorted(listdir(args.patchset)) if path.splitext(x)[1].lower() == '.patch']
|
2019-06-29 14:13:18 +00:00
|
|
|
git('am', *patchfiles)
|