Add git pre-commit hook script to uncrustify
PATCH 1/1 Add git pre-commit hook script to uncrustify
The script is self installing if you call it with "install" as the first parameter. Once installed as the pre-commit hook it will check files to be committed according to the rules in uncrustify.conf and abort the commit if there's formatting issues. The script produces a patch in /tmp which can be git apply'ed to fix all issues found.
The script was originally authored by David Martin [1] and slightly modified to fit our needs. At the time it had a 2-clause BSD license.
[1] https://github.com/ddddavidmartin/Pre-commit-hooks
--- dev-tools/git-pre-commit-uncrustify.sh | 146 +++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 dev-tools/git-pre-commit-uncrustify.sh
dev-tools/git-pre-commit-uncrustify.sh new file +146 -0
@@ -0,0 +1,146 @@ +#!/bin/sh + +# 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 + ROOTDIR=$(git rev-parse --show-toplevel) + HOOKSDIR="$ROOTDIR/.git/hooks" + TARGET="$HOOKSDIR/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="$PWD/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 +prefix="ovpn-fmt" +suffix="$(date +%C%y%m%d%H%M%S)" +patch="/tmp/$prefix-$suffix.patch" + +# 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 any of the extensions specified in $FILE_EXTS + if ! matches_extension "$file"; then + continue; + fi + + # escape special characters in the source filename: + # - '\': backslash needs to be escaped + # - '*': used as matching string => '*' would mean expansion + # (curiously, '?' must not be escaped) + # - '[': used as matching string => '[' would mean start of set + # - '|': used as sed split char instead of '/', so it needs to be escaped + # in the filename + # printf %s particularly important if the filename contains the % character + file_escaped_source=$(printf "%s" "$file" | sed -e 's/[\*[|]/\\&/g') + + # 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 + # --- $file timestamp + # +++ - 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'. + "$UNCRUSTIFY" -q -c "$UNCRUST_CONFIG" -f "$file" | \ + diff -u -- "$file" - | \ + sed -e "1s|--- $file_escaped_source|--- \"a/$file_escaped_target\"|" -e "2s|+++ -|+++ \"b/$file_escaped_target\"|" >> "$patch" +done + +# 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
Signature · 1 line
2.32.0
Definitive NACK due to licensing concern mentioned below.
Quoted 28 lines
Heiko Hund <[email protected]...> hat am 21.04.2022 01:31 geschrieben:
The script is self installing if you call it with "install" as the first parameter. Once installed as the pre-commit hook it will check files to be committed according to the rules in uncrustify.conf and abort the commit if there's formatting issues. The script produces a patch in /tmp which can be git apply'ed to fix all issues found.
The script was originally authored by David Martin [1] and slightly modified to fit our needs. At the time it had a 2-clause BSD license.
[1] https://github.com/ddddavidmartin/Pre-commit-hooks
Signed-off-by Heiko Hund <[email protected]...>--- dev-tools/git-pre-commit-uncrustify.sh | 146 +++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 dev-tools/git-pre-commit-uncrustify.shdev-tools/git-pre-commit-uncrustify.sh new file +2
-0@@ -0,0 +1,146 @@ +#!/bin/sh +
dev-tools/git-pre-commit-uncrustify.sh line 12
Quoted 10 lines
dev-tools/git-pre-commit-uncrustify.sh new file +10
-0+# 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
The BSD-2 license requires keeping the original copyright and license statement. While the original pre-commit-uncrustify file had none of those, there is a LICENSE file included in that repo. So I think you need to either copy that LICENSE file and reference it here or just paste it completely into this file.
dev-tools/git-pre-commit-uncrustify.sh line 21
Quoted 8 lines
dev-tools/git-pre-commit-uncrustify.sh new file +8
-0+# exit on error +set -e + + +# If called so, install this script as pre-commit hook +if [ "$1" = "install" ] ; then + ROOTDIR=$(git rev-parse --show-toplevel) + HOOKSDIR="$ROOTDIR/.git/hooks"
Actually, the correct way to find the actual hooks directory is git rev-parse --git-path hooks
That takes all configuration like $GIT_DIR and core.hooksDir into account.
dev-tools/git-pre-commit-uncrustify.sh line 25
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+ TARGET="$HOOKSDIR/pre-commit" + + if [ -e "$TARGET" ] ; then + printf "$TARGET file exists. Won't overwrite.\n"
Any reason we're using printf here instead of echo? The latter would avoid the ugly line endings.
[...]
dev-tools/git-pre-commit-uncrustify.sh line 77
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+# create a filename to store our generated patch +prefix="ovpn-fmt" +suffix="$(date +%C%y%m%d%H%M%S)" +patch="/tmp/$prefix-$suffix.patch"
Are insecure tmp files still a concern? Because this is definitely an insecure tmp file...
[...]
Regards,
--
Frank Lichtenheld
Hi,
Quoted 1 line · Frank Lichtenheld
Definitive NACK due to licensing concern mentioned below.
Streams crossed here, but I saw your NAK before pushing.
So, yes, all valid concerns and I withdraw my ACK.
Quoted 7 lines
[...]
dev-tools/git-pre-commit-uncrustify.sh line 77
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+# create a filename to store our generated patch +prefix="ovpn-fmt" +suffix="$(date +%C%y%m%d%H%M%S)" +patch="/tmp/$prefix-$suffix.patch"Are insecure tmp files still a concern? Because this is definitely an insecure tmp file...
Especially this one is something I only noticed after I sent my mail - so this definitely needs to to mktemp-style temp files, or a private tmp directory (in the local repo?)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi all,
Did you consider to use the pre-commit framework [1] written in Python? There is a maintained hook for uncrustify [2]. This would allow it to easily integrate other linters/checks. See the list of supported hooks [3].
I would understand if you don't like to add yet another dependency, but in the case of Python, it's already part of the development environment.
[1] https://pre-commit.com/
[2] https://github.com/pocc/pre-commit-hooks
[3] https://pre-commit.com/hooks.html
David
Quoted 34 lines · Gert Doering
Hi,
Quoted 1 line · Frank Lichtenheld
Definitive NACK due to licensing concern mentioned below.
Streams crossed here, but I saw your NAK before pushing.
So, yes, all valid concerns and I withdraw my ACK.
Quoted 7 lines
[...]
dev-tools/git-pre-commit-uncrustify.sh line 77
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+# create a filename to store our generated patch +prefix="ovpn-fmt" +suffix="$(date +%C%y%m%d%H%M%S)" +patch="/tmp/$prefix-$suffix.patch"Are insecure tmp files still a concern? Because this is definitely an insecure tmp file...
Especially this one is something I only noticed after I sent my mail - so this definitely needs to to mktemp-style temp files, or a private tmp directory (in the local repo?)
gert
--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh MistressGert Doering - Munich, Germany [email protected]...
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Hi David
Quoted 4 lines · On Donnerstag, 21. April 2022 13:41:58 CEST David Schneider wrote:
Did you consider to use the pre-commit framework [1] written in Python? There is a maintained hook for uncrustify [2]. This would allow it to easily integrate other linters/checks. See the list of supported hooks [3].
I stumbled across this, but it seemed too big for the purpose. Was looking for something small that could integrate into the dev-tools. That said, I don't think it holds anyone back from using the pre-commit stuff with the repo's uncrustify.conf. So, if you already do use it please feel free to add some short howto to the developer documentation (something I missed doing, but will add in v2).
Heiko
Hi
Quoted 5 lines · On Donnerstag, 21. April 2022 10:16:03 CEST Gert Doering wrote:
Quoted 1 line · Frank Lichtenheld
Definitive NACK due to licensing concern mentioned below.
Streams crossed here, but I saw your NAK before pushing. So, yes, all valid concerns and I withdraw my ACK.
Will post a v2 with the license inline .
Quoted 10 lines
Quoted 7 lines
dev-tools/git-pre-commit-uncrustify.sh line 77
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+# create a filename to store our generated patch +prefix="ovpn-fmt" +suffix="$(date +%C%y%m%d%H%M%S)" +patch="/tmp/$prefix-$suffix.patch"Are insecure tmp files still a concern? Because this is definitely an insecure tmp file...
Especially this one is something I only noticed after I sent my mail - so this definitely needs to to mktemp-style temp files, or a private tmp directory (in the local repo?)
I don't really see the need for secure tmp files here, as there's no chance to exploit anything by racing for the file(name). Or am I just not creative enough?
Cheers,
Heiko
Hi,
Quoted 7 lines · Heiko Hund
Quoted 3 lines
Especially this one is something I only noticed after I sent my mail - so this definitely needs to to mktemp-style temp files, or a private tmp directory (in the local repo?)
I don't really see the need for secure tmp files here, as there's no chance to exploit anything by racing for the file(name). Or am I just not creative enough?
The classic approach is "create a symlink from /tmp/$known_name to something you want overwritten", and on next "git commit", some file owned by the committer gets overwritten (or created as empty)
Since an attacker can not control the content *and* the comitter won't do this as root (at least I hope so) *and* you actually need to have different users on the machine, the risk is comparatively low, but still, this is a code construct that should be avoided.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi Frank
Quoted 8 lines · On Donnerstag, 21. April 2022 10:01:29 CEST Frank Lichtenheld wrote:
dev-tools/git-pre-commit-uncrustify.sh line 21
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+# If called so, install this script as pre-commit hook +if [ "$1" = "install" ] ; then + ROOTDIR=$(git rev-parse --show-toplevel) + HOOKSDIR="$ROOTDIR/.git/hooks"Actually, the correct way to find the actual hooks directory is git rev-parse --git-path hooks
That takes all configuration like $GIT_DIR and core.hooksDir into account.
Nice one, will add in v2.
Quoted 7 lines
dev-tools/git-pre-commit-uncrustify.sh line 25
Quoted 4 lines
dev-tools/git-pre-commit-uncrustify.sh new file +4
-0+ TARGET="$HOOKSDIR/pre-commit" + + if [ -e "$TARGET" ] ; then + printf "$TARGET file exists. Won't overwrite.\n"Any reason we're using printf here instead of echo? The latter would avoid the ugly line endings.
Consistency. The script uses printf everywhere.
Heiko
I've minimally tested this
- clone the repo
- run "sh dev-tools/git-pre-commit-uncrustify.sh install"
- edit a file, introduce some of our typical things (tab, c++ comment)
- try to commit, get a refusal message, with diff, plus pointer-to-patch
- run "git apply $thatpatch"
- errors fixed, commit applies
.. and it does exactly what it says on the lid. Great stuff.
Your patch has been applied to the master branch.
commit 01424c932115a1b159f304e97a2dead254dc4c6d
Author: Heiko Hund Date: Thu Apr 21 01:31:48 2022 +0200
Add git pre-commit hook script to uncrustify
Signed-off-by: Heiko Hund <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24124.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v2 1/1 Add git pre-commit hook script to uncrustify
In reply to Heiko HundThe script is self installing if you call it with "install" as the first parameter. Once installed as the pre-commit hook it will check files to be committed according to the rules in uncrustify.conf and abort the commit if there's formatting issues. The script produces a patch in /tmp which can be git apply'ed to fix all issues found.
The script was originally authored by David Martin [1] and slightly modified to fit our needs. At the time it had a 2-clause BSD license.
[1] https://github.com/ddddavidmartin/Pre-commit-hooks
--- CONTRIBUTING.rst | 7 + dev-tools/git-pre-commit-uncrustify.sh | 171 +++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100755 dev-tools/git-pre-commit-uncrustify.sh
CONTRIBUTING.rst +7 -0
@@ -18,6 +18,13 @@ When sending patches to "openvpn-devel" the subject line should be prefixed with git-format-patch or sent using git-send-email. Try to split large patches into small, atomic pieces to make reviews easier. +Please make sure that the source code formatting follows the guidelines. While +there are no human readable guidelines, a configuration file for uncrustify +(http://uncrustify.sourceforge.net/) can be found at dev-tools/uncrustify.conf. +There is also a git pre-commit hook script, which runs uncrustify automatically +and lets you reformat your code conveniently, when needed. +To install the hook simply run: dev-tools/git-pre-commit-uncrustify.sh install + If you want quick feedback on a patch before sending it to openvpn-devel mailing list, you can visit the #openvpn-devel channel on irc.libera.chat. Note that you need to be logged in to Libera to join the channel:
dev-tools/git-pre-commit-uncrustify.sh new file +171 -0
171 lines not on this page · raw message
Signature · 1 line
2.32.0
Quoted 2 lines
Heiko Hund <[email protected]...> hat am 21.04.2022 15:58 geschrieben:
[...]
dev-tools/git-pre-commit-uncrustify.sh line 48
Quoted 5 lines
dev-tools/git-pre-commit-uncrustify.sh new file +5
-0+ROOTDIR=$(git rev-parse --show-toplevel) + +# If called so, install this script as pre-commit hook +if [ "$1" = "install" ] ; then + HOOKSDIR="$ROOTDIR/$(git rev-parse --git-path hooks)"
Hmm, no. While in a default install $(git rev-parse --git-path hooks) will be a relative path, there is not guarantee that it is (e.g. if core.hooksPath is an absolute path). I see not reason why you need an absolute path here. A relative path should be completely fine?
Apart from this, the script seems to work fine and the licensing issue is fixed.
Regards,
--
Frank Lichtenheld
Quoted 13 lines · On Donnerstag, 21. April 2022 16:20:14 CEST Frank Lichtenheld wrote:
Quoted 1 line
Heiko Hund <[email protected]...> hat am 21.04.2022 15:58 geschrieben:
[...]
dev-tools/git-pre-commit-uncrustify.sh line 48
Quoted 5 lines
dev-tools/git-pre-commit-uncrustify.sh new file +5
-0+ROOTDIR=$(git rev-parse --show-toplevel) + +# If called so, install this script as pre-commit hook +if [ "$1" = "install" ] ; then + HOOKSDIR="$ROOTDIR/$(git rev-parse --git-path hooks)"Hmm, no. While in a default install $(git rev-parse --git-path hooks) will be a relative path, there is not guarantee that it is (e.g. if core.hooksPath is an absolute path). I see not reason why you need an absolute path here. A relative path should be completely fine?
I thought it spits out a path relative to the git root, but it turns out to be relative to the current dir, so you're right. Will post a v3, but will wait for more feedback before I send it.
Heiko
PATCH v3 1/1 Add git pre-commit hook script to uncrustify
In reply to Heiko HundThe script is self installing if you call it with "install" as the first parameter. Once installed as the pre-commit hook it will check files to be committed according to the rules in uncrustify.conf and abort the commit if there's formatting issues. The script produces a patch in /tmp which can be git apply'ed to fix all issues found.
The script was originally authored by David Martin [1] and slightly modified to fit our needs. At the time it had a 2-clause BSD license.
[1] https://github.com/ddddavidmartin/Pre-commit-hooks
--- CONTRIBUTING.rst | 8 ++ dev-tools/git-pre-commit-uncrustify.sh | 168 +++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100755 dev-tools/git-pre-commit-uncrustify.sh
CONTRIBUTING.rst +8 -0
@@ -18,6 +18,14 @@ When sending patches to "openvpn-devel" the subject line should be prefixed with git-format-patch or sent using git-send-email. Try to split large patches into small, atomic pieces to make reviews easier. +Please make sure that the source code formatting follows the guidelines at +https://community.openvpn.net/openvpn/wiki/CodeStyle. Automated checking can be +done with uncrustify (http://uncrustify.sourceforge.net/) and the configuration +file which can be found in the git repository at dev-tools/uncrustify.conf. +There is also a git pre-commit hook script, which runs uncrustify automatically +each time you commit and lets you format your code conveniently, if needed. +To install the hook simply run: dev-tools/git-pre-commit-uncrustify.sh install + If you want quick feedback on a patch before sending it to openvpn-devel mailing list, you can visit the #openvpn-devel channel on irc.libera.chat. Note that you need to be logged in to Libera to join the channel:
dev-tools/git-pre-commit-uncrustify.sh new file +168 -0
168 lines not on this page · raw message
Signature · 1 line
2.32.0
Quoted 223 lines
Heiko Hund <[email protected]...> hat am 21.04.2022 18:43 geschrieben:
The script is self installing if you call it with "install" as the first parameter. Once installed as the pre-commit hook it will check files to be committed according to the rules in uncrustify.conf and abort the commit if there's formatting issues. The script produces a patch in /tmp which can be git apply'ed to fix all issues found.
The script was originally authored by David Martin [1] and slightly modified to fit our needs. At the time it had a 2-clause BSD license.
[1] https://github.com/ddddavidmartin/Pre-commit-hooks
Signed-off-by Heiko Hund <[email protected]...>--- CONTRIBUTING.rst | 8 ++ dev-tools/git-pre-commit-uncrustify.sh | 168 +++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100755 dev-tools/git-pre-commit-uncrustify.shCONTRIBUTING.rst +8
-0@@ -18,6 +18,14 @@ When sending patches to "openvpn-devel" the subject line should be prefixed with git-format-patch or sent using git-send-email. Try to split large patches into small, atomic pieces to make reviews easier. +Please make sure that the source code formatting follows the guidelines at +https://community.openvpn.net/openvpn/wiki/CodeStyle. Automated checking can be +done with uncrustify (http://uncrustify.sourceforge.net/) and the configuration +file which can be found in the git repository at dev-tools/uncrustify.conf. +There is also a git pre-commit hook script, which runs uncrustify automatically +each time you commit and lets you format your code conveniently, if needed. +To install the hook simply run: dev-tools/git-pre-commit-uncrustify.sh install + If you want quick feedback on a patch before sending it to openvpn-devel mailing list, you can visit the #openvpn-devel channel on irc.libera.chat. Note that you need to be logged in to Libera to join the channel:dev-tools/git-pre-commit-uncrustify.sh new file +168
-0@@ -0,0 +1,168 @@ +#!/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) + +# 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 any of the extensions specified in $FILE_EXTS + if ! matches_extension "$file"; then + continue; + fi + + # escape special characters in the source filename: + # - '\': backslash needs to be escaped + # - '*': used as matching string => '*' would mean expansion + # (curiously, '?' must not be escaped) + # - '[': used as matching string => '[' would mean start of set + # - '|': used as sed split char instead of '/', so it needs to be escaped + # in the filename + # printf %s particularly important if the filename contains the % character + file_escaped_source=$(printf "%s" "$file" | sed -e 's/[\*[|]/\\&/g') + + # 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 + # --- $file timestamp + # +++ - 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'. + "$UNCRUSTIFY" -q -c "$UNCRUST_CONFIG" -f "$file" | \ + diff -u -- "$file" - | \ + sed -e "1s|--- $file_escaped_source|--- \"a/$file_escaped_target\"|" -e "2s|+++ -|+++ \"b/$file_escaped_target\"|" >> "$patch" +done + +# 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 1Signature · 8 lines
2.32.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
--
Frank Lichtenheld
Thanks for fixing the minor issues. Re-tested, still "great stuff!!" ;-)
Your patch has been applied to the master branch.
commit 237ba888684e9b035e3b05d661dd44011be0dcb2
Author: Heiko Hund Date: Thu Apr 21 18:43:30 2022 +0200
Add git pre-commit hook script to uncrustify
Signed-off-by: Heiko Hund <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24141.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering