Skip to content

Fix for diff when run from non-git root directory #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

turculaurentiu91
Copy link

@turculaurentiu91 turculaurentiu91 commented Jul 1, 2025

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 status and git diff. Let's take a laravel project, that is stored in the project folder of a git repository. So the composer file is in project/composer.json

When git status is 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 the project directory, will output routes/web.php

$process = tap(new Process(['git', 'status', '--short', '--', '**.php']))->run();

When git diff is run from the diff method it returns a full path starting from the git root directory. so running git diff --name-only --diff-filter=AM main..HEAD -- '**.php' from the project directory, will output project/routes/web.php

$files = [
'committed' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', "{$branch}...HEAD", '--', '**.php']))->run(),
'staged' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', '--cached', '--', '**.php']))->run(),
'unstaged' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', '--', '**.php']))->run(),
'untracked' => tap(new Process(['git', 'ls-files', '--others', '--exclude-standard', '--', '**.php']))->run(),
];

Later down the line, when we append the path to the output of any of those functions, in the processFileNames method, we append the path that already includes the project directory, causing the fully qualified path to become /home/laurentiu/git-root/project/project/routes/web.php

return $this->path.DIRECTORY_SEPARATOR.$file;

Proposed solution

My proposed solution is to only use the git diff command for both dirty and diff methods, then get the git root directory using git rev-parse --show-toplevel to 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.

@turculaurentiu91
Copy link
Author

Also note that the windows server 2019 runner was retired causing the CI checks to fail actions/runner-images#12045

@turculaurentiu91 turculaurentiu91 force-pushed the main branch 3 times, most recently from ef26abc to 1ac133c Compare July 1, 2025 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant