From patchwork Thu Apr 21 06:43:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiko Hund X-Patchwork-Id: 2382 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director15.mail.ord1d.rsapps.net ([172.30.191.6]) by backend41.mail.ord1d.rsapps.net with LMTP id 4BTxHv6JYWIWHgAAqwncew (envelope-from ) for ; Thu, 21 Apr 2022 12:44:46 -0400 Received: from proxy7.mail.ord1d.rsapps.net ([172.30.191.6]) by director15.mail.ord1d.rsapps.net with LMTP id sFWWNv6JYWJjDAAAIcMcQg (envelope-from ) for ; Thu, 21 Apr 2022 12:44:46 -0400 Received: from smtp14.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy7.mail.ord1d.rsapps.net with LMTPS id QMIDNv6JYWJkVQAAMe1Fpw (envelope-from ) for ; Thu, 21 Apr 2022 12:44:46 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp14.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=ist.eigentlich.net X-Suspicious-Flag: YES X-Classification-ID: 598e0238-c192-11ec-96b4-525400504bae-1-1 Received: from [216.105.38.7] ([216.105.38.7:52212] helo=lists.sourceforge.net) by smtp14.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 06/15-16649-DF981626; Thu, 21 Apr 2022 12:44:46 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1nhZuC-0004MC-FY; Thu, 21 Apr 2022 16:43:45 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nhZuA-0004M1-GH for openvpn-devel@lists.sourceforge.net; Thu, 21 Apr 2022 16:43:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=SdYuK1yMMYsWVOBA6oUrYKiwEmDfYZPN8zhJghW7CXw=; b=BmaSknUki2yWab5tkPQHLtorBx UVu17XB5Vwmf9bLkXnJ4WaQbIgbkrmjaA2+mIrCISiOyipTsVVcdjpwg47h05E+5LgCilnfsnKUxW WHycGOfqqZsS85r7sENQ6mYo8/j7B5gpf9PyJK5Sgl18UKlrZWQCOsZTMHm1VuCYAlCw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=SdYuK1yMMYsWVOBA6oUrYKiwEmDfYZPN8zhJghW7CXw=; b=A/YsSc6ZaghIVqAVAwD93rEaKw 2SJyn7kAXl3WNO7tTWOcDHYmTzvhVSkQEMcr8INU8J3Xkrb0oM+fYrlhkv6ta6X0o/G/0U79LdfNp rAuv5kDxcfSRb2OS8R+/DTnbSVuwWiXHe0w7zOfdMIL5paIbR4Eb9ebSixrldbPGd/4w=; Received: from exit0.net ([85.25.119.185]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.94.2) id 1nhZu9-0007TC-Fb for openvpn-devel@lists.sourceforge.net; Thu, 21 Apr 2022 16:43:43 +0000 Received: from coruscant.fritz.box (unknown [87.123.247.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by exit0.net (Postfix) with ESMTPSA id DEF8764800AB for ; Thu, 21 Apr 2022 18:43:34 +0200 (CEST) From: Heiko Hund To: openvpn-devel@lists.sourceforge.net Date: Thu, 21 Apr 2022 18:43:30 +0200 Message-Id: <20220421164330.2483743-1-heiko@ist.eigentlich.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220420233148.2384208-1-heiko@ist.eigentlich.net> References: <20220420233148.2384208-1-heiko@ist.eigentlich.net> MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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 [...] Content analysis details: (0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1nhZu9-0007TC-Fb Subject: [Openvpn-devel] [PATCH v3] Add git pre-commit hook script to uncrustify X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox 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 Acked-By: Frank Lichtenheld --- 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 diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index db47f397..a848f899 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -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: diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-uncrustify.sh new file mode 100755 index 00000000..a5cf3d41 --- /dev/null +++ b/dev-tools/git-pre-commit-uncrustify.sh @@ -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 1