Skip to content

Commit

Permalink
Add git hooks, GitHub action and pre-commit hook for clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
giarve committed Mar 19, 2021
1 parent 781ba86 commit a1b6f3a
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 8 deletions.
8 changes: 6 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
# AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: true
# AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: true
# AlignEscapedNewlines: Right
# AlignOperands: true
AlignTrailingComments: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
# AllowShortBlocksOnASingleLine: false
# AllowShortCaseLabelsOnASingleLine: false
Expand Down Expand Up @@ -96,11 +99,12 @@ KeepEmptyLinesAtTheStartOfBlocks: false
# SpaceBeforeAssignmentOperators: true
# SpaceBeforeParens: ControlStatements
# SpaceInEmptyParentheses: false
# SpacesBeforeTrailingComments: 1
SpacesBeforeTrailingComments: 1
# SpacesInAngles: false
# SpacesInContainerLiterals: true
# SpacesInCStyleCastParentheses: false
# SpacesInParentheses: false
# SpacesInSquareBrackets: false
TabWidth: 4
UseCRLF: true
UseTab: Always
13 changes: 13 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Code formatting check
on: [push, pull_request]
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run clang-format style check for C/C++.
uses: jidicula/clang-format-action@v3.2.0
with:
clang-format-version: '11'
check-path: 'source'
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ install(FILES README.md DESTINATION ${INSTALL_ROOT} COMPONENT runtime)

# # Install runtime data
# install(DIRECTORY ${PROJECT_SOURCE_DIR}/data DESTINATION ${INSTALL_DATA} COMPONENT runtime)


#
# Install githooks directory
#

message("Installing git hooks at ${CMAKE_CURRENT_SOURCE_DIR} with `git config --local core.hooksPath githooks`")
execute_process(
COMMAND git config --local core.hooksPath githooks
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
2 changes: 1 addition & 1 deletion cmake/ClangDevTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Variables of interest on this file: ${CLANG_FORMAT_VERSION} and ${CLANG_FORMAT_EXECUTABLE}

# Get only C/C++ files for now
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.hpp *.h *.c)
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.hpp *.h *.c *.cc *.hh *.cxx)

# clang-tidy not implemented yet
#add_custom_target(
Expand Down
8 changes: 3 additions & 5 deletions cmake/FindClangFormat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
# ${CLANG_FORMAT_VERSION}") endif()

find_program(CLANG_FORMAT_EXECUTABLE
NAMES clang-format
clang-format-7
clang-format-8
clang-format-9
NAMES clang-format-11
clang-format
clang-format-10
clang-format-11
clang-format-12
clang-format-9
DOC "clang-format executable")
mark_as_advanced(CLANG_FORMAT_EXECUTABLE)

Expand Down
48 changes: 48 additions & 0 deletions githooks/canonicalize_filename.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

# Provide the canonicalize filename (physical filename with out any symlinks)
# like the GNU version readlink with the -f option regardless of the version of
# readlink (GNU or BSD).

# This file is part of a set of unofficial pre-commit hooks available
# at github.
# Link: https://github.com/githubbrowser/Pre-commit-hooks
# Contact: David Martin, david.martin.mailbox@googlemail.com

###########################################################
# There should be no need to change anything below this line.

# Canonicalize by recursively following every symlink in every component of the
# specified filename. This should reproduce the results of the GNU version of
# readlink with the -f option.
#
# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
canonicalize_filename () {
local target_file="$1"
local physical_directory=""
local result=""

# Need to restore the working directory after work.
local working_dir="`pwd`"

cd -- "$(dirname -- "$target_file")"
target_file="$(basename -- "$target_file")"

# Iterate down a (possible) chain of symlinks
while [ -L "$target_file" ]
do
target_file="$(readlink -- "$target_file")"
cd -- "$(dirname -- "$target_file")"
target_file="$(basename -- "$target_file")"
done

# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
physical_directory="`pwd -P`"
result="$physical_directory/$target_file"

# restore the working directory after work.
cd -- "$working_dir"

echo "$result"
}
50 changes: 50 additions & 0 deletions githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
# Git pre-commit hook that runs multiple hooks specified in $HOOKS.
# Make sure this script is executable. Bypass hooks with git commit --no-verify.

# This file is part of a set of unofficial pre-commit hooks available
# at github.
# Link: https://github.com/githubbrowser/Pre-commit-hooks
# Contact: David Martin, david.martin.mailbox@googlemail.com


###########################################################
# CONFIGURATION:
# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder
# as this script. Hooks should return 0 if successful and nonzero to cancel the
# commit. They are executed in the order in which they are listed.
#HOOKS="pre-commit-compile pre-commit-uncrustify"
HOOKS="pre-commit-clang-format"
###########################################################
# There should be no need to change anything below this line.

. "$(dirname -- "$0")/canonicalize_filename.sh"

# exit on error
set -e

# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT="$(canonicalize_filename "$0")"

# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH="$(dirname -- "$SCRIPT")"


for hook in $HOOKS
do
echo "Running hook: $hook"
# run hook if it exists
# if it returns with nonzero exit with 1 and thus abort the commit
if [ -f "$SCRIPTPATH/$hook" ]; then
"$SCRIPTPATH/$hook"
if [ $? != 0 ]; then
exit 1
fi
else
echo "Error: file $hook not found."
echo "Aborting commit. Make sure the hook is in $SCRIPTPATH and executable."
echo "You can disable it by removing it from the list in $SCRIPT."
echo "You can skip all pre-commit hooks with --no-verify (not recommended)."
exit 1
fi
done
Loading

0 comments on commit a1b6f3a

Please sign in to comment.