When --staged (or --cached) use the index for formatting as well, not just for the line numbers
to format.
Without this change git-clang-format gets the changed line numbers based on the index, but then
formats these lines on the working tree version of the file.
This is a problem when the working tree and index differ. One common case would be
(and is the motivation behind this patch) when applying the suggested changes git-clang-format --staged,
then forgetting to add the applied changes. When git-clang-format --staged --diff is used in a
pre-commit hook in this scenario, then the hook would allow committing the improperly formatted changes,
as the file is correctly formatted in the work tree.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Can you open an issue at https://github.com/llvm/llvm-project/issues? See https://github.com/llvm/llvm-project/issues/56736 for an example.
Updated to add missing comma after 'git ls-tree'. It broke normal (non staged) operation.
So trying to understand the problem statement, is it:
- you have some files staged
- and then you have to change them locally (review comment)
- but forget to git add them,
- you run git clang-format --staged
- and it formats the new file (not added) based on the line numbers that are changing in the already staged file, which could be a different range to the original (is that correct?)
- You think you've formatted the new file correctly (but likely missed a line or two)
- You perform git add (as you now notice you've missed adding it)
- You commit, but a commit hook tells you that you didn't clang format. (or worse still you are able to commit and push but you are pushing a badly formatted file, potentially)
is that correct?
Not exactly, in 5 git-clang-format --staged would refuse to change anything, because the same file is modified in both the index and the worktree.
But with --diff, yes it would run clang-format on the new file, but with the line numbers based on the index, then show the diff with this result.
Also at 8 there would be no problem, as there's no two versions of the file anymore. The issue can only appear when there is a file that is different in the index and on disk (Not all changes to it are staged).
I think this captures the original case well:
- Have a commit hook script that uses git-clang-format --staged --diff to reject commits that would be badly formatted
- Change a file, add changes with incorrect formatting
- add the file to the index via git add
- Try to commit
- The commit hook rejects the commit, gives the diff that needs to be applied to fix
- Fix the formatting, but forget to git add
- Commit
- Commit succeeds, but it should have failed, the resulting commit has incorrect formatting (because it does not include the formatting changes)
Sorry, I somehow missed the big obvious checkbox to mark the comments, fixed now.
I don't have commit access could you commit if the last change seems okay to you?
Name: Gergely Meszaros
Email: meszaros.gergely97@gmail.com
To refactor the code a little.