Using Formatter in a Git pre-commit hook?

30 views
Skip to first unread message

Axel Aguado

unread,
Dec 7, 2022, 11:54:13 AM12/7/22
to Verible Users
Hello,
I am evaluating Verible for use in my organization.  One of the use cases I was hoping the formatter tool would support is usage in a pre-commit hook.

We have other tools that do various formatting checks on our non-systemverilog source code.  As a simple example:
  • The tool checks that no source file has trailing whitespace on any line.  
  • If trailing whitespace is found by the tool, the user is alerted that their code failed the check, and the commit is rejected. 
  • The user then has the option of re-running the tool in "fix it for me" mode, which removes the whitespace.  
    • They could alternatively manually fix the whitespace issues if they decide to do that for whatever reason - the point here is that user's code isn't touched without their explicit direction.
  • The user then stages the fixed code and attempts their commit again
  • The tool doesn't find any trailing whitespace, and allows the commit to proceed.
When evaluating the Verible formatter, however, it appears the tool simply runs and makes changes, either editing the file inplace or dumping the output to the terminal.

Is there any way that we can make the formatter tool behave as I describe above?  

Thanks in advance for any advice.

David Fang

unread,
Dec 7, 2022, 7:37:44 PM12/7/22
to Axel Aguado, Verible Users
Hi there,

There's one script that follows a similar flow:

Roughly, this does:
* detect which revision control system is being used, like git
* detect locally modified files
* format all files into temporary outputs
* create a diff from unformatted vs. formatted files
* pass the diff through a generic interactive patch program, and prompt you to accept changes one hunk at a time
(Up to the user to `git add` these changes after formatting.)

The formatter should already delete trailing whitespace, so the diff vs. formatted code is the check.

Does this help?


--
You received this message because you are subscribed to the Google Groups "Verible Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to verible-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/verible-users/9123d9e3-04bc-41fa-bac2-1e74dbc87d58n%40googlegroups.com.

David Fang

unread,
Dec 7, 2022, 7:40:53 PM12/7/22
to Axel Aguado, Verible Users
Also: https://github.com/chipsalliance/verible/blob/master/verilog/tools/formatter/git-verible-verilog-format.sh

This script runs the formatter on only changed lines, which can help reduce churn on untouched lines.

Axel Aguado

unread,
Dec 8, 2022, 11:27:41 AM12/8/22
to Verible Users
Thanks very much!  That does help quite a bit.  I think I can use those scripts as a starting point.

Really appreciate your reply.  Have a great day.

Reply all
Reply to author
Forward
0 new messages