Skip to content
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

fix: Recipe update skip ignored files #885

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/Update/RecipePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ public function applyPatch(RecipePatch $patch): bool

public function generatePatch(array $originalFiles, array $newFiles): RecipePatch
{
$ignoredFiles = $this->getIgnoredFiles(array_keys($originalFiles) + array_keys($newFiles));

// null implies "file does not exist"
$originalFiles = array_filter($originalFiles, function ($file) {
return null !== $file;
});
$newFiles = array_filter($newFiles, function ($file) {
return null !== $file;
});
$originalFiles = array_filter($originalFiles, function ($file, $fileName) use ($ignoredFiles) {
return null !== $file && !\in_array($fileName, $ignoredFiles);
}, \ARRAY_FILTER_USE_BOTH);

$newFiles = array_filter($newFiles, function ($file, $fileName) use ($ignoredFiles) {
return null !== $file && !\in_array($fileName, $ignoredFiles);
}, \ARRAY_FILTER_USE_BOTH);

$deletedFiles = [];
// find removed files & record that they are deleted
Expand Down Expand Up @@ -240,4 +243,13 @@ private function _applyPatchFile(RecipePatch $patch)
}
}
}

private function getIgnoredFiles(array $fileNames): array
{
$args = implode(' ', array_map([ProcessExecutor::class, 'escape'], $fileNames));
$output = '';
$this->processExecutor->execute(sprintf('git check-ignore %s', $args), $output, $this->rootDir);

return $this->processExecutor->splitLines($output);
}
}
23 changes: 23 additions & 0 deletions tests/Update/RecipePatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public function testGeneratePatch(array $originalFiles, array $newFiles, string
// original files need to be present to avoid patcher thinking they were deleting and skipping patch
foreach ($originalFiles as $file => $contents) {
touch(FLEX_TEST_DIR.'/'.$file);
if ('.gitignore' === $file) {
file_put_contents(FLEX_TEST_DIR.'/'.$file, $contents);
}
}

// make sure the test directory is a git repo
(new Process(['git', 'init'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'config', 'user.name', '"Flex Updater"'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'config', 'user.email', '""'], FLEX_TEST_DIR))->mustRun();
if (0 !== \count($originalFiles)) {
(new Process(['git', 'add', '-A'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'commit', '-m', '"original files"'], FLEX_TEST_DIR))->mustRun();
}

$patcher = new RecipePatcher(FLEX_TEST_DIR, $this->createMock(IOInterface::class));
Expand All @@ -46,6 +58,11 @@ public function testGeneratePatch(array $originalFiles, array $newFiles, string
$this->assertSame($expectedPatch, rtrim($patch->getPatch(), "\n"));
$this->assertSame($expectedDeletedFiles, $patch->getDeletedFiles());

// when testing ignored files the patch is empty
if ('' === $expectedPatch) {
return;
}

// find all "index 7d30dc7.." in patch
$matches = [];
preg_match_all('/index\ ([0-9|a-z]+)\.\./', $patch->getPatch(), $matches);
Expand Down Expand Up @@ -158,6 +175,12 @@ public function getGeneratePatchTests(): iterable
,
['will_be_deleted.txt'],
];

yield 'ignored_file' => [
['file1.txt' => 'Original contents', '.gitignore' => 'file1.txt'],
['file1.txt' => 'Updated contents', '.gitignore' => 'file1.txt'],
'',
];
}

public function testGeneratePatchOnDeletedFile()
Expand Down