Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ zap.pl crlf=input
# ghostflow-director GitHub automatic check for maximum repository file size
* hooks-max-size=100000
Modules/Numerics/FEM/src/dsrc2c.c hooks-max-size=260000
Modules/ThirdParty/** hooks-max-size=300000
Modules/ThirdParty/** hooks-max-size=300000 hooks.style=
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Modules/ThirdParty/NIFTI/src/nifti/nifti2/nifti2_io.c hooks-max-size=400000
Modules/ThirdParty/VNL/src/vxl/v3p/netlib/triangle.c hooks-max-size=670000
Modules/Filtering/Denoising/include/itkPatchBasedDenoisingImageFilter.hxx hooks-max-size=120000
Expand Down
2 changes: 1 addition & 1 deletion CMake/ITKModuleClangFormat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
option(ITK_USE_CLANG_FORMAT "Enable the use of clang-format for coding style formatting." ${BUILD_TESTING})
mark_as_advanced(ITK_USE_CLANG_FORMAT)

if(BUILD_TESTING AND ITK_USE_CLANG_FORMAT AND NOT CMAKE_CROSSCOMPILING)
if(BUILD_TESTING AND ITK_USE_CLANG_FORMAT AND NOT CMAKE_CROSSCOMPILING AND NOT ITK_FORBID_DOWNLOADS)
include(${ITK_CMAKE_DIR}/../Utilities/ClangFormat/DownloadClangFormat.cmake)
endif()
118 changes: 71 additions & 47 deletions Utilities/Hooks/pre-commit-style.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,9 @@ die() {
exit 1
}

do_KWStyle=$(git config --bool hooks.KWStyle) || do_KWStyle=false
do_KWStyle=$(git config --bool hooks.KWStyle) || do_KWStyle=true

do_clangformat=$(git config --bool hooks.clangformat) || do_clangformat=false

if [ "$(uname)" == "Darwin" ]; then
console="</dev/tty" # MacOSX
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
console="</dev/tty" # Linuxes
else
echo "uname: "$(uname -a)
console="" # Windows (Msys, MinGW, Cygwin etc)
fi
do_clangformat=$(git config --bool hooks.clangformat) || do_clangformat=true

#-----------------------------------------------------------------------------
# Check if we want to run the style on a given file. Uses git attributes. If
Expand All @@ -54,7 +45,16 @@ run_style_on_file() {
if git diff-index --cached HEAD -- "$1" | grep -q '^:...... 160000'; then
return 1
fi
if ! git diff-files --quiet -- "$1"; then
style=$(git check-attr hooks.style -- "$1" |
sed 's/^[^:]*: hooks.style: //')
has_style_attr=1
case "$style" in
'unset') has_style_attr=1 ;;
'set') has_style_attr=0 ;;
'unspecified') has_style_attr=1 ;;
*) echo ",$style," | grep -iq ",$2," && has_style_attr=0 ;;
esac
if ! git diff-files --quiet -- "$1" && test $has_style_attr -eq 0; then
# A way to always allow skipping.
skip_unstaged=$(git config --bool hooks.styleSkipUnstaged) ||
skip_unstaged=false
Expand All @@ -79,15 +79,7 @@ Allow skipping the style check for this commit with
fi
return 1
fi
style=$(git check-attr hooks.style -- "$1" |
sed 's/^[^:]*: hooks.style: //')
case "$style" in
'unset') return 1 ;;
'set') return 0 ;;
'unspecified') return 1 ;;
*) echo ",$style," | grep -iq ",$2," && return 0 ;;
esac
return 1
return $has_style_attr
}

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -140,6 +132,7 @@ run_KWStyle_on_file() {
Line numbers in the errors shown refer to the file:
${1}.kws"
fi
return 0
}

run_KWStyle() {
Expand All @@ -154,14 +147,37 @@ run_KWStyle() {
#-----------------------------------------------------------------------------
# clangformat.
check_for_clangformat() {
clangformat_path=$(git config hooks.clangformat.path) ||
clangformat_path=$(which clangformat) ||
die "clangformat executable was not found.
clangformat_required_version=8.0.1
system_tools="
clang-format-$clangformat_required_version
clang-format
"
for tool in $system_tools; do
if type -p "$tool" >/dev/null; then
system_clang_format="$tool"
break
fi
done
clangformat_path=$(git config clangFormat.binary) ||
clangformat_path=$(type -p "$system_clang_format" >/dev/null) ||
die "clang-format executable was not found.
Comment thread
hjmjohnson marked this conversation as resolved.

Please install clang-format version $clangformat_required_version or set the executable location with

Please install clang-format version 8.0 or set the executable location with
git config clangFormat.binary /path/to/clang-format
"
if ! "$clangformat_path" --version | grep "clang-format version $clangformat_required_version" >/dev/null 2>/dev/null; then
die "clang-format version $clangformat_required_version is required (exactly)

git config hooks.clangformat.path /path/to/clang-format
"
Set the path the clang-format $clangformat_required_version executable with

git config clangFormat.binary /path/to/clang-format

or disable the clang-format pre-commit hook with

git config hooks.clangformat false
"
fi
}

run_clang_format_check_attr()
Expand All @@ -188,36 +204,44 @@ run_clangformat_on_file() {
# We temporarily change MERGED because the file might already be open, and
# the text editor may complain.
MERGED="$NEW_MERGED"
cp -- "$BACKUP" "$MERGED"
cp -- "$BACKUP" "$LOCAL"

run_clang_format_check_attr "$LOCAL" "$REMOTE" "$ERROR_LOG"
clang_format_status=$?
if [ $clang_format_status -ne 0 ]; then
mv -- "$BACKUP" "$OLD_MERGED"

if test "$merge_keep_temporaries" = "false"; then
if $merge_keep_temporaries; then
rm -f -- "$LOCAL" "$REMOTE" "$BACKUP"
fi
die "error when running clangformat on $OLD_MERGED"
die "error when running clang-format on $OLD_MERGED"
fi
cp -- "$REMOTE" "$MERGED"

if test $(git hash-object -- "$LOCAL") != $(git hash-object -- "$REMOTE"); then
if [ "$(uname)" == "Darwin" ]; then
run_merge_tool "$merge_tool" "false" </dev/tty
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
run_merge_tool "$merge_tool" "false" </dev/tty
else
run_merge_tool "$merge_tool" "false"
fi
if [ $? -ne 0 ]; then
mv -- "$BACKUP" "$OLD_MERGED"

if test $(git hash-object -- "$LOCAL") != $(git hash-object -- "$REMOTE") &&
! run_merge_tool "$merge_tool" "false" $console; then
mv -- "$BACKUP" "$OLD_MERGED"
if $merge_keep_temporaries; then
rm -f -- "$LOCAL" "$REMOTE" "$BACKUP" "$NEW_MERGED"
fi

if test "$merge_keep_temporaries" = "false"; then
rm -f -- "$LOCAL" "$REMOTE" "$BACKUP" "$NEW_MERGED"
die "clang-format merge of $OLD_MERGED failed
Error log: $ERROR_LOG"
fi

die "clang-format merge of $OLD_MERGED failed
Error log: $ERROR_LOG"
fi

mv -- "$NEW_MERGED" "$OLD_MERGED"
MERGED="$OLD_MERGED"

if test "$merge_keep_backup" = "true"; then
if $merge_keep_backup; then
mv -- "$BACKUP" "$MERGED.orig"
else
rm -- "$BACKUP"
Expand All @@ -226,7 +250,7 @@ run_clangformat_on_file() {
git add -- "$MERGED"
rm -f -- "$LOCAL" "$REMOTE" "$BACKUP" "$ERROR_LOG" "$NEW_MERGED"

fi # end if run clangformat on file
fi # end if run clang-format on file

if $do_KWStyle &&
$have_KWStyle &&
Expand All @@ -239,6 +263,13 @@ run_clangformat_on_file() {
}

run_clangformat() {
$do_KWStyle && check_for_KWStyle
if test $?; then
have_KWStyle=true
else
have_KWStyle=false
fi

merge_tool=$(get_merge_tool "$merge_tool") || die "Merge tool not configured.

Set the merge tool with
Expand All @@ -254,13 +285,6 @@ For more information, see
while read MERGED; do
run_clangformat_on_file "$MERGED" || return
done # end for changed files

$do_KWStyle && check_for_KWStyle
if test $?; then
have_KWStyle=false
else
have_KWStyle=true
fi
}

# Do not run during merge commits for now.
Expand Down
6 changes: 5 additions & 1 deletion Utilities/SetupForDevelopment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ messages for interaction with Gerrit. Also, removing the "gerrit" and "stage" re
git remote remove stage
fi

# Style hook configuration
git config hooks.KWStyle.conf "Utilities/KWStyle/ITK.kws.xml"
git config hooks.KWStyle.overwriteRulesConf "Utilities/KWStyle/ITKOverwrite.txt"

# Make sure we are inside the repository.
cd "$(echo "$0"|sed 's/[^/]*$//')"/..

Expand Down Expand Up @@ -103,5 +107,5 @@ echo -e "Git version $git_version is OK.\n"


# Record the version of this setup so Hooks/pre-commit can check it.
SetupForDevelopment_VERSION=6
SetupForDevelopment_VERSION=7
git config hooks.SetupForDevelopment ${SetupForDevelopment_VERSION}