evilmartians / lefthook

Fast and powerful Git hooks manager for any type of projects.
MIT License
4.66k stars 211 forks source link
git go golang hacktoberfest hooks lefthook manager

Build Status Coverage Status

Lefthook

The fastest polyglot Git hooks manager out there

<img align="right" width="147" height="100" title="Lefthook logo" src="./logo_sign.svg">

A Git hooks manager for Node.js, Ruby and many other types of projects.

📖 Read the introduction post

Sponsored by Evil Martians

Install

With Go (>= 1.22):

go install github.com/evilmartians/lefthook@latest

With NPM:

npm install lefthook --save-dev

With Ruby:

gem install lefthook

Installation guide with more ways to install lefthook: apt, brew, winget, and others.

Usage

Configure your hooks, install them once and forget about it: rely on the magic underneath.

TL;DR

# Configure your hooks
vim lefthook.yml

# Install them to the git project
lefthook install

# Enjoy your work with git
git add -A && git commit -m '...'

More details


Why Lefthook

pre-push:
  parallel: true
pre-commit:
  commands:
    frontend-linter:
      run: yarn eslint {staged_files}
    backend-linter:
      run: bundle exec rubocop --force-exclusion {all_files}
    frontend-style:
      files: git diff --name-only HEAD @{push}
      run: yarn stylelint {files}
pre-commit:
  commands:
    backend-linter:
      glob: "*.rb" # glob filter
      exclude: '(^|/)(application|routes)\.rb$' # regexp filter
      run: bundle exec rubocop --force-exclusion {all_files}
pre-commit:
  commands:
    backend-linter:
      root: "api/" # Careful to have only trailing slash
      glob: "*.rb" # glob filter
      run: bundle exec rubocop {all_files}

If oneline commands are not enough, you can execute files. Example.

commit-msg:
  scripts:
    "template_checker":
      runner: bash
pre-push:
  commands:
    packages-audit:
      tags: frontend security
      run: yarn audit
    gems-audit:
      tags: backend security
      run: bundle audit

If you are in the Docker environment. Example.

pre-commit:
  scripts:
    "good_job.js":
      runner: docker run -it --rm <container_id_or_name> {cmd}

If you a frontend/backend developer and want to skip unnecessary commands or override something into Docker. Description.

# lefthook-local.yml
pre-push:
  exclude_tags:
    - frontend
  commands:
    packages-audit:
      skip: true

If you want to run hooks group directly.

$ lefthook run pre-commit

If you want to run specific group of commands directly.

fixer:
  commands:
    ruby-fixer:
      run: bundle exec rubocop --force-exclusion --safe-auto-correct {staged_files}
    js-fixer:
      run: yarn eslint --fix {staged_files}
$ lefthook run fixer

If you don't want to see supporting information:

skip_output:
  - meta #(version and which hook running)
  - success #(output from runners with exit code 0)

Table of contents:

Guides

Examples

Articles