Skip to content

Commit

Permalink
Use git init instead of creating a fake git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
cweagans committed Jul 1, 2023
1 parent e786bc1 commit a14cba7
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/Patcher/GitInitPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace cweagans\Composer\Patcher;

use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
use cweagans\Composer\Patch;

class GitInitPatcher extends GitPatcher
Expand All @@ -14,28 +15,25 @@ public function apply(Patch $patch, string $path): bool
return false;
}

$this->io->write("Creating temporary fake git repo in $path to apply patch", true, IOInterface::VERBOSE);
$this->io->write("Creating temporary git repo in $path to apply patch", true, IOInterface::VERBOSE);

// Create a fake Git repo -- just enough to make Git think it's looking at a real repo.
$dirs = [
$path . '/.git',
$path . '/.git/objects',
$path . '/.git/refs',
];
foreach ($dirs as $dir) {
mkdir($dir);
}
file_put_contents($path . '/.git/HEAD', "ref: refs/heads/main");
// Create a temporary git repository.
$status = $this->executeCommand(
'%s -C %s init',
$this->patchTool(),
$path
);

// If we couldn't create the Git repo, bail out.
if (!$status) {
return false;
}

// Use the git patcher to apply the patch.
$status = parent::apply($patch, $path);

// Clean up the fake git repo.
unlink($path . '/.git/HEAD');
foreach (array_reverse($dirs) as $dir) {
rmdir($dir);
}
// Clean up the git repo.
(new Filesystem($this->executor))->removeDirectory($path . '/.git');

return $status;
}
Expand Down

0 comments on commit a14cba7

Please sign in to comment.