Add Git hooks to format code automically#197
Conversation
YohannDudouit
left a comment
There was a problem hiding this comment.
I am not super versed into this type of configuration files, so I used AI to help me make sense of this PR. I thought the AI comments exposed real concerns, so I thought I would share them to get your feedback, the most worrying concern is the one about committing unstaged changes IMHO:
"The general direction looks useful: adding a local pre-commit hook for clang-format, Ruff, and optional clang-tidy should reduce formatting noise and catch simple issues earlier.
I have one main concern before approving: CMake currently mutates the local Git configuration by setting core.hooksPath to .githooks during configure. This can overwrite an existing developer hook configuration, despite the comment saying it respects overrides. Could we make this opt-in, or only set core.hooksPath when it is currently unset?
I would also like us to consider the behavior of auto-formatting plus git add: this can accidentally stage unrelated unstaged hunks in partially staged files. At minimum this should be documented; a safer design would be to fail and ask the user to re-run formatting, or to preserve unstaged changes.
Minor points: should Ruff be a dev dependency rather than a runtime dependency, should the final Ruff check avoid suppressing diagnostics, and can we confirm .githooks/pre-commit is executable?"
|
@YohannDudouit The first point is a valid concern that I also had. I implemented that "CMake hook" to make sure everyone follows the rules but it does modify your Git config so it is somewhat invasive. I am open to make it optional where developers will have to add the hook manually with About the auto format comment: I feel Indeed |
|
@lpottier The part that worried me is: "this can accidentally stage unrelated unstaged hunks in partially staged files", if this is a real risk I would be on the fence, I often have uncommitted work that I keep local on don't commit, so this is worrying to me, as it might result in committing work unintentionally. |
|
@YohannDudouit |
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
f3ff628 to
430bcad
Compare
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
This PR adds Git hooks for Clang format and Clang Tidy.
To add the hook, you can run:
git config core.hooksPath .githooksOtherwise, our CMake will add the hook automatically upon building.
the
.githook/pre-commitscript leverages a few environment variables:AMS_SKIP_TIDYif set, skip tidy, still run format. Example:AMS_SKIP_TIDY=1 git commitAMS_BUILD_DIRpoints to the build directory if you are not using something likebuild-*orbuild. Mandatory forclang-tidy.If you want to deactivate all hooks:
git commit --no-verify.If you want to run the hook without committing:
.githook/pre-commitUpdate: I pushed a script to reformat the codebase:
scripts/run-code-quality.shThe git hook is now calling this script and do not perform any changes by default.