paint-brush
如何使用 Git Hooks 自动化 Git 工作流经过@smy
新歷史

如何使用 Git Hooks 自动化 Git 工作流

经过 Syed Muhammad Yaseen4m2024/07/30
Read on Terminal Reader

太長; 讀書

Git 钩子是 Git 存储库中发生特定事件时自动运行的脚本。它们允许您自定义 Git 的内部行为,并在开发生命周期的关键点触发可自定义的操作。例如,您想在提交文件之前运行 Lint 检查,因此我们使用预提交钩子。
featured image - 如何使用 Git Hooks 自动化 Git 工作流
Syed Muhammad Yaseen HackerNoon profile picture
0-item
1-item

你好!


希望你一切顺利!这是 SMY!👋 让我们马上开始 🚀


....

我已经设置了一个示例 commit-msg 钩子,在每次提交时


↳ 检查分支名称约定,即 [type]/ABC-[ticketnumber]-[objective]


↳ 检查提交消息约定,即 [ABC-ticketnumber] [type]: objective


↳ 检查 lints、prettier 并运行单元测试


要点链接: https://gist.github.com/smyaseen/17083d60d02a07b2a3122410e2d39b6f

.....

内容:

  • Wait What?
  • But Why?
  • But How?

1️⃣ 什么-


↳ Git 钩子是每当 Git 存储库中发生特定事件时自动运行的脚本。


↳ 它们让您自定义 Git 的内部行为并在开发生命周期的关键点触发可自定义的操作。


↳ 有本地钩子和服务器端钩子。

↳ 本地钩子在您的机器上和远程存储库的服务器端运行。


本地钩子:


↳ 预提交 ↳ 准备提交消息 ↳ 提交消息 ↳ 提交后 ↳ 签出后 ↳ 预变基


服务器挂钩:


↳ 接收前 ↳ 更新 ↳ 接收后


2️⃣为什么-


↳ 例如,您想在提交文件之前运行 Lint 检查,因此我们使用预提交钩子。


↳ 另一个例子,您想要强制执行标准分支名称和 commit-msg 约定,因此我们使用 commit-msg 钩子。


↳ 还有更多...


3️⃣ 如何-


↳ 为了便于设置,在 JavaScript 项目中安装一个名为 Husky 的库并按照文档进行操作

https://www.npmjs.com/package/husky


↳ 一个例子是我想要强制分支名称、提交消息约定并运行 lint、prettier 和测试。


↳ 我们可以使用多个钩子。就我而言,我将添加一个 commit-msg 钩子。我没有选择预提交的原因是它在提交之前运行时不会检查 commit-msg。我不希望开发人员运行检查并在提交消息时失败,修复并再次运行所有操作。我想先检查提交消息。


↳ 按照 Husky 的文档创建一个 commit-msg 钩子,并粘贴以下示例:


 #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" # Checks for branch name currentBranch=$(git rev-parse --abbrev-ref HEAD) requiredPattern="^(build|chore|feat|docs|refactor|perf|test)/ABC-\d+-.+$" if ! echo "$currentBranch" | grep -qE $requiredPattern; then echo "\nInvalid branch name: $currentBranch" echo "-" echo "Should follow this pattern: build|chore|feat|docs|refactor|perf|test/ABC-ticketnumber-any-text" echo "-" echo "example: docs/ABC-123-update-readme.md" echo "-" echo "Refer to this for convention:" echo "-" echo "build : Changes related to building the code (eg adding npm dependencies or external libraries)." echo "-" echo "chore: Changes that do not affect the external user (eg updating the .gitignore file or .prettierrc file)." echo "-" echo "feat: A new feature." echo "-" echo "fix: A bug fix." echo "-" echo "docs: Documentation a related changes." echo "-" echo "refactor: A code that neither fix bug nor adds a feature." echo "-" echo "perf: A code that improves performance style: A code that is related to styling." echo "-" echo "test: Adding new test or making changes to existing test" echo "-\n" exit 1 # Branch name doesn't match the pattern, exit with error code fi # Checks for commit message commit_message="$(cat "$1")" pattern='^\[ABC-[0-9]+\] (build|chore|feat|docs|refactor|perf|test): .+$' if [[ ! $commit_message =~ $pattern ]]; then echo "\nInvalid commit message: $commit_message" echo "-" echo "Should follow this pattern: [ABC-ticketnumber] build|chore|feat|docs|refactor|perf|test: objective" echo "-" echo "example: [ABC-15] chore: updated .gitignore" echo "-" echo "Refer to this for convention:" echo "-" echo "build : Changes related to building the code (eg adding npm dependencies or external libraries)." echo "-" echo "chore: Changes that do not affect the external user (eg updating the .gitignore file or .prettierrc file)." echo "-" echo "feat: A new feature." echo "-" echo "fix: A bug fix." echo "-" echo "docs: Documentation a related changes." echo "-" echo "refactor: A code that neither fix bug nor adds a feature." echo "-" echo "perf: A code that improves performance style: A code that is related to styling." echo "-" echo "test: Adding new test or making changes to existing test" echo "-\n" exit 1 fi # npx lint-staged -- uncomment when have lint setted up # npm run test -- uncomment when have test setted up


↳ 现在,无论何时提交,它都会检查分支和提交命名约定以及运行 lint 检查。最后,在一切准备就绪后,它将在最后运行测试。

4️⃣ 结果 -

✨ 一致性。

✨ 标准化。

✨ 不要介意琐事。

✨ 自动化并关注影响力。


包起来:


我们刚刚使用 Git Hooks 提升了您的开发工作流程。🚀


.....


现在你可以增强你的开发工作流程了🚀


就这样吧,朋友们!希望你们读起来愉快。谢谢!✨


👉 在GitHubLinkedIn上关注我