I just found out my script was woefully incomplete. It is probably still 
wrong even with this change but needs

current_source=$(git diff --staged --name-only | egrep 
"(\.[chF]'|\.F90|\.hpp|\.cpp|\.cxx|\.cu)" | tr '\n' ' ')





> On Jun 18, 2023, at 7:51 PM, Barry Smith <bsm...@petsc.dev> wrote:
> 
> 
>   Jacobi,
> 
>   Thanks for the reminder about using hooks for this purpose. Perhaps git 
> hooks are not understood well enough at the time by the masses (that is, me), 
> or perhaps hooks were presented as THE solution, not as a tool for helping 
> users to get to the solution?  That is, git-hooks as a way to enforce rules 
> on the pushed code versus git hooks used by individuals to make their 
> workflow easier or less bug-prone?
> 
>   I found your https://gitlab.com/petsc/petsc/-/merge_requests/4063 but 
> couldn't find any discussion of pre-commits for clang-format in my mail 
> searches.
> 
>    I discovered a .git/hooks/pre-commit~ which presumably you sent around 
> (listed at the bottom of this email).  It seems to work as advertised, warns 
> me, and prevents my commit of badly formatted code. 
> 
>    I modified it to 
> 
> /bin/bash
> 
> current_source=$(git diff --staged --name-only)
> badsource=$(make -e GITSRC="${current_source}" checkbadSource 2>&1)
> 
> if [ "$badsource" != "" ]
> then
>     echo "git commit aborted due to violations of basic source code 
> formatting"
>     make -e GITSRC="${current_source}" checkbadSource
>     exit 1
> fi
> 
> clangformatout=$(git clang-format --diff --staged -q)
> 
> if [ "$clangformatout" != "" ]
> then
>     echo "git commit aborted. Use 'git add -u ; git clang-format --staged' to 
> fix source code formatting"
>     exit 1
> fi
> 
> and it seems to suit my needs.
> 
> Thanks again for the suggestion
> 
> /bin/bash
>   
> 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  
>   
> # Test clang-format  
> clangformatout=$(git clang-format --diff --staged -q)  
>   
> # Redirect output to stderr.  
> exec 1>&2  
>   
> if [ "$clangformatout" != "" ]  
> then
>     echo "Format error!"
>     echo "Use git clang-format"
>     exit 1
> fi
> 
> 
>> On Jun 18, 2023, at 4:37 PM, Jacob Faibussowitsch <jacob....@gmail.com> 
>> wrote:
>> 
>>> I want to automatically run make checkclangformat checkbadSource before 
>>> every git push (because I am an idiot and have too many silly failed CIs 
>>> due to bad source). 
>> 
>> What you *actually* want are git hooks 
>> (https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) combined with 
>> clang-format-diff 
>> (https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting).
>> 
>> I floated the idea of adding git hooks ages ago, but it was shot down for 
>> some reason.
>> 
>> Best regards,
>> 
>> Jacob Faibussowitsch
>> (Jacob Fai - booss - oh - vitch)
>> 
>>> On Jun 18, 2023, at 16:33, Barry Smith <bsm...@petsc.dev> wrote:
>>> 
>>> 
>>> I have never understood how to do make rule dependencies when one is not 
>>> explicitly generating new files from old ones. For example, .o from .c
>>> 
>>> I want to automatically run make checkclangformat checkbadSource before 
>>> every git push (because I am an idiot and have too many silly failed CIs 
>>> due to bad source). 
>>> 
>>> Currently, we use git ls-files and check every single file each time, even 
>>> though I have not changed all 5,000 source files in PETSc before a push.
>>> Is there someone who understands make dependencies when no files are 
>>> generated who could reduce make checkclangformat and checkbadSource to 
>>> under a second?
>>> 
>>> 
>>> Thanks
>>> 
>>> Barry
>>> 
>> 
> 

Reply via email to