Convert Figma logo to code with AI

evilmartians logolefthook

Fast and powerful Git hooks manager for any type of projects.

4,745
211
4,745
53

Top Related Projects

A framework for managing and maintaining multi-language pre-commit hooks.

32,324

Git hooks made easy 🐶 woof!

A fully configurable and extendable Git hook manager

Fast and powerful Git hooks manager for any type of projects.

16,798

The commitizen command line utility. #BlackLivesMatter

📓 Lint commit messages

Quick Overview

Lefthook is a fast and powerful Git hooks manager for software development teams. It allows developers to easily manage, share, and execute scripts before committing or pushing code, ensuring consistent code quality and workflow across projects.

Pros

  • Fast execution due to parallel running of tasks
  • Language-agnostic, supporting various programming languages and tools
  • Easy to configure and share across team members
  • Supports both pre-commit and pre-push hooks

Cons

  • Requires initial setup and configuration
  • May add complexity to the development process for small projects
  • Learning curve for team members unfamiliar with Git hooks
  • Potential for over-reliance on automated checks

Code Examples

  1. Basic configuration in lefthook.yml:
pre-commit:
  commands:
    linter:
      run: rubocop {staged_files}
    tests:
      run: rspec

This configuration runs RuboCop on staged files and executes RSpec tests before each commit.

  1. Using glob patterns to target specific files:
pre-push:
  commands:
    js-linter:
      glob: "*.{js,jsx}"
      run: eslint {all_files}

This example runs ESLint on all JavaScript and JSX files before pushing.

  1. Running commands in parallel:
pre-commit:
  parallel: true
  commands:
    linter:
      run: rubocop {staged_files}
    tests:
      run: rspec
    security:
      run: brakeman

This configuration runs linting, tests, and security checks in parallel before committing.

Getting Started

  1. Install Lefthook:

    gem install lefthook
    
  2. Initialize Lefthook in your project:

    lefthook install
    
  3. Create a lefthook.yml file in your project root:

    pre-commit:
      commands:
        linter:
          run: rubocop {staged_files}
    
  4. Commit changes to activate the hooks:

    git add lefthook.yml
    git commit -m "Add Lefthook configuration"
    

Lefthook is now set up and will run the configured hooks on future commits.

Competitor Comparisons

A framework for managing and maintaining multi-language pre-commit hooks.

Pros of pre-commit

  • Extensive plugin ecosystem with many pre-built hooks
  • Language-agnostic, supporting multiple programming languages
  • Well-established project with a large community and extensive documentation

Cons of pre-commit

  • Configuration can be more complex, especially for beginners
  • Requires Python installation, which may not be ideal for all projects
  • Performance can be slower compared to native Git hooks

Code Comparison

pre-commit configuration example:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.1.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer

Lefthook configuration example:

pre-commit:
  commands:
    lint:
      glob: "*.{js,ts}"
      run: npm run lint {staged_files}
    format:
      glob: "*.{js,ts}"
      run: npm run format {staged_files}

Both tools offer flexible configuration options, but pre-commit relies more on external repositories for hooks, while Lefthook focuses on running local commands. pre-commit's YAML structure is centered around repositories and their associated hooks, whereas Lefthook organizes hooks by Git events and allows direct command execution.

32,324

Git hooks made easy 🐶 woof!

Pros of Husky

  • Widely adopted and well-established in the JavaScript ecosystem
  • Simpler setup process with automatic Git hooks installation
  • Extensive documentation and community support

Cons of Husky

  • Limited to JavaScript/Node.js projects
  • Can be slower for large projects due to Node.js startup time
  • Less flexible configuration options compared to Lefthook

Code Comparison

Husky configuration (.huskyrc.json):

{
  "hooks": {
    "pre-commit": "npm test",
    "pre-push": "npm run lint"
  }
}

Lefthook configuration (lefthook.yml):

pre-commit:
  commands:
    tests:
      run: npm test
pre-push:
  commands:
    linter:
      run: npm run lint

Both tools aim to simplify Git hooks management, but Lefthook offers a more language-agnostic approach and advanced features like parallel execution and selective hook running. Husky, on the other hand, provides a more straightforward setup for JavaScript projects and has a larger user base. The choice between the two depends on project requirements, language preferences, and desired level of customization.

A fully configurable and extendable Git hook manager

Pros of Overcommit

  • Written in Ruby, which may be preferred by Ruby developers
  • Extensive plugin ecosystem with many pre-built hooks
  • Supports parallel hook execution for improved performance

Cons of Overcommit

  • Requires Ruby runtime, which may not be available in all environments
  • Configuration can be more complex due to its extensive feature set
  • Slower startup time compared to Lefthook

Code Comparison

Overcommit configuration (.overcommit.yml):

PreCommit:
  RuboCop:
    enabled: true
    command: ['bundle', 'exec', 'rubocop']
  ESLint:
    enabled: true
    command: ['yarn', 'run', 'eslint']

Lefthook configuration (lefthook.yml):

pre-commit:
  commands:
    rubocop:
      run: bundle exec rubocop
    eslint:
      run: yarn run eslint

Both tools offer similar functionality for managing Git hooks, but Lefthook's configuration is generally more straightforward. Overcommit provides a richer set of built-in hooks and plugins, while Lefthook focuses on simplicity and performance. The choice between the two may depend on your project's specific needs and the development team's preferences.

