Message-ID: <2ff2762d-44d5-8192-9e90-e56f3c0c2367@...2181...> Date: Wed, 18 May 2022 15:35:58 +0200 MIME-Version: 1.0 Content-Language: en-US References: <20220517210121.1312072-1-heiko@...2662...> From: Antonio Quartulli In-Reply-To: <20220517210121.1312072-1-heiko@...2662...> Subject: Re: [Openvpn-devel] [PATCH] pre-commit: uncrustify based on staged changes List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit To: Heiko Hund , openvpn-devel@lists.sourceforge.net Hi, On 17/05/2022 23:01, Heiko Hund wrote: > Previously the generated patch was based on the file(s) in the working > directory. This is a problem if you have not to be commited changes > there and these changes fix formatting issues that exist in the staging > area. This effectively circumventes the script from rejecting the > commit. > > An example: > git add file.c > git commit > ... pre-commit hooks complains about formatting ... > ... you fix the file manually, forget to git add ... > git commit > ... succeeds, even though the commit still has issues ... > > Signed-off-by: Heiko Hund I tested the case described in the example (that I actually reported to Heiko) and I can confirm it is now fixed. Normal workflow seems to be still working as well :-) minor comments below > --- > dev-tools/git-pre-commit-uncrustify.sh | 25 +++++++++---------------- > 1 file changed, 9 insertions(+), 16 deletions(-) > > diff --git a/dev-tools/git-pre-commit-uncrustify.sh b/dev-tools/git-pre-commit-uncrustify.sh > index a5cf3d41..9851c212 100755 > --- a/dev-tools/git-pre-commit-uncrustify.sh > +++ b/dev-tools/git-pre-commit-uncrustify.sh > @@ -97,6 +97,7 @@ 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 > @@ -106,21 +107,11 @@ 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 > + # does not match the extensions .c or .h is this unrelated? > 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') > - this seems unrelated too? > # escape special characters in the target filename: > # phase 1 (characters escaped in the output diff): > # - '\': backslash needs to be escaped in the output diff > @@ -136,15 +127,17 @@ do > > # 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 > + # --- - timestamp > + # +++ $tmpout timestamp same? > # 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" > + 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" -- Antonio Quartulli