diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 75339faa4e..de0d6889e2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,8 +16,9 @@ repos: - id: gst-indent name: gst-indent language: python - entry: ./scripts/gst-indent.py - types_or: ["c"] + entry: ./scripts/gst-indent-all.py + pass_filenames: false + types_or: ["c", "c++"] # The rust hook uses cargo fmt, which requires a Cargo.toml # We use a local hook to run rustfmt directly - id: rustfmt diff --git a/scripts/gst-indent-all.py b/scripts/gst-indent-all.py new file mode 100755 index 0000000000..a933608561 --- /dev/null +++ b/scripts/gst-indent-all.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +from itertools import filterfalse +import os +import re +import subprocess +from gst_indent_common import indent + +def readfile(f): + if os.path.exists(f): + expressions = open(f, 'r', encoding='utf-8').read().splitlines() + expressions = [re.compile(i) for i in expressions] + return lambda x: any(i.match(x) for i in expressions) + else: + return None + + +def listfiles(single_glob): + if os.environ.get("CI_PROJECT_NAME"): + return subprocess.check_output(['git', 'ls-files', single_glob], + universal_newlines=True).splitlines() + else: + return subprocess.check_output(['git', 'diff-index', '--cached', '--name-only', 'HEAD', '--diff-filter=ACMR', single_glob], + universal_newlines=True).splitlines() + + +if __name__ == '__main__': + basedir = os.path.dirname(__file__) + + filter_in_c = readfile('.indentignore') + listing = listfiles('*.c') + if filter_in_c: + listing = filterfalse(filter_in_c, listing) + + for entry in listing: + indent(entry) + + filter_in_cpp = readfile('.indent_cpp_list') + listing = listfiles('*.cpp') + if filter_in_cpp: + listing = filter(filter_in_cpp, listing) + + for entry in listing: + indent(entry)