Remove the old bash based include fixer with a better python version
This commit is contained in:
parent
7fb2125c4b
commit
48e97095c7
|
@ -18,13 +18,6 @@ repos:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: mixed-line-ending
|
- id: mixed-line-ending
|
||||||
|
|
||||||
# - repo: local
|
|
||||||
# hooks:
|
|
||||||
# - id: include-fixer
|
|
||||||
# name: Fixing 'serene' includes
|
|
||||||
# language: script
|
|
||||||
# entry: ./scripts/include-fixer.sh
|
|
||||||
# files: ".*.(h|cpp)"
|
|
||||||
- repo: local
|
- repo: local
|
||||||
hooks:
|
hooks:
|
||||||
- id: include-fixer py
|
- id: include-fixer py
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
import fileinput
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
target_header_pattern = re.compile(r"\#include [<\"](.+)[\">](.*)$", re.M)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# In the `.pre-commit-config.yaml` configuration we have set:
|
||||||
|
# files: ".*.(h|cpp)"
|
||||||
|
# for this file. So it will only receive cpp and h files. No need to
|
||||||
|
# fiter.
|
||||||
|
files = sys.argv[1:]
|
||||||
|
|
||||||
|
src_dir = Path(__file__).parent.parent / "serene" / "src"
|
||||||
|
headers_files = (src_dir).rglob("*.h")
|
||||||
|
|
||||||
|
headers = []
|
||||||
|
for h in headers_files:
|
||||||
|
headers.append(str(h.relative_to(src_dir)))
|
||||||
|
|
||||||
|
# Loop over every line in every input file and write lines
|
||||||
|
# one by one and modify any file that needs to be rewritten
|
||||||
|
for line in fileinput.input(files=files, inplace=True):
|
||||||
|
m = re.match(target_header_pattern, line)
|
||||||
|
if m:
|
||||||
|
header = m.group(1)
|
||||||
|
rest = m.group(2)
|
||||||
|
if header in headers or header.startswith("serene/"):
|
||||||
|
print(f"#include \"{header}\"{rest}")
|
||||||
|
else:
|
||||||
|
print(f"#include <{header}>{rest}")
|
||||||
|
else:
|
||||||
|
print(line, end='')
|
||||||
|
|
||||||
|
# # For debugging purposes I leave this here.
|
||||||
|
# for f in files:
|
||||||
|
# with open(f, "r") as fd:
|
||||||
|
# lines = fd.readlines()
|
||||||
|
# for line in lines:
|
||||||
|
# m = re.match(target_header_pattern, line)
|
||||||
|
# if m:
|
||||||
|
# header = m.group(1)
|
||||||
|
# print("header: ", header)
|
||||||
|
# rest = m.group(2)
|
||||||
|
# if header in headers or header == "serene/config.h":
|
||||||
|
# print(f"#include \"{header}\"{rest}")
|
||||||
|
# else:
|
||||||
|
# print(f"#include <{header}>{rest}")
|
||||||
|
# else:
|
||||||
|
# print(line, end='')
|
||||||
|
# raise TypeError()
|
|
@ -1,47 +0,0 @@
|
||||||
#! /bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# function get_changed_files() {
|
|
||||||
# local add_modified
|
|
||||||
# local renamed
|
|
||||||
# add_modified=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '($1 != "D" && $1 !~ /R.+/) { print $2 }')
|
|
||||||
# renamed=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 ~ /^R.+/ { print $3 }')
|
|
||||||
|
|
||||||
# echo $add_modified | sed '/^[[:space:]]*$/d'
|
|
||||||
# echo $renamed | sed '/^[[:space:]]*$/d'
|
|
||||||
# }
|
|
||||||
|
|
||||||
function fix_includes(){
|
|
||||||
#local files=$(get_changed_files)
|
|
||||||
|
|
||||||
echo "Fixing '#include' syntax in '$1'"
|
|
||||||
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
# function clang_format_staged() {
|
|
||||||
# local files=$(get_changed_files)
|
|
||||||
|
|
||||||
# if [[ "$files" ]];
|
|
||||||
# then
|
|
||||||
# for file in $(get_changed_files)
|
|
||||||
# do
|
|
||||||
# echo "Reformatting $file..."
|
|
||||||
# clang-format -i $file
|
|
||||||
# done
|
|
||||||
# fi
|
|
||||||
|
|
||||||
# }
|
|
||||||
|
|
||||||
# function git_add_changes() {
|
|
||||||
# local files=$(get_changed_files)
|
|
||||||
|
|
||||||
# if [[ "$files" ]];
|
|
||||||
# then
|
|
||||||
# git add $files
|
|
||||||
# fi
|
|
||||||
# }
|
|
||||||
|
|
||||||
fix_includes "$1"
|
|
||||||
# clang_format_staged
|
|
||||||
# git_add_changes
|
|
Loading…
Reference in New Issue