Pre-commit: Git hooks management tool

Estimated read time 6 min read

When you use Git to execute commit, push and other commands on a daily basis, the Git repository will record these data changes and keep them. In order to ensure the security of the code and improve the quality of the code, sometimes we want to perform some checks before the user performs operations such as commit and push. In this case, we need to use Git hooks.

pre-commit is an open source hook framework with currently 11,000 stars. can help developers run a series of code inspection and formatting operations, such as code style and specification checks, before submitting code to Git. It helps teams automatically perform various checks before committing code to ensure code quality and consistency. Key features of the pre-commit tool include:

  1. Easy to configure : pre-commit uses the .pre-commit-config.yaml file as a configuration file where you can specify various hooks and commands to run. The file can be stored in the project ‘s root directory or in the user’s home directory for global use.
  2. Multi-language support : pre-commit supports multiple programming languages ​​and tools. It can be integrated with various formatting tools, static code analyzers, code style checkers, etc.
  3. Extensibility : pre-commit allows customization and adding your own hook scripts. You can write your own scripts to run specific code inspection or formatting operations.
  4. Automation : pre-commit can be run automatically before each code commit.

1. Install pre-commit

1.1 pip installation

Pip is the package manager of Python . The prerequisite for installing pip is that you need to have a local Python environment.

pip install pre-commit

1.2 MacOs installation

brew install pre-commit

1.3 Conda installation

conda install -c conda-forge pre-commit

2. Quick Start

2.1 Create a file named .pre-commit-config.yaml

You can use the pre-commit sample-config command, which will output a basic configuration file with the following content

-   repo:
    rev: v3.2.0
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files

The specific meaning is as follows:

  • repos: Defines a series of warehouse mappings.
  • repo: defines which repository the hooks script used next will pull from.
  • rev: specifies the pulled tag data
  • hooks: defines the list of hooks to be used.

In this example, four hooks are configured:

  • trailing-whitespace: Used to check and fix trailing whitespace characters in code.
  • end-of-file-fixer: used to check and fix the line ending character at the end of the file.
  • check-yaml: used to check whether the syntax of YAML files is correct.
  • check-added-large-files: used to check whether submitted files are too large (default detection threshold is 500KB)

2.2 Manual operation

After the hook is added, pre-commit will only detect the changed files by default. Therefore, it is generally recommended to run a comprehensive detection of the project after adding it.

admin@admindeMacBook-Pro LearnGit %  pre-commit run --all-files
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed

2.3 Install Git Hook

The above is a manual test to check whether the project is compliant. In a real project, it certainly cannot be checked manually every time. At this time, you should also run the installation command and load the pre-commit configuration file into git hooks, so that every time git When committing, it will be automatically detected

admin@admindeMacBook-Pro LearnGit %pre-commit install
pre-commit installed at .git/hooks/pre-commit

PS: If the global hookPath path has been set before on the local computer, the following error will be reported during direct installation.

admin@admindeMacBook-Pro LearnGit % pre-commit install
[ERROR] Cowardly refusing to install hooks with `core.hooksPath` set.
hint: `git config --unset-all core.hooksPath`

You need to clear the core.hooksPath setting in the global configuration first

git config --global --unset-all core.hooksPath

After the installation is complete, view the contents of the pre-commit file

2.4 Automatic operation

Executing the git commit command will automatically detect whether it complies with relevant specifications. As shown below, the commit fails because the submitted content contains large files.

3. Create a new hook

pre-commit supports hooks written in multiple languages. As long as the git repository is an installable package (gem, npm, pypi, etc.) or a public executable file, it can be used with pre-commit. A Git repository containing a pre-commit plugin must contain a .pre-commit-hooks.yaml file

- id: trailing-whitespace
    name: Trim Trailing Whitespace
    description: This hook trims trailing whitespace.
    entry: trailing-whitespace-fixer
    language: python
    types: [text]

The meaning of this file

  • id: trailing-whitespace: The unique identifier of the plugin. In this file, each plug-in needs a unique ID to identify it.
  • name: Trim Trailing Whitespace: The name of the plug-in, used to describe the function of the plug-in.
  • description: This hook trims trailing whitespace.: Description of the plug-in, indicating that the function of the plug-in is to remove trailing whitespace characters.
  • entry: trailing-whitespace-fixer: The entry point of the plug-in. This specifies the file or command where the plug-in’s actual execution logic resides.
  • language: python: The programming language of the plug-in. This means that the plug-in is written in Python.
  • types: [text]: Filter for file types. In this example, the plugin will only run on text files, other types of files will be ignored.

With this configuration, the pre-commit tool will run a plugin named “Trim Trailing Whitespace” and use the specified Python file trailing-whitespace-fixer to perform the operation of trimming trailing whitespace characters. This plugin only works on text files.

4. Advanced functions

4.1 Uninstall

pre-commit uninstall

4.2 Temporarily disable a hook

By setting the SKIP environment variable, use the following command to skip flake8 detection

SKIP=flake8 git commit -m "foo"

4.3 Limiting the hook execution phase

Pre-commit supports many different types of git hooks. By default, it is detected during the commit phase, but it also supports detection at stages such as pre-push, pre-merge-commit, commit-msg, etc. As shown in the following command, you can specify –hook- type to determine the execution phase

admin@admindeMacBook-Pro LearnGit % pre-commit install --hook-type pre-commit --hook-type pre-push
pre-commit installed at .git/hooks/pre-commit
pre-commit installed at .git/hooks/pre-push

The supported Git hooks are as follows:

  • commit-msg
  • Post-checkout
  • Post-commit
  • Post-merge
  • Post-rewrite
  • Pre-commit
  • Pre-merge-commit
  • Pre-push
  • Pre-rebase
  • prepare-commit-msg

You May Also Like

More From Author

+ There are no comments

Add yours