git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] check-headers: add header usage checks for .c files

From
David Aguilar <davvid@gmail.com>
Date
Sep 14, 2014, 07:40 UTC
Message-ID
<1410680445-84593-1-git-send-email-davvid@gmail.com>

Teach check-header.sh to ensure that the first included header in .c files is either git-compat-util.h, builtin.h, or cache.h.

Ensure that common-cmds.h is only included by help.c.
Move the logic into functions so that we can skip parts of the check.
Signed-off-by: David Aguilar <davvid@gmail.com>
---
This depends on my previous patch that adds check-header.sh.
 check-headers.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 61 insertions(+), 5 deletions(-)
diff --git a/check-headers.sh b/check-headers.sh
index ef06f56..7f25e7a 100755
--- a/check-headers.sh
+++ b/check-headers.sh
@@ -1,5 +1,9 @@
 #!/bin/sh
 
+# This script is run via make.
+# "make check-headers SKIP_HEADER_CHECK=1" skips the header dependency check.
+# "make check-headers SKIP_USAGE_CHECK=1" skips the header usage check.
+
 exit_code=0
 
 maybe_exit () {
@@ -14,11 +18,11 @@ maybe_exit () {
 	fi
 }
 
-for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
-do
+check_header () {
+	header="$1"
 	case "$header" in
 	common-cmds.h)
-		# should only be included by help.c
+		# should only be included by help.c, not checked
 		;;
 	*)
 		subdir=$(dirname "$header") &&
@@ -29,6 +33,58 @@ do
 		maybe_exit $?
 		;;
 	esac
-done
+}
+
+check_headers () {
+	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
+	do
+		check_header "$header"
+	done
+}
+
+check_header_usage () {
+	first=$(grep '^#include' "$1" |
+		head -n1 |
+		sed -e 's,#include ",,' -e 's,"$,,')
+
+	case "$first" in
+	cache.h|builtin.h|git-compat-util.h)
+		# happy
+		;;
+	*)
+		echo "error: $1 must #include \"git-compat-util.h\" before $first"
+		maybe_exit 1
+		;;
+	esac
+
+	if grep common-cmds.h "$1" >/dev/null && test "$1" != help.c
+	then
+		echo "error: $1 must not include common-cmds.h"
+		maybe_exit 1
+	fi
+}
+
+check_usage () {
+	# Implementation files should #include git-compat-util.h, cache.h,
+	# or builtin.h before any others.
+	for impl in *.c builtin/*.c
+	do
+		check_header_usage "$impl"
+	done
+}
+
+main () {
+	if test -z "$SKIP_HEADER_CHECK"
+	then
+		check_headers "$@"
+	fi
+
+	if test -z "$SKIP_USAGE_CHECK"
+	then
+		check_usage
+	fi
+
+	exit $exit_code
+}
 
-exit $exit_code
+main "$@"
-- 
2.1.0.241.ga16d620
Next: David Aguilar
Message 1 of 6 in “check-headers: add header usage checks for .c files”
  1. 1/2 check-headers: add header usage checks for .c filesDavid Aguilar, Sep 14, 2014
  2. 2/2 cleanups: ensure that git-compat-util.h is included firstDavid Aguilar, Sep 14, 2014
  3. Junio C HamanoSep 15, 2014
  4. Junio C HamanoSep 15, 2014
  5. Junio C HamanoSep 15, 2014
  6. David AguilarSep 15, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.