All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] check-headers: add header usage checks for .c files
@ 2014-09-14  7:40 David Aguilar
  2014-09-14  7:40 ` [PATCH 2/2] cleanups: ensure that git-compat-util.h is included first David Aguilar
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David Aguilar @ 2014-09-14  7:40 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] cleanups: ensure that git-compat-util.h is included first
  2014-09-14  7:40 [PATCH 1/2] check-headers: add header usage checks for .c files David Aguilar
@ 2014-09-14  7:40 ` David Aguilar
  2014-09-15 19:14 ` [PATCH 1/2] check-headers: add header usage checks for .c files Junio C Hamano
  2014-09-15 19:16 ` Junio C Hamano
  2 siblings, 0 replies; 6+ messages in thread
From: David Aguilar @ 2014-09-14  7:40 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

CodingGuidelines states that the first #include in C files should be
git-compat-util.h or another header file that includes it, such as
cache.h or builtin.h.

Tweak the tiny minority of files that do not follow this advice.
This makes "make check-headers SKIP_HEADER_CHECK=1" happy.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 bulk-checkin.c    | 1 +
 bulk-checkin.h    | 2 --
 http.c            | 1 +
 merge-recursive.c | 2 +-
 sigchain.c        | 2 +-
 test-regex.c      | 2 +-
 test-sigchain.c   | 2 +-
 varint.c          | 1 +
 varint.h          | 2 --
 9 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/bulk-checkin.c b/bulk-checkin.c
index 98e651c..0c4b8a7 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2011, Google Inc.
  */
+#include "cache.h"
 #include "bulk-checkin.h"
 #include "csum-file.h"
 #include "pack.h"
diff --git a/bulk-checkin.h b/bulk-checkin.h
index 4f599f8..fbd40fc 100644
--- a/bulk-checkin.h
+++ b/bulk-checkin.h
@@ -4,8 +4,6 @@
 #ifndef BULK_CHECKIN_H
 #define BULK_CHECKIN_H
 
-#include "cache.h"
-
 extern int index_bulk_checkin(unsigned char sha1[],
 			      int fd, size_t size, enum object_type type,
 			      const char *path, unsigned flags);
diff --git a/http.c b/http.c
index 0adcec4..040f362 100644
--- a/http.c
+++ b/http.c
@@ -1,3 +1,4 @@
+#include "git-compat-util.h"
 #include "http.h"
 #include "pack.h"
 #include "sideband.h"
diff --git a/merge-recursive.c b/merge-recursive.c
index 8ab944c..df8157b 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -3,8 +3,8 @@
  * Fredrik Kuivinen.
  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
  */
-#include "advice.h"
 #include "cache.h"
+#include "advice.h"
 #include "cache-tree.h"
 #include "commit.h"
 #include "blob.h"
diff --git a/sigchain.c b/sigchain.c
index 1118b99..faa375d 100644
--- a/sigchain.c
+++ b/sigchain.c
@@ -1,5 +1,5 @@
-#include "sigchain.h"
 #include "cache.h"
+#include "sigchain.h"
 
 #define SIGCHAIN_MAX_SIGNALS 32
 