Fast and powerful Git hooks manager for any type of projects.

Pros of Lefthook

  • Lightweight and fast Git hooks manager
  • Supports multiple languages and frameworks
  • Easy configuration with YAML files

Cons of Lefthook

  • Limited documentation for advanced use cases
  • Fewer integrations compared to some alternatives
  • May require additional setup for complex workflows

Code Comparison

Both repositories contain the same codebase, as they are the same project. Here's a sample configuration from Lefthook:

pre-commit:
  parallel: true
  commands:
    linter:
      glob: "*.{js,ts}"
      run: yarn eslint {staged_files}
    tests:
      glob: "*.{js,ts}"
      run: yarn jest --findRelatedTests {staged_files}

Summary

Lefthook is a versatile Git hooks manager that offers simplicity and flexibility. It's particularly useful for projects requiring custom pre-commit or pre-push checks. The tool supports parallel execution of commands and integrates well with various development environments. However, users might face challenges with more complex setups due to limited advanced documentation. Overall, Lefthook provides a solid foundation for managing Git hooks in development workflows.

16,798

The commitizen command line utility. #BlackLivesMatter

Pros of cz-cli

  • Standardizes commit messages across teams and projects
  • Provides an interactive CLI for creating structured commits
  • Integrates well with semantic versioning and changelog generation

Cons of cz-cli

  • Focuses primarily on commit message formatting, not overall git hooks
  • May require additional setup for custom commit types or scopes
  • Less flexible for implementing custom pre-commit or pre-push checks

Code Comparison

cz-cli:

module.exports = {
  types: [
    { value: 'feat', name: 'feat:     A new feature' },
    { value: 'fix', name: 'fix:      A bug fix' },
    // ... more types
  ],
  // ... other configuration options
};

Lefthook:

pre-commit:
  parallel: true
  commands:
    linter:
      glob: "*.{js,ts}"
      run: yarn eslint {staged_files}
    tests:
      run: yarn test

Key Differences

  • cz-cli focuses on standardizing commit messages, while Lefthook is a more general-purpose git hooks manager
  • Lefthook offers broader functionality for running various pre-commit and pre-push tasks
  • cz-cli provides a guided interface for creating commits, whereas Lefthook automates checks and tasks before commits or pushes
  • Lefthook has a YAML-based configuration, while cz-cli typically uses JavaScript for configuration

📓 Lint commit messages

Pros of commitlint

  • Specialized focus on enforcing commit message conventions
  • Extensive configuration options for custom commit message rules
  • Integrates well with other conventional commit tools and workflows

Cons of commitlint

  • Limited to commit message linting, lacks broader Git hook functionality
  • Requires additional setup and tools for full Git hook management
  • May have a steeper learning curve for complex rule configurations

Code Comparison

commitlint configuration example:

{
  "extends": ["@commitlint/config-conventional"],
  "rules": {
    "type-enum": [2, "always", ["feat", "fix", "docs", "style", "refactor", "test", "chore"]]
  }
}

Lefthook configuration example:

pre-commit:
  commands:
    lint:
      glob: "*.{js,ts}"
      run: npm run lint
commit-msg:
  commands:
    commitlint:
      run: npx commitlint --edit {1}

While commitlint focuses solely on commit message linting, Lefthook provides a more comprehensive Git hooks management solution. commitlint excels in enforcing commit conventions but requires additional setup for other Git hooks. Lefthook offers a simpler, all-in-one approach to managing various Git hooks, including commit message linting when integrated with commitlint.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status Coverage Status

Lefthook

The fastest polyglot Git hooks manager out there

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

  • Fast. It is written in Go. Can run commands in parallel.
  • Powerful. It allows to control execution and files you pass to your commands.
  • Simple. It is single dependency-free binary which can work in any environment.

📖 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

  • Configuration for lefthook.yml config options.
  • Usage for lefthook CLI options, supported ENVs, and usage tips.
  • Discussions for questions, ideas, suggestions.

Why Lefthook

  • Parallel execution

Gives you more speed. Example

pre-push:
  parallel: true
  • Flexible list of files

If you want your own list. Custom and prebuilt examples.

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}
  • Glob and regexp filters

If you want to filter list of files. You could find more glob pattern examples here.

pre-commit:
  commands:
    backend-linter:
      glob: "*.rb" # glob filter
      exclude: '(^|/)(application|routes)\.rb$' # regexp filter
      run: bundle exec rubocop --force-exclusion {all_files}
  • Execute in sub-directory

If you want to execute the commands in a relative path

pre-commit:
  commands:
    backend-linter:
      root: "api/" # Careful to have only trailing slash
      glob: "*.rb" # glob filter
      run: bundle exec rubocop {all_files}
  • Run scripts

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

commit-msg:
  scripts:
    "template_checker":
      runner: bash
  • Tags

If you want to control a group of commands. Example.

pre-push:
  commands:
    packages-audit:
      tags: frontend security
      run: yarn audit
    gems-audit:
      tags: backend security
      run: bundle audit
  • Support Docker

If you are in the Docker environment. Example.

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

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
  • Direct control

If you want to run hooks group directly.

$ lefthook run pre-commit
  • Your own tasks

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
  • Optional output

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

NPM DownloadsLast 30 Days