Skip to content

Add Git hooks to format code automically#197

Open
lpottier wants to merge 6 commits into
developfrom
features/git-hooks
Open

Add Git hooks to format code automically#197
lpottier wants to merge 6 commits into
developfrom
features/git-hooks

Conversation

@lpottier

@lpottier lpottier commented Jun 4, 2026

Copy link
Copy Markdown
Member

This PR adds Git hooks for Clang format and Clang Tidy.

To add the hook, you can run: git config core.hooksPath .githooks
Otherwise, our CMake will add the hook automatically upon building.

the .githook/pre-commit script leverages a few environment variables:

  • AMS_SKIP_TIDY if set, skip tidy, still run format. Example: AMS_SKIP_TIDY=1 git commit
  • AMS_BUILD_DIR points to the build directory if you are not using something like build-* or build. Mandatory for clang-tidy.

If you want to deactivate all hooks: git commit --no-verify.
If you want to run the hook without committing: .githook/pre-commit

Update: I pushed a script to reformat the codebase: scripts/run-code-quality.sh
The git hook is now calling this script and do not perform any changes by default.

@YohannDudouit YohannDudouit left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?"

@lpottier

lpottier commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@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 git config core.hooksPath .githooks. Thoughts?

About the auto format comment: I feel clang-format should be automatic, I am more on the fence for clang-tidy. In general, the workflow for a developer is more to code and then manually run .githook/pre-commit to get the feedback from all the tools, fix the code and then commit.

Indeed ruff should be a dev dependency. I will add that.

@lpottier lpottier requested a review from rblake-llnl June 8, 2026 21:23
@YohannDudouit

Copy link
Copy Markdown
Collaborator

@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.

@lpottier

lpottier commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@YohannDudouit clang-format cannot lead to accidentally stage unrelated unstaged hunks in partially staged files to the best of knowledge but clang-tidy might. Let me make remove the --fix flag in clang-tidy so it does not autofix anything.

lpottier added 4 commits June 23, 2026 11:40
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>
lpottier added 2 commits June 23, 2026 13:30
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Signed-off-by: Loic Pottier <pottier1@llnl.gov>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants