From 4f357445053352ff38d152fd8f291e06a16fdc64 Mon Sep 17 00:00:00 2001 From: Carter McKinnon Date: Wed, 18 Oct 2023 08:24:47 -0700 Subject: [PATCH] Add fmt/lint for trailing spaces --- Makefile | 2 ++ hack/fmt-trailing-spaces.sh | 21 +++++++++++++++++++++ hack/lint-trailing-spaces.sh | 4 ++++ 3 files changed, 27 insertions(+) create mode 100755 hack/fmt-trailing-spaces.sh create mode 100755 hack/lint-trailing-spaces.sh diff --git a/Makefile b/Makefile index f7c64c618..40d952791 100644 --- a/Makefile +++ b/Makefile @@ -79,6 +79,7 @@ endif .PHONY: fmt fmt: ## Format the source files $(SHFMT_COMMAND) $(SHFMT_FLAGS) --write $(MAKEFILE_DIR) + hack/fmt-trailing-spaces.sh SHELLCHECK_COMMAND := $(shell which shellcheck) ifeq (, $(SHELLCHECK_COMMAND)) @@ -96,6 +97,7 @@ transform-al2-to-al2023: lint: lint-docs ## Check the source files for syntax and format issues $(SHFMT_COMMAND) $(SHFMT_FLAGS) --diff $(MAKEFILE_DIR) $(SHELLCHECK_COMMAND) --format gcc --severity error $(SHELL_FILES) + hack/lint-trailing-spaces.sh .PHONY: test test: ## run the test-harness diff --git a/hack/fmt-trailing-spaces.sh b/hack/fmt-trailing-spaces.sh new file mode 100755 index 000000000..80e7770a5 --- /dev/null +++ b/hack/fmt-trailing-spaces.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +cd $(dirname $0)/.. + +OS=$(uname) +for FILE in $(find . -type f -not -path "*/.git/*"); do + if [ "$OS" = "Darwin" ]; then + # macOS sed doesn't support '-i' + sed 's/[[:space:]]*$//g' "$FILE" > "$FILE.tmp" + # macOS chmod doesn't support '--reference' + chmod "$(stat -f "%Mp%Lp" $FILE)" "$FILE.tmp" + mv "$FILE.tmp" "$FILE" + else + # assume we have a sane sed + sed -i 's/[[:space:]]*$//g' "$FILE" + fi +done diff --git a/hack/lint-trailing-spaces.sh b/hack/lint-trailing-spaces.sh new file mode 100755 index 000000000..60bf8802e --- /dev/null +++ b/hack/lint-trailing-spaces.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cd $(dirname $0)/.. +git diff-tree --check $(git hash-object -t tree /dev/null) HEAD