From 27f078331d5e13a2c3e46dfa8911d0dca297ad40 Mon Sep 17 00:00:00 2001 From: iceBear67 Date: Fri, 14 Aug 2026 04:52:37 +0000 Subject: [PATCH] Fix apply --force against a dirty tree; clarify patch-count guard Two issues found by exercising the scripts end to end: - `git checkout -B` refuses to run when the worktree is dirty, so `make apply FORCE=1` aborted in exactly the situation FORCE exists to handle. Use `checkout -f -B`; the non-forced path has already verified the tree is clean, so forcing changes nothing there. - The commit-count guard claimed the user forgot `make rebuild`, but deleting a patch file on purpose trips it identically. Say both, and document the FORCE=1 form in the README's "drop the example patches" instructions, which would otherwise have failed. Co-Authored-By: Claude Opus 5 --- README.md | 5 +++-- scripts/apply-patches.sh | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 210da94..edb1979 100644 --- a/README.md +++ b/README.md @@ -175,11 +175,12 @@ git format-patch --no-stat -N --zero-commit --full-index --no-signature (或 `)`)结尾——**改上游代码时顺手确认它的测试仍然成立**,这个补丁本身就是个例子。 2. **`0002`** 新增文件——根目录 `FORK.md`。 -不需要就直接删: +不需要就直接删。注意要带 `FORCE=1`——`work/` 里还留着这两个补丁的提交,而删掉补丁文件后 +`patches/` 比 `work/` 少,安全检查会拦下来(它无法区分「你故意删的」和「你忘了 rebuild」): ```sh rm patches/0001-*.patch patches/0002-*.patch -make apply +make apply FORCE=1 ``` ## 许可 diff --git a/scripts/apply-patches.sh b/scripts/apply-patches.sh index 2d5a42a..b7362cf 100755 --- a/scripts/apply-patches.sh +++ b/scripts/apply-patches.sh @@ -38,8 +38,9 @@ else want="$(count_patches)" if [[ "$have" != "$want" ]]; then die "work/ has $have commit(s) above $BASE_TAG but patches/ has $want patch file(s). - You probably forgot to run 'make rebuild' after committing. - To discard the work/ commits and replay patches/ as-is: make apply FORCE=1" + Either you committed in work/ without running 'make rebuild' (export it: + make rebuild), or you added/removed patch files on purpose -- in which case + discard the work/ commits and replay patches/ as-is: make apply FORCE=1" fi fi fi @@ -58,7 +59,10 @@ gitdir="$(git rev-parse --git-dir)" [[ -d "$gitdir/rebase-apply" ]] && git am --abort >/dev/null 2>&1 || true info "Resetting work/ to upstream $(git rev-parse --short "$REV")" -git checkout -q -B "$WORK_BRANCH" "$REV" +# -f is required: without it checkout refuses to run against a dirty tree, which +# is precisely the case FORCE=1 exists to handle. The tree is already known +# clean on the non-forced path, so forcing costs nothing there. +git checkout -q -f -B "$WORK_BRANCH" "$REV" git reset -q --hard "$REV" # -x removes ignored files too, which is what we want for a pristine tree --