From 101dc2f98ba702f1157a3bec4aa292d6a7193778 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 11:19:14 +0200 Subject: [PATCH 01/10] add a new hook for checking if targets that are marked as PHONY actually exists --- pre_commit_hooks/make/phony-targets.sh | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 pre_commit_hooks/make/phony-targets.sh diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh new file mode 100755 index 0000000..e64641a --- /dev/null +++ b/pre_commit_hooks/make/phony-targets.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -e + +# Make environment variables working in OSX GUI apps such as Github Desktop +# https://stackoverflow.com/q/135688/483528 +export PATH=$PATH:/usr/local/bin + +EXITCODE=0 + +validate_phony_targets() { + local source=$1 + local targets=$(sed -n 's#^.PHONY:\(.*\)#\1#p' <<< $source) + + for target in $targets; do + # The q command for setting exit codes if a pattern doesn't match, + # isn't supported in OSX / BSD sed. + # We check the exact match as a workaround instead. + # If the pattern doesn't match that means that the target doesn't exist. + if test -z "$(sed -ne "s#^\(${target}:\).*#\1#p" <<< $source)" + then + echo "No target found for PHONY target: '$target'" + EXITCODE=1 + fi + done +} + +for file in "$@"; do + SOURCE="$(cat $file)" + validate_phony_targets "$SOURCE" +done + +exit $EXITCODE + + From 5fe4f347f4f94d318d57c5dfa733a613714c9bf7 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 11:23:25 +0200 Subject: [PATCH 02/10] add new hook to configurations --- .pre-commit-hooks.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 85f038d..90dd5a7 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -72,3 +72,12 @@ language: script files: \.go$ exclude: vendor\/.*$ +# --------------------------------------------------------------------------------------------------------------------- +# GNU Make specific hooks +# --------------------------------------------------------------------------------------------------------------------- + - id: phony-targets + name: phony-targets + description: Linter for detecting targets marked as PHONY that doesn't exist. + entry: pre_commit_hooks/make/phony-targets.sh + language: script + files: \Makefile$ \ No newline at end of file From 820886125f1ddf2ab3eb42b1709057d7f6226e3f Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 11:26:09 +0200 Subject: [PATCH 03/10] add phony-targets to README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 88f0ead..7b13923 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ Currently, the following hooks are supported: It's fast: on average 5 times faster than gometalinter. It's easy to integrate and use, has nice output and has a minimum number of false positives. +**GNU Make** +- [phony-targets](https://github.com/mineiros-io/pre-commit-hooks/blob/master/pre_commit_hooks/make/phony-targets.sh): + This hook validates if targets that are marked as `PHONY` actually exist. + ## Installation & Dependencies Install [pre-commit](https://pre-commit.com/). E.g. `brew install pre-commit` @@ -49,6 +53,7 @@ repos: - id: gofmt - id: goimports - id: golint + - id: phony-targets ``` Once you created the configuration file inside your repository, you must run `pre-commit install` to activate the hooks. From c43221584e3419263cbda67a0d45de19a8c284f7 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 11:30:28 +0200 Subject: [PATCH 04/10] format files properly --- .pre-commit-hooks.yaml | 2 +- pre_commit_hooks/make/phony-targets.sh | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 90dd5a7..5df19f8 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -80,4 +80,4 @@ description: Linter for detecting targets marked as PHONY that doesn't exist. entry: pre_commit_hooks/make/phony-targets.sh language: script - files: \Makefile$ \ No newline at end of file + files: \Makefile$ diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh index e64641a..95800ec 100755 --- a/pre_commit_hooks/make/phony-targets.sh +++ b/pre_commit_hooks/make/phony-targets.sh @@ -17,8 +17,7 @@ validate_phony_targets() { # isn't supported in OSX / BSD sed. # We check the exact match as a workaround instead. # If the pattern doesn't match that means that the target doesn't exist. - if test -z "$(sed -ne "s#^\(${target}:\).*#\1#p" <<< $source)" - then + if test -z "$(sed -ne "s#^\(${target}:\).*#\1#p" <<< $source)"; then echo "No target found for PHONY target: '$target'" EXITCODE=1 fi @@ -31,5 +30,3 @@ for file in "$@"; do done exit $EXITCODE - - From 8078214e3f3be9ca825848a3afef4c52f12fc67c Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 13:46:48 +0200 Subject: [PATCH 05/10] restrict files to apply the hook for --- .pre-commit-hooks.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 5df19f8..4a702cb 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -72,12 +72,13 @@ language: script files: \.go$ exclude: vendor\/.*$ + # --------------------------------------------------------------------------------------------------------------------- # GNU Make specific hooks # --------------------------------------------------------------------------------------------------------------------- - id: phony-targets name: phony-targets - description: Linter for detecting targets marked as PHONY that doesn't exist. + description: Linter for detecting targets marked as PHONY that don't exist. entry: pre_commit_hooks/make/phony-targets.sh language: script - files: \Makefile$ + files: \(Makefile|.mak|.mk)$ From e41813e0a747fa6a385c5c14d6eb5e91818078e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Martius?= Date: Mon, 25 May 2020 13:48:40 +0200 Subject: [PATCH 06/10] render make file to replace any variables and get the final phony targets and just capture all targets Co-authored-by: Marius Tolzmann --- pre_commit_hooks/make/phony-targets.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh index 95800ec..70a885b 100755 --- a/pre_commit_hooks/make/phony-targets.sh +++ b/pre_commit_hooks/make/phony-targets.sh @@ -25,7 +25,7 @@ validate_phony_targets() { } for file in "$@"; do - SOURCE="$(cat $file)" + SOURCE=$(make -npq -f "${file}" | grep -E $'^[^ \t#][^:#]*:') validate_phony_targets "$SOURCE" done From 05bc62fdda87fe8fbfa94838cf2475076615b1f4 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 13:51:58 +0200 Subject: [PATCH 07/10] remove old workaround for github desktop --- pre_commit_hooks/make/phony-targets.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh index 70a885b..a74d678 100755 --- a/pre_commit_hooks/make/phony-targets.sh +++ b/pre_commit_hooks/make/phony-targets.sh @@ -2,10 +2,6 @@ set -e -# Make environment variables working in OSX GUI apps such as Github Desktop -# https://stackoverflow.com/q/135688/483528 -export PATH=$PATH:/usr/local/bin - EXITCODE=0 validate_phony_targets() { From 7513892b46a2c7f05a9b5fc3349ab6ae61fdf7a3 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 14:33:48 +0200 Subject: [PATCH 08/10] add more files and ident --- .pre-commit-hooks.yaml | 6 ++++-- pre_commit_hooks/make/phony-targets.sh | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 4a702cb..1f99080 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -76,9 +76,11 @@ # --------------------------------------------------------------------------------------------------------------------- # GNU Make specific hooks # --------------------------------------------------------------------------------------------------------------------- - - id: phony-targets + +# Validate targets that are marked as PHONY +- id: phony-targets name: phony-targets description: Linter for detecting targets marked as PHONY that don't exist. entry: pre_commit_hooks/make/phony-targets.sh language: script - files: \(Makefile|.mak|.mk)$ + files: \(Makefile|Makefile.*|.mak|.mk)$ diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh index a74d678..50c6229 100755 --- a/pre_commit_hooks/make/phony-targets.sh +++ b/pre_commit_hooks/make/phony-targets.sh @@ -21,7 +21,7 @@ validate_phony_targets() { } for file in "$@"; do - SOURCE=$(make -npq -f "${file}" | grep -E $'^[^ \t#][^:#]*:') + SOURCE="$(cat $file)" validate_phony_targets "$SOURCE" done From f967811490f00a30433e42f7e168ab024069c87f Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 15:09:08 +0200 Subject: [PATCH 09/10] fix regex for excluded files --- .pre-commit-hooks.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 1f99080..bed99e6 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -83,4 +83,9 @@ description: Linter for detecting targets marked as PHONY that don't exist. entry: pre_commit_hooks/make/phony-targets.sh language: script - files: \(Makefile|Makefile.*|.mak|.mk)$ + files: (Makefile|Makefile.*|.+.mak|.+.mk)$ + exclude: > + (?x)^( + .+\.vendor\/.*$| + .+\.terraform\/.*$| + )$ From b474bbbc10e7234cb169aa20bd46dcb6fff94bb7 Mon Sep 17 00:00:00 2001 From: Soren Martius Date: Mon, 25 May 2020 20:51:43 +0200 Subject: [PATCH 10/10] use grep instead of sed --- pre_commit_hooks/make/phony-targets.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pre_commit_hooks/make/phony-targets.sh b/pre_commit_hooks/make/phony-targets.sh index 50c6229..d24cf7c 100755 --- a/pre_commit_hooks/make/phony-targets.sh +++ b/pre_commit_hooks/make/phony-targets.sh @@ -5,15 +5,16 @@ set -e EXITCODE=0 validate_phony_targets() { - local source=$1 - local targets=$(sed -n 's#^.PHONY:\(.*\)#\1#p' <<< $source) + local file=$1 + local source=$(make -npq -f "${file}" | grep -E '^.PHONY:') + local targets=$(sed -n 's#^.PHONY:\(.*\)#\1#p' <<< "${source}") for target in $targets; do # The q command for setting exit codes if a pattern doesn't match, # isn't supported in OSX / BSD sed. # We check the exact match as a workaround instead. # If the pattern doesn't match that means that the target doesn't exist. - if test -z "$(sed -ne "s#^\(${target}:\).*#\1#p" <<< $source)"; then + if test -z "$(sed -ne "s#^\(${target}:\).*#\1#p" "${file}")"; then echo "No target found for PHONY target: '$target'" EXITCODE=1 fi @@ -21,8 +22,7 @@ validate_phony_targets() { } for file in "$@"; do - SOURCE="$(cat $file)" - validate_phony_targets "$SOURCE" + validate_phony_targets "$file" done exit $EXITCODE