In diff and diffstat modes, the return code is != 0 even when there are no
changes between commits. This issue can be fixed by passing --exit-code to
git-diff command that returns 0 when there are no changes and using that as
the return code for git-clang-format.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Replaced subprocess.check_call with subprocess.call since the former crashes when the return code is not zero.
+ formatting changes
clang/tools/clang-format/git-clang-format | ||
---|---|---|
539–540 | Can we use run() instead of call() here and below? |
clang/tools/clang-format/git-clang-format | ||
---|---|---|
539–541 | --exit-code is not implied. From the docs: --exit-code Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences. |
clang/tools/clang-format/git-clang-format | ||
---|---|---|
539–541 |
git diff [<options>] --no-index [--] <path> <path> This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git. This form implies --exit-code. | |
578 | Good catch with check=True. Should we add it to the other two instances of run() above? |
clang/tools/clang-format/git-clang-format | ||
---|---|---|
539–541 | This seems to be an incorrect usage of -- in the git-diff command. -- is used in git-diff to diff between two paths. In such cases, --exit-code is implied. But when diffing two commits, -- is not needed. In this script, git-diff is used only on commits. The old-tree and new-tree variables point to git-tree hashes. Hence, using -- on the git hashes is incorrect as git tries to interpret the hashes as file names. This issue was not seen earlier because it was added at the end of the command and was being omitted. Now, since the git-diff is not on paths, --exit-code is not implied. For diff of hashes, --exit-code has to be specified explicitely. | |
578 | Not really. We want the above two commands to return non-zero exit code when there is a diff. Adding check=True will crash the process in such cases. |
clang/tools/clang-format/git-clang-format | ||
---|---|---|
201–210 | Actually, doesn't line 175 make sure it will return 0 if there is no diff? Can you open an issue at https://github.com/llvm/llvm-project/issues and give an example where this isn't true? |
clang/tools/clang-format/git-clang-format | ||
---|---|---|
201–210 | Created https://github.com/llvm/llvm-project/issues/56736 Line 175 is only checking if there are any relevant files that have been modified. There is an option to only consider a subset of changed files. So this line is checking if there are any changed lines after filtering the relevant files. Then, the tool generates a new git tree for the relevant changes after running clang-format. Line 198 is checking if the old and the new git trees are the same git hashes. When there are code changes, these two hashes won't be the same and we won't hit this case too. Finally, the actual formatting changes are checked by running git diff in line 201. This function call will define the exit code of the program. Currently, that's not being accounted for and the tool always returns 1 after reaching this point. |
Thanks for the explanations! LGTM.
clang/tools/clang-format/git-clang-format | ||
---|---|---|
539–541 | Nit: align --exit-code with git or binpack the arguments. |