Skip to main content

预提交钩子

你可以将 Prettier 与预提交工具一起使用。这可以在提交之前重新格式化通过 git add 标记为“暂存”的文件。

¥You can use Prettier with a pre-commit tool. This can re-format your files that are marked as “staged” via git add before you commit.

选项 1。lint-staged

¥Option 1. lint-staged

用例:当你想将其他代码质量工具与 Prettier(例如 ESLint、Stylelint 等)一起使用时,或者如果你需要对部分暂存文件 (git add --patch) 的支持时,很有用。

¥Use Case: Useful for when you want to use other code quality tools along with Prettier (e.g. ESLint, Stylelint, etc.) or if you need support for partially staged files (git add --patch).

在你继续之前,请确保 Prettier 已安装并且在你的 devDependencies 中。

¥Make sure Prettier is installed and is in your devDependencies before you proceed.

npx mrm@2 lint-staged

这将安装 huskylint-staged,然后向项目的 package.json 添加一个配置,该配置将在预提交钩子中自动格式化支持的文件。

¥This will install husky and lint-staged, then add a configuration to the project’s package.json that will automatically format supported files in a pre-commit hook.

lint-staged 存储库中阅读更多内容。

¥Read more at the lint-staged repo.

选项 2。Husky.Net

¥Option 2. Husky.Net

用例:将 Prettier 与其他代码质量工具(例如 dotnet-format、ESLint、Stylelint 等)一起使用的 dotnet 解决方案。它支持多种文件状态(暂存 - 最后提交、git 文件等)

¥Use Case: A dotnet solution to use Prettier along with other code quality tools (e.g. dotnet-format, ESLint, Stylelint, etc.). It supports multiple file states (staged - last-commit, git-files etc.)

dotnet new tool-manifest
dotnet tool install husky
dotnet husky install
dotnet husky add pre-commit

安装后,你可以将 Prettier 任务添加到 task-runner.json

¥after installation you can add prettier task to the task-runner.json.

{
"command": "npx",
"args": ["prettier", "--ignore-unknown", "--write", "${staged}"],
"pathMode": "absolute"
}

选项 3。git-format-staged

¥Option 3. git-format-staged

用例:非常适合当你想要格式化部分暂存的文件,而其他选项不适合你的项目时。

¥Use Case: Great for when you want to format partially-staged files, and other options do not provide a good fit for your project.

Git-format-staged 用于运行任何可以通过标准输入接受文件内容的格式化程序。它的操作方式不同于其他格式化部分暂存文件的工具:它将格式化程序直接应用于 git 对象数据库中的对象,并将更改合并回工作树。这个过程提供了几个保证:

¥Git-format-staged is used to run any formatter that can accept file content via stdin. It operates differently than other tools that format partially-staged files: it applies the formatter directly to objects in the git object database, and merges changes back to the working tree. This procedure provides several guarantees:

  1. 提交中的更改总是格式化的。

    ¥Changes in commits are always formatted.

  2. 在格式化过程中,在任何情况下都不会进行未暂存的更改。

    ¥Unstaged changes are never, under any circumstances staged during the formatting process.

  3. 如果格式化、暂存的更改和未暂存的更改之间存在冲突,那么你的工作树文件将保持不变 - 你的工作不会被覆盖,也没有需要清理的隐藏内容。

    ¥If there are conflicts between formatted, staged changes and unstaged changes then your working tree files are left untouched - your work won’t be overwritten, and there are no stashes to clean up.

  4. 未暂存的更改未格式化。

    ¥Unstaged changes are not formatted.

Git-format-staged 需要 Python v3 或 v2.7。Python 通常预装在 Linux 和 macOS 上,但不会预装在 Windows 上。将 git-format-staged 与 husky 一起使用:

¥Git-format-staged requires Python v3 or v2.7. Python is usually pre-installed on Linux and macOS, but not on Windows. Use git-format-staged with husky:

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')"

添加或删除文件扩展名以适合你的项目。请注意,无论你列出哪些扩展名,格式都会尊重项目中的任何 .prettierignore 文件。

¥Add or remove file extensions to suit your project. Note that regardless of which extensions you list formatting will respect any .prettierignore files in your project.

要了解 git-format-staged 的工作原理,请参阅 部分暂存文件的自动代码格式化

¥To read about how git-format-staged works see Automatic Code Formatting for Partially-Staged Files.

选项 4。Shell 脚本

¥Option 4. Shell script

或者,你可以将此脚本另存为 .git/hooks/pre-commit 并授予其执行权限:

¥Alternately you can save this script as .git/hooks/pre-commit and give it execute permission:

#!/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 报告你的美化文件在提交后仍然被修改,你可能需要添加一个 用于更新 git 索引的提交后脚本

¥If git is reporting that your prettified files are still modified after committing, you may need to add a post-commit script to update git’s index.

.git/hooks/post-commit 中添加如下内容:

¥Add something like the following to .git/hooks/post-commit:

#!/bin/sh
git update-index -g