diff --git a/test-regex.c b/test-regex.c
index b5bfd54..0dc598e 100644
--- a/test-regex.c
+++ b/test-regex.c
@@ -1,4 +1,4 @@
-#include <git-compat-util.h>
+#include "git-compat-util.h"
 
 int main(int argc, char **argv)
 {
diff --git a/test-sigchain.c b/test-sigchain.c
index 42db234..e499fce 100644
--- a/test-sigchain.c
+++ b/test-sigchain.c
@@ -1,5 +1,5 @@
-#include "sigchain.h"
 #include "cache.h"
+#include "sigchain.h"
 
 #define X(f) \
 static void f(int sig) { \
diff --git a/varint.c b/varint.c
index 4ed7729..409c497 100644
--- a/varint.c
+++ b/varint.c
@@ -1,3 +1,4 @@
+#include "git-compat-util.h"
 #include "varint.h"
 
 uintmax_t decode_varint(const unsigned char **bufp)
diff --git a/varint.h b/varint.h
index 0321195..c1c44d9 100644
--- a/varint.h
+++ b/varint.h
@@ -1,8 +1,6 @@
 #ifndef VARINT_H
 #define VARINT_H
 
-#include "git-compat-util.h"
-
 extern int encode_varint(uintmax_t, unsigned char *);
 extern uintmax_t decode_varint(const unsigned char **);
 
-- 
2.1.0.241.ga16d620

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] check-headers: add header usage checks for .c files
  2014-09-14  7:40 [PATCH 1/2] check-headers: add header usage checks for .c files David Aguilar
  2014-09-14  7:40 ` [PATCH 2/2] cleanups: ensure that git-compat-util.h is included first David Aguilar
@ 2014-09-15 19:14 ` Junio C Hamano
  2014-09-15 19:16 ` Junio C Hamano
  2 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2014-09-15 19:14 UTC (permalink / raw)
  To: David Aguilar
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

David Aguilar <davvid@gmail.com> writes:

> +check_header_usage () {
> +	first=$(grep '^#include' "$1" |
> +		head -n1 |
> +		sed -e 's,#include ",,' -e 's,"$,,')

perhaps a single "sed" invocation suffices?

	sed -n -e '/^#include/{
        	s/#include ["<]\(.*\)".*/\1/p
                q
	}' "$1"

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] check-headers: add header usage checks for .c files
  2014-09-14  7:40 [PATCH 1/2] check-headers: add header usage checks for .c files David Aguilar
  2014-09-14  7:40 ` [PATCH 2/2] cleanups: ensure that git-compat-util.h is included first David Aguilar
  2014-09-15 19:14 ` [PATCH 1/2] check-headers: add header usage checks for .c files Junio C Hamano
@ 2014-09-15 19:16 ` Junio C Hamano
  2014-09-15 19:19   ` Junio C Hamano
  2 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2014-09-15 19:16 UTC (permalink / raw)
  To: David Aguilar
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

David Aguilar <davvid@gmail.com> writes:

> 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 () {
> +	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
> +	do
> +		check_header "$header"

Hmmmm, doesn't check_header run "$@" as a command?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] check-headers: add header usage checks for .c files
  2014-09-15 19:16 ` Junio C Hamano
@ 2014-09-15 19:19   ` Junio C Hamano
  2014-09-15 19:49     ` David Aguilar
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2014-09-15 19:19 UTC (permalink / raw)
  To: David Aguilar
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

Junio C Hamano <gitster@pobox.com> writes:

> David Aguilar <davvid@gmail.com> writes:
>
>> 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 () {
>> +	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
>> +	do
>> +		check_header "$header"
>
> Hmmmm, doesn't check_header run "$@" as a command?

Taking the previous two together, perhaps

 check-headers.sh | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/check-headers.sh b/check-headers.sh
index 7f25e7a..526381e 100755
--- a/check-headers.sh
+++ b/check-headers.sh
@@ -20,6 +20,7 @@ maybe_exit () {
 
 check_header () {
 	header="$1"
+	shift
 	case "$header" in
 	common-cmds.h)
 		# should only be included by help.c, not checked
@@ -38,15 +39,17 @@ check_header () {
 check_headers () {
 	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
 	do
-		check_header "$header"
+		check_header "$header" "$@"
 	done
 }
 
 check_header_usage () {
-	first=$(grep '^#include' "$1" |
-		head -n1 |
-		sed -e 's,#include ",,' -e 's,"$,,')
-
+	first=$( 
+		sed -n -e '/^#include/{
+			s/#include ["<]\(.*\)".*/\1/p
+			q
+		}' "$1"
+	)
 	case "$first" in
 	cache.h|builtin.h|git-compat-util.h)
 		# happy

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] check-headers: add header usage checks for .c files
  2014-09-15 19:19   ` Junio C Hamano
@ 2014-09-15 19:49     ` David Aguilar
  0 siblings, 0 replies; 6+ messages in thread
From: David Aguilar @ 2014-09-15 19:49 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Jonathan Nieder, René Scharfe, Matthieu Moy

On Mon, Sep 15, 2014 at 12:19:06PM -0700, Junio C Hamano wrote:
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > David Aguilar <davvid@gmail.com> writes:
> >
> >> 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 () {
> >> +	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
> >> +	do
> >> +		check_header "$header"
> >
> > Hmmmm, doesn't check_header run "$@" as a command?
> 
> Taking the previous two together, perhaps

Thanks, yes, that's better.


>  check-headers.sh | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/check-headers.sh b/check-headers.sh
> index 7f25e7a..526381e 100755
> --- a/check-headers.sh
> +++ b/check-headers.sh
> @@ -20,6 +20,7 @@ maybe_exit () {
>  
>  check_header () {
>  	header="$1"
> +	shift
>  	case "$header" in
>  	common-cmds.h)
>  		# should only be included by help.c, not checked
> @@ -38,15 +39,17 @@ check_header () {
>  check_headers () {
>  	for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h
>  	do
> -		check_header "$header"
> +		check_header "$header" "$@"
>  	done
>  }
>  
>  check_header_usage () {
> -	first=$(grep '^#include' "$1" |
> -		head -n1 |
> -		sed -e 's,#include ",,' -e 's,"$,,')
> -
> +	first=$( 
> +		sed -n -e '/^#include/{
> +			s/#include ["<]\(.*\)".*/\1/p
> +			q
> +		}' "$1"
> +	)
>  	case "$first" in
>  	cache.h|builtin.h|git-compat-util.h)
>  		# happy

-- 
David

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-09-15 19:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-14  7:40 [PATCH 1/2] check-headers: add header usage checks for .c files David Aguilar
2014-09-14  7:40 ` [PATCH 2/2] cleanups: ensure that git-compat-util.h is included first David Aguilar
2014-09-15 19:14 ` [PATCH 1/2] check-headers: add header usage checks for .c files Junio C Hamano
2014-09-15 19:16 ` Junio C Hamano
2014-09-15 19:19   ` Junio C Hamano
2014-09-15 19:49     ` David Aguilar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.