From: flichtenheld (C. Review) <ge...@op...> - 2025-06-27 09:39:55
|
Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email to review the following change. Change subject: Update git-pre-commit-uncrustify.sh to handle clang-format ...................................................................... Update git-pre-commit-uncrustify.sh to handle clang-format Rename it as well, since it is not specific to uncrustify anymore. Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Signed-off-by: Frank Lichtenheld <fr...@li...> --- R dev-tools/git-pre-commit-format.sh 1 file changed, 32 insertions(+), 14 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/70/1070/1 diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-format.sh similarity index 81% rename from dev-tools/git-pre-commit-uncrustify.sh rename to dev-tools/git-pre-commit-format.sh index 9851c21..6e1ac71 100755 --- a/dev-tools/git-pre-commit-uncrustify.sh +++ b/dev-tools/git-pre-commit-format.sh @@ -2,6 +2,7 @@ # Copyright (c) 2015, David Martin # 2022, Heiko Hund +# 2025, Frank Lichtenheld # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -26,10 +27,12 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# git pre-commit hook that runs an Uncrustify stylecheck. +# git pre-commit hook that runs a stylecheck. # Features: # - abort commit when commit does not comply with the style guidelines # - create a patch of the proposed style changes +# - use clang-format or uncrustify depending on presence of .clang-format +# config file # # More info on Uncrustify: http://uncrustify.sourceforge.net/ @@ -77,27 +80,42 @@ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi -UNCRUSTIFY=$(command -v uncrustify) -UNCRUST_CONFIG="$(git rev-parse --show-toplevel)/dev-tools/uncrustify.conf" +TOPDIR="$(git rev-parse --show-toplevel)" +if [ -e "${TOPDIR}/.clang-format" ]; then + TOOL=clang-format + TOOL_BIN=$(command -v clang-format) + TOOL_CMD="$TOOL_BIN" -# make sure the config file and executable are correctly set -if [ ! -f "$UNCRUST_CONFIG" ] ; then - printf "Error: uncrustify config file not found.\n" - printf "Expected to find it at $UNCRUST_CONFIG.\n" - printf "Aborting commit.\n" - exit 1 + # Allow to use in parallel with pre-commit + if [ $(basename "$0") = "pre-commit.legacy" ]; then + echo "Skipping clang-format check in favor of pre-commit" + exit 0 + fi +else + TOOL=uncrustify + TOOL_BIN=$(command -v uncrustify) + UNCRUST_CONFIG="${TOPDIR}/dev-tools/uncrustify.conf" + TOOL_CMD="$TOOL_BIN -q -l C -c $UNCRUST_CONFIG" + + # make sure the config file is correctly set + if [ ! -f "$UNCRUST_CONFIG" ] ; then + printf "Error: uncrustify config file not found.\n" + printf "Expected to find it at $UNCRUST_CONFIG.\n" + printf "Aborting commit.\n" + exit 1 + fi fi -if [ -z "$UNCRUSTIFY" ] ; then - printf "Error: uncrustify executable not found.\n" +if [ -z "$TOOL_BIN" ] ; then + printf "Error: $TOOL executable not found.\n" printf "Is it installed and in your \$PATH?\n" printf "Aborting commit.\n" exit 1 fi # create a filename to store our generated patch -patch=$(mktemp /tmp/ovpn-fmt-XXXXXX) -tmpout=$(mktemp /tmp/uncrustify-XXXXXX) +patch=$(mktemp /tmp/ovpn-fmt-patch-XXXXXX) +tmpout=$(mktemp /tmp/ovpn-fmt-tmp-XXXXXX) # create one patch containing all changes to the files # sed to remove quotes around the filename, if inserted by the system @@ -131,7 +149,7 @@ # +++ $tmpout timestamp # to both lines working on the same file and having a a/ and b/ prefix. # Else it can not be applied with 'git apply'. - git show ":$file" | "$UNCRUSTIFY" -q -l C -c "$UNCRUST_CONFIG" -o "$tmpout" + git show ":$file" | $TOOL_CMD > "$tmpout" git show ":$file" | diff -u -- - "$tmpout" | \ sed -e "1s|--- -|--- \"b/$file_escaped_target\"|" -e "2s|+++ $tmpout|+++ \"a/$file_escaped_target\"|" >> "$patch" done -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Gerrit-Change-Number: 1070 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-Attention: plaisthos <arn...@rf...> Gerrit-MessageType: newchange |
From: cron2 (C. Review) <ge...@op...> - 2025-08-04 09:19:18
|
Attention is currently required from: flichtenheld, plaisthos. cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email ) Change subject: Update git-pre-commit-uncrustify.sh to handle clang-format ...................................................................... Patch Set 3: Code-Review+2 -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Gerrit-Change-Number: 1070 Gerrit-PatchSet: 3 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-Attention: plaisthos <arn...@rf...> Gerrit-Attention: flichtenheld <fr...@li...> Gerrit-Comment-Date: Mon, 04 Aug 2025 09:19:09 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment |
From: Gert D. <ge...@gr...> - 2025-08-04 09:27:53
|
From: Frank Lichtenheld <fr...@li...> Rename it as well, since it is not specific to uncrustify anymore. Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Signed-off-by: Frank Lichtenheld <fr...@li...> Acked-by: Gert Doering <ge...@gr...> --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1070 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering <ge...@gr...> diff --git a/dev-tools/git-pre-commit-format.sh b/dev-tools/git-pre-commit-format.sh new file mode 100755 index 0000000..6e1ac71 --- /dev/null +++ b/dev-tools/git-pre-commit-format.sh @@ -0,0 +1,179 @@ +#!/bin/sh + +# Copyright (c) 2015, David Martin +# 2022, Heiko Hund +# 2025, Frank Lichtenheld +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +# git pre-commit hook that runs a stylecheck. +# Features: +# - abort commit when commit does not comply with the style guidelines +# - create a patch of the proposed style changes +# - use clang-format or uncrustify depending on presence of .clang-format +# config file +# +# More info on Uncrustify: http://uncrustify.sourceforge.net/ + +# This file was taken from a set of unofficial pre-commit hooks available +# at https://github.com/ddddavidmartin/Pre-commit-hooks and modified to +# fit the openvpn project's needs + +# exit on error +set -e + + +# If called so, install this script as pre-commit hook +if [ "$1" = "install" ] ; then + TARGET="$(git rev-parse --git-path hooks)/pre-commit" + + if [ -e "$TARGET" ] ; then + printf "$TARGET file exists. Won't overwrite.\n" + printf "Aborting installation.\n" + exit 1 + fi + + read -p "Install as $TARGET? [y/N] " INPUT + [ "$INPUT" = "y" ] || exit 0 + cp "$0" "$TARGET" + chmod +x $TARGET + exit 0 +fi + +# check whether the given file matches any of the set extensions +matches_extension() { + local filename="$(basename -- "$1")" + local extension=".${filename##*.}" + local ext + + for ext in .c .h ; do [ "$ext" = "$extension" ] && return 0; done + + return 1 +} + +# necessary check for initial commit +if git rev-parse --verify HEAD >/dev/null 2>&1 ; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +TOPDIR="$(git rev-parse --show-toplevel)" +if [ -e "${TOPDIR}/.clang-format" ]; then + TOOL=clang-format + TOOL_BIN=$(command -v clang-format) + TOOL_CMD="$TOOL_BIN" + + # Allow to use in parallel with pre-commit + if [ $(basename "$0") = "pre-commit.legacy" ]; then + echo "Skipping clang-format check in favor of pre-commit" + exit 0 + fi +else + TOOL=uncrustify + TOOL_BIN=$(command -v uncrustify) + UNCRUST_CONFIG="${TOPDIR}/dev-tools/uncrustify.conf" + TOOL_CMD="$TOOL_BIN -q -l C -c $UNCRUST_CONFIG" + + # make sure the config file is correctly set + if [ ! -f "$UNCRUST_CONFIG" ] ; then + printf "Error: uncrustify config file not found.\n" + printf "Expected to find it at $UNCRUST_CONFIG.\n" + printf "Aborting commit.\n" + exit 1 + fi +fi + +if [ -z "$TOOL_BIN" ] ; then + printf "Error: $TOOL executable not found.\n" + printf "Is it installed and in your \$PATH?\n" + printf "Aborting commit.\n" + exit 1 +fi + +# create a filename to store our generated patch +patch=$(mktemp /tmp/ovpn-fmt-patch-XXXXXX) +tmpout=$(mktemp /tmp/ovpn-fmt-tmp-XXXXXX) + +# create one patch containing all changes to the files +# sed to remove quotes around the filename, if inserted by the system +# (done sometimes, if the filename contains special characters, like the quote itself) +git diff-index --cached --diff-filter=ACMR --name-only $against -- | \ +sed -e 's/^"\(.*\)"$/\1/' | \ +while read file +do + # ignore file if we do check for file extensions and the file + # does not match the extensions .c or .h + if ! matches_extension "$file"; then + continue; + fi + + # escape special characters in the target filename: + # phase 1 (characters escaped in the output diff): + # - '\': backslash needs to be escaped in the output diff + # - '"': quote needs to be escaped in the output diff if present inside + # of the filename, as it used to bracket the entire filename part + # phase 2 (characters escaped in the match replacement): + # - '\': backslash needs to be escaped again for sed itself + # (i.e. double escaping after phase 1) + # - '&': would expand to matched string + # - '|': used as sed split char instead of '/' + # printf %s particularly important if the filename contains the % character + file_escaped_target=$(printf "%s" "$file" | sed -e 's/[\"]/\\&/g' -e 's/[\&|]/\\&/g') + + # uncrustify our sourcefile, create a patch with diff and append it to our $patch + # The sed call is necessary to transform the patch from + # --- - timestamp + # +++ $tmpout timestamp + # to both lines working on the same file and having a a/ and b/ prefix. + # Else it can not be applied with 'git apply'. + git show ":$file" | $TOOL_CMD > "$tmpout" + git show ":$file" | diff -u -- - "$tmpout" | \ + sed -e "1s|--- -|--- \"b/$file_escaped_target\"|" -e "2s|+++ $tmpout|+++ \"a/$file_escaped_target\"|" >> "$patch" +done + +rm -f "$tmpout" + +# if no patch has been generated all is ok, clean up the file stub and exit +if [ ! -s "$patch" ] ; then + rm -f "$patch" + exit 0 +fi + +# a patch has been created, notify the user and exit +printf "Formatting of some code does not follow the project guidelines.\n" + +if [ $(wc -l < $patch) -gt 80 ] ; then + printf "The file $patch contains the necessary fixes.\n" +else + printf "Here's the patch that fixes the formatting:\n\n" + cat $patch +fi + +printf "\nYou can apply these changes with:\n git apply $patch\n" +printf "(from the root directory of the repository) and then commit again.\n" +printf "\nAborting commit.\n" + +exit 1 diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-uncrustify.sh deleted file mode 100755 index 9851c21..0000000 --- a/dev-tools/git-pre-commit-uncrustify.sh +++ /dev/null @@ -1,161 +0,0 @@ -#!/bin/sh - -# Copyright (c) 2015, David Martin -# 2022, Heiko Hund -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -# git pre-commit hook that runs an Uncrustify stylecheck. -# Features: -# - abort commit when commit does not comply with the style guidelines -# - create a patch of the proposed style changes -# -# More info on Uncrustify: http://uncrustify.sourceforge.net/ - -# This file was taken from a set of unofficial pre-commit hooks available -# at https://github.com/ddddavidmartin/Pre-commit-hooks and modified to -# fit the openvpn project's needs - -# exit on error -set -e - - -# If called so, install this script as pre-commit hook -if [ "$1" = "install" ] ; then - TARGET="$(git rev-parse --git-path hooks)/pre-commit" - - if [ -e "$TARGET" ] ; then - printf "$TARGET file exists. Won't overwrite.\n" - printf "Aborting installation.\n" - exit 1 - fi - - read -p "Install as $TARGET? [y/N] " INPUT - [ "$INPUT" = "y" ] || exit 0 - cp "$0" "$TARGET" - chmod +x $TARGET - exit 0 -fi - -# check whether the given file matches any of the set extensions -matches_extension() { - local filename="$(basename -- "$1")" - local extension=".${filename##*.}" - local ext - - for ext in .c .h ; do [ "$ext" = "$extension" ] && return 0; done - - return 1 -} - -# necessary check for initial commit -if git rev-parse --verify HEAD >/dev/null 2>&1 ; then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -UNCRUSTIFY=$(command -v uncrustify) -UNCRUST_CONFIG="$(git rev-parse --show-toplevel)/dev-tools/uncrustify.conf" - -# make sure the config file and executable are correctly set -if [ ! -f "$UNCRUST_CONFIG" ] ; then - printf "Error: uncrustify config file not found.\n" - printf "Expected to find it at $UNCRUST_CONFIG.\n" - printf "Aborting commit.\n" - exit 1 -fi - -if [ -z "$UNCRUSTIFY" ] ; then - printf "Error: uncrustify executable not found.\n" - printf "Is it installed and in your \$PATH?\n" - printf "Aborting commit.\n" - exit 1 -fi - -# create a filename to store our generated patch -patch=$(mktemp /tmp/ovpn-fmt-XXXXXX) -tmpout=$(mktemp /tmp/uncrustify-XXXXXX) - -# create one patch containing all changes to the files -# sed to remove quotes around the filename, if inserted by the system -# (done sometimes, if the filename contains special characters, like the quote itself) -git diff-index --cached --diff-filter=ACMR --name-only $against -- | \ -sed -e 's/^"\(.*\)"$/\1/' | \ -while read file -do - # ignore file if we do check for file extensions and the file - # does not match the extensions .c or .h - if ! matches_extension "$file"; then - continue; - fi - - # escape special characters in the target filename: - # phase 1 (characters escaped in the output diff): - # - '\': backslash needs to be escaped in the output diff - # - '"': quote needs to be escaped in the output diff if present inside - # of the filename, as it used to bracket the entire filename part - # phase 2 (characters escaped in the match replacement): - # - '\': backslash needs to be escaped again for sed itself - # (i.e. double escaping after phase 1) - # - '&': would expand to matched string - # - '|': used as sed split char instead of '/' - # printf %s particularly important if the filename contains the % character - file_escaped_target=$(printf "%s" "$file" | sed -e 's/[\"]/\\&/g' -e 's/[\&|]/\\&/g') - - # uncrustify our sourcefile, create a patch with diff and append it to our $patch - # The sed call is necessary to transform the patch from - # --- - timestamp - # +++ $tmpout timestamp - # to both lines working on the same file and having a a/ and b/ prefix. - # Else it can not be applied with 'git apply'. - git show ":$file" | "$UNCRUSTIFY" -q -l C -c "$UNCRUST_CONFIG" -o "$tmpout" - git show ":$file" | diff -u -- - "$tmpout" | \ - sed -e "1s|--- -|--- \"b/$file_escaped_target\"|" -e "2s|+++ $tmpout|+++ \"a/$file_escaped_target\"|" >> "$patch" -done - -rm -f "$tmpout" - -# if no patch has been generated all is ok, clean up the file stub and exit -if [ ! -s "$patch" ] ; then - rm -f "$patch" - exit 0 -fi - -# a patch has been created, notify the user and exit -printf "Formatting of some code does not follow the project guidelines.\n" - -if [ $(wc -l < $patch) -gt 80 ] ; then - printf "The file $patch contains the necessary fixes.\n" -else - printf "Here's the patch that fixes the formatting:\n\n" - cat $patch -fi - -printf "\nYou can apply these changes with:\n git apply $patch\n" -printf "(from the root directory of the repository) and then commit again.\n" -printf "\nAborting commit.\n" - -exit 1 |
From: Gert D. <ge...@gr...> - 2025-08-04 09:34:50
|
So this brings us a new pre-commit-hook that will (when installed!) look at $ROOT/.clang-format and decide whether to run "clang-format" or "uncrustify". Which is important for people like me that have to switch back and forth between branches with different formatting rules (2.6 and earlier will stay on uncrustify, 2.7-ish and master-after-2.7 will have clang-format). So, thanks for that. (After the big reformatting, folks using this git-hook need to remember to actually enable it with "dev-tools/git-pre-commit-format.sh install") Your patch has been applied to the master branch. commit d18f8e4e60aef5802006f7d33642b0cecbdfb255 Author: Frank Lichtenheld Date: Mon Aug 4 11:27:34 2025 +0200 Update git-pre-commit-uncrustify.sh to handle clang-format Signed-off-by: Frank Lichtenheld <fr...@li...> Acked-by: Gert Doering <ge...@gr...> Message-Id: <202...@gr...> URL: https://www.mail-archive.com/ope...@li.../msg32497.html Signed-off-by: Gert Doering <ge...@gr...> -- kind regards, Gert Doering |
From: cron2 (C. Review) <ge...@op...> - 2025-08-04 09:35:02
|
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email ) Change subject: Update git-pre-commit-uncrustify.sh to handle clang-format ...................................................................... Update git-pre-commit-uncrustify.sh to handle clang-format Rename it as well, since it is not specific to uncrustify anymore. Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Signed-off-by: Frank Lichtenheld <fr...@li...> Acked-by: Gert Doering <ge...@gr...> Message-Id: <202...@gr...> URL: https://www.mail-archive.com/ope...@li.../msg32497.html Signed-off-by: Gert Doering <ge...@gr...> --- R dev-tools/git-pre-commit-format.sh 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-format.sh similarity index 81% rename from dev-tools/git-pre-commit-uncrustify.sh rename to dev-tools/git-pre-commit-format.sh index 9851c21..6e1ac71 100755 --- a/dev-tools/git-pre-commit-uncrustify.sh +++ b/dev-tools/git-pre-commit-format.sh @@ -2,6 +2,7 @@ # Copyright (c) 2015, David Martin # 2022, Heiko Hund +# 2025, Frank Lichtenheld # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -26,10 +27,12 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# git pre-commit hook that runs an Uncrustify stylecheck. +# git pre-commit hook that runs a stylecheck. # Features: # - abort commit when commit does not comply with the style guidelines # - create a patch of the proposed style changes +# - use clang-format or uncrustify depending on presence of .clang-format +# config file # # More info on Uncrustify: http://uncrustify.sourceforge.net/ @@ -77,27 +80,42 @@ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi -UNCRUSTIFY=$(command -v uncrustify) -UNCRUST_CONFIG="$(git rev-parse --show-toplevel)/dev-tools/uncrustify.conf" +TOPDIR="$(git rev-parse --show-toplevel)" +if [ -e "${TOPDIR}/.clang-format" ]; then + TOOL=clang-format + TOOL_BIN=$(command -v clang-format) + TOOL_CMD="$TOOL_BIN" -# make sure the config file and executable are correctly set -if [ ! -f "$UNCRUST_CONFIG" ] ; then - printf "Error: uncrustify config file not found.\n" - printf "Expected to find it at $UNCRUST_CONFIG.\n" - printf "Aborting commit.\n" - exit 1 + # Allow to use in parallel with pre-commit + if [ $(basename "$0") = "pre-commit.legacy" ]; then + echo "Skipping clang-format check in favor of pre-commit" + exit 0 + fi +else + TOOL=uncrustify + TOOL_BIN=$(command -v uncrustify) + UNCRUST_CONFIG="${TOPDIR}/dev-tools/uncrustify.conf" + TOOL_CMD="$TOOL_BIN -q -l C -c $UNCRUST_CONFIG" + + # make sure the config file is correctly set + if [ ! -f "$UNCRUST_CONFIG" ] ; then + printf "Error: uncrustify config file not found.\n" + printf "Expected to find it at $UNCRUST_CONFIG.\n" + printf "Aborting commit.\n" + exit 1 + fi fi -if [ -z "$UNCRUSTIFY" ] ; then - printf "Error: uncrustify executable not found.\n" +if [ -z "$TOOL_BIN" ] ; then + printf "Error: $TOOL executable not found.\n" printf "Is it installed and in your \$PATH?\n" printf "Aborting commit.\n" exit 1 fi # create a filename to store our generated patch -patch=$(mktemp /tmp/ovpn-fmt-XXXXXX) -tmpout=$(mktemp /tmp/uncrustify-XXXXXX) +patch=$(mktemp /tmp/ovpn-fmt-patch-XXXXXX) +tmpout=$(mktemp /tmp/ovpn-fmt-tmp-XXXXXX) # create one patch containing all changes to the files # sed to remove quotes around the filename, if inserted by the system @@ -131,7 +149,7 @@ # +++ $tmpout timestamp # to both lines working on the same file and having a a/ and b/ prefix. # Else it can not be applied with 'git apply'. - git show ":$file" | "$UNCRUSTIFY" -q -l C -c "$UNCRUST_CONFIG" -o "$tmpout" + git show ":$file" | $TOOL_CMD > "$tmpout" git show ":$file" | diff -u -- - "$tmpout" | \ sed -e "1s|--- -|--- \"b/$file_escaped_target\"|" -e "2s|+++ $tmpout|+++ \"a/$file_escaped_target\"|" >> "$patch" done -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Gerrit-Change-Number: 1070 Gerrit-PatchSet: 4 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-MessageType: merged |
From: cron2 (C. Review) <ge...@op...> - 2025-08-04 09:35:05
|
cron2 has uploaded a new patch set (#4) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: Update git-pre-commit-uncrustify.sh to handle clang-format ...................................................................... Update git-pre-commit-uncrustify.sh to handle clang-format Rename it as well, since it is not specific to uncrustify anymore. Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Signed-off-by: Frank Lichtenheld <fr...@li...> Acked-by: Gert Doering <ge...@gr...> Message-Id: <202...@gr...> URL: https://www.mail-archive.com/ope...@li.../msg32497.html Signed-off-by: Gert Doering <ge...@gr...> --- R dev-tools/git-pre-commit-format.sh 1 file changed, 32 insertions(+), 14 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/70/1070/4 diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-format.sh similarity index 81% rename from dev-tools/git-pre-commit-uncrustify.sh rename to dev-tools/git-pre-commit-format.sh index 9851c21..6e1ac71 100755 --- a/dev-tools/git-pre-commit-uncrustify.sh +++ b/dev-tools/git-pre-commit-format.sh @@ -2,6 +2,7 @@ # Copyright (c) 2015, David Martin # 2022, Heiko Hund +# 2025, Frank Lichtenheld # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -26,10 +27,12 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# git pre-commit hook that runs an Uncrustify stylecheck. +# git pre-commit hook that runs a stylecheck. # Features: # - abort commit when commit does not comply with the style guidelines # - create a patch of the proposed style changes +# - use clang-format or uncrustify depending on presence of .clang-format +# config file # # More info on Uncrustify: http://uncrustify.sourceforge.net/ @@ -77,27 +80,42 @@ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi -UNCRUSTIFY=$(command -v uncrustify) -UNCRUST_CONFIG="$(git rev-parse --show-toplevel)/dev-tools/uncrustify.conf" +TOPDIR="$(git rev-parse --show-toplevel)" +if [ -e "${TOPDIR}/.clang-format" ]; then + TOOL=clang-format + TOOL_BIN=$(command -v clang-format) + TOOL_CMD="$TOOL_BIN" -# make sure the config file and executable are correctly set -if [ ! -f "$UNCRUST_CONFIG" ] ; then - printf "Error: uncrustify config file not found.\n" - printf "Expected to find it at $UNCRUST_CONFIG.\n" - printf "Aborting commit.\n" - exit 1 + # Allow to use in parallel with pre-commit + if [ $(basename "$0") = "pre-commit.legacy" ]; then + echo "Skipping clang-format check in favor of pre-commit" + exit 0 + fi +else + TOOL=uncrustify + TOOL_BIN=$(command -v uncrustify) + UNCRUST_CONFIG="${TOPDIR}/dev-tools/uncrustify.conf" + TOOL_CMD="$TOOL_BIN -q -l C -c $UNCRUST_CONFIG" + + # make sure the config file is correctly set + if [ ! -f "$UNCRUST_CONFIG" ] ; then + printf "Error: uncrustify config file not found.\n" + printf "Expected to find it at $UNCRUST_CONFIG.\n" + printf "Aborting commit.\n" + exit 1 + fi fi -if [ -z "$UNCRUSTIFY" ] ; then - printf "Error: uncrustify executable not found.\n" +if [ -z "$TOOL_BIN" ] ; then + printf "Error: $TOOL executable not found.\n" printf "Is it installed and in your \$PATH?\n" printf "Aborting commit.\n" exit 1 fi # create a filename to store our generated patch -patch=$(mktemp /tmp/ovpn-fmt-XXXXXX) -tmpout=$(mktemp /tmp/uncrustify-XXXXXX) +patch=$(mktemp /tmp/ovpn-fmt-patch-XXXXXX) +tmpout=$(mktemp /tmp/ovpn-fmt-tmp-XXXXXX) # create one patch containing all changes to the files # sed to remove quotes around the filename, if inserted by the system @@ -131,7 +149,7 @@ # +++ $tmpout timestamp # to both lines working on the same file and having a a/ and b/ prefix. # Else it can not be applied with 'git apply'. - git show ":$file" | "$UNCRUSTIFY" -q -l C -c "$UNCRUST_CONFIG" -o "$tmpout" + git show ":$file" | $TOOL_CMD > "$tmpout" git show ":$file" | diff -u -- - "$tmpout" | \ sed -e "1s|--- -|--- \"b/$file_escaped_target\"|" -e "2s|+++ $tmpout|+++ \"a/$file_escaped_target\"|" >> "$patch" done -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1070?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I03195c21807cdef0a2f903f424982ec29a555103 Gerrit-Change-Number: 1070 Gerrit-PatchSet: 4 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-MessageType: newpatchset |