-
Notifications
You must be signed in to change notification settings - Fork 130
Added git-clang-format as a pre-commit hook #6592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,11 @@ | |
| # - trailing-whitespace: Removes trailing whitespace from lines | ||
| # (preserves Markdown hard line breaks with --markdown-linebreak-ext=md) | ||
| # - end-of-file-fixer: Ensures files end with exactly one newline | ||
| # - git-clang-format-fix: Applies clang-format to staged C/C++/CUDA files (only the diff) | ||
| # | ||
| # Note: | ||
| # - The git-clang-format-fix hook will re-stage any changes it makes. After it runs, you will need to review the changes and re-run your commit. | ||
| # - The git-clang-format-fix can be disabled by setting SKIP=git-clang-format-fix=1 in your environment, eg. `SKIP=git-clang-format-fix=1 git commit -m "Your commit message"` | ||
|
|
||
| repos: | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
|
|
@@ -40,3 +44,30 @@ repos: | |
| - id: end-of-file-fixer | ||
| types_or: [c, c++, python, shell, yaml, cmake, markdown, text] | ||
| exclude: '^(build/|\.git/)' | ||
| - repo: local | ||
| hooks: | ||
| - id: git-clang-format-fix | ||
| name: git-clang-format (diff-only, auto-fix) | ||
| entry: | | ||
| bash -c ' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be no need for bash. I would rather use sh. |
||
| # First check if anything needs formatting | ||
| if git clang-format --staged --diff --quiet; then | ||
| # No changes needed | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "[git-clang-format] Applying formatting to staged changes..." | ||
| git clang-format --staged | ||
| # Re-add anything the formatter touched | ||
| git add -A | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will stage everything in the repo, right? We should not add back files that were not staged originally. |
||
|
|
||
| echo | ||
| echo "[git-clang-format] Changes have been applied." | ||
| echo "Please review and re-run your commit." | ||
| echo | ||
| # Force user to re-run commit so they see the changes | ||
| exit 1 | ||
| ' | ||
| language: system | ||
| types_or: [c++, c, cuda] | ||
| pass_filenames: false | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a note that it will use the .clang-format file in the root? Also what will happen if
git-clang-formatis not installed?