Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

patches.py: count a patch as landed only when git says it is already applied (#322)

v0lumehi53a3402c8525
diff --git a/tools/patches.py b/tools/patches.py
index 2f1f303c..93d20bbd 100644
--- a/tools/patches.py
+++ b/tools/patches.py
@@ -182,7 +182,7 @@ def apply_series(wt, patches, stop=True):
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:
+ if not unmerged and clean and re.search(r'No changes|already applied', r.stdout + 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()
diff --git a/tools/tests/test_patches.py b/tools/tests/test_patches.py
index 7b0adab6..734562c0 100644
--- a/tools/tests/test_patches.py
+++ b/tools/tests/test_patches.py
@@ -75,6 +75,57 @@ class Queue(unittest.TestCase):
self.assertEqual(r.returncode, 2)
+def mkgit(cwd, *a):
+ 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=os.devnull, GIT_CONFIG_SYSTEM=os.devnull)
+ r = subprocess.run(['git', '-c', 'commit.gpgsign=false', *a], cwd=cwd, capture_output=True, text=True, env=env)
+ if r.returncode: raise AssertionError(f'git {a}: {r.stderr}')
+ return r.stdout.strip()
+
+
+class ApplySeries(unittest.TestCase):
+ """apply_series sorts a patch into applied, landed or conflicts (LNPANDY/NX2Apple#322)"""
+ def setUp(self):
+ self.tmp = Path(tempfile.mkdtemp(prefix='test-patches-'))
+ self.addCleanup(shutil.rmtree, self.tmp, True)
+ self.wt = self.tmp / 'up'; self.wt.mkdir()
+ mkgit(self.wt, 'init', '-q', '-b', 'main')
+ self.write('a.c', 'a\nb\nc\nd\ne\nf\ng\n')
+ mkgit(self.wt, 'add', '-A'); mkgit(self.wt, 'commit', '-q', '-m', 'base')
+
+ def write(self, name, text): (self.wt / name).write_text(text)
+
+ def patch_from(self, text, name):
+ """a patch that turns the base a.c into `text`, made on a scratch clone"""
+ mk = self.tmp / 'mk'
+ shutil.rmtree(mk, True)
+ mkgit(self.tmp, 'clone', '-q', str(self.wt), str(mk))
+ (mk / 'a.c').write_text(text)
+ mkgit(mk, 'commit', '-q', '-am', name)
+ f = self.tmp / f'{name}.patch'
+ f.write_text(mkgit(mk, 'format-patch', '--zero-commit', '--no-signature', '--stdout', '-1', 'HEAD') + '\n')
+ return P.Patch(f)
+
+ def test_a_patch_upstream_already_has_is_landed(self):
+ p = self.patch_from('a\nB\nc\nd\ne\nf\ng\n', 'line2')
+ self.write('a.c', 'a\nB\nc\nD\ne\nf\ng\n') # the same change, plus one near it, so the patch does not apply as it is
+ mkgit(self.wt, 'commit', '-q', '-am', 'upstream')
+ applied, landed, conflicts = P.apply_series(self.wt, [p])
+ self.assertEqual(([x.name for x, _ in applied], [x.name for x in landed], conflicts), ([], [p.name], []))
+
+ def test_a_patch_the_three_way_merge_cannot_start_is_a_conflict(self):
+ # the context is gone and the blobs of the patch are not in this repository: git has no base to merge with
+ p = self.patch_from('a\nB\nc\nd\ne\nf\ng\n', 'line2')
+ self.write('a.c', 'x\ny\nz\n')
+ mkgit(self.wt, 'commit', '-q', '-am', 'upstream')
+ text = '\n'.join('index 1234567..7654321 100644' if l.startswith('index ') else l for l in p.path.read_text().splitlines()) + '\n'
+ p.path.write_text(text)
+ applied, landed, conflicts = P.apply_series(self.wt, [p])
+ self.assertEqual(([x.name for x, _ in applied], [x.name for x in landed]), ([], []))
+ self.assertEqual([x.name for x, _ in conflicts], [p.name])
+ self.assertIn('Patch failed', conflicts[0][1])
+
+
class ChainRule(unittest.TestCase):
def test_the_chain_paths_come_from_the_engines_agents_md(self):
g = P.chain_paths(SRC)