自动二分会检出历史提交并执行其中的测试脚本
原文依据:2 处流程建议让 Git 自动切换到多个历史提交,并在每个提交上运行 npm 测试。历史版本中的 package 脚本与当前版本同样可以执行任意本地命令,而该指引没有要求先审查这些脚本或隔离执行环境。
若被检查的提交包含恶意或不再可信的测试脚本,运行二分可能读取可用凭据、访问网络或更改用户文件;中断的二分也会让工作区停留在临时检出的提交。
该技能明确建议用 `git bisect` 切换历史提交,并通过 `git bisect run` 自动运行仓库测试命令。若历史提交中的测试或 npm 生命周期脚本不可信,命令会在用户机器上执行其代码。此处没有要求先审查脚本或使用隔离环境。用户可要求作者加入可信仓库前提、脚本审查和沙箱限制。
```bash# Find which commit introduced the buggit bisect startgit bisect bad # Current commit is brokengit bisect good <known-good-sha> # This commit worked# Git will checkout midpoint commits; run your test at eachgit bisect run npm test -- --grep "failing test" # substitute the repository's focused-test command```查看另外 1 个位置
**Use bisection for regression bugs:**```bash# Find which commit introduced the buggit bisect startgit bisect bad # Current commit is brokengit bisect good <known-good-sha> # This commit worked# Git will checkout midpoint commits; run your test at eachgit bisect run npm test -- --grep "failing test" # substitute the repository's focused-test command```