This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix the return code of git-clang-format
ClosedPublic

Authored by sridhar_gopinath on Jul 7 2022, 11:25 AM.

Details

Summary

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.

Fixes https://github.com/llvm/llvm-project/issues/56736.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2022, 11:25 AM
sridhar_gopinath requested review of this revision.Jul 7 2022, 11:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2022, 11:25 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
owenpan added inline comments.Jul 7 2022, 3:32 PM
clang/tools/clang-format/git-clang-format
200–201
200–201

You can delete this line.

539–541

--exit-code is implied?

550–552

Replaced subprocess.check_call with subprocess.call since the former crashes when the return code is not zero.
+ formatting changes

Could someone please review this change? Thanks!

— Sridhar

owenpan added inline comments.Jul 16 2022, 12:57 PM
clang/tools/clang-format/git-clang-format
539–540

Can we use run() instead of call() here and below?

Updated subprocess.call -> subprocess.run

Please mark as done if you have addressed an inline comment.

clang/tools/clang-format/git-clang-format
539–541

Omit --exit-code (if implied) and fix the location of --.

551–553

Ditto.

578

While we are at it, let's also replace this one with run().

sridhar_gopinath marked 4 inline comments as done.

Changes to the args order and changing check_call -> run

sridhar_gopinath marked 4 inline comments as done.Jul 19 2022, 3:04 PM
sridhar_gopinath added inline comments.
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.
owenpan added inline comments.Jul 19 2022, 6:06 PM
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.

From https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgt--no-index--ltpathgtltpathgt:

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?

Removed '--' in the git-diff commands as it only applies to files
and not commits.

sridhar_gopinath marked 2 inline comments as done.Jul 21 2022, 11:20 AM
sridhar_gopinath added inline comments.
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.

owenpan added inline comments.Jul 24 2022, 9:25 PM
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?

sridhar_gopinath marked 3 inline comments as done.Jul 26 2022, 11:55 AM
sridhar_gopinath added inline comments.
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.

owenpan accepted this revision.Jul 26 2022, 10:27 PM

Thanks for the explanations! LGTM.

clang/tools/clang-format/git-clang-format
539–541

Nit: align --exit-code with git or binpack the arguments.

This revision is now accepted and ready to land.Jul 26 2022, 10:27 PM
sridhar_gopinath marked an inline comment as done.

Addressed nitpick

sridhar_gopinath marked an inline comment as done.Jul 27 2022, 11:03 AM

I do not have commit access. Could you please commit on my behalf? Thank you!

owenpan retitled this revision from [clang-format] Update return code to [clang-format] Fix the return code of git-clang-format.Jul 27 2022, 2:25 PM
owenpan edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.