-
-
Notifications
You must be signed in to change notification settings - Fork 12
Support diffing against a non-existing composer.lock file #46
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
base: main
Are you sure you want to change the base?
Changes from all commits
4c1e61c
3311fab
6f365b0
c8e7b3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ | |
| /** @var UrlGenerator */ | ||
| protected $urlGenerator; | ||
|
|
||
| /** @var bool */ | ||
| protected $skipIoErrors = false; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->urlGenerator = new GeneratorContainer(); | ||
|
|
@@ -38,6 +41,16 @@ | |
| $this->urlGenerator = $urlGenerator; | ||
| } | ||
|
|
||
| /** | ||
| * @param bool $skipIoErrors | ||
| * | ||
| * @return void | ||
| */ | ||
| public function setSkipIoErrors($skipIoErrors) | ||
| { | ||
| $this->skipIoErrors = (bool) $skipIoErrors; | ||
|
Check warning on line 51 in src/PackageDiff.php
|
||
| } | ||
|
|
||
| /** | ||
| * @param string[] $directPackages | ||
| * @param bool $onlyDirect | ||
|
|
@@ -65,7 +78,7 @@ | |
| /** | ||
| * @return array<InstallOperation|UpdateOperation|UninstallOperation> | ||
| */ | ||
| public function getOperations(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages) | ||
|
Check warning on line 81 in src/PackageDiff.php
|
||
| { | ||
| $operations = array(); | ||
|
|
||
|
|
@@ -213,28 +226,71 @@ | |
| return file_get_contents($localPath); | ||
| } | ||
|
|
||
| if (false === strpos($originalPath, self::GIT_SEPARATOR)) { | ||
| $path .= self::GIT_SEPARATOR.self::COMPOSER.($lockFile ? self::EXTENSION_LOCK : self::EXTENSION_JSON); | ||
| } | ||
| $attemptedSpecs = array(); | ||
| $lastError = ''; | ||
|
|
||
| if (!$lockFile) { | ||
| $path = $this->getJsonPath($path); | ||
| } | ||
| foreach ($this->getGitCandidates($path, $lockFile) as $gitPath) { | ||
| $attemptedSpecs[] = $gitPath; | ||
| $output = array(); | ||
| @exec(sprintf('git show %s 2>&1', escapeshellarg($gitPath)), $output, $exit); | ||
| $outputString = implode("\n", $output); | ||
|
|
||
| $output = array(); | ||
| @exec(sprintf('git show %s 2>&1', escapeshellarg($path)), $output, $exit); | ||
| $outputString = implode("\n", $output); | ||
| if (0 !== $exit) { | ||
| $lastError = $outputString; | ||
|
|
||
| if (0 !== $exit) { | ||
| if ($lockFile) { | ||
| throw new \RuntimeException(sprintf('Could not open file %s or find it in git as %s: %s', $originalPath, $path, $outputString)); | ||
| continue; | ||
| } | ||
|
|
||
| /* @infection-ignore-all False-positive */ | ||
| return '{}'; // Do not throw exception for composer.json as it might not exist and that's fine | ||
| if (!$this->looksLikeJsonDocument($outputString)) { | ||
| $lastError = sprintf('Unexpected non-JSON output for git spec %s', $gitPath); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| return $outputString; | ||
| } | ||
|
|
||
| if ($lockFile && !$this->skipIoErrors) { | ||
| throw new \RuntimeException(sprintf('Could not open file %s or find it in git as %s: %s', $originalPath, implode(', ', $attemptedSpecs), $lastError)); | ||
| } | ||
|
|
||
| return $outputString; | ||
| /* @infection-ignore-all False-positive */ | ||
| // Do not throw exception for composer.json as it might not exist and that's fine. | ||
| // For composer.lock this fallback is optional and controlled by setSkipIoErrors(). | ||
| return '{}'; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $path | ||
| * @param bool $lockFile | ||
| * | ||
| * @return string[] | ||
| */ | ||
| private function getGitCandidates($path, $lockFile = true) | ||
| { | ||
| $candidates = array(); | ||
|
|
||
| if ($lockFile) { | ||
| $candidates[] = $path; | ||
| $candidates[] = $path.self::GIT_SEPARATOR.self::COMPOSER.self::EXTENSION_LOCK; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously there was a |
||
| } else { | ||
| $candidates[] = $this->getJsonPath($path); | ||
| $candidates[] = $this->getJsonPath($path.self::GIT_SEPARATOR.self::COMPOSER.self::EXTENSION_LOCK); | ||
| } | ||
|
|
||
| return array_values(array_unique($candidates)); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $contents | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function looksLikeJsonDocument($contents) | ||
| { | ||
| $data = json_decode($contents, true); | ||
|
|
||
| return JSON_ERROR_NONE === json_last_error() && is_array($data); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -248,6 +304,6 @@ | |
| return substr($path, 0, -strlen(self::EXTENSION_LOCK)).self::EXTENSION_JSON; | ||
| } | ||
|
|
||
| return $path; | ||
|
Check warning on line 307 in src/PackageDiff.php
|
||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause decoding the json twice. Also, if the file exists but is not a well-formed JSON, I wouldn't consider it a skippable IO error and throw exception here. Let me know what you think.