From: Marc Fehling Date: Mon, 20 Jan 2025 13:17:21 +0000 (+0100) Subject: Add detect-include-cycle check to pre-commit. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1089580bd22d52f2255cff7d418f17a2e8bf4a68;p=dealii.git Add detect-include-cycle check to pre-commit. --- diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 54f5539a0f..50a2aa6c00 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -16,4 +16,5 @@ jobs: steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 + - run: python -m pip install networkx - uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0f656dc75c..0b0ab6d024 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,3 +46,10 @@ repos: hooks: - id: typos files: (?x) ^(doc|examples|include|source|tests)/ +- repo: local + hooks: + - id: detect-include-cycles + name: Detect '#include' cycles + entry: python contrib/utilities/detect_include_cycles.py + pass_filenames: false + language: system diff --git a/contrib/utilities/detect_include_cycles.py b/contrib/utilities/detect_include_cycles.py index 0bc9baa7e8..81838f2c0a 100644 --- a/contrib/utilities/detect_include_cycles.py +++ b/contrib/utilities/detect_include_cycles.py @@ -22,10 +22,10 @@ # other module partitions. # # Call this script via -# python3 detect_include_cycles.py include/deal.II/*/*.h +# python3 contrib/utilities/detect_include_cycles.py # from the top-level directory. -import sys +from glob import glob import networkx as nx @@ -47,11 +47,13 @@ def add_includes_for_file(header_file_name, G) : -# Take the list of call arguments, excluding the first that contains -# the name of the executable (i.e., this program). For each, add the -# includes as the edges of a directed graph. +# Create a list of all header files in the include folder. +filelist = glob("include/**/*.h", recursive=True) +assert filelist, "Please call the script from the top-level directory." + +# For each header file, add the includes as the edges of a directed graph. G = nx.DiGraph() -for header_file_name in sys.argv[1:] : +for header_file_name in filelist : add_includes_for_file(header_file_name, G) # Then figure out whether there are cycles and if so, print them: