From eeaa783e56b3fb9848c60ac6b34ca1c68fc4a74a Mon Sep 17 00:00:00 2001 From: Cian Hatton Date: Thu, 23 Feb 2023 15:44:50 +0000 Subject: [PATCH] Adding scripts to run linting on only modified files (#3192) --- Makefile | 9 ++++++++- scripts/linting/lint-changed-go-files.sh | 13 +++++++++++++ scripts/linting/lint-changed-md-files.sh | 13 +++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100755 scripts/linting/lint-changed-go-files.sh create mode 100755 scripts/linting/lint-changed-md-files.sh diff --git a/Makefile b/Makefile index 037571717c3..73c89cb236b 100644 --- a/Makefile +++ b/Makefile @@ -295,7 +295,9 @@ lint: lint-fix: golangci-lint run --fix --out-format=tab --issues-exit-code=0 -.PHONY: lint lint-fix + +lint-fix-changed: + ./scripts/linting/lint-changed-go-files.sh format: find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./docs/client/statik/statik.go" -not -path "./tests/mocks/*" -not -name '*.pb.go' -not -name '*.pb.gw.go' | xargs gofumpt -w @@ -305,6 +307,11 @@ format: docs-lint: markdownlint . --fix +docs-lint-changed: + ./scripts/linting/lint-changed-md-files.sh + +.PHONY: lint lint-fix lint-fix-changed docs-lint docs-lint-changed + ############################################################################### ### Protobuf ### ############################################################################### diff --git a/scripts/linting/lint-changed-go-files.sh b/scripts/linting/lint-changed-go-files.sh new file mode 100755 index 00000000000..5d3b5ad923c --- /dev/null +++ b/scripts/linting/lint-changed-go-files.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# lint_modified_go_files runs the linter only if changes have been made to any go files. +function lint_modified_go_files() { + local go_files="$(git diff --name-only | grep \.go$)" + for f in $go_files; do + golangci-lint run "${f}" --fix --out-format=tab --issues-exit-code=0 + done +} + +lint_modified_go_files diff --git a/scripts/linting/lint-changed-md-files.sh b/scripts/linting/lint-changed-md-files.sh new file mode 100755 index 00000000000..214ec70f622 --- /dev/null +++ b/scripts/linting/lint-changed-md-files.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# lint_modified_markdown_files runs the linter only if changes have been made to any md files. +function lint_modified_markdown_files() { + local markdown_files="$(git diff --name-only | grep \.md$)" + for f in $markdown_files; do + markdownlint "${f}" --fix + done +} + +lint_modified_markdown_files