Skip to content

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

A failed sync puts engine/ back as it was

v0lumehiffbfa06eb529
diff --git a/patches/README.md b/patches/README.md
index 4476e9d9..0ff04120 100644
--- a/patches/README.md
+++ b/patches/README.md
@@ -16,7 +16,7 @@ python3 tools/engine.py bump --dry --to main # which patches a bump would drop o
```
`sync` never throws away work in `engine/`: with commits or changes there it stops and names `export` (or `--force`, which keeps
-them on a backup branch). Offline, it says so and names the fix (`NX2APPLE_ENGINE_URL` takes a mirror or a local clone). The launchers
+them on a backup branch). A sync that fails (a patch does not apply) puts `engine/` back as it was, so the next sync does not see commits nobody made. Offline, it says so and names the fix (`NX2APPLE_ENGINE_URL` takes a mirror or a local clone). The launchers
run it on the first start of a checkout, CI runs it before every job that reads the engine, and the release app carries the engine tree
instead (below). A PR that adds files under `dm/`, `scripts/`, `docs/` or `ref/` fails the `pr-scope` check.
diff --git a/tools/engine.py b/tools/engine.py
index 23035412..fbd0906f 100755
--- a/tools/engine.py
+++ b/tools/engine.py
@@ -110,12 +110,27 @@ def head(eng):
return r.stdout.strip() if r.returncode == 0 else ''
-def local_work(eng, st):
+SYNC_COMMITTERS = ('patches@localhost', 'engine@localhost') # `git am` in patches.py and the generated-files commit above
+
+
+def made_by_sync(eng, h, pins):
+ """True when the commits from HEAD down to a pin were all committed by sync itself: what a sync that failed halfway
+ (older versions did not roll back) leaves behind. A commit made by a person has another committer."""
+ pins = {p for p in pins if p}
+ n = 0
+ while h not in pins:
+ r = git('log', '-1', '--format=%P%n%ce', h, cwd=eng, check=False).stdout.split('\n')
+ if len(r) < 2 or len(r[0].split()) != 1 or r[1] not in SYNC_COMMITTERS: return False
+ h, n = r[0], n + 1
+ return n > 0
+
+
+def local_work(eng, st, pins=()):
"""What sync would throw away: commits after the last sync, and changes to tracked or new files (ignored build products
such as dm/host/gen are not work and survive a sync)."""
out = []
h = head(eng)
- if st.get('head') and h and h != st['head']:
+ if st.get('head') and h and h != st['head'] and not made_by_sync(eng, h, pins):
n = git('rev-list', '--count', f'{st["head"]}..{h}', cwd=eng, check=False).stdout.strip() or '?'
out.append(f'{n} commit(s) after the last sync (HEAD {h[:12]})')
dirty = git('status', '--porcelain', cwd=eng, check=False).stdout.splitlines()
@@ -188,6 +203,12 @@ def build(root, eng, patches, sha):
return applied, landed
+def restore(eng, rev, branch):
+ git('am', '--abort', cwd=eng, check=False)
+ git('checkout', '--quiet', '--force', '-B', BRANCH, rev, cwd=eng)
+ if branch and branch != BRANCH: git('checkout', '--quiet', '--force', branch, cwd=eng)
+
+
def cmd_sync(a):
root = a.root
eng = engine_dir(root)
@@ -198,7 +219,7 @@ def cmd_sync(a):
patches = P.load(root)
ensure_repo(eng)
st = read_stamp(eng)
- work = local_work(eng, st) if head(eng) else []
+ work = local_work(eng, st, (pin(root), st.get('pin'))) if head(eng) else []
if work and st and not a.force:
raise Fail('engine/ has work a sync would throw away: ' + '; '.join(work) + '.\n'
' Turn commits into patches with `python3 tools/engine.py export`, then sync. Or keep them on a branch and '
@@ -211,7 +232,13 @@ def cmd_sync(a):
sha = pin(root)
got = fetch(eng, sha)
say(f'engine: building {sha[:12]} + {len(patches)} patches ({reason or "forced"}; upstream commit {got})')
- applied, _ = build(root, eng, patches, sha)
+ before = head(eng)
+ branch = git('symbolic-ref', '--quiet', '--short', 'HEAD', cwd=eng, check=False).stdout.strip()
+ try:
+ applied, _ = build(root, eng, patches, sha)
+ except Fail:
+ if before: restore(eng, before, branch) # a failed sync leaves engine/ as it was: the next sync must not see commits nobody made
+ raise
write_stamp(eng, root, head(eng))
say(f'engine: ready, {len(applied)} patches on upstream {sha[:12]} in {eng}')
return 0
diff --git a/tools/tests/test_engine.py b/tools/tests/test_engine.py
index e04bf155..67f5d82b 100644
--- a/tools/tests/test_engine.py
+++ b/tools/tests/test_engine.py
@@ -176,6 +176,58 @@ class LocalWork(Fixture):
self.assertIn('export', out); self.assertIn('1 commit(s)', out)
self.assertEqual((self.eng / 'dm/b.c').read_text(), 'b1\nb2 engine\n')
+ def conflict_patch(self):
+ self.add_patch('0002-conflict.patch', 'dm/a.c', 'other\n', 'Conflicting')
+
+ def fix_series(self):
+ (self.r / 'patches/0002-conflict.patch').unlink()
+ (self.r / 'patches/series').write_text('# order\n0001-line-2.patch\n')
+ self.add_patch('0002-b.patch', 'dm/b.c', 'b1\nb2 fork\n', 'Fine', header=HEADER.replace('#1', '#2'))
+
+ def test_a_failed_sync_leaves_engine_as_it_was_and_the_next_sync_needs_no_force(self):
+ self.tool('sync')
+ before, stamp = git(self.eng, 'rev-parse', 'HEAD'), (self.eng / '.git/nx2apple-stamp').read_text()
+ self.conflict_patch()
+ self.assertIn('does not apply', self.tool('sync', expect=2))
+ self.assertEqual(git(self.eng, 'rev-parse', 'HEAD'), before)
+ self.assertEqual((self.eng / '.git/nx2apple-stamp').read_text(), stamp)
+ self.assertEqual(git(self.eng, 'status', '--porcelain'), '')
+ self.fix_series()
+ self.assertIn('engine: ready', self.tool('sync'))
+ self.assertEqual((self.eng / 'dm/b.c').read_text(), 'b1\nb2 fork\n')
+
+ def test_a_failed_first_sync_does_not_block_the_next(self):
+ self.conflict_patch()
+ self.tool('sync', expect=2)
+ self.fix_series()
+ self.assertIn('engine: ready', self.tool('sync'))
+
+ def test_a_failed_sync_keeps_a_branch_the_user_was_on(self):
+ self.tool('sync')
+ git(self.eng, 'checkout', '-q', '-b', 'mine')
+ self.conflict_patch()
+ self.tool('sync', '--force', expect=2)
+ self.assertEqual(git(self.eng, 'rev-parse', '--abbrev-ref', 'HEAD'), 'mine')
+
+ def stuck(self):
+ """What an older sync left after a conflict: the pin plus the patches that applied, committed by `git am`, with the old stamp."""
+ self.tool('sync')
+ git(self.eng, 'checkout', '-q', '--force', '-B', 'nx2apple', self.up1)
+ env = {k: v for k, v in BASE_ENV.items() if not k.startswith('GIT_COMMITTER')} # the committer comes from -c, as in patches.am
+ subprocess.run(['git', '-c', 'user.name=patches.py', '-c', 'user.email=patches@localhost', '-c', 'commit.gpgsign=false', 'am', '-3', '--quiet',
+ str(self.r / 'patches/0001-line-2.patch')], cwd=self.eng, env=env, check=True)
+ self.assertNotEqual(git(self.eng, 'rev-parse', 'HEAD'), (self.eng / '.git/nx2apple-stamp').read_text().split('head ')[1].strip())
+
+ def test_commits_that_a_failed_older_sync_left_are_not_work(self):
+ self.stuck()
+ self.assertIn('engine: ready', self.tool('sync'))
+
+ def test_a_real_commit_on_top_of_what_a_failed_sync_left_is_still_work(self):
+ self.stuck()
+ self.commit_in_engine()
+ self.assertIn('commit(s) after the last sync', self.tool('sync', expect=2))
+ self.assertEqual((self.eng / 'dm/b.c').read_text(), 'b1\nb2 engine\n')
+
def test_sync_refuses_to_drop_an_uncommitted_change(self):
self.tool('sync')
self.wu('dm/b.c', 'dirty\n', self.eng)