diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 524b2e0b8..19f7e59e6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -93,6 +93,12 @@ repos: types: [c++] language: script + - id: fix-pragma-once + name: fix missing '#pragma once' declarations in header files + language: python + entry: ./pre-commit-hooks/fix_pragma_once.py + files: \.(h|hpp)$ + - repo: https://github.com/pre-commit/mirrors-clang-format rev: dd18dad857d6133e90bbe478f4f2f22ec0030269 # frozen: v22.1.5 hooks: diff --git a/pre-commit-hooks/fix_pragma_once.py b/pre-commit-hooks/fix_pragma_once.py new file mode 100755 index 000000000..08a505b6d --- /dev/null +++ b/pre-commit-hooks/fix_pragma_once.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +""" +Adds "#pragma once" to the top of header files that don't already have it. + +Usage: ./bin/pre-commit/fix_pragma_once.py ... +""" + +import sys +from pathlib import Path + +PRAGMA_ONCE = "#pragma once\n\n" + + +def fix_pragma_once(path: Path) -> bool: + original = path.read_text(encoding="utf-8") + if PRAGMA_ONCE not in original: + path.write_text(PRAGMA_ONCE + original, encoding="utf-8") + return False + return True + + +def main() -> int: + files = [Path(f) for f in sys.argv[1:]] + success = True + + for path in files: + success &= fix_pragma_once(path) + + return 0 if success else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/rpc/handlers/LedgerIndex.hpp b/src/rpc/handlers/LedgerIndex.hpp index 65c709c55..682fadc0e 100644 --- a/src/rpc/handlers/LedgerIndex.hpp +++ b/src/rpc/handlers/LedgerIndex.hpp @@ -1,4 +1,5 @@ #pragma once + #include "data/BackendInterface.hpp" #include "rpc/JS.hpp" #include "rpc/common/Specs.hpp" diff --git a/tests/common/data/cassandra/FakesAndMocks.hpp b/tests/common/data/cassandra/FakesAndMocks.hpp index 4e805751d..2f36e5c2e 100644 --- a/tests/common/data/cassandra/FakesAndMocks.hpp +++ b/tests/common/data/cassandra/FakesAndMocks.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "data/cassandra/Error.hpp" #include "data/cassandra/impl/AsyncExecutor.hpp" diff --git a/tests/common/migration/TestMigrators.hpp b/tests/common/migration/TestMigrators.hpp index 82a0fd5e9..290d534e3 100644 --- a/tests/common/migration/TestMigrators.hpp +++ b/tests/common/migration/TestMigrators.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "util/MockMigrationBackend.hpp" #include "util/config/ObjectView.hpp" diff --git a/tests/common/util/LoggerBuffer.hpp b/tests/common/util/LoggerBuffer.hpp index 8c62f66a7..138736f56 100644 --- a/tests/common/util/LoggerBuffer.hpp +++ b/tests/common/util/LoggerBuffer.hpp @@ -1,4 +1,5 @@ #pragma once + #include "util/StringBuffer.hpp" #include diff --git a/tests/common/util/MockRPCEngine.hpp b/tests/common/util/MockRPCEngine.hpp index c395ecaa1..90fb35808 100644 --- a/tests/common/util/MockRPCEngine.hpp +++ b/tests/common/util/MockRPCEngine.hpp @@ -1,4 +1,5 @@ #pragma once + #include "rpc/common/Types.hpp" #include "util/Spawn.hpp" #include "web/Context.hpp" diff --git a/tests/common/util/MockRandomGenerator.hpp b/tests/common/util/MockRandomGenerator.hpp index f2f124dbb..b9f5eacf4 100644 --- a/tests/common/util/MockRandomGenerator.hpp +++ b/tests/common/util/MockRandomGenerator.hpp @@ -1,4 +1,5 @@ #pragma once + #include "util/Random.hpp" #include