Fix for diff when run from non-git root directory #375
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem
As observed in the issue #361, when you run pint with
--diff=[branch]from an inner directory (non-git root directory) it affects 0 files..The cause is the different behaviour between
git statusandgit diff. Let's take a laravel project, that is stored in theprojectfolder of a git repository. So the composer file is inproject/composer.jsonWhen
git statusis run from the dirty method it returns a relative path to the changed files, starting from the current directory.Running
git status --short -- '**.php*'from theprojectdirectory, will outputroutes/web.phppint/app/Repositories/GitPathsRepository.php
Line 35 in 6f113c1
When
git diffis run from the diff method it returns a full path starting from the git root directory. so runninggit diff --name-only --diff-filter=AM main..HEAD -- '**.php'from theprojectdirectory, will outputproject/routes/web.phppint/app/Repositories/GitPathsRepository.php
Lines 55 to 60 in 6f113c1
Later down the line, when we append the path to the output of any of those functions, in the
processFileNamesmethod, we append the path that already includes theprojectdirectory, causing the fully qualified path to become/home/laurentiu/git-root/project/project/routes/web.phppint/app/Repositories/GitPathsRepository.php
Line 92 in 6f113c1
Proposed solution
My proposed solution is to only use the
git diffcommand for bothdirtyanddiffmethods, then get the git root directory usinggit rev-parse --show-toplevelto find the fully qualified path to the git repository and append this instead.I have not included any tests, as tests that runs git commands, that includes git repository are not that straight forward to write.