# pre-commit

## What is Pre-commit?

As an engineer, you have your coding style after years, it might be you moved from a different language to Python or your previous company had different rules. It seems a trivial thing, but trust me, it can be a long discussion when you start asking `which way is better?` Especially, we all think our way is better than others. :)

That is why `pre-commit` is handy, it not only detects the issue in the code but also can have a standard coding style. If you are unfamiliar with what CI is, you can refer to my previous article on [GitHub Actions](https://blog.francischuang.com/github-actions#heading-what-does-cicd-achieve).

In this article, I will cover a few basic functionalities in `pre-commit`, such as

1. Lint
    
2. Format
    
3. Test
    

## How to set up?

You can run Pip or Poetry to install `pre-commit`, because my project use `poetry`, I will go with `poetry`.

1. Install `pre-commit`
    
    ```bash
    poetry add pre-commit --group dev
    ```
    
2. Add a configuration file called `.pre-commit-config.yaml` in the repo, here is a [sample](https://github.com/FrancisZhuang/workflows/blob/main/.pre-commit-config.yaml).
    
    %[https://gist.github.com/FrancisZhuang/52f6879eedd4f54b85d22e21fbe7ca23] 
    
3. Install the git hook scripts
    
    ```bash
    pre-commit install
    ```
    
4. If your project doesn't use `pre-commit` before, it's better to scan all files first
    
    ```bash
    pre-commit run --all-files
    ```
    
    After that, `pre-commit` will automatically detect the changed files whenever you run `git commit`
    
    ```bash
    [INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
    [INFO] Initializing environment for https://github.com/astral-sh/ruff-pre-commit.
    [INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
    [INFO] Once installed this environment will be reused.
    [INFO] This may take a few minutes...
    [INFO] Installing environment for https://github.com/astral-sh/ruff-pre-commit.
    [INFO] Once installed this environment will be reused.
    [INFO] This may take a few minutes...
    Check Yaml...............................................................Passed
    Fix End of Files.........................................................Passed
    Trim Trailing Whitespace.................................................Passed
    ruff.................................................(no files to check)Skipped
    ruff-format..........................................(no files to check)Skipped
    pytest...................................................................Passed
    ```
    
5. Bear in mind that if you see them `Failed` in the above message, you need to `git add` the changed files again, `pre-commit` it only helps you to fix the files, but it won't add the files `staged state` automatically. If you miss this step, you will always fail in the same step.
    

## **Conclusion**

If the company had 100 engineers in the team, there would be 100 different coding styles in the project if there were no standards. `pre-commit` can save a bunch of time from arguing about which is better, engineers can concentrate on the more important stuff, like structure and functionality

Happy coding!
