<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56797>56797</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] git-clang-format checks formatting of the work tree when using --staged 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Maetveis
      </td>
    </tr>
</table>

<pre>
    `git-clang-format` gets the changed line numbers based on the files in the index in `--staged` mode, but then
formats these lines on the working tree version of the file. When the working tree and the index differ, with correctly formatted files in the working tree and incorrectly in the index, `git-clang-format --diff --staged` does not show the errors.

To reproduce apply this sample patch:
```diff
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 22e8ca70c..32e58a7b1 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -1646,8 +1646,8 @@ void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
   assert(cast<PointerType>(getOperand(0)->getType())
              ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&
          "Ptr must be a pointer to Val type!");
-  assert(Ordering != AtomicOrdering::NotAtomic &&
-         "AtomicRMW instructions must be atomic!");
+    assert(Ordering != AtomicOrdering::NotAtomic &&
+          "AtomicRMW instructions must be atomic!");
 }
 
 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
```
Stage the changes, then revert them in the worktree, then run `git-clang-format --diff --staged`. No formatting problems are printed, but if the changes were to be committed now, then incorrect formatting would be introduced.

Assuming that the above patch is saved as `sample.patch`
```
❯ git apply sample.patch
❯ git add llvm/lib/IR/Instructions.cpp
❯ git apply --reverse sample.patch
❯ clang/tools/clang-format/git-clang-format -v --binary /path/to/clang-format --diff --staged
Running clang-format on the following files:
    llvm/lib/IR/Instructions.cpp
old tree: a94c06e1502d9aac3efba3b9722d893b72419237
new tree: a94c06e1502d9aac3efba3b9722d893b72419237
clang-format did not modify any files
❯ echo $?
0
```

This situation can arise when e.g. `git-clang-format --staged` is used to format the changes before committing, but forgetting to add the changes via `git add`.

Proposed fix here: https://reviews.llvm.org/D130108
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVsuS2jgU_RqzUdllJLBhwYIOoSqLTnf1pCZr2RJYE9vySDKEv597JR52053qyQzlwpZ0de77SIUWp1WUpXvl4rLm7T7eadNwB1NkL50lrpKkrGBBClKrVpK2bwppLCm4hSndeomdqqUlKgxUK-RPHABIHFvHYS_iNVrIiH4iRe9Qro3STZSugz6vyEqvwl5Qj9r8UO2eOCMlOYBSBQt6d9WYkO8Acy_KWzEwRKjdThpUfFSuIqU2RpauPpGg2YEXI_PvoFR72zN0ESHfCB2JY1RJhq4LDfitdsRW-ugRpDHa2CTEIPx_08TIzmjRl6C460Cdq5QlljddLUnHXVlF7CwMqOFBXWHqrBXsITyi27o-NPhSBfx_ecG_1jrTlw7CaJOy60jxIbGAHoJJqVyUPE_LJGFUzhc8L6ZkmqbZbBbE4jj-oPazH_QhPP_KmGiWwkPiaTbLIA0LAgC377B40EqQtdONKl8evyMOBo-tv7TKRXTxoNqnjjx10nAEx2T-yeteAtT62ZnRGD5gHFQTQri10iBGyRH007NWrZPm26mTEfsM89A5HrgVMEgjuoxhHia9BF3ABD4XuMEP5ZR96vjfvXwyAVfirkdMvrSvsafvYIPRGT6vVESUgmuk6a0jBdQY6YLlxGl0ljiPAaAUYdjDOaUDj5-MkAa7A8XY5hzfy2wI8FftwvTIinhoxTUt0Em3_N4M88v3lmCZkP_FmjPSfzWIRPnm8hVebxTceOr3K-_a8mH4B7LLgJ8tbkReBRYBsvQc2wxJDQntJtO3HySvhHzVF67EWANDFbVsLOEGSMlgAYkLravd0CBylCACtQURLHXTKE-2rT5erbhS61DBUfe1wD0AHdhQjHhybW3feIauuPeS8EIfzgRJPGEeQA-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