Explicitly specify git directory when running git #15
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.
Backstory
This was an interesting one track down. The symptom was that PHPStan only worked
when run directly, but not when run as part of a git invoked workflow (e.g. through
git rebase --exec
).The cause was that phpstan-drupal includes a file from the Drupal framework that queries
the runner to figure out which PHPUnit version is being used.
The version returned when running git did not match the actual PHPUnit version installed
which caused the alias that Drupal created to a PHPUnit bridge class to break.
Problem
The
Version
class uses agit
command to find the tag of the currently installed packageversion for packages that composer has checked out from git. This works when executed
directly.
However, when used in a git based workflow such as
git rebase --exec
orgit bisect
thenthis fails. The cause is that for those commands git will add environment variables that specify
the
GIT_DIR
on which its performing those workflows, so that moving around and calling gitcommands still operates according to the git repository the workflow is being executed on.
As a result when
Version
callsgit describe --tags
this does not use the.git
folder in theworking directory of
proc_open
but instead uses the git folder for which the git workflow isin progress.
Solution
To remedy this the
Version
class should specify--git-dir
directly. A few lines above we alreadyknow explicitly what git directory we're interested in. Specifying the argument to git overrides any
environment variables that may be set. This allows the library to work in automated git workflows
invoked from other repositories.