mirror of
				https://github.com/XRPLF/clio.git
				synced 2025-11-04 11:55:51 +00:00 
			
		
		
		
	fix: Make fix-local-includes.sh work on file-basis (#2099)
Fix: https://github.com/XRPLF/clio/issues/2098
This commit is contained in:
		@@ -42,6 +42,15 @@ repos:
 | 
			
		||||
    hooks:
 | 
			
		||||
      - id: typos
 | 
			
		||||
 | 
			
		||||
  # Running fix-local-includes before clang-format
 | 
			
		||||
  # to ensure that the include order is correct.
 | 
			
		||||
  - repo: local
 | 
			
		||||
    hooks:
 | 
			
		||||
      - id: fix-local-includes
 | 
			
		||||
        name: Fix Local Includes
 | 
			
		||||
        entry: pre-commit-hooks/fix-local-includes.sh
 | 
			
		||||
        types: [c++]
 | 
			
		||||
        language: script
 | 
			
		||||
  - repo: https://github.com/pre-commit/mirrors-clang-format
 | 
			
		||||
    rev: v19.1.7
 | 
			
		||||
    hooks:
 | 
			
		||||
@@ -75,19 +84,13 @@ repos:
 | 
			
		||||
        name: Check Doxygen Documentation
 | 
			
		||||
        entry: pre-commit-hooks/check-doxygen-docs.sh
 | 
			
		||||
        types: [text]
 | 
			
		||||
        language: system
 | 
			
		||||
        pass_filenames: false
 | 
			
		||||
      - id: fix-local-includes
 | 
			
		||||
        name: Fix Local Includes
 | 
			
		||||
        entry: pre-commit-hooks/fix-local-includes.sh
 | 
			
		||||
        types: [c++]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
        pass_filenames: false
 | 
			
		||||
      - id: verify-commits
 | 
			
		||||
        name: Verify Commits
 | 
			
		||||
        entry: pre-commit-hooks/verify-commits.sh
 | 
			
		||||
        types: [text]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
        pass_filenames: false
 | 
			
		||||
 | 
			
		||||
  - repo: local
 | 
			
		||||
@@ -97,22 +100,22 @@ repos:
 | 
			
		||||
        entry: pre-commit-hooks/lfs/post-checkout
 | 
			
		||||
        types: [text]
 | 
			
		||||
        stages: [post-checkout]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
      - id: lfs-post-commit
 | 
			
		||||
        name: LFS Post Commit
 | 
			
		||||
        entry: pre-commit-hooks/lfs/post-commit
 | 
			
		||||
        types: [text]
 | 
			
		||||
        stages: [post-commit]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
      - id: lfs-post-merge
 | 
			
		||||
        name: LFS Post Merge
 | 
			
		||||
        entry: pre-commit-hooks/lfs/post-merge
 | 
			
		||||
        types: [text]
 | 
			
		||||
        stages: [post-merge]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
      - id: lfs-pre-push
 | 
			
		||||
        name: LFS Pre Push
 | 
			
		||||
        entry: pre-commit-hooks/lfs/pre-push
 | 
			
		||||
        types: [text]
 | 
			
		||||
        stages: [pre-push]
 | 
			
		||||
        language: system
 | 
			
		||||
        language: script
 | 
			
		||||
 
 | 
			
		||||
@@ -4,29 +4,24 @@
 | 
			
		||||
#
 | 
			
		||||
# This script checks will fix local includes in the C++ code.
 | 
			
		||||
 | 
			
		||||
# paths to fix include statements
 | 
			
		||||
sources="src tests"
 | 
			
		||||
file_path="$1"
 | 
			
		||||
 | 
			
		||||
echo "+ Fixing local includes..."
 | 
			
		||||
 | 
			
		||||
function grep_code {
 | 
			
		||||
    grep -l "${1}" ${sources} -r --include \*.hpp --include \*.cpp
 | 
			
		||||
}
 | 
			
		||||
echo "+ Fixing includes in $file_path..."
 | 
			
		||||
 | 
			
		||||
GNU_SED=$(sed --version 2>&1 | grep -q 'GNU' && echo true || echo false)
 | 
			
		||||
 | 
			
		||||
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
 | 
			
		||||
    # make all includes to be <...> style
 | 
			
		||||
    grep_code '#include ".*"' | xargs sed -i '' -E 's|#include "(.*)"|#include <\1>|g'
 | 
			
		||||
    sed -i '' -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
 | 
			
		||||
 | 
			
		||||
    # make local includes to be "..." style
 | 
			
		||||
    main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | tr '\n' '|' | sed 's/|$//' | sed 's/|/\\|/g')
 | 
			
		||||
    grep_code "#include <\($main_src_dirs\)/.*>" | xargs sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g"
 | 
			
		||||
    sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
 | 
			
		||||
else
 | 
			
		||||
    # make all includes to be <...> style
 | 
			
		||||
    grep_code '#include ".*"' | xargs sed -i -E 's|#include "(.*)"|#include <\1>|g'
 | 
			
		||||
    sed -i -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
 | 
			
		||||
 | 
			
		||||
    # make local includes to be "..." style
 | 
			
		||||
    main_src_dirs=$(find ./src -maxdepth 1 -type d  -exec basename {} \; | paste -sd '|' | sed 's/|/\\|/g')
 | 
			
		||||
    grep_code "#include <\($main_src_dirs\)/.*>" | xargs sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g"
 | 
			
		||||
    sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
 | 
			
		||||
fi
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user