ویرگول
ورودثبت نام
پویا ابراهیمی
پویا ابراهیمی
خواندن ۳ دقیقه·۲۴ روز پیش

husky vs git hooks

Git Hooks and Husky

Git Hooks

  • Git supports hooks, which are scripts that run at various stages (such as before a commit, after a push, etc.).
  • The Git hooks directory is located at:.git/hooks

Categorized

Git hooks are categorized into client-sideand server-side hooks, each triggered at different stages of Git operations. Here's a list of all the available Git hooks:

Client-Side Hooks

These hooks run on the developer's machine during operations like commits, merges, and checkouts.

  1. pre-commit:
  2. prepare-commit-msg:
  3. commit-msg:
  4. post-commit:
  5. pre-rebase:
  6. post-checkout:
  7. post-merge:
  8. pre-push:
  9. pre-applypatch:
  10. post-applypatch:
  11. pre-commit-msg (for interactive rebases):

Server-Side Hooks

These hooks run on the Git server, typically during a push operation or when interacting with remote repositories.

  1. pre-receive:
  2. update:
  3. post-receive:
  4. post-update:
  5. push-to-checkout:

Commit Process in Git

When the commit command is executed:

git commit -m &quottest&quot
  1. hook : Pre-commit
  2. hook : prepare-commit-msg -
  3. A file named .git/COMMIT_EDITMSGis created.
  4. The commit message is written into this file.
  5. Hook : commit-msg
  6. hook : pre-applypatch
  7. commit is completed.
  8. Hook : post-commit
  9. Hook : post-applypatch

Note: .git/COMMIT_EDITMSGis file is not removed after the commit.


Relationship Between Husky and Git

  • Git: A version control system that supports hooks.
  • Husky: A tool for managing Git hooks more easily, allowing you to handle hooks automatically with simple code, without needing to manually edit hook files.
  • Summary: Husky is an external tool that integrates with Git to simplify hook management, but it is not part of Git itself.
  • It is true that Husky changes Git hooks, but it does so in a streamlined and user-friendly way.

note : example of server side git hooks -> CI/CD pipelines run on a server (such as Jenkins, GitHub Actions, GitLab CI) after the code is pushed to a remote repository.

Installing Husky

To install Husky: npm install --save-dev husky
  1. To initialize Husky (for older versions):
npx husky-init && husky install

Installing Commitlint

  1. To install Commitlint for validating commit messages:
npm install --save-dev @commitlint/config-conventional @commitlint/cli
  1. Create a file named commitlint.config.js in the root of your project with the following content:
module.exports = { extends: ['@commitlint/config-conventional'] };
  1. Add a hook to validate commit messages using Husky:
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'


npx: Executes the tool without installing it permanently.
--no-install: Prevents reinstalling a package that's already installed.
commitlint: Validates the commit message.
--edit "$1": Retrieves the commit message from the file generated by Git.


Configuring Commitlint Rules

  1. Example Commitlint configuration:
module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'header-max-length': [2, 'always', 100], 'type-enum': [2, 'always', [ 'feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'merge' ]], 'header-case': [2, 'always', 'sentence-case'], 'merge-message-format': [2, 'always', 'merged: <branch> into <target>'] }, };
  1. Explanation of values in the configuration:
    level: Specifies the strictness of the rule.
    0: The rule is ignored.
    1: Warning, the commit is not blocked.
    2: Error, the commit is blocked.
    when: Specifies whether the rule should always ('always') or never ('never') be applied.
    value: The expected value or format.

Adding a Pre-commit Hook

To run tests before committing:

npx husky add .husky/pre-commit &quotnpm test&quot

Summary

  • Git hooks are tools that allow running specific scripts during different stages of the commit and push process.
  • Husky improves this process by providing a simple, maintainable tool for managing Git hooks.
  • Commitlint is used to ensure that commit messages follow a consistent structure and meet quality standards.
git hooksgithuskycommitprecommi
شاید از این پست‌ها خوشتان بیاید