Skip to content

Instantly share code, notes, and snippets.

@mordilion
Created January 11, 2022 07:37
Show Gist options
  • Save mordilion/9c1565f53e1957286e40acae5f84cba0 to your computer and use it in GitHub Desktop.
Save mordilion/9c1565f53e1957286e40acae5f84cba0 to your computer and use it in GitHub Desktop.
Apply patch files to your code. Really helpful to apply changes to existing libraries, which you may use via composer.
<?php
declare(strict_types=1);
$baseDir = \dirname(__DIR__);
$directoryIterator = new DirectoryIterator($baseDir . '/patches');
$patchLevels = ['-p1', '-p0', '-p2', '-p4'];
foreach ($directoryIterator as $fileInfo) {
if ($fileInfo->getExtension() !== 'patch') {
continue;
}
foreach ($patchLevels as $patchLevel) {
$commandCheck = sprintf('git apply --check %s %s', $patchLevel, $fileInfo->getPathname());
$commandReal = sprintf('git apply %s %s', $patchLevel, $fileInfo->getPathname());
$output = [];
exec($commandCheck, $output, $resultCode);
if ($resultCode !== 0) {
echo sprintf('Check "%s" failed! ... skipping', $commandCheck) . \PHP_EOL;
continue;
}
echo sprintf('Check "%s" succeed! ... start patching', $commandCheck) . \PHP_EOL;
exec($commandReal, $output, $resultCode);
if ($resultCode !== 0) {
echo sprintf('Patch "%s" failed!', $commandReal) . \PHP_EOL;
echo print_r($output, true) . \PHP_EOL;
break;
}
echo 'Patch "%s" successfully applied!', $commandReal . \PHP_EOL;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment