[Code Quality] Lefthook

2023-03-11 hit count image

Let's see how to use Lefthook to execute the test code or Linter when you commit on Git.


When I develop the frontend, I use Husky and lint-staged to execute ESLint and Prettier automatically when I commit on Git.

Recently, I use Golang to develop the web server and use Flutter to develop the mobile app. So, I need a tool like Husky and lint-staged, that is not based Node environment, to execute the Linter and test code .

In this blog post, I will introduce how to use Lefthook to execute Linter and test code when you commit on Git.

Install Lefthook

Lefthook supports various languages and OS. You can install Lefthook depending on your language or OS.

In my case, I use macOS, so I executed the following commant to install Lefthook.

brew install lefthook

Configure Lefthook

To use Lefthook, go to your project folder, and create the ./lefthook.yml file and modify it like the below.

  parallel: true
      glob: './src/**'
      run: npx prettier --check {staged_files}
      glob: 'src/**/*.{ts,tsx}'
      run: npx eslint {staged_files}

You can use pre-commit or pre-push to bind some commands before commiting or pushing on Git. You can use parallel option to execute the commands in parallel. You can configure commands to assign the command to the run option with specific names(pretty, linter, etc.). Alos, you can filter the file list with the glob option, and you can execute the command on the specific folder by setting the root optoin.

      root: "api/" # Careful to have only trailing slash
      glob: "*.rb" # glob filter
      run: bundle exec rubocop {all_files}

In addition, you can use the following features.

Execute Lefhook

To check the Lefthook is configured well, execute the following command.

lefthook run pre-commit

After it, you can see the commands that we’ve configured in the ./lefthook.yml file are executed.

Apply Lefthook

The lefthook run ... command just executes the ./lefthook.yml file contents one time. However, we want to execute the ./lefthook.yml file contents by every Git commit event. So, execute the following command to configure the ./lefthook.yml file contents are executed when every Git commit event.

lefthook install

Ater then, when you execute the commit command of Git, you can see the ./lefthook.yml file contents are executed.

Execute hooks if subfolder changes

When you use Lefthook, sometimes you want to execute the specific commands when only the specific subfolder files change. For example, there is a project which contains the frontend and backend, and when you modify only frontend code, you don’t want to execute the commands about the backend.

At this time, you can use the files option like the following to execute the specific commands when the files in the specific folder are changed.

  parallel: true
      files: git diff --name-only HEAD | grep "frontend/" || echo ''
      root: "frontend/"
      run: npm run lint
      files: git diff --name-only HEAD | grep "backend/" || echo ''
      root: "backend/"
      run: go test ./backend/test/...


Done! we’ve seen how to use Lefthook to execute the Linter or test code when the commit command of Git is executed. Lefthook supports various development environments, so you can use it in the project not based on Node.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.