Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
scripts/patches.py: apply, bump, export, status and check for the patch queue
v0lumehic37859e4759e
diff --git a/scripts/patches.py b/scripts/patches.pynew file mode 100644index 00000000..a5fdb9cb--- /dev/null+++ b/scripts/patches.py@@ -0,0 +1,463 @@+#!/usr/bin/env python3+"""The fork's patch queue on a pinned upstream (LNPANDY/NX2Apple#123).++The vendored paths (dm/ scripts/ docs/ ref/ AGENTS.md) are generated, not hand-edited: seedds/nxnative at the commit+in .upstream-ref, plus the patches in patches/ applied in the order of patches/series with `git am -3`. Each patch is a+`git format-patch` file whose message carries a header:++ Upstream-Status: forwarded seedds/nxnative#475 | pending | fork-only | backport <sha>+ Reason: <one line>+ Issue: <repo>#<n>++Subcommands (Python 3.9+, stdlib and git):+ apply [--ref SHA] build the vendored tree from upstream at the ref plus the series (a temporary worktree), copy it+ into the checkout, and write dm/fork_patches.txt+ bump --to SHA move the pin: re-apply the series on SHA, drop the patches that come out empty (upstream has them)+ and report them, refresh the rest; on a conflict stop, naming the patch, its Reason and its+ Upstream-Status, and leave the conflict in a worktree for a human. --dry changes nothing and+ reports every patch that would conflict+ export <commit|range> turn local commits on the vendored tree into patches with the header template+ status a table of the patches: Upstream-Status, Reason, age+ check the headers are valid, no patch touches the shader chain, the series applies to the pinned ref and+ reproduces the vendored tree of the checkout byte for byte++Scratch worktrees go under $PATCHES_TMP (or --tmp), else the system temporary folder.+"""+import argparse, fnmatch, os, re, shutil, subprocess, sys, tempfile, time+from pathlib import Path++REPO = Path(__file__).resolve().parent.parent+VENDORED = ('dm', 'scripts', 'docs', 'AGENTS.md', 'ref')+# Inside the vendored paths but not part of the generated tree: the tool, its test, and the list it writes.+OWN = ('scripts/patches.py', 'scripts/tests/test_patches.py', 'dm/fork_patches.txt')+FORK_PATCHES = 'dm/fork_patches.txt'+KINDS = ('forwarded', 'pending', 'fork-only', 'backport')+# The shader chain and the tool pins (AGENTS.md: the DMSH_CHAIN paragraph names them; chain_paths() reads it).+# Used only when AGENTS.md has no such paragraph.+CHAIN_FALLBACK = ('dm/gfx/nvn/dmsh_chain.h', 'dm/gfx/tools/dmsh_*', 'dm/gfx/tools/Program.cs', 'dm/gfx/tools/ios_metal.py',+ 'dm/gfx/tools/dmsh_chain.lock', 'dm/gfx/tools/dmsh_xlate_bin*')+++class Fail(Exception):+ pass+++def git(*a, cwd=None, check=True, inp=None, binary=False):+ r = subprocess.run(['git', *a], cwd=cwd or REPO, input=inp, capture_output=True, text=not binary)+ if check and r.returncode:+ raise Fail(f'git {" ".join(a)} failed: {(r.stderr if not binary else r.stderr.decode(errors="replace")).strip()[-1500:]}')+ return r+++def say(*a):+ print(*a, flush=True)+++# ---- the series and the headers ----++class Patch:+ def __init__(self, path):+ self.path = path+ self.file = path.name+ self.name = re.sub(r'^\d+-', '', re.sub(r'\.patch$', '', self.file))+ text = path.read_text(encoding='utf-8', errors='replace')+ head = text.split('\n---\n', 1)[0]+ m = re.search(r'(?m)^Subject: (?:\[PATCH[^\]]*\] )?(.*(?:\n .*)*)', head)+ self.subject = ' '.join(m.group(1).split()) if m else ''+ self.date = (re.search(r'(?m)^Date: (.*)$', head) or [None, ''])[1]+ self.status = self.header(head, 'Upstream-Status')+ self.reason = self.header(head, 'Reason')+ self.issue = self.header(head, 'Issue')+ self.kind = (self.status.split() or [''])[0]+ self.files = sorted({f for m in re.finditer(r'(?m)^diff --git a/(\S+) b/(\S+)$', text) for f in m.groups()})++ @staticmethod+ def header(head, key):+ m = re.search(r'(?m)^' + key + r': *(.*)$', head)+ return m.group(1).strip() if m else ''++ def age_days(self):+ import email.utils+ try:+ return max(0, int((time.time() - email.utils.mktime_tz(email.utils.parsedate_tz(self.date))) // 86400))+ except Exception:+ return -1++ def problems(self):+ out = []+ if self.kind not in KINDS:+ out.append(f'Upstream-Status must start with one of {", ".join(KINDS)} (is: {self.status or "missing"})')+ elif self.kind == 'forwarded' and not re.search(r'#\d+', self.status):+ out.append('Upstream-Status: forwarded needs the upstream PR, e.g. forwarded seedds/nxnative#475')+ elif self.kind == 'backport' and not re.search(r'\b[0-9a-f]{7,40}\b', self.status):+ out.append('Upstream-Status: backport needs the upstream commit')+ for key, val in (('Reason', self.reason), ('Issue', self.issue)):+ if not val: out.append(f'{key} is missing')+ elif val.startswith('TODO'): out.append(f'{key} is still the export template ({val})')+ return out+++def read_series(root):+ s = root / 'patches/series'+ if not s.is_file(): raise Fail(f'{s} is missing')+ out = []+ for line in s.read_text().splitlines():+ line = line.split('#', 1)[0].strip()+ if line: out.append(line)+ return out+++def load(root):+ pd = root / 'patches'+ names = read_series(root)+ missing = [n for n in names if not (pd / n).is_file()]+ if missing: raise Fail('listed in patches/series but missing: ' + ', '.join(missing))+ if len(set(names)) != len(names): raise Fail('patches/series lists a patch twice')+ return [Patch(pd / n) for n in names]+++def chain_paths(root):+ """The globs of the shader chain, read from AGENTS.md's chain paragraph (the one naming DMSH_CHAIN)."""+ try: text = (root / 'AGENTS.md').read_text()+ except OSError: return list(CHAIN_FALLBACK)+ para = next((l for l in text.splitlines() if 'DMSH_CHAIN' in l and 'dm/gfx/tools/dmsh_' in l), None)+ found = []+ if para:+ for tok in re.findall(r'`([^`]+)`', para):+ if re.fullmatch(r'dm/gfx/(?:nvn|tools)/[\w./*-]+', tok): found.append(tok)+ elif re.fullmatch(r'[\w-]+\.(?:cs|py|lock)', tok) and not tok.startswith('dmsh_'): found.append('dm/gfx/tools/' + tok)+ elif tok == 'dmsh_chain.lock': found.append('dm/gfx/tools/' + tok)+ m = re.search(r'translator is `([^`]+)`', text)+ if m and re.fullmatch(r'dm/gfx/tools/[\w./-]+', m.group(1)): found.append(m.group(1) + '*')+ return sorted(set(found)) if len(found) >= 4 else list(CHAIN_FALLBACK)+++def chain_hits(files, root):+ globs = chain_paths(root)+ return sorted(f for f in files if any(fnmatch.fnmatchcase(f, g) for g in globs))+++# ---- applying the series in a scratch worktree ----++def resolve(ref, root):+ r = git('rev-parse', '--verify', '--quiet', f'{ref}^{{commit}}', cwd=root, check=False)+ if r.returncode:+ raise Fail(f'{ref} is not a commit in this repository: fetch it first (git fetch seedds {ref})')+ return r.stdout.strip()+++class Scratch:+ """A detached worktree of upstream at a commit, removed on exit unless kept."""+ def __init__(self, root, sha, tmp=None, keep=False):+ self.root, self.sha, self.keep = root, sha, keep+ self.base = Path(tempfile.mkdtemp(prefix='patches-', dir=tmp or os.environ.get('PATCHES_TMP') or None))+ self.dir = self.base / 'tree'+ git('worktree', 'add', '--detach', '-q', str(self.dir), sha, cwd=root)++ def close(self):+ if self.keep: return+ git('worktree', 'remove', '--force', str(self.dir), cwd=self.root, check=False)+ shutil.rmtree(self.base, ignore_errors=True)+ git('worktree', 'prune', cwd=self.root, check=False)++ def __enter__(self): return self+ def __exit__(self, *e): self.close()+++def am(wt, patch):+ return git('-c', 'user.name=patches.py', '-c', 'user.email=patches@localhost', '-c', 'commit.gpgsign=false', '-c', 'core.autocrlf=false',+ 'am', '-3', '--quiet', str(patch.path), cwd=wt, check=False)+++def apply_series(wt, patches, stop=True):+ """Apply each patch in turn. Returns (applied, landed, conflicts): applied = [(patch, commit)], landed = patches that came+ out empty (upstream has them), conflicts = [(patch, message)]. With stop, the first conflict stays in the worktree."""+ applied, landed, conflicts = [], [], []+ for p in patches:+ before = git('rev-parse', 'HEAD', cwd=wt).stdout.strip()+ r = am(wt, p)+ if r.returncode == 0:+ after = git('rev-parse', 'HEAD', cwd=wt).stdout.strip()+ if after == before or git('rev-parse', 'HEAD^{tree}', cwd=wt).stdout == git('rev-parse', before + '^{tree}', cwd=wt).stdout:+ git('reset', '--hard', '-q', before, cwd=wt) # the merge changed nothing: upstream has this patch+ landed.append(p); continue+ applied.append((p, after)); continue+ unmerged = git('ls-files', '-u', cwd=wt).stdout.strip()+ clean = git('diff', '--cached', '--quiet', cwd=wt, check=False).returncode == 0 and git('diff', '--quiet', cwd=wt, check=False).returncode == 0+ if not unmerged and clean and 'Patch failed' not in r.stderr and 'patch does not apply' not in r.stderr:+ git('am', '--skip', cwd=wt, check=False)+ landed.append(p); continue+ files = git('diff', '--name-only', '--diff-filter=U', cwd=wt, check=False).stdout.split()+ errs = [l for l in (r.stderr + r.stdout).splitlines() if re.match(r'(error|CONFLICT|Patch failed|Applying)', l)]+ conflicts.append((p, ('conflicting files: ' + ', '.join(files) + '\n' if files else '') + '\n'.join(errs[-6:])))+ if stop: break+ git('am', '--abort', cwd=wt, check=False)+ return applied, landed, conflicts+++def tree_files(wt):+ """{path: (mode, blob)} of the vendored paths at HEAD of a worktree, without the tool's own files."""+ out = git('ls-tree', '-r', '-z', 'HEAD', '--', *VENDORED, cwd=wt).stdout+ files = {}+ for rec in out.split('\0'):+ if not rec: continue+ meta, path = rec.split('\t', 1)+ mode, _, blob = meta.split()+ if path not in OWN: files[path] = (mode, blob)+ return files+++def fork_patch_list(patches):+ return ''.join(p.name + '\n' for p in patches)+++def local_files(root):+ """{path: (mode, blob)} of the checkout's working tree under the vendored paths: tracked files that still exist plus+ untracked ones git does not ignore, without the tool's own files."""+ r = git('ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', *VENDORED, cwd=root).stdout+ paths = sorted({p for p in r.split('\0') if p and p not in OWN and (root / p).is_file()})+ files = {}+ if paths:+ blobs = git('hash-object', '--stdin-paths', cwd=root, inp='\n'.join(paths) + '\n').stdout.split()+ for p, b in zip(paths, blobs):+ files[p] = ('100755' if os.access(root / p, os.X_OK) else '100644', b)+ return files+++def diff_trees(want, have):+ out = []+ for p in sorted(set(want) | set(have)):+ if p not in have: out.append(f' missing in the checkout: {p}')+ elif p not in want: out.append(f' not produced by the series: {p}')+ elif want[p] != have[p]:+ what = 'mode' if want[p][1] == have[p][1] else 'content'+ out.append(f' differs ({what}): {p}')+ return out+++def install(root, wt, patches):+ """Copy the generated vendored tree into the checkout: delete tracked vendored files it no longer has, write the rest."""+ want = tree_files(wt)+ for p in git('ls-files', '-z', '--', *VENDORED, cwd=root).stdout.split('\0'):+ if p and p not in want and p not in OWN and (root / p).exists(): (root / p).unlink()+ for p, (mode, blob) in want.items():+ dest = root / p+ dest.parent.mkdir(parents=True, exist_ok=True)+ if dest.is_symlink(): dest.unlink()+ dest.write_bytes((wt / p).read_bytes())+ dest.chmod(0o755 if mode == '100755' else 0o644)+ (root / FORK_PATCHES).write_text(fork_patch_list(patches))+++# ---- subcommands ----++def cmd_check(a):+ root = a.root+ patches = load(root)+ bad = []+ pd = root / 'patches'+ stray = sorted(f.name for f in pd.glob('*.patch') if f.name not in {p.file for p in patches})+ if stray: bad.append('patch files not in patches/series: ' + ', '.join(stray))+ for p in patches:+ for m in p.problems(): bad.append(f'{p.file}: {m}')+ hits = chain_hits(p.files, root)+ if hits: bad.append(f'{p.file}: touches the shader chain or the tool pins, which patches must not change ({", ".join(hits)}); see patches/README.md, Patch policy')+ if bad:+ say('patch queue: invalid\n ' + '\n '.join(bad)); return 1+ ref = (root / '.upstream-ref').read_text().strip()+ sha = resolve(ref, root)+ with Scratch(root, sha, a.tmp) as s:+ applied, landed, conflicts = apply_series(s.dir, patches)+ if conflicts:+ p, msg = conflicts[0]+ say(f'patch queue: {p.file} does not apply to the pinned upstream {sha[:12]}\n Reason: {p.reason}\n Upstream-Status: {p.status}\n {msg}'); return 1+ if landed:+ say('patch queue: patches that are empty on the pinned upstream (it has them already; run bump to drop them): ' + ', '.join(p.file for p in landed)); return 1+ want = tree_files(s.dir)+ diff = diff_trees(want, local_files(root))+ expect = fork_patch_list(patches)+ have_list = (root / FORK_PATCHES).read_text() if (root / FORK_PATCHES).exists() else None+ if have_list != expect: diff.append(f' {FORK_PATCHES} is not the list of the series (run patches.py apply)')+ if diff:+ say(f'patch queue: the vendored tree is not upstream {sha[:12]} plus the series ({len(diff)} differences):')+ say('\n'.join(diff[:40]) + ('\n ...' if len(diff) > 40 else ''))+ say('A fix to a vendored file belongs in a patch: commit it, then `scripts/patches.py export <commit>` (patches/README.md).')+ return 1+ say(f'patch queue: ok. {len(patches)} patches apply to upstream {sha[:12]} and give the committed vendored tree ({len(want)} files)')+ return 0+++def cmd_apply(a):+ root = a.root+ patches = load(root)+ ref = a.ref or (root / '.upstream-ref').read_text().strip()+ sha = resolve(ref, root)+ with Scratch(root, sha, a.tmp) as s:+ applied, landed, conflicts = apply_series(s.dir, patches)+ if conflicts:+ p, msg = conflicts[0]+ raise Fail(f'{p.file} does not apply to {sha[:12]}\n Reason: {p.reason}\n Upstream-Status: {p.status}\n {msg}')+ install(root, s.dir, [p for p, _ in applied])+ if landed: say('empty at this ref (upstream has them): ' + ', '.join(p.file for p in landed) + ' (patches.py bump drops them)')+ say(f'applied {len(applied)} patches to upstream {sha[:12]}; the vendored tree is written')+ return 0+++def cmd_bump(a):+ root = a.root+ patches = load(root)+ old = (root / '.upstream-ref').read_text().strip() if (root / '.upstream-ref').exists() else '?'+ sha = resolve(a.to, root)+ keep = not a.dry+ s = Scratch(root, sha, a.tmp, keep=keep)+ try:+ applied, landed, conflicts = apply_series(s.dir, patches, stop=not a.dry)+ report(old, sha, applied, landed, conflicts)+ if conflicts:+ if a.dry:+ s.close(); return 1+ p = conflicts[0][0]+ say(f'\nStopped at {p.file}. The conflict is left in {s.dir}:\n'+ f' resolve it there, `git am --continue`, apply the rest with `git am -3 <patches>`, then run\n'+ f' scripts/patches.py refresh {s.dir} --to {sha[:12]}')+ return 1+ if a.dry:+ return 0+ write_series(root, s.dir, applied, landed, patches)+ (root / '.upstream-ref').write_text(sha + '\n')+ install(root, s.dir, [p for p, _ in applied])+ s.close()+ return 0+ except BaseException:+ s.close()+ raise+++def report(old, sha, applied, landed, conflicts):+ say(f'bump {old[:12]} -> {sha[:12]}')+ say(f' applied cleanly ({len(applied)}): ' + (', '.join(p.name for p, _ in applied) or 'none'))+ say(f' landed upstream, dropped ({len(landed)}): ' + (', '.join(p.name for p in landed) or 'none'))+ say(f' conflicts ({len(conflicts)}):' + ('' if conflicts else ' none'))+ for p, msg in conflicts:+ say(f' {p.file}\n Reason: {p.reason}\n Upstream-Status: {p.status}')+ for l in msg.splitlines(): say(' | ' + l)+++def write_series(root, wt, applied, landed, patches):+ """Rewrite patches/ from the rebased commits: the kept patches refreshed, the landed ones deleted."""+ pd = root / 'patches'+ for p, commit in applied:+ text = git('format-patch', '--binary', '--zero-commit', '--no-signature', '--no-numbered', '--stdout', '-1', commit, cwd=wt, binary=True).stdout+ p.path.write_bytes(text)+ for p in landed: p.path.unlink()+ keep = {p.file for p, _ in applied}+ lines = [l for l in (pd / 'series').read_text().splitlines()]+ out = [l for l in lines if l.split('#', 1)[0].strip() == '' or l.split('#', 1)[0].strip() in keep]+ (pd / 'series').write_text('\n'.join(out) + '\n')+++def cmd_refresh(a):+ """After a human resolved a bump: the worktree's commits (upstream..HEAD) become the series."""+ root, wt = a.root, Path(a.worktree)+ sha = resolve(a.to, root)+ old = load(root)+ commits = git('rev-list', '--reverse', f'{sha}..HEAD', cwd=wt).stdout.split()+ if len(commits) > len(old): raise Fail('more commits than patches')+ by_subject = {p.subject: p for p in old}+ applied, used = [], set()+ for c in commits:+ subj = git('log', '-1', '--format=%s', c, cwd=wt).stdout.strip()+ p = by_subject.get(subj)+ if not p: raise Fail(f'commit "{subj}" matches no patch subject')+ applied.append((p, c)); used.add(p.file)+ landed = [p for p in old if p.file not in used]+ say('landed upstream, dropped: ' + (', '.join(p.name for p in landed) or 'none'))+ write_series(root, wt, applied, landed, old)+ (root / '.upstream-ref').write_text(sha + '\n')+ install(root, wt, [p for p, _ in applied])+ return 0+++def slug(s):+ return re.sub(r'[^a-z0-9]+', '-', s.lower()).strip('-')[:48].strip('-') or 'patch'+++def cmd_export(a):+ root = a.root+ spec = a.rev if '..' in a.rev else f'{a.rev}^..{a.rev}'+ existing = load(root) if (root / 'patches/series').exists() else []+ pd = root / 'patches'; pd.mkdir(exist_ok=True)+ n = max([int(re.match(r'(\d+)-', p.file).group(1)) for p in existing if re.match(r'(\d+)-', p.file)] or [0])+ commits = git('rev-list', '--reverse', spec, cwd=root).stdout.split()+ if not commits: raise Fail(f'no commits in {spec}')+ series = (pd / 'series').read_text() if (pd / 'series').exists() else ''+ if series and not series.endswith('\n'): series += '\n'+ made = []+ for c in commits:+ text = git('format-patch', '--binary', '--zero-commit', '--no-signature', '--no-numbered', '--stdout', f'{c}^..{c}', '--', *VENDORED,+ *(f':(exclude){o}' for o in OWN), cwd=root, binary=True).stdout.decode(errors='surrogateescape')+ if not re.search(r'(?m)^diff --git ', text):+ say(f'skipped {c[:10]}: no change under the vendored paths'); continue+ files = sorted({f for m in re.finditer(r'(?m)^diff --git a/(\S+) b/(\S+)$', text) for f in m.groups()})+ hits = chain_hits(files, root)+ if hits:+ raise Fail(f'{c[:10]} touches the shader chain or the tool pins ({", ".join(hits)}): patches must not change them (patches/README.md, Patch policy)')+ head, sep, rest = text.partition('\n---\n')+ add = []+ for key, val in (('Upstream-Status', a.status), ('Reason', a.reason), ('Issue', a.issue)):+ if not re.search(r'(?m)^' + key + ':', head): add.append(f'{key}: {val}')+ if add: head = head.rstrip('\n') + '\n\n' + '\n'.join(add) + '\n'+ text = head + sep + rest+ subj = re.search(r'(?m)^Subject: (?:\[PATCH[^\]]*\] )?(.*)$', text).group(1)+ n += 1+ name = f'{n:04d}-{slug(subj)}.patch'+ (pd / name).write_bytes(text.encode(errors='surrogateescape'))+ series += name + '\n'; made.append(name)+ (pd / 'series').write_text(series)+ if made: (root / FORK_PATCHES).write_text(fork_patch_list(load(root))) # the new patch is carried from now on+ for m in made: say('wrote patches/' + m)+ if made and any('TODO' in x for x in (a.reason, a.issue)):+ say('Fill in Reason and Issue in the new patch headers, then run scripts/patches.py check.')+ return 0+++def cmd_status(a):+ patches = load(a.root)+ rows = [('patch', 'upstream status', 'reason', 'age')]+ for p in patches:+ age = p.age_days()+ rows.append((p.name, p.status, p.reason, f'{age}d' if age >= 0 else '?'))+ w = [max(len(r[0]) for r in rows), 42]+ for i, r in enumerate(rows):+ st = r[1] if len(r[1]) <= 42 else r[1][:39] + '...'+ reason = r[2] if len(r[2]) <= 70 else r[2][:67] + '...'+ say(f'{r[0]:<{w[0]}} {st:<42} {r[3]:>5} {reason}')+ if i == 0: say('-' * (sum(w) + 12))+ c = {k: sum(1 for p in patches if p.kind == k) for k in KINDS}+ say(f'\n{len(patches)} patches: ' + ', '.join(f'{v} {k}' for k, v in c.items()))+ return 0+++def main(argv=None):+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument('--repo', default=str(REPO), help='the checkout (default: this script\'s)')+ ap.add_argument('--tmp', help='folder for the scratch worktree (default $PATCHES_TMP, else the system temporary folder)')+ sub = ap.add_subparsers(dest='cmd', required=True)+ s = sub.add_parser('apply'); s.add_argument('--ref', help='upstream commit (default: .upstream-ref)'); s.set_defaults(f=cmd_apply)+ s = sub.add_parser('bump'); s.add_argument('--to', required=True, help='the new upstream commit'); s.add_argument('--dry', action='store_true', help='change nothing, report every conflict'); s.set_defaults(f=cmd_bump)+ s = sub.add_parser('refresh'); s.add_argument('worktree'); s.add_argument('--to', required=True); s.set_defaults(f=cmd_refresh)+ s = sub.add_parser('export'); s.add_argument('rev', help='a commit or a range')+ s.add_argument('--status', default='pending'); s.add_argument('--reason', default='TODO: why the fork carries this'); s.add_argument('--issue', default='TODO: LNPANDY/NX2Apple#<n>'); s.set_defaults(f=cmd_export)+ sub.add_parser('status').set_defaults(f=cmd_status)+ sub.add_parser('check').set_defaults(f=cmd_check)+ a = ap.parse_args(argv)+ a.root = Path(a.repo).resolve()+ try:+ return a.f(a)+ except Fail as e:+ print(f'patches.py: {e}', file=sys.stderr)+ return 2+++if __name__ == '__main__':+ sys.exit(main())diff --git a/scripts/tests/test_patches.py b/scripts/tests/test_patches.pynew file mode 100644index 00000000..22bae4da--- /dev/null+++ b/scripts/tests/test_patches.py@@ -0,0 +1,238 @@+"""scripts/patches.py on a fixture repository: the series reproduces the vendored tree, a patch that upstream took drops on a bump,+a conflict names its patch, export writes the header, a patch that touches the shader chain is rejected, and the list of carried+patches is generated. No game files, no network: upstream is a branch of the fixture."""+import importlib.util, os, shutil, subprocess, sys, tempfile, unittest+from pathlib import Path++REPO = Path(__file__).resolve().parents[2]+TOOL = REPO / 'scripts/patches.py'+spec = importlib.util.spec_from_file_location('patches_tool', TOOL)+P = importlib.util.module_from_spec(spec); spec.loader.exec_module(P)++ENV = dict(os.environ, GIT_AUTHOR_NAME='t', GIT_AUTHOR_EMAIL='[email protected]', GIT_COMMITTER_NAME='t', GIT_COMMITTER_EMAIL='[email protected]',+ GIT_CONFIG_GLOBAL='/dev/null', GIT_CONFIG_SYSTEM='/dev/null')+++def git(cwd, *a, check=True):+ r = subprocess.run(['git', '-c', 'commit.gpgsign=false', *a], cwd=cwd, capture_output=True, text=True, env=ENV)+ if check and r.returncode: raise AssertionError(f'git {a}: {r.stderr}')+ return r.stdout.strip()+++class Fixture(unittest.TestCase):+ """repo/: upstream's first commit (branch up) with the fork's one edit on top, exported as a patch, and the tool installed."""+ def setUp(self):+ self.tmp = Path(tempfile.mkdtemp(prefix='test-patches-'))+ self.addCleanup(shutil.rmtree, self.tmp, True)+ self.r = self.tmp / 'repo'; self.r.mkdir()+ git(self.r, 'init', '-q', '-b', 'main')+ self.w('dm/a.c', 'line1\nline2\nline3\nline4\nline5\nline6\nline7\n')+ self.w('dm/b.c', 'b1\nb2\n')+ self.w('docs/x.md', 'x\n')+ shutil.copy(REPO / 'AGENTS.md', self.r / 'AGENTS.md')+ git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', 'upstream 1')+ self.up1 = git(self.r, 'rev-parse', 'HEAD')+ git(self.r, 'branch', 'up')+ # the tool lives in the fork's scripts/, which is vendored but not part of the generated tree+ (self.r / 'scripts').mkdir(); shutil.copy(TOOL, self.r / 'scripts/patches.py')+ self.w('dm/a.c', 'line1\nline2 fork\nline3\nline4\nline5\nline6\nline7\n')+ git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', 'Make line 2 the fork\'s\n\nWhy: a test.')+ self.run_tool('export', 'HEAD', '--status', 'forwarded seedds/nxnative#475', '--reason', 'the fork needs line 2', '--issue', 'LNPANDY/NX2Apple#1')+ (self.r / '.upstream-ref').write_text(self.up1 + '\n')+ git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', 'patches')+ self.patch = next((self.r / 'patches').glob('0001-*.patch'))++ def w(self, path, text):+ f = self.r / path; f.parent.mkdir(parents=True, exist_ok=True); f.write_text(text)++ def run_tool(self, *a, expect=0):+ r = subprocess.run([sys.executable, str(self.r / 'scripts/patches.py'), '--tmp', str(self.tmp), *a], cwd=self.r, capture_output=True, text=True, env=ENV)+ if expect is not None: self.assertEqual(r.returncode, expect, r.stdout + r.stderr)+ return r.stdout + r.stderr++ def upstream_commit(self, files, msg):+ """A new commit on branch up (a child of the last one), made in a scratch worktree."""+ wt = self.tmp / 'upwt'+ git(self.r, 'worktree', 'add', '-q', str(wt), 'up')+ for p, t in files.items():+ (wt / p).parent.mkdir(parents=True, exist_ok=True); (wt / p).write_text(t)+ git(wt, 'add', '-A'); git(wt, 'commit', '-q', '-m', msg)+ sha = git(wt, 'rev-parse', 'HEAD')+ git(self.r, 'worktree', 'remove', '--force', str(wt))+ return sha+++class ApplyAndCheck(Fixture):+ def test_series_on_the_pin_reproduces_the_tree(self):+ out = self.run_tool('check')+ self.assertIn('1 patches apply', out)+ self.assertEqual((self.r / 'dm/fork_patches.txt').read_text(), 'make-line-2-the-fork-s\n')++ def test_a_hand_edit_of_a_vendored_file_fails_check_and_apply_restores_it(self):+ self.w('dm/b.c', 'b1\nb2 edited\n')+ out = self.run_tool('check', expect=1)+ self.assertIn('dm/b.c', out); self.assertIn('patches.py export', out)+ self.run_tool('apply')+ self.assertEqual((self.r / 'dm/b.c').read_text(), 'b1\nb2\n')+ self.run_tool('check')++ def test_apply_deletes_a_tracked_file_the_series_does_not_produce(self):+ self.w('dm/stray.c', 'x\n'); git(self.r, 'add', 'dm/stray.c')+ self.run_tool('check', expect=1)+ self.run_tool('apply')+ self.assertFalse((self.r / 'dm/stray.c').exists())++ def test_a_patch_without_a_header_fails_check(self):+ text = self.patch.read_text().replace('Reason: the fork needs line 2\n', '')+ self.patch.write_text(text)+ self.assertIn('Reason is missing', self.run_tool('check', expect=1))++ def test_status_lists_the_patch(self):+ out = self.run_tool('status')+ self.assertIn('forwarded seedds/nxnative#475', out); self.assertIn('the fork needs line 2', out)+++class Bump(Fixture):+ def test_a_patch_upstream_has_taken_drops_and_is_listed(self):+ new = self.upstream_commit({'dm/a.c': 'line1\nline2 fork\nline3\nline4\nline5\nline6\nline7\n', 'docs/y.md': 'y\n'}, 'upstream 2: takes the fix')+ out = self.run_tool('bump', '--to', new)+ self.assertIn('landed upstream, dropped (1): make-line-2-the-fork-s', out)+ self.assertFalse(self.patch.exists())+ self.assertNotIn('0001', (self.r / 'patches/series').read_text())+ self.assertEqual((self.r / '.upstream-ref').read_text().strip(), new)+ self.assertEqual((self.r / 'docs/y.md').read_text(), 'y\n')+ self.assertEqual((self.r / 'dm/fork_patches.txt').read_text(), '')+ self.run_tool('check')++ def test_a_patch_that_still_applies_is_refreshed_on_the_new_base(self):+ new = self.upstream_commit({'dm/a.c': 'line1\nline2\nline3\nline4\nline5\nline6\nline7 upstream\n'}, 'upstream 2: touches line 7')+ out = self.run_tool('bump', '--to', new)+ self.assertIn('applied cleanly (1): make-line-2-the-fork-s', out)+ self.assertEqual((self.r / 'dm/a.c').read_text(), 'line1\nline2 fork\nline3\nline4\nline5\nline6\nline7 upstream\n')+ self.run_tool('check')++ def test_a_conflict_names_the_patch_its_reason_and_status_and_changes_nothing(self):+ new = self.upstream_commit({'dm/a.c': 'line1\nline2 upstream\nline3\nline4\nline5\nline6\nline7\n'}, 'upstream 2: its own line 2')+ before = (self.r / 'dm/a.c').read_text()+ for args in (('--dry',), ()):+ out = self.run_tool('bump', *args, '--to', new, expect=1)+ self.assertIn(self.patch.name, out)+ self.assertIn('Reason: the fork needs line 2', out)+ self.assertIn('Upstream-Status: forwarded seedds/nxnative#475', out)+ self.assertIn('dm/a.c', out)+ self.assertEqual((self.r / '.upstream-ref').read_text().strip(), self.up1)+ self.assertEqual((self.r / 'dm/a.c').read_text(), before)+ self.assertTrue(self.patch.exists())++ def test_the_conflict_is_left_in_a_worktree_for_a_human(self):+ new = self.upstream_commit({'dm/a.c': 'line1\nline2 upstream\nline3\nline4\nline5\nline6\nline7\n'}, 'upstream 2: its own line 2')+ out = self.run_tool('bump', '--to', new, expect=1)+ wt = next(l.split(' is left in ')[1].split(':')[0] for l in out.splitlines() if ' is left in ' in l)+ self.assertTrue((Path(wt) / 'dm/a.c').read_text().count('<<<<<<<') == 1)+ git(wt, 'checkout', '--theirs', 'dm/a.c'); git(wt, 'add', 'dm/a.c')+ git(wt, '-c', 'user.name=t', '-c', '[email protected]', 'am', '--continue')+ self.run_tool('refresh', wt, '--to', new)+ self.assertEqual((self.r / '.upstream-ref').read_text().strip(), new)+ self.assertEqual((self.r / 'dm/a.c').read_text().splitlines()[1], 'line2 fork')+ self.run_tool('check')+ git(self.r, 'worktree', 'remove', '--force', wt)+++class Export(Fixture):+ def commit_edit(self, msg, path='dm/b.c', text='b1\nb2 changed\n'):+ self.w(path, text); git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', msg)++ def test_export_writes_the_header_template_and_extends_the_series(self):+ self.commit_edit('Change b2')+ out = self.run_tool('export', 'HEAD')+ new = sorted((self.r / 'patches').glob('0002-*.patch'))+ self.assertEqual(len(new), 1, out)+ text = new[0].read_text()+ for key in ('Upstream-Status: pending', 'Reason: TODO', 'Issue: TODO'): self.assertIn(key, text)+ self.assertIn('Subject: [PATCH] Change b2', text)+ self.assertIn('0002-change-b2.patch', (self.r / 'patches/series').read_text().splitlines())+ self.assertEqual((self.r / 'dm/fork_patches.txt').read_text(), 'make-line-2-the-fork-s\nchange-b2\n')+ self.assertIn('still the export template', self.run_tool('check', expect=1)) # the header has to be filled in++ def test_export_keeps_a_header_the_commit_already_has(self):+ self.commit_edit('Change b2\n\nUpstream-Status: backport abcdef1\nReason: r\nIssue: LNPANDY/NX2Apple#2')+ self.run_tool('export', 'HEAD')+ text = next((self.r / 'patches').glob('0002-*.patch')).read_text()+ self.assertEqual(text.count('Upstream-Status:'), 1); self.assertIn('backport abcdef1', text)+ self.run_tool('check')++ def test_export_of_a_range_makes_one_patch_per_commit(self):+ base = git(self.r, 'rev-parse', 'HEAD')+ self.commit_edit('First', text='b1\nb2 first\n'); self.commit_edit('Second', 'docs/x.md', 'x2\n')+ self.run_tool('export', f'{base}..HEAD', '--reason', 'r', '--issue', 'LNPANDY/NX2Apple#3')+ self.assertEqual(len(list((self.r / 'patches').glob('000[23]-*.patch'))), 2)+ self.run_tool('check')++ def test_export_skips_a_commit_outside_the_vendored_paths(self):+ self.commit_edit('Frontend only', 'frontend/x.swift', 'x\n')+ out = self.run_tool('export', 'HEAD')+ self.assertIn('no change under the vendored paths', out)+ self.assertFalse(list((self.r / 'patches').glob('0002-*')))+++class ChainRule(Fixture):+ def test_the_chain_paths_come_from_agents_md(self):+ g = P.chain_paths(self.r)+ for want in ('dm/gfx/nvn/dmsh_chain.h', 'dm/gfx/tools/dmsh_*', 'dm/gfx/tools/Program.cs', 'dm/gfx/tools/ios_metal.py', 'dm/gfx/tools/dmsh_chain.lock', 'dm/gfx/tools/dmsh_xlate_bin*'):+ self.assertIn(want, g)++ def test_export_rejects_a_commit_that_touches_the_chain(self):+ for path in ('dm/gfx/nvn/dmsh_chain.h', 'dm/gfx/tools/dmsh_build.py', 'dm/gfx/tools/Program.cs', 'dm/gfx/tools/dmsh_chain.lock', 'dm/gfx/tools/dmsh_xlate_bin/x'):+ self.w(path, 'x\n'); git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', f'touch {path}')+ out = self.run_tool('export', 'HEAD', expect=2)+ self.assertIn(path, out); self.assertIn('shader chain', out)+ self.assertFalse(list((self.r / 'patches').glob('0002-*')))++ def test_check_rejects_a_patch_in_the_series_that_touches_the_chain(self):+ self.w('dm/gfx/tools/dmsh_pull.py', 'x\n'); git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', 'chain tool')+ diff = git(self.r, 'format-patch', '--stdout', '-1', 'HEAD')+ text = diff.replace('\n---\n', '\n\nUpstream-Status: pending\nReason: r\nIssue: LNPANDY/NX2Apple#4\n---\n', 1) + '\n'+ (self.r / 'patches/0002-chain.patch').write_text(text)+ s = self.r / 'patches/series'; s.write_text(s.read_text() + '0002-chain.patch\n')+ out = self.run_tool('check', expect=1)+ self.assertIn('0002-chain.patch', out); self.assertIn('dm/gfx/tools/dmsh_pull.py', out)++ def test_chain_free_patches_pass(self):+ self.run_tool('check')+++class ForkPatchList(Fixture):+ def test_check_fails_when_the_generated_list_is_stale(self):+ (self.r / 'dm/fork_patches.txt').write_text('something-else\n')+ self.assertIn('fork_patches.txt', self.run_tool('check', expect=1))+ self.run_tool('apply')+ self.run_tool('check')++ def test_the_list_follows_the_series_order(self):+ self.w('dm/b.c', 'b1\nb2 changed\n'); git(self.r, 'add', '-A'); git(self.r, 'commit', '-q', '-m', 'Second thing')+ self.run_tool('export', 'HEAD', '--reason', 'r', '--issue', 'LNPANDY/NX2Apple#5')+ self.run_tool('apply')+ self.assertEqual((self.r / 'dm/fork_patches.txt').read_text().split(), ['make-line-2-the-fork-s', 'second-thing'])++ def test_the_mac_app_stamps_the_list_into_info_plist(self):+ """pack_app.py's DMForkPatches is dm/fork_patches.txt, comma-separated, and empty without the file"""+ sys.path.insert(0, str(REPO / 'dm/tools'))+ spec = importlib.util.spec_from_file_location('pack_app_under_test', REPO / 'dm/mac/pack_app.py')+ mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)+ fake = self.tmp / 'dm/mac'; fake.mkdir(parents=True)+ mod.__file__ = str(fake / 'pack_app.py')+ self.assertEqual(mod.fork_patches(), '')+ (self.tmp / 'dm/fork_patches.txt').write_text('runtime-profile\nbc-cpu-decode\n')+ self.assertEqual(mod.fork_patches(), 'runtime-profile,bc-cpu-decode')+ args = type('A', (), dict(name='G', bundle_id=None, title_id='0100000000000000', version=None, category=None, marker=None, lang=None))()+ self.assertEqual(mod.info_plist(args, '26.0', False)['DMForkPatches'], 'runtime-profile,bc-cpu-decode')++ def test_the_first_log_line_and_the_ios_key_are_in_the_sources(self):+ """a read of the sources, not a run: the iOS app and the Mac launcher write the line, project.yml carries the key"""+ self.assertIn('"fork patches: %s\\n"', (REPO / 'dm/ios/Sources/main.m').read_text())+ self.assertIn('"fork patches: %s\\n"', (REPO / 'dm/mac/launcher.c').read_text())+ self.assertIn('DMForkPatches: ${DM_FORK_PATCHES}', (REPO / 'dm/ios/project.yml').read_text())+++if __name__ == '__main__':+ unittest.main()