From 84dbba8903519b2d48a3a99c7d4c9fe415dd8208 Mon Sep 17 00:00:00 2001 From: Lukas Eipert <leipert@gitlab.com> Date: Fri, 10 Mar 2023 12:54:46 +0100 Subject: [PATCH] Add lefthook target to autofix files A custom lefthook target to run all the linters with with auto-fix capabilities, but just on the files which changed in your branch. ```shell # If installed globally lefthook run auto-fix # Or if installed via ruby gem bundle exec lefthook run auto-fix ``` --- doc/development/contributing/style_guides.md | 22 ++++++++++++++++- lefthook.yml | 25 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/doc/development/contributing/style_guides.md b/doc/development/contributing/style_guides.md index 36b38e2e16b4d..d24875e559a7e 100644 --- a/doc/development/contributing/style_guides.md +++ b/doc/development/contributing/style_guides.md @@ -30,7 +30,9 @@ We were using Overcommit prior to Lefthook, so you may want to uninstall it firs ### Install Lefthook -1. Install the `lefthook` Ruby gem: +1. You can install lefthook in [different ways](https://github.com/evilmartians/lefthook/blob/master/docs/install.md#install-lefthook). + If you do not choose to install it globally (e.g. via Homebrew or package managers), and only want to use it for the GitLab project, + you can install the Ruby gem via: ```shell bundle install @@ -39,12 +41,18 @@ We were using Overcommit prior to Lefthook, so you may want to uninstall it firs 1. Install Lefthook managed Git hooks: ```shell + # If installed globally + lefthook install + # Or if installed via ruby gem bundle exec lefthook install ``` 1. Test Lefthook is working by running the Lefthook `pre-push` Git hook: ```shell + # If installed globally + lefthook run pre-push + # Or if installed via ruby gem bundle exec lefthook run pre-push ``` @@ -57,6 +65,18 @@ Lefthook is configured with a combination of: - Project configuration in [`lefthook.yml`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lefthook.yml). - Any [local configuration](https://github.com/evilmartians/lefthook/blob/master/README.md#local-config). +### Lefthook auto-fixing files + +We have a custom lefthook target to run all the linters with auto-fix capabilities, +but just on the files which changed in your branch. + +```shell +# If installed globally +lefthook run auto-fix +# Or if installed via ruby gem +bundle exec lefthook run auto-fix +``` + ### Disable Lefthook temporarily To disable Lefthook temporarily, you can set the `LEFTHOOK` environment variable to `0`. For instance: diff --git a/lefthook.yml b/lefthook.yml index 6a80713450fb7..d5c0230f12b6c 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -96,6 +96,7 @@ pre-push: "merge_conflicts": skip: true # This is disabled by default. You can enable this check by adding skip: false in lefhook-local.yml https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md#skip runner: bash + pre-commit: parallel: true commands: @@ -103,3 +104,27 @@ pre-commit: tags: secrets files: git diff --name-only --diff-filter=d --staged run: 'if command -v gitleaks > /dev/null 2>&1; then gitleaks protect --no-banner --staged --redact --verbose; else echo "WARNING: gitleaks is not installed. Please install it. See https://github.com/zricethezav/gitleaks#installing."; fi' + +auto-fix: + parallel: true + commands: + frontend: + tags: frontend style + files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD + glob: '*.{js,vue}' + run: 'yarn run lint:eslint:fix {files} && yarn run prettier --write --list-different {files}' + jsonlint: + tags: style + files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD + glob: '*.{json}' + run: scripts/lint-json --format --verbose {files} + prettier-graphql: + tags: frontend style + files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD + glob: '*.{graphql}' + run: yarn run prettier --write --list-different {files} + rubocop: + tags: backend style + files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD + glob: '*.{rb,rake}' + run: REVEAL_RUBOCOP_TODO=0 bundle exec rubocop --parallel --autocorrect --force-exclusion {files} -- GitLab