From 78d5419620f0cc6f70c43ac35d3317671d274152 Mon Sep 17 00:00:00 2001
From: Patrick Baudin <patrick.baudin@cea.fr>
Date: Tue, 26 Jul 2022 10:43:26 +0200
Subject: [PATCH] [Headers] adds targets related to commits

---
 share/Makefile.headers | 82 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/share/Makefile.headers b/share/Makefile.headers
index b6a36166a79..8b243dae357 100644
--- a/share/Makefile.headers
+++ b/share/Makefile.headers
@@ -204,6 +204,88 @@ headers: $(FRAMAC_HDRCK)
 	$(error "Please set HEADER_SPEC variable to a file containing the header specification.")
 else
 
+ifneq ($(HDRCK.HAS_GIT),)
+
+define HDRCK.file-list-from-git-diff
+$(GIT) diff --name-status $(1) | $(GREP) -v "^D" | $(SED) "s/^.[ \t]*//" | $(TR) '\n' '\000' | $(HDRCK.FILE_ATTR)
+endef
+
+HDRCK.main-targets=check-headers headers
+define HDRCK.file-list-filter-with-git-diff
+HDRCK.$(1)-filter-targets=$$(addsuffix .$(1),$$(HDRCK.main-targets))
+.PHONY: $(LINT.$(1)-filter-targets)
+$$(HDRCK.$(1)-filter-targets):: HDRCK.SPEC=$$(call HDRCK.file-list-from-git-diff,$(2))
+$$(HDRCK.$(1)-filter-targets):: %.$(1) : $(3) %
+	@echo [HDRCK] Done: HDRCK_DIFF=\"$(2)\" make $$(basename $$@)
+	@echo [HDRCK] checked file list:
+	$$(GIT) diff --name-status $(2)
+endef
+
+ifneq ($(HEADER_DIFF),)
+
+$(info [HDRCK] Looking at files modified from branch/commit: '$(HEADER_DIFF)')
+HDRCK.SPEC=$(call LINT.file-list-from-git-diff,$(HEADER_DIFF))
+$(info $(HEADER.FILE_LIST))
+
+endif # $(HEADER_DIFF)
+
+#### rules for make <headers-target>.before-push
+##   looks at files modified from origin/$(git branch --show-current) and checks that there is no staged nor unstaged files
+
+PHONY: HDRCK.checked-unmodified-ok
+HDRCK.checked-unmodified-ok:
+	$(eval HDRCK.checked-unmodified := $(shell $(GIT) diff --name-status HEAD | $(WC) -l))
+	if [ 0 != $(HDRCK.checked-unmodified) ]; then \
+           echo "[HDRCK] Staged or unstaged files=$(HDRCK.checked-unmodified):"; \
+            $(GIT) diff --name-status HEAD; \
+           echo "[HDRCK] Error: may check some staged or unstaged file versions" ; \
+           exit 1; \
+        fi;
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,before-push,--cached origin/$(shell git branch --show-current),HDRCK.checked-unmodified-ok))
+
+#### rules for make <headers-target>.unstaged
+##   looks at unstaged files
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,unstaged, ))
+
+#### rules for make <headers-target>.before-commit
+##   looks at staged files and checks that there is no unstaged files
+
+PHONY: HDRCK.checked-unstaged-ok
+HDRCK.checked-unstaged-ok:
+	$(eval HDRCK.checked-unstaged := $(shell $(GIT) diff --name-status | $(WC) -l))
+	if [ 0 != $(HDRCK.checked-unstaged) ]; then \
+	   echo "[HDRCK] Unstaged files=$(HDRCK.checked-unstaged):"; \
+           $(GIT) diff --name-status ; \
+           echo "[HDRCK] Error: may check some unstaged version files version." ; \
+           exit 1; \
+        fi;
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,before-commit,--cached HEAD,HDRCK.checked-unstaged-ok))
+
+#### rules for make <headers-target>.before-commit-a
+## looks at unstaged and staged files
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,before-commit-a,HEAD,))
+
+#### rules for make <headers-target>.before-commit-a--ammend
+##   looks at unstaged, staged and previously committed files
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,before-commit-a--ammend,HEAD~1,))
+
+#### rules for make <headers-target>.before-commit--amend
+##   looks at staged and previously committed files and checks that there is no unstaged files
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,before-commit--ammend,HEAD~1,HDRCK.checked-unstaged-ok))
+
+#### rules for make <headers-target>.previous-commit
+## looks at files of the last commit and checks that there is no staged nor unstaged files
+
+$(eval $(call HDRCK.file-list-filter-with-git-diff,previous-commit,HEAD~1,HDRCK.checked-unmodified-ok))
+
+endif # $(HDRCK.HAS_GIT)
+
 .PHONY: check-headers
 check-headers: $(FRAMAC_HDRCK)
 	$(HDRCK.SPEC) | $(HDRCK.CMD) $(HDRCK.OPTS) $(HDRCK_EXTRA)
-- 
GitLab