预提交钩子
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
您可以将 Prettier 与预提交工具结合使用。这样可以在通过 git add 标记为"暂存"的文件提交前重新格式化它们。
选项 1. lint-staged
适用场景: 当您需要同时使用其他代码质量工具(如 ESLint、Stylelint 等)或需要支持部分暂存文件 (git add --patch) 时非常有用。
请确保 Prettier 已安装并在您的 devDependencies 中。
npx mrm@2 lint-staged
此操作将安装 husky 和 lint-staged,然后在项目的 package.json 中添加配置,通过预提交钩子自动格式化支持的文件。
在 lint-staged 仓库中了解更多信息。
选项 2. pretty-quick
适用场景: 当您希望对变更/暂存文件进行整个文件格式化时非常理想。
与 simple-git-hooks 一同安装:
- npm
- yarn
- pnpm
- bun
npm install --save-dev simple-git-hooks pretty-quick
echo '{\n "pre-commit": "npx pretty-quick --staged"\n}\n' > .simple-git-hooks.json
npx simple-git-hooks
yarn add --dev simple-git-hooks pretty-quick
echo '{\n "pre-commit": "yarn pretty-quick --staged"\n}\n' > .simple-git-hooks.json
yarn simple-git-hooks
pnpm add --save-dev simple-git-hooks pretty-quick
echo '{\n "pre-commit": "pnpm pretty-quick --staged"\n}\n' > .simple-git-hooks.json
pnpm simple-git-hooks
bun add --dev simple-git-hooks pretty-quick
echo '{\n "pre-commit": "bun pretty-quick --staged"\n}\n' > .simple-git-hooks.json
bun simple-git-hooks
在 pretty-quick 仓库中了解更多信息。
选项 3. Husky.Net
适用场景: 用于在 .NET 环境中将 Prettier 与其他代码质量工具(如 dotnet-format、ESLint、Stylelint 等)结合使用的解决方案。支持多种文件状态(暂存区、上次提交、git 文件等)。
dotnet new tool-manifest
dotnet tool install husky
dotnet husky install
dotnet husky add pre-commit
安装完成后,您可以将 Prettier 任务添加到 task-runner.json 中。
{
"command": "npx",
"args": ["prettier", "--ignore-unknown", "--write", "${staged}"],
"pathMode": "absolute"
}
选项 4. git-format-staged
适用场景: 当您需要格式化部分暂存文件,且其他选项无法满足项目需求时的理想选择。
Git-format-staged 用于运行任何能通过 stdin 接受文件内容的格式化工具。其处理部分暂存文件的方式与其他工具不同:它直接将格式化程序应用于 git 对象数据库中的对象,并将变更合并回工作树。这种方法提供以下保证:
-
提交中的变更始终会被格式化。
-
未暂存的变更在任何情况下都不会在格式化过程中被暂存。
-
如果格式化后的暂存变更与未暂存变更存在冲突,您的工作树文件将保持原状——您的工作不会被覆盖,也无需清理任何存储内容。
-
未暂存的变更不会被格式化。
Git-format-staged 需要 Python v3 或 v2.7。Linux 和 macOS 通常预装 Python,但 Windows 没有。配合 husky 使用 git-format-staged:
- npm
- yarn
- pnpm
- bun
npx husky init
npm install --save-dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
yarn husky init
yarn add --dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
pnpm exec husky init
pnpm add --save-dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
bunx husky init
bun add --dev git-format-staged
bun --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
根据项目需求添加或删除文件扩展名。请注意,无论您列出哪些扩展名,格式化过程都会遵循项目中的任何 .prettierignore 文件。
要了解 git-format-staged 的工作原理,请参阅部分暂存文件的自动代码格式化。
选项 5. Shell 脚本
您也可以将此脚本保存为 .git/hooks/pre-commit 并赋予执行权限:
#!/bin/sh
FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0
# Prettify all selected files
echo "$FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --write
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
exit 0
如果 git 报告显示经过 Prettier 格式化的文件在提交后仍被标记为已修改,你可能需要添加一个 post-commit 脚本来更新 git 索引。
将类似以下内容添加到 .git/hooks/post-commit 文件中:
#!/bin/sh
git update-index -g