Skip to content

Commit fe68bd4

Browse files
committed
Support diffing against a non-existing composer.lock file
1 parent 8907711 commit fe68bd4

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ composer diff --help # Display detailed usage instructions
8383
```shell script
8484
composer diff master # Compare current composer.lock with the one on master branch
8585
composer diff master:composer.lock develop:composer.lock -p # Compare master and develop branches, including platform dependencies
86+
composer diff /path/to/missing/composer.lock composer.lock # Compare a new lock file against a non-existing base lock file
8687
composer diff --no-dev # ignore dev dependencies
8788
composer diff -p # include platform dependencies
8889
composer diff -f json # Output as JSON instead of table

src/PackageDiff.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ private function getFileContents($path, $lockFile = true)
213213
return file_get_contents($localPath);
214214
}
215215

216+
if ($lockFile && false === strpos($originalPath, self::GIT_SEPARATOR) && $this->looksLikeComposerLockFile($localPath)) {
217+
return '{}';
218+
}
219+
216220
if (false === strpos($originalPath, self::GIT_SEPARATOR)) {
217221
$path .= self::GIT_SEPARATOR.self::COMPOSER.($lockFile ? self::EXTENSION_LOCK : self::EXTENSION_JSON);
218222
}
@@ -226,6 +230,10 @@ private function getFileContents($path, $lockFile = true)
226230
$outputString = implode("\n", $output);
227231

228232
if (0 !== $exit) {
233+
if ($lockFile && $this->isMissingFileError($outputString)) {
234+
return '{}';
235+
}
236+
229237
if ($lockFile) {
230238
throw new \RuntimeException(sprintf('Could not open file %s or find it in git as %s: %s', $originalPath, $path, $outputString));
231239
}
@@ -237,6 +245,27 @@ private function getFileContents($path, $lockFile = true)
237245
return $outputString;
238246
}
239247

248+
/**
249+
* @param string $path
250+
*
251+
* @return bool
252+
*/
253+
private function looksLikeComposerLockFile($path)
254+
{
255+
return self::EXTENSION_LOCK === substr($path, -strlen(self::EXTENSION_LOCK));
256+
}
257+
258+
/**
259+
* @param string $gitOutput
260+
*
261+
* @return bool
262+
*/
263+
private function isMissingFileError($gitOutput)
264+
{
265+
return false !== stripos($gitOutput, 'does not exist in') || false !== stripos($gitOutput, 'exists on disk, but not in');
266+
}
267+
268+
240269
/**
241270
* @param string $path
242271
*

tests/PackageDiffTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ public function testLoadFromEmptyArray()
152152
$this->assertInstanceOf('Composer\Repository\ArrayRepository', $diff->loadPackagesFromArray(array(), true, true));
153153
}
154154

155+
public function testDiffAgainstMissingBaseLockFile()
156+
{
157+
$diff = new PackageDiff();
158+
159+
$prodOperations = $diff->getPackageDiff(
160+
__DIR__.'/fixtures/missing/composer.lock',
161+
__DIR__.'/fixtures/empty-target/composer.lock',
162+
false,
163+
false
164+
);
165+
$devOperations = $diff->getPackageDiff(
166+
__DIR__.'/fixtures/missing/composer.lock',
167+
__DIR__.'/fixtures/empty-target/composer.lock',
168+
true,
169+
false
170+
);
171+
172+
$this->assertSame(array(
173+
'install example/package 1.2.3',
174+
), array_map(array($this, 'entryToString'), $prodOperations->getArrayCopy()));
175+
$this->assertSame(array(
176+
'install example/dev-package 2.3.4',
177+
), array_map(array($this, 'entryToString'), $devOperations->getArrayCopy()));
178+
}
179+
180+
public function testDiffAgainstMissingGitLockFilePath()
181+
{
182+
$diff = new PackageDiff();
183+
$this->prepareGit();
184+
$operations = $diff->getPackageDiff('HEAD:missing/composer.lock', '', false, false);
185+
186+
$this->assertNotEmpty($operations);
187+
foreach ($operations as $entry) {
188+
$this->assertInstanceOf('Composer\DependencyResolver\Operation\InstallOperation', $entry->getOperation());
189+
}
190+
}
191+
155192
public function diffOperationsProvider()
156193
{
157194
return array(

tests/fixtures/empty-target/composer.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)