1414# limitations under the License.
1515# =============================================================================
1616
17- # Run clangformat and KWStyle pre-commit hooks.
17+ # Run clangformat, KWStyle, black pre-commit hooks.
1818#
1919# 'git config' is used to enable the hooks and set their configuration files.
2020# The repository .gitattributes must also enable the hooks on the targeted
@@ -32,6 +32,8 @@ do_KWStyle=$(git config --bool hooks.KWStyle) || do_KWStyle=false
3232
3333do_clangformat=$( git config --bool hooks.clangformat) || do_clangformat=true
3434
35+ do_black=$( git config --bool hooks.blackformat) || do_blackformat=true
36+
3537# -----------------------------------------------------------------------------
3638# Check if we want to run the style on a given file. Uses git attributes. If
3739# the hook.style attribute is set, then all styles are executed. If the
@@ -164,8 +166,8 @@ check_for_clangformat() {
164166 clangformat_path=$( type -p " $system_clang_format " > /dev/null) ||
165167 die " clang-format executable was not found.
166168
167- A clang-format binary will be downloaded and configured when ITK
168- is built with the BUILD_TESTING CMake configuration option enabled.
169+ A clang-format binary will be downloaded and configured when the ITKExamples
170+ Superbuild is built with the BUILD_TESTING CMake configuration option enabled.
169171
170172Alternatively, install clang-format version $clangformat_required_version or set the executable location with
171173
@@ -292,25 +294,81 @@ For more information, see
292294 done # end for changed files
293295}
294296
297+ # -----------------------------------------------------------------------------
298+ # black.
299+ check_for_black () {
300+ system_black=" "
301+ if type -p " black" > /dev/null; then
302+ system_black=" black"
303+ fi
304+ black_path=$( git config black.executable) ||
305+ black_path=$( type -p " $system_black " > /dev/null) ||
306+ die " black executable was not found.
307+
308+ A black Python formatting tool will be downloaded and configured when the ITKExamples
309+ Superbuild is built with the BUILD_TESTING CMake configuration option enabled.
310+
311+ Alternatively, install black with:
312+
313+ python -m pip install black
314+
315+ and set the executable location with
316+
317+ git config black.executable /path/to/black
318+
319+ if it is not in the system's PATH.
320+ "
321+ }
322+
323+ run_black_on_file () {
324+ " $black_path " " $1 "
325+
326+ if test $? -ne 0; then
327+ die " black style application failed."
328+ fi
329+ return 0
330+ }
331+
332+ run_black () {
333+ $do_black && check_for_black
334+ if test $? ; then
335+ have_black=true
336+ else
337+ have_black=false
338+ fi
339+
340+ git diff-index --cached --diff-filter=ACMR --name-only HEAD -- |
341+ while read f; do
342+ if run_style_on_file " $f " " black" ; then
343+ run_black_on_file " $f "
344+ fi || return
345+ done
346+ }
347+
295348# Do not run during merge commits for now.
296349if test -f " $GIT_DIR /MERGE_HEAD" ; then
297350 :
298- elif $do_clangformat ; then
299- # We use git-mergetool settings to review the clangformat changes.
300- TOOL_MODE=merge
301- . " $( git --exec-path) /git-mergetool--lib"
302- # Redefine check_unchanged because we do not need to check if the merge was
303- # successful.
304- check_unchanged () {
305- status=0
306- }
307- check_for_clangformat
308- run_clangformat || exit 1
309- # do_clangformat will run KWStyle on the files incrementally so excessive
310- # clangformat merges do not have to occur.
311- elif $do_KWStyle ; then
312- if check_for_KWStyle; then
313- run_KWStyle || exit 1
351+ else
352+ if $do_clangformat ; then
353+ # We use git-mergetool settings to review the clangformat changes.
354+ TOOL_MODE=merge
355+ . " $( git --exec-path) /git-mergetool--lib"
356+ # Redefine check_unchanged because we do not need to check if the merge was
357+ # successful.
358+ check_unchanged () {
359+ status=0
360+ }
361+ check_for_clangformat
362+ run_clangformat || exit 1
363+ # do_clangformat will run KWStyle on the files incrementally so excessive
364+ # clangformat merges do not have to occur.
365+ elif $do_KWStyle ; then
366+ if check_for_KWStyle; then
367+ run_KWStyle || exit 1
368+ fi
369+ fi
370+ if $do_black ; then
371+ run_black || exit 1
314372 fi
315373fi
316374
0 commit comments