### Example Setup for Pre-commit Hook Source: https://lefthook.dev/configuration/setup This snippet shows how to configure a `pre-commit` hook with a `setup` instruction. It checks if `golangci-lint` is installed and installs it if not, before running the linter on staged Go files. ```yaml # lefthook.yml pre-commit: setup: - run: | if ! command -v golangci-lint >/dev/null 2>&1; go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 fi jobs: - run: golangci-lint -- {staged_files} glob: "*.go" ``` -------------------------------- ### Set up RPM Repository and Install lefthook Source: https://lefthook.dev/installation/rpm Use this command to download and execute the Cloudsmith setup script for lefthook's RPM repository, followed by installing lefthook using yum. ```bash curl -1sLf 'https://dl.cloudsmith.io/public/evilmartians/lefthook/setup.rpm.sh' | sudo -E bash sudo yum install lefthook ``` -------------------------------- ### Run `yarn lint` on `pre-commit` hook Source: https://lefthook.dev/configuration/run This is a basic example of running a command on a hook. Ensure `yarn lint` is installed and accessible in your environment. ```yaml # lefthook.yml pre-commit: commands: lint: run: yarn lint ``` -------------------------------- ### Example Main Configuration Source: https://lefthook.dev/usage/features/local This is an example of a main `lefthook.yml` configuration file that is committed to your repository. ```yaml # lefthook.yml (committed into your repo) pre-commit: jobs: - name: linter run: yarn lint - name: tests run: yarn test ``` -------------------------------- ### Install Right Executable with @evilmartians/lefthook-installer Source: https://lefthook.dev/installation/node Install the legacy @evilmartians/lefthook-installer package to fetch the correct executable during installation. ```bash npm install --save-dev @evilmartians/lefthook-installer ``` -------------------------------- ### Install Commitlint and Commitzen Dependencies Source: https://lefthook.dev/examples/commitlint Install the necessary packages for Commitlint and Commitzen as development dependencies. ```bash yarn add -D @commitlint/cli @commitlint/config-conventional # For commitzen yarn add -D commitizen cz-conventional-changelog ``` -------------------------------- ### Install Lefthook as a Go Tool Source: https://lefthook.dev/installation/go Integrate Lefthook into your project by installing it as a go tool. This is suitable for project-specific usage. ```bash go get -tool github.com/evilmartians/lefthook/v2 ``` -------------------------------- ### Install Lefthook from AUR (Compile from Sources) Source: https://lefthook.dev/installation/arch Use this command to install Lefthook by compiling it from source using the `yay` AUR helper. ```bash yay -S lefthook ``` -------------------------------- ### Install All OS Executables with @evilmartians/lefthook Source: https://lefthook.dev/installation/node Install the legacy @evilmartians/lefthook package to get executables for all operating systems. ```bash npm install --save-dev @evilmartians/lefthook ``` -------------------------------- ### Install Lefthook with pipx Source: https://lefthook.dev/installation/python Install Lefthook globally using pipx for command-line access. ```bash pipx install lefthook ``` -------------------------------- ### Main Configuration Example Source: https://lefthook.dev/examples/wrap-commands Defines a 'rubocop' command to be executed using 'bundle exec'. This is the standard command definition. ```yaml # lefthook.yml pre-commit: jobs: - name: rubocop run: bundle exec rubocop -A -- {staged_files} ``` -------------------------------- ### Install with Mint Source: https://lefthook.dev/installation/swift This command installs the lefthook Swift wrapper plugin using mint, a package manager for Swift command-line tools. ```bash mint run csjones/lefthook-plugin ``` -------------------------------- ### Install Lefthook with Scoop Source: https://lefthook.dev/installation/scoop Use this command to install Lefthook via Scoop. Ensure Scoop is installed and configured on your Windows system. ```bash scoop install lefthook ``` -------------------------------- ### Install lefthook with Homebrew Source: https://lefthook.dev/installation/homebrew Use this command to install lefthook via Homebrew. Ensure Homebrew is already installed on your system. ```bash brew install lefthook ``` -------------------------------- ### Install Lefthook with uv Source: https://lefthook.dev/installation/python Install Lefthook as a development dependency using the uv package manager. ```bash uv add --dev lefthook ``` -------------------------------- ### lefthook.yml Configuration Example Source: https://lefthook.dev/usage/commands/run This is an example configuration file for lefthook, defining pre-commit and test hooks with their respective jobs and run commands. ```yaml # lefthook.yml pre-commit: jobs: - name: lint run: yarn lint --fix {staged_files} test: jobs: - name: test run: yarn test ``` -------------------------------- ### Install Lefthook using Winget Source: https://lefthook.dev/installation/winget Use this command to install Lefthook on Windows via the Winget package manager. ```bash winget install evilmartians.lefthook ``` -------------------------------- ### Install Lefthook from AUR (Pre-compiled Binaries) Source: https://lefthook.dev/installation/arch Use this command to install only the pre-compiled Lefthook executable from the AUR using the `yay` AUR helper. ```bash yay -S lefthook-bin ``` -------------------------------- ### Install Lefthook Globally with Go Source: https://lefthook.dev/installation/go Use this command to install Lefthook as a global package. Ensure your Go version is 1.26 or higher. ```bash go install github.com/evilmartians/lefthook/v2@v2.1.9 ``` -------------------------------- ### Test Commit Generation and Validation Source: https://lefthook.dev/examples/commitlint Initiate a Git commit to test the integrated Commitzen and Commitlint setup. You can either let Commitzen guide you or provide a message directly if only using Commitlint. ```bash # You can type it without message, if you are using commitzen git commit # Or provide a commit message is using only commitlint git commit -am 'fix: typo' ``` -------------------------------- ### Add Lefthook Repository and Install Source: https://lefthook.dev/installation/deb Installs Lefthook on Debian-based systems by adding the official repository and then installing the package. ```bash curl -1sLf 'https://dl.cloudsmith.io/public/evilmartians/lefthook/setup.deb.sh' | sudo -E bash sudo apt install lefthook ``` -------------------------------- ### Configuring Command and Script Priorities Source: https://lefthook.dev/configuration/priority This example demonstrates how to set priorities for both commands and scripts within a `post-checkout` hook. Commands and scripts with higher priority values will execute first. ```yaml # lefthook.yml post-checkout: piped: true commands: db-create: priority: 1 run: rails db:create db-migrate: priority: 2 run: rails db:migrate db-seed: priority: 3 run: rails db:seed scripts: "check-spelling.sh": runner: bash priority: 1 "check-grammar.rb": runner: ruby priority: 2 ``` -------------------------------- ### Install Lefthook on Alpine Source: https://lefthook.dev/installation/alpine Installs bash and curl, adds the Lefthook Cloudsmith repository, and then installs the lefthook package. ```bash sudo apk add --no-cache bash curl curl -1sLf 'https://dl.cloudsmith.io/public/evilmartians/lefthook/setup.alpine.sh' | sudo -E bash sudo apk add lefthook ``` -------------------------------- ### Install Lefthook with NPM Source: https://lefthook.dev/installation/node Use this command to install Lefthook as a development dependency with npm. ```bash npm install --save-dev lefthook ``` -------------------------------- ### Install Lefthook with Yarn Source: https://lefthook.dev/installation/node Use this command to install Lefthook as a development dependency with yarn. ```bash yarn add --dev lefthook ``` -------------------------------- ### Install Lefthook via Snap Source: https://lefthook.dev/installation/snap Use this command to install the Lefthook snap package in classic mode on your Linux system. This ensures Lefthook has the necessary permissions to operate correctly. ```bash snap install --classic lefthook ``` -------------------------------- ### Configure Pre-commit Commands Source: https://lefthook.dev/configuration/Commands Example of how to define commands for the pre-commit hook in a lefthook.yml configuration file. ```yaml # lefthook.yml pre-commit: commands: lint: ... # command options ``` -------------------------------- ### Install lefthook Hook Source: https://lefthook.dev/usage/commands/run Command to install the lefthook hook into your Git repository. ```bash $ lefthook install ``` -------------------------------- ### Display Full lefthook Version Source: https://lefthook.dev/usage/commands/version Prints the current binary version along with its commit hash. Use this command to verify the exact build installed. ```bash $ lefthook version --full 1.1.3 bb099d13c24114d2859815d9d23671a32932ffe2 ``` -------------------------------- ### Install Lefthook with pip Source: https://lefthook.dev/installation/python Use this command to install Lefthook for your Python project using pip. ```bash python -m pip install --user lefthook ``` -------------------------------- ### Install Lefthook git hooks Source: https://lefthook.dev/configuration/rc After configuring the rc file, ensure the git hooks are updated to reflect the changes. ```sh # Make sure you updated git hooks. This is important. $ lefthook install -f ``` -------------------------------- ### Local Configuration Example with Dip Source: https://lefthook.dev/examples/wrap-commands Overrides the 'rubocop' command in the local configuration to use 'dip'. This allows running the command within a Dockerized environment managed by 'dip'. ```yaml # lefthook-local.yml pre-commit: jobs: - name: rubocop run: dip {cmd} ``` -------------------------------- ### Prevent Lefthook Installation in CI Source: https://lefthook.dev/usage/envs/CI Set `CI=true` before running your package manager install command to skip lefthook's postinstall script. This is useful for CI environments where hook installation is not desired. ```bash CI=true npm install ``` ```bash CI=true yarn install ``` ```bash CI=true pnpm install ``` -------------------------------- ### Lefthook configuration with npm script Source: https://lefthook.dev/configuration/rc Example of a Lefthook configuration that runs an npm script for linting staged files. ```yaml # lefthook.yml pre-commit: commands: lint: run: npm run eslint {staged_files} ``` -------------------------------- ### Configure pre-push hook commands Source: https://lefthook.dev/usage/commands/add Define the commands to be executed for the `pre-push` hook in your `lefthook.yml` configuration file. This example specifies running an `audit.sh` script using bash. ```yaml pre-push: jobs: - script: "audit.sh" runner: bash ``` -------------------------------- ### Install Lefthook with PNPM Source: https://lefthook.dev/installation/node Use this command to install Lefthook as a development dependency with pnpm. Ensure your pnpm configuration is updated as per the notes. ```bash pnpm add -D lefthook ``` -------------------------------- ### Enable LEFTHOOK during NPM install Source: https://lefthook.dev/usage/envs/LEFTHOOK When using the NPM package in CI, set LEFTHOOK=1 or LEFTHOOK=true to install hooks in the postinstall script. ```bash LEFTHOOK=1 npm install ``` ```bash LEFTHOOK=1 yarn install ``` ```bash LEFTHOOK=1 pnpm install ``` -------------------------------- ### Example Lefthook Configuration Source: https://lefthook.dev/ This configuration sets up the `pre-commit` hook to run linters in parallel. It specifies jobs for Stylelint on CSS files and ESLint on TypeScript and JavaScript files, fixing issues and staging changes. ```yaml # lefthook.yml pre-commit: parallel: true jobs: - run: yarn run stylelint --fix '{staged_files}' glob: "*.css" stage_fixed: true - run: yarn run eslint --fix '{staged_files}' glob: - "*.ts" - "*.js" - "*.tsx" - "*.jsx" stage_fixed: true ``` -------------------------------- ### Basic Lefthook CLI Commands Source: https://lefthook.dev/usage These commands cover the fundamental operations of Lefthook, including installation, running hooks, validation, and configuration dumping. Ensure you have a `lefthook.yml` file for commands like `run`. ```bash lefthook install ``` ```bash lefthook run pre-commit ``` ```bash lefthook validate ``` ```bash lefthook dump ``` -------------------------------- ### Install Lefthook Globally Source: https://lefthook.dev/installation/ruby Install Lefthook as a global Ruby gem for use across all your projects. This is a convenient option if you want Lefthook available everywhere. ```bash gem install lefthook ``` -------------------------------- ### Example Job Name Configuration Source: https://lefthook.dev/configuration/name This snippet shows how to define a job named 'lint and fix' in a Lefthook configuration file. ```yaml # lefthook.yml pre-commit: jobs: - name: lint and fix run: yarn run eslint --fix {staged_files} ``` -------------------------------- ### Defining Tags for Commands Source: https://lefthook.dev/configuration/exclude_tags Commands can have multiple tags. This example shows 'packages-audit' with 'frontend' and 'security' tags, and 'gems-audit' with 'backend' and 'security' tags. ```yaml # lefthook.yml pre-push: commands: packages-audit: tags: - frontend - security run: yarn audit gems-audit: tags: - backend - security run: bundle audit ``` -------------------------------- ### Example Local Override Configuration Source: https://lefthook.dev/usage/features/local This `lefthook-local.yml` file demonstrates how to override or extend the main configuration, such as skipping jobs or linting only staged files. It should be ignored by git. ```yaml # lefthook-local.yml (ignored by git) pre-commit: jobs: - name: tests skip: true # don't want to run tests on every commit - name: linter run: yarn lint {staged_files} # lint only staged files ``` -------------------------------- ### Default `source_dir` Structure Source: https://lefthook.dev/configuration/source_dir This example shows the default directory structure for custom script files when using the `source_dir` option. Scripts are organized by hook type. ```bash .lefthook/ ├── pre-commit/ │ ├── lint.sh │ └── test.py └── pre-push/ └── check-files.rb ``` -------------------------------- ### Run Specific Hook or Job Source: https://lefthook.dev/usage/commands/run Examples of running specific hooks or jobs using the `lefthook run` command. This includes running all jobs for a hook, a specific hook, or a specific job within a hook. ```bash $ lefthook run test # will run 'yarn test' ``` ```bash $ git commit # will run pre-commit hook ('yarn lint --fix') ``` ```bash $ lefthook run pre-commit # will run pre-commit hook (`yarn lint --fix`) ``` ```bash $ lefthook run pre-commit --job lints --job pretty --tag checks ``` -------------------------------- ### Configure Glob Matcher in `lefthook.yml` Source: https://lefthook.dev/configuration/glob_matcher Set the `glob_matcher` to `doublestar` in your `lefthook.yml` configuration file. This example also shows a `pre-commit` job with a `glob` pattern. ```yaml # lefthook.yml glob_matcher: doublestar pre-commit: jobs: - name: lint run: yarn eslint {staged_files} glob: "**/*.{js,ts}" ``` -------------------------------- ### Example `linter.sh` script Source: https://lefthook.dev/configuration/script A simple bash script that prints a success message. This script is intended to be executed by lefthook. ```bash # .lefthook/pre-commit/linter.sh echo "Everything is OK" ``` -------------------------------- ### Using `args` with `script` and `run` in Lefthook Source: https://lefthook.dev/configuration/args This example demonstrates how to use the `args` option with both `script` and `run` commands in a Lefthook configuration file. It shows passing staged files as arguments to a shell script and a Node.js script. ```yaml # lefthook.yml pre-commit: jobs: - script: check-python-files.sh runner: bash args: "{staged_files}" glob: "*.py" - run: yarn lint args: "{staged_files}" glob: - "*.ts" - "*.js" ``` -------------------------------- ### Setting `source_dir_local` in `lefthook-local.yml` Source: https://lefthook.dev/configuration/source_dir_local This example shows how to set the `source_dir_local` option within a `lefthook-local.yml` configuration file to specify `.lefthook-local/` as the directory for local scripts. ```yaml # lefthook-local.yml source_dir_local: .lefthook-local/ ``` -------------------------------- ### Quoting staged files with double or single quotes Source: https://lefthook.dev/configuration/run Shows how to quote file paths when using `{staged_files}`. Double quotes are recommended for Windows compatibility, while single quotes can also be used. The `format` example demonstrates quoting where needed. ```yaml # lefthook.yml pre-commit: commands: lint: glob: "*.js" # Quoting with double quotes `"` might be helpful for Windows users run: yarn eslint "{staged_files}" # will run `yarn eslint "file1.js" "file2.js" "[strange name].js" test: glob: "*.{spec.js}" run: yarn test '{staged_files}' # will run `yarn eslint 'file1.spec.js' 'file2.spec.js' '[strange name].spec.js'` format: glob: "*.js" # Will quote where needed with single quotes run: yarn test {staged_files} # will run `yarn eslint file1.js file2.js '[strange name].spec.js'` ``` -------------------------------- ### Example of `fail_text` output Source: https://lefthook.dev/configuration/fail_text This output demonstrates how the custom `fail_text` appears in the console when a commit hook fails. It shows the hook execution summary and the specified failure message. ```bash $ git commit -m 'fix: Some bug' Lefthook v1.1.3 RUNNING HOOK: pre-commit EXECUTE > lint SUMMARY: (done in 0.01 seconds) 🥊 lint: Add node executable to $PATH env ``` -------------------------------- ### Edit hook script Source: https://lefthook.dev/usage/commands/add After adding a hook and configuring its script, you can edit the script file directly. This example shows how to open the `audit.sh` script using `vim`. ```bash $ vim .lefthook/pre-push/audit.sh ... ``` -------------------------------- ### Specify Runner for Scripts in Lefthook Source: https://lefthook.dev/configuration/runner Configure the `runner` for different script files within your Lefthook configuration. This example shows how to set `node` as the runner for `lint.js` and `go run` for `check.go`. ```yaml # lefthook.yml pre-commit: scripts: "lint.js": runner: node "check.go": runner: go run ``` -------------------------------- ### Configure Filters in lefthook.yml Source: https://lefthook.dev/examples/filters Use glob, root, exclude, and file_types to filter staged files for a command. This example filters for JavaScript and TypeScript files in the 'frontend' directory, excluding config files and non-executable files. ```yaml # lefthook.yml pre-commit: commands: lint: run: yarn lint {staged_files} --fix glob: "*.{js,ts}" root: frontend exclude: - "*.config.js" - "*.config.ts" file_types: - not executable ``` -------------------------------- ### Reduce Redundancy with Template Wrapper Source: https://lefthook.dev/configuration/templates Define a 'wrapper' template in `lefthook.yml` to encapsulate common command prefixes, reducing repetition in job definitions. This is useful for commands that require a consistent setup, like Docker. ```yaml # lefthook.yml templates: wrapper: docker-compose run --rm -v $(pwd):/app service pre-commit: jobs: - run: {wrapper} yarn format - run: {wrapper} yarn lint - run: {wrapper} yarn test ``` -------------------------------- ### Run `go vet` on `.go` files using `{files}` Source: https://lefthook.dev/configuration/run Executes `go vet` on files identified by `git ls-files -m` that have a `.go` extension. The `{files}` template substitutes the list of files found. ```yaml # lefthook.yml pre-commit: commands: govet: files: git ls-files -m glob: "*.go" run: go vet -- {files} ``` -------------------------------- ### Disable Automatic Hook Installation with `no_auto_install` Source: https://lefthook.dev/configuration/no_auto_install Set `no_auto_install` to `true` in your `lefthook.yml` to prevent Lefthook from automatically installing or updating hooks when the configuration is modified. This is useful if you want manual control over hook installations. ```yaml # lefthook.yml no_auto_install: true pre-commit: commands: lint: run: npm run lint ``` -------------------------------- ### Configure Commitlint Source: https://lefthook.dev/examples/commitlint Set up the Commitlint configuration file to use conventional commit standards. ```javascript // commitlint.config.js module.exports = {extends: ['@commitlint/config-conventional']}; ``` -------------------------------- ### Merge Multiple Configurations from Multiple Remotes Source: https://lefthook.dev/configuration/configs Demonstrates how to merge configurations from several remote repositories. Each remote can specify its own reference (like a branch or tag) and a list of configuration files to include. This allows for a flexible and modular approach to hook management. ```yaml # lefthook.yml remotes: - git_url: git@github.com:org/lefthook-configs ref: v1.0.0 configs: - examples/ruby-linter.yml - examples/test.yml - git_url: https://github.com/org2/lefthook-configs configs: - lefthooks/pre_commit.yml - lefthooks/post_merge.yml - git_url: https://github.com/org3/lefthook-configs ref: feature/new configs: - configs/pre-push.yml ``` -------------------------------- ### Display Folder Structure Source: https://lefthook.dev/configuration/root Shows the directory structure of a project, including a `client/` folder with `package.json` and `node_modules/`. ```bash # Folders structure $ tree . . ├── client/ │ ├── package.json │ ├── node_modules/ | ├── ... ├── server/ | ... ``` -------------------------------- ### Specify Files for Hook Execution Source: https://lefthook.dev/usage/commands/run Demonstrates how to specify files to be processed by a hook, either all files or a specific list. The `--all-files` option acts as a template replacement for `{all_files}`. ```bash $ lefthook run pre-commit --all-files ``` ```bash $ lefthook run pre-commit --file file1.js --file file2.js ``` -------------------------------- ### Using `{cmd}` for commands and scripts Source: https://lefthook.dev/configuration/run Demonstrates the use of the `{cmd}` template, which is a shorthand for the command defined in `lefthook.yml`. This is useful for abstracting commands, especially when wrapping them in Docker. ```yaml # lefthook.yml pre-commit: commands: lint: run: yarn lint scripts: "good_job.js": runner: node ``` ```yaml # lefthook-local.yml pre-commit: commands: lint: run: docker run -it --rm {cmd} scripts: "good_job.js": runner: docker run -it --rm {cmd} ``` -------------------------------- ### Add pre-push hook with directory creation Source: https://lefthook.dev/usage/commands/add Use this command to add a `pre-push` hook and ensure its corresponding directory is created in `.git/hooks/`. This is useful for organizing hook scripts. ```bash $ lefthook add pre-push --dirs ``` -------------------------------- ### Set up environment variables in rc file Source: https://lefthook.dev/configuration/rc Configure the rc file to set up environment variables for nvm or fnm, or to modify the PATH. ```sh # ~/.lefthookrc # An nvm way export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # An fnm way export FNM_DIR="$HOME/.fnm" [ -s "$FNM_DIR/fnm.sh" ] && \. "$FNM_DIR/fnm.sh" # Or maybe just PATH=$PATH:$HOME/.nvm/versions/node/v15.14.0/bin ``` -------------------------------- ### Exclude Tags in `lefthook.yml` Source: https://lefthook.dev/configuration/exclude_tags Define `exclude_tags` in your `lefthook.yml` to prevent commands with the specified tag from running. This example shows how commands tagged 'frontend' will be excluded. ```yaml # lefthook.yml pre-commit: exclude_tags: frontend commands: lint: tags: frontend ... test: tags: frontend ... check-syntax: tags: documentation ``` -------------------------------- ### Glob Matching Behavior Comparison Source: https://lefthook.dev/configuration/glob_matcher Understand the difference in how `gobwas` (default) and `doublestar` match file paths. `doublestar` is more permissive with `**`. ```text # gobwas (default): **/*.js matches src/app.js but NOT app.js # doublestar: **/*.js matches app.js, src/app.js, a/b/c/app.js ``` -------------------------------- ### Enable `assert_lefthook_installed` Source: https://lefthook.dev/configuration/assert_lefthook_installed Set this option to `true` in your `lefthook.yml` configuration to enforce the presence of the `lefthook` executable. This will cause the build to fail if `lefthook` cannot be found. ```yaml # lefthook.yml assert_lefthook_installed: true ``` -------------------------------- ### Enable `follow` in `lefthook.yml` Source: https://lefthook.dev/configuration/follow Set `follow: true` in your `lefthook.yml` to enable streaming output for commands. Avoid using `follow` with `parallel` to prevent output confusion. ```yaml # lefthook.yml pre-push: follow: true commands: backend-tests: run: bundle exec rspec frontend-tests: run: yarn test ``` -------------------------------- ### Excluding Multiple Tags in `lefthook-local.yml` Source: https://lefthook.dev/configuration/exclude_tags Use `exclude_tags` in `lefthook-local.yml` to skip commands locally without altering the shared configuration. This example excludes commands tagged 'frontend'. ```yaml # lefthook-local.yml pre-push: exclude_tags: - frontend ``` -------------------------------- ### `**` Pattern Behavior Source: https://lefthook.dev/configuration/glob Demonstrates the behavior of the `**` pattern, which matches one or more directories deep by default. Use `glob_matcher: doublestar` to enable standard behavior. ```text glob: "src/**/*.js" # does NOT match src/file.js glob: "src/*.js" # matches src/file.js only ``` -------------------------------- ### Enable Diff Output for `fail_on_changes` Source: https://lefthook.dev/configuration/fail_on_changes_diff Set `fail_on_changes_diff` to `true` to always show the diff when `fail_on_changes` triggers, regardless of the environment. This example demonstrates its usage within a `lefthook.yml` configuration file. ```yaml # lefthook.yml pre-commit: parallel: true fail_on_changes: "always" fail_on_changes_diff: true commands: lint: run: yarn lint test: run: yarn test ``` -------------------------------- ### Skip command if only excluded files are staged Source: https://lefthook.dev/configuration/exclude If `exclude` is set and no files remain after filtering `{staged_files}` (for pre-commit) or `{push_files}` (for pre-push), the command will be skipped. This example shows how Rubocop would be skipped if only `application.rb` was staged. ```yaml # lefthook.yml pre-commit: exclude: - "*/application.rb" jobs: - name: lint run: bundle exec rubocop # will skip if only application.rb was staged ``` -------------------------------- ### Configure pre-push hook to use stdin Source: https://lefthook.dev/configuration/use_stdin This configuration enables the `use_stdin: true` option for a `pre-push` hook script, ensuring that the script receives standard input from the operating system. ```yaml # lefthook.yml pre-push: scripts: "do-the-magic.sh": runner: bash use_stdin: true ``` -------------------------------- ### Exclude Commands/Scripts by Tag or Name with LEFTHOOK_EXCLUDE Source: https://lefthook.dev/usage/envs/LEFTHOOK_EXCLUDE Use LEFTHOOK_EXCLUDE to specify a comma-separated list of tags or command names to skip. This example skips commands tagged 'ruby' and 'security', as well as any command named 'lint'. ```bash LEFTHOOK_EXCLUDE=ruby,security,lint git commit -am "Skip some tag checks" ``` -------------------------------- ### Lefthook Configuration for prepare-commit-msg Source: https://lefthook.dev/usage/features/git-args This `lefthook.yml` configuration sets up the `prepare-commit-msg` hook to run a script and also execute an inline echo command, demonstrating how Git arguments can be accessed. ```yaml # lefthook.yml prepare-commit-msg: jobs: - script: "message.sh" runner: bash - run: echo "Git args: {1} {2} {3}" ``` -------------------------------- ### Local Skip Configuration Source: https://lefthook.dev/configuration/skip Demonstrates how to use a local configuration file (`lefthook-local.yml`) to override and skip commands defined in the main configuration (`lefthook.yml`). This is useful for disabling commands only in a local development environment. ```yaml # lefthook.yml pre-commit: commands: lint: run: yarn lint ``` ```yaml # lefthook-local.yml pre-commit: commands: lint: skip: true ``` -------------------------------- ### Extending PATH in Environment Variables Source: https://lefthook.dev/configuration/env Extend the system's PATH environment variable for Lefthook commands, particularly useful when running hooks from GUI programs or across different operating systems. This example shows how to add a custom path to `$PATH` using `lefthook-local.yml`. ```yaml # lefthook-local.yml pre-commit: commands: test: env: PATH: $PATH:/home/me/path/to/yarn ``` -------------------------------- ### Run `bundle exec rubocop` on all `.rb` files using `{all_files}` Source: https://lefthook.dev/configuration/run Executes `bundle exec rubocop` on all tracked Ruby files, excluding specified configuration files. The `--force-exclusion` flag ensures RuboCop's `Exclude` settings are respected. ```yaml # lefthook.yml pre-commit: commands: rubocop: tags: - backend - style glob: "*.rb" exclude: - config/application.rb - config/routes.rb run: bundle exec rubocop --force-exclusion -- {all_files} ``` -------------------------------- ### Call Custom Script for Files Source: https://lefthook.dev/configuration/files Execute a custom script to generate the file list. This provides flexibility for complex file selection logic. ```yaml # lefthook.yml pre-push: commands: rubocop: tags: backend glob: "**/*.rb" files: node ./lefthook-scripts/ls-files.js # you can call your own scripts run: bundle exec rubocop --force-exclusion --parallel -- {files} ``` -------------------------------- ### Conditional Execution During Rebasing Source: https://lefthook.dev/configuration/only This configuration demonstrates how to conditionally execute commands based on the operation type. It runs a quick linter during rebasing while skipping the standard linter and tests, and then runs a specific quick linter only on rebase operations. ```yaml # lefthook.yml pre-commit: commands: lint: skip: rebase run: yarn lint test: skip: rebase run: yarn test lint-on-rebase: only: rebase run: yarn lint-quickly ``` -------------------------------- ### Use Git Command for Files Source: https://lefthook.dev/configuration/files Specify a git command to list files. This is useful for tracking changes relative to a specific branch. ```yaml # lefthook.yml pre-push: commands: stylelint: tags: - frontend - style files: git diff --name-only master glob: "*.js" run: yarn stylelint {files} ``` -------------------------------- ### Base lefthook.yml Configuration Source: https://lefthook.dev/examples/lefthook-local Defines the default pre-commit hook with linting and link-checking commands. ```yaml # lefthook.yml pre-commit: commands: lint: run: bundle exec rubocop -- {staged_files} glob: "*.rb" check-links: run: lychee -- {staged_files} ``` -------------------------------- ### Specify a command to run lefthook Source: https://lefthook.dev/configuration/lefthook Use this to execute lefthook via a command, such as changing directories and then running lefthook with a package manager. This is useful for monorepos or complex project structures. ```yaml lefthook: | cd project-with-lefthook pnpm lefthook pre-commit: jobs: - run: yarn lint root: project-with-lefthook ``` -------------------------------- ### Configure `script` in `lefthook.yml` Source: https://lefthook.dev/configuration/script Define a pre-commit hook that executes a shell script named `linter.sh` using the bash runner. ```yaml # lefthook.yml pre-commit: jobs: - script: linter.sh runner: bash ``` -------------------------------- ### Use Latest Lefthook with Mise Source: https://lefthook.dev/installation/mise Use this command to set the latest available version of lefthook for your project via Mise. ```bash mise use lefthook@latest ``` -------------------------------- ### Specify lefthook executable path Source: https://lefthook.dev/configuration/lefthook Use this when you need to point to a specific lefthook executable. Ensure the path is correct for your system. ```yaml lefthook: /usr/bin/lefthook pre-commit: jobs: - run: yarn lint ``` -------------------------------- ### Configure Pre-commit Hook with Root Option Source: https://lefthook.dev/configuration/root Configures a `pre-commit` hook to lint and fix JavaScript/TypeScript files within the `client/` directory using `yarn eslint`. The `root` option specifies the working directory for the command, and `glob` filters the files. ```yaml # lefthook.yml pre-commit: commands: lint: root: "client/" glob: "*.{js,ts}" run: yarn eslint --fix {staged_files} && git add {staged_files} ``` -------------------------------- ### lefthook-local.yml Overrides and Extensions Source: https://lefthook.dev/examples/lefthook-local Overrides the 'lint' command to use Docker, skips 'check-links', and adds a 'post-merge' hook. ```yaml # lefthook-local.yml pre-commit: parallel: true # run all commands concurrently commands: lint: run: docker-compose run backend {cmd} # wrap the original command with docker-compose check-links: skip: true # skip checking links # Add another hook post-merge: files: "git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD" commands: dependencies: glob: "Gemfile*" run: docker-compose run backend bundle install ``` -------------------------------- ### Configure Git URL with SSH Source: https://lefthook.dev/configuration/git_url Use this format for SSH-based Git URLs. Ensure your SSH keys are properly configured on the machine running lefthook. ```yaml # lefthook.yml remotes: - git_url: git@github.com:evilmartians/lefthook ``` -------------------------------- ### Extend Output Configuration with Environment Variable Source: https://lefthook.dev/configuration/output Extend the output configuration using the `LEFTHOOK_OUTPUT` environment variable. This is useful for temporary adjustments or CI/CD environments where you might want to override the file configuration. ```bash LEFTHOOK_OUTPUT="meta,success,summary" lefthook run pre-commit ``` -------------------------------- ### Specify custom files for pre-commit hook Source: https://lefthook.dev/configuration/files-global Use the `files` key within a hook configuration to define a shell command that outputs the files to be processed. If this command yields an empty result, the hook's commands will not execute. ```yaml # lefthook.yml pre-commit: files: git diff --name-only master # custom list of files commands: ... ``` -------------------------------- ### Extending Lefthook Configuration with Multiple Files Source: https://lefthook.dev/configuration/extends Specify multiple YAML files to extend your main Lefthook configuration. Supports glob patterns for dynamic file inclusion. ```yaml # lefthook.yml extends: - /home/user/work/lefthook-extend.yml - /home/user/work/lefthook-extend-2.yml - lefthook-extends/file.yml - ../extend.yml - projects/*/specific-lefthook-config.yml ``` -------------------------------- ### Configure Remote Repository with Specific Configs Source: https://lefthook.dev/configuration/configs Define a remote repository and specify which configuration files to load from it. This is useful for managing shared hook configurations. ```yaml # lefthook.yml remotes: - git_url: git@github.com:evilmartians/lefthook ref: v1.0.0 configs: - examples/ruby-linter.yml - examples/test.yml ``` -------------------------------- ### Conditionally Skip Command Based on Tool Existence Source: https://lefthook.dev/configuration/skip Skip a command if a specific tool does not exist in the system's PATH. The `!` prefix negates the `run` condition, executing the skip if the command fails (e.g., `which aiautocommit` returns non-zero). ```yaml prepare-commit-msg: skip: - merge - rebase commands: aiautocommit: interactive: true run: aiautocommit commit --output-file "{1}" env: LOG_LEVEL: info skip: # only run this if the tool exists - run: "! which aiautocommit" ``` -------------------------------- ### Configure Bash Script for pre-commit Hook Source: https://lefthook.dev/configuration/Scripts Add this configuration to your `lefthook.yml` to run a bash script named `my-script.sh` during the `pre-commit` hook. Ensure the script is placed in the correct directory. ```yaml # lefthook.yml pre-commit: scripts: "my-script.sh": runner: bash ``` -------------------------------- ### Configure rc file path in lefthook-local.yml Source: https://lefthook.dev/configuration/rc Specify the path to the custom rc file within the lefthook-local.yml configuration. ```yaml # lefthook-local.yml # You can choose whatever name you want. # You can share it between projects where you use lefthook. # Make sure the path is absolute. rc: ~/.lefthookrc ``` ```yaml # lefthook-local.yml # If the path contains spaces, you need to quote it. rc: "'${XDG_CONFIG_HOME:-$HOME/.config}/lefthookrc'" ``` -------------------------------- ### Configure Multiple Remote Lefthook Configurations Source: https://lefthook.dev/configuration/remotes Provide multiple remote configurations to share lefthook settings across projects. Lefthook merges these into the local `lefthook.yml`. ```yaml # lefthook.yml remotes: - git_url: git@github.com:evilmartians/lefthook ref: v1.0.0 configs: - examples/ruby-linter.yml ``` -------------------------------- ### Merged Configuration Used by Lefthook Source: https://lefthook.dev/examples/lefthook-local Illustrates the final configuration after merging `lefthook.yml` and `lefthook-local.yml`. ```yaml pre-commit: parallel: true commands: lint: run: docker-compose run backend bundle exec rubocop -- {staged_files} glob: "*.rb" check-links: run: lychee -- {staged_files} skip: true post-merge: files: "git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD" commands: dependencies: glob: "Gemfile*" run: docker-compose run backend bundle install ``` -------------------------------- ### Configure Git Hooks with lefthook Source: https://lefthook.dev/configuration/Hook Define pre-commit hook with a yarn lint command and a custom 'check-docs' hook with multiple commands. Use {staged_files} to pass modified files to commands. ```yaml # lefthook.yml # Git hook pre-commit: jobs: - run: yarn lint {staged_files} --fix stage_fixed: true # Custom hook check-docs: jobs: - run: yarn check-docs - run: typos ``` -------------------------------- ### Running a script with a loop in `pre-commit` hook Source: https://lefthook.dev/configuration/run Defines a job within the `pre-commit` hook that executes a shell script. This script iterates through files in the current directory and runs `yarn lint` on each. ```yaml # lefthook.yml pre-commit: jobs: - name: a whole script in a run run: | for file in $(ls .); do yarn lint $file done ``` -------------------------------- ### Force using a version from Rubygems Source: https://lefthook.dev/configuration/lefthook Configure lefthook to be executed using `bundle exec` to ensure a specific version from Rubygems is used. This is common in Ruby projects. ```yaml lefthook: bundle exec lefthook pre-commit: jobs: - run: bundle exec rubocop -- {staged_files} ``` -------------------------------- ### Configure Remote Repository for Lefthook Source: https://lefthook.dev/examples/remotes Specify a remote Git repository URL and the configuration files to include from it. Lefthook will download and merge these configurations. ```yaml remotes: - git_url: https://github.com/evilmartians/lefthook configs: - examples/remote/ping.yml ```