Skip to content

Commit

Permalink
Clang-format git hook pre-commit enforcement (#760)
Browse files Browse the repository at this point in the history
* Added clang-format git pre-commit hook

Signed-off-by: The MathWorks, Inc. <alchrist@mathworks.com>

* Updated README

* Updated help instructions to include '--style=file'

* Added '--style=file' flag in actual pre-commit hook check

* Updated README

* Removed unnecessary code in pre-commit hook

* Updated README

Signed-off-by: The MathWorks, Inc. <alchrist@mathworks.com>
  • Loading branch information
achristoforides committed Dec 5, 2022
1 parent 8899851 commit 196dec7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# Run clang-format pre-commit hook
sh .githooks/pre-commit-clang-format-hook
27 changes: 27 additions & 0 deletions .githooks/pre-commit-clang-format-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

printf "Running clang-format pre-commit hook... ";

CLANG_FORMAT=$(which clang-format)

if [ ! -x "$CLANG_FORMAT" ]; then
printf "Error.\n";
printf "The clang-format cannot be found in your PATH. Please ensure clang-format is installed and on the PATH."
exit 1
fi

# Get clang-format output and store it for later checking
clangformatoutput=$(git clang-format --style=file --diff --staged -q)

# Redirect output to stderr
exec 1>&2

if [ "$clangformatoutput" != "" ];
then
printf "Error.\n";
printf "clang-format detected that changes have been made and are not properly formatted!\n"
printf "Please run \`git clang-format --style=file --staged\`, re-stage modified files, and commit.\n"
exit 1
else
printf "Success.\n";
fi
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ foreach(policy ${policies_new})
endif()
endforeach()

#-----------------------------------------------------------------------------
# Setup git hooks
#-----------------------------------------------------------------------------
find_package(Git)
if (GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
execute_process(COMMAND ${GIT_EXECUTABLE} config core.hooksPath "${PROJECT_SOURCE_DIR}/.githooks"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_CONFIGMOD_RESULT)
if (NOT GIT_CONFIGMOD_RESULT EQUAL "0")
message(WARNING "Command `git config core.hooksPath ${PROJECT_SOURCE_DIR}/.githooks` failed with ${GIT_CONFIGMOD_RESULT}")
endif()
endif()

#-----------------------------------------------------------------------------
# Check minimum required compiler versions
#------------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,22 @@ file for details about the contribution process.
:target: https://codecov.io/gh/cppmicroservices/CppMicroServices/branch/master
.. |Code Coverage Status (development)| image:: https://img.shields.io/codecov/c/github/CppMicroServices/CppMicroServices/development.svg?style=flat-square
:target: https://codecov.io/gh/cppmicroservices/CppMicroServices/branch/development

Git Hooks General Information
-----------------------------

The CppMicroServices repository defines its git hooks in the `.githooks` directory. This directory is
set as the directory for git hooks via executing `git config core.hooksPath <path>` in our `CMakeLists.txt` file.

Git Hooks Failure Help
----------------------

If the clang-format pre-commit hook fails because `clang-format` is not installed, please install it and
put it on the path. Similarly, if `git-clang-format` is not installed, do the same. `git-clang-format` comes
with the LLVM distribution of `clang-format`.

If this is not feasible for you, you can specify `--no-verify` when committing your changes. This is heavily discouraged
and you must provide a justification as to why you are unable to format your commit.

We reserve the right to reject any pull requests that are not properly formatted and do not have a
valid justification specified.
4 changes: 4 additions & 0 deletions third_party/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"DisableFormat": true,
"SortIncludes": false,
}

0 comments on commit 196dec7

Please sign in to comment.