From 45f21d26ee750918e85dd03a9567659d88a5177e Mon Sep 17 00:00:00 2001
From: Patrick Baudin <patrick.baudin@cea.fr>
Date: Thu, 7 Jul 2022 17:18:07 +0200
Subject: [PATCH] [Linting] Allows LINT_DIFF=<commit/branch> make
 <lint|check-xxx|fix-xxx>

---
 share/Makefile.linting | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/share/Makefile.linting b/share/Makefile.linting
index e02bbe19b32..85911ad2b41 100644
--- a/share/Makefile.linting
+++ b/share/Makefile.linting
@@ -33,8 +33,11 @@
 # - make LINT_DIR=<subdir> <lint-target>
 # It is possible to force the action to given files:
 # - make LINT_FILE=<file> <lint-target>
+# or to the files modified between HEAD and a specific branch/commit:
+# - make LINT_DIFF=<branch/commit> <lint-target>
 # Notes:
-# - when LINT_FILE is given, .gitattributes and LINT_DIR are ignored;
+# - when LINT_FILE is given, .gitattributes, LINT_DIFF and LINT_DIR are ignored;
+# - when LINT_DIFF is given, LINT_DIR is ignored (but .gitattributes is used);
 # - to use fix-utf8 target, the variable LINT_FROM_ENCODING=<from-encoding-name>
 #   has to be set.
 
@@ -57,6 +60,8 @@ CLANG_FORMAT ?= clang-format
 # Default values necessary for
 #   LINT_MAKEFILE=<this-makefile> make -f <this-makefile> <lint-target>`
 # Otherwise theses variables are defined into share/Makefile.common
+GREP    ?= grep
+TR      ?= tr
 SED     ?= LC_ALL=C sed
 ISED    ?= sed -i
 FIND    ?= find
@@ -119,7 +124,7 @@ endif
           echo "error: $(CLANG_FORMAT) must be installed"; \
           exit 1; \
         fi;
-	mkdir -p $(dir $@)
+	$(MKDIR) $(dir $@)
 	$(TOUCH) $@
 
 ###############
@@ -178,6 +183,13 @@ ifeq ($(LINT_FILE),)
 
 LINT.dir=$(wildcard $(LINT_DIR))
 
+ifeq ($(LINT_DIFF),)
+FILE_LIST=$(GIT) ls-files $(LINT.dir) -z
+else
+$(info [LINT] Looking at files modified from branch/commit: '$(LINT_DIFF)')
+FILE_LIST=$(GIT) diff --name-status --cached $(LINT_DIFF) | $(GREP) -v "^D" | $(SED) "s/^.[ \t]*//" | $(TR) '\n' '\000'
+endif
+
 #### check-XXX targets
 
 # Same list on both lines
@@ -188,7 +200,7 @@ LINT.check-targets= \
 # Generic rule
 $(LINT.check-targets):
 	echo "[LINT] Checking from GIT attribute $@..."
-	$(GIT) ls-files $(LINT.dir) -z \
+	$(FILE_LIST) \
         | $(GIT) check-attr --stdin -z $@ \
         | $(SED) -zne 'x;n;n;s/^set$$//;t print;b;:print;x;p' \
         | $(XARGS) -0 -IXX sh -c '$(LINT.make) LINT_FILE="XX" $@ || exit 255'
@@ -203,7 +215,7 @@ LINT.fix-targets= \
 # Generic rule
 $(LINT.fix-targets):
 	echo "[LINT] Fixing from GIT attribute $(patsubst fix-%,check-%,$@)..."
-	$(GIT) ls-files $(LINT.dir) -z \
+	$(FILE_LIST) \
         | $(GIT) check-attr --stdin -z $(patsubst fix-%,check-%,$@) \
         | $(SED) -zne 'x;n;n;s/^set$$//;t print;b;:print;x;p' \
         | $(XARGS) -0 -IXX sh -c '$(LINT.make) LINT_FILE="XX" $@ || exit 255'
-- 
GitLab