diff --git a/.github/workflows/clang_format.yml b/.github/workflows/clang_format.yml index 7b9be4909..c8257b44c 100644 --- a/.github/workflows/clang_format.yml +++ b/.github/workflows/clang_format.yml @@ -20,18 +20,4 @@ jobs: - name: Run clang-format run: | - clang-format --version - for i in $(git ls-files | grep ".[ch]pp$"); - do - clang-format -i "$i" > /dev/null 2>&1; - done - modified_files=$(git diff --name-only) - if [ -n "$modified_files" ]; - then - echo "Some files are not well formatted:" - echo $modified_files - echo "" - echo "The diff is:" - git diff - exit 1 - fi + bash bin/format.sh diff --git a/bin/format.sh b/bin/format.sh new file mode 100755 index 000000000..549ea44e7 --- /dev/null +++ b/bin/format.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -euo pipefail + +clang_format_version="19.1.3" + +script_dir="$(dirname "${BASH_SOURCE[0]}")" +venv_dir="$script_dir/../.clang-format-venv" + +if [ ! -d "$venv_dir" ]; then + python3 -m venv "$venv_dir" + source "$venv_dir/bin/activate" + pip install clang-format=="$clang_format_version" + deactivate +fi + +source "$venv_dir/bin/activate" + +# Check if the installed version matches the expected version +installed_version="$(pip show clang-format | grep Version | cut -d ' ' -f 2)" + +if [ "$installed_version" != "$clang_format_version" ]; then + echo "Error: clang-format version mismatch. Expected $clang_format_version, got $installed_version" + echo "Please remove the virtual environment and run the script again:" + echo " rm -r \"$venv_dir\" && \"$0\"" + exit 1 +fi + +clang-format --version +for i in $(git ls-files | grep ".[ch]pp$"); do + clang-format -i "$i" > /dev/null 2>&1 +done + +modified_files=$(git diff --name-only) +if [ -n "$modified_files" ]; then + echo "Some files are not well formatted:" + echo "$modified_files" + echo "" + echo "The diff is:" + git --no-pager diff + exit 1 +fi diff --git a/doc/developer_guide.md b/doc/developer_guide.md index 4537bef20..45e86b0f4 100644 --- a/doc/developer_guide.md +++ b/doc/developer_guide.md @@ -41,22 +41,16 @@ tests. ### Code formatting The project is formatted using clang-format version 12.0.1 and CI will complain if a commit isn't formatted accordingly. The `.clang-format` is at the root of -the git repository. Conveniently, `clang-format` is available via `pip`: +the git repository. Conveniently, `clang-format` is available via `pip`. +Formatting the entire code base can be done with: ```bash -python -m venv venv -source venv/bin/activate - -pip install clang-format==12.0.1 + bin/format.sh ``` +which will install the required version of clang-format in a venv called +`.clang-format-venv`. -The changed lines can be formatted with `git-clang-format`, e.g. to format all lines changed compared to master: - -```bash -git-clang-format master -``` -(add `-f` to allow formatting unstaged changes if you trust it to not destroy -your changes.) +To format only the changed files `git-clang-format` can be used. ## Releasing HighFive Before releasing a new version perform the following: diff --git a/include/highfive/H5Group.hpp b/include/highfive/H5Group.hpp index eebfcbb3e..5c09069de 100644 --- a/include/highfive/H5Group.hpp +++ b/include/highfive/H5Group.hpp @@ -57,7 +57,7 @@ class Group: public Object, } Group(Object&& o) noexcept - : Object(std::move(o)){}; + : Object(std::move(o)) {}; protected: using Object::Object; diff --git a/include/highfive/H5Reference.hpp b/include/highfive/H5Reference.hpp index 38062e987..56d1bdd9c 100644 --- a/include/highfive/H5Reference.hpp +++ b/include/highfive/H5Reference.hpp @@ -58,7 +58,7 @@ class Reference { protected: /// \brief Create a Reference from a low-level HDF5 object reference inline explicit Reference(const hobj_ref_t h5_ref) - : href(h5_ref){}; + : href(h5_ref) {}; /// \brief Create the low-level reference and store it at refptr /// diff --git a/include/highfive/bits/H5Converter_misc.hpp b/include/highfive/bits/H5Converter_misc.hpp index a89cb132a..4bc65a5c0 100644 --- a/include/highfive/bits/H5Converter_misc.hpp +++ b/include/highfive/bits/H5Converter_misc.hpp @@ -45,7 +45,7 @@ struct ShallowCopyBuffer { ShallowCopyBuffer() = delete; explicit ShallowCopyBuffer(typename std::conditional::type val) - : ptr(inspector::data(val)){}; + : ptr(inspector::data(val)) {}; hdf5_type* getPointer() const { return ptr; @@ -349,7 +349,7 @@ struct Writer::type>: public ShallowCopyBuffe explicit Writer(const T& val, const std::vector& /* dims */, const DataType& /* file_datatype */) - : super(val){}; + : super(val) {}; }; template diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index b6b74ef1f..06c94ecbe 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -114,16 +114,22 @@ TEST_CASE("Test open modes in HighFive") { CHECK_THROWS_AS(File(file_name, File::ReadWrite), FileException); // But with Create flag should be fine - { File file(file_name, File::ReadWrite | File::Create); } + { + File file(file_name, File::ReadWrite | File::Create); + } // But if its there and exclusive is given, should fail CHECK_THROWS_AS(File(file_name, File::ReadWrite | File::Excl), FileException); // ReadWrite and Excl flags are fine together (posix) std::remove(file_name.c_str()); - { File file(file_name, File::ReadWrite | File::Excl); } + { + File file(file_name, File::ReadWrite | File::Excl); + } // All three are fine as well (as long as the file does not exist) std::remove(file_name.c_str()); - { File file(file_name, File::ReadWrite | File::Create | File::Excl); } + { + File file(file_name, File::ReadWrite | File::Create | File::Excl); + } // Just a few combinations are incompatible, detected by hdf5lib CHECK_THROWS_AS(File(file_name, File::Truncate | File::Excl), FileException); @@ -132,12 +138,18 @@ TEST_CASE("Test open modes in HighFive") { CHECK_THROWS_AS(File(file_name, File::Truncate | File::Excl), FileException); // But in most cases we will truncate and that should always work - { File file(file_name, File::Truncate); } + { + File file(file_name, File::Truncate); + } std::remove(file_name.c_str()); - { File file(file_name, File::Truncate); } + { + File file(file_name, File::Truncate); + } // Last but not least, defaults should be ok - { File file(file_name); } // ReadOnly + { + File file(file_name); + } // ReadOnly } void check_access_mode(File::AccessMode mode) {