|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Roave\BackwardCompatibility\Git; |
|
6
|
|
|
|
|
7
|
|
|
use Assert\Assert; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException; |
|
10
|
|
|
use Symfony\Component\Process\Process; |
|
11
|
|
|
use function file_exists; |
|
12
|
|
|
use function is_dir; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
use function sys_get_temp_dir; |
|
15
|
|
|
use function uniqid; |
|
16
|
|
|
|
|
17
|
|
|
final class GitCheckoutRevisionToTemporaryPath implements PerformCheckoutOfRevision |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var callable */ |
|
20
|
|
|
private $uniquenessFunction; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(?callable $uniquenessFunction = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->uniquenessFunction = $uniquenessFunction ?? function (string $nonUniqueThing) : string { |
|
25
|
|
|
return uniqid($nonUniqueThing, true); |
|
26
|
|
|
}; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
* @throws ProcessRuntimeException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function checkout(CheckedOutRepository $sourceRepository, Revision $revision) : CheckedOutRepository |
|
34
|
|
|
{ |
|
35
|
|
|
$checkoutDirectory = $this->generateTemporaryPathFor($revision); |
|
36
|
|
|
|
|
37
|
|
|
(new Process(['git', 'clone', (string) $sourceRepository, $checkoutDirectory]))->mustRun(); |
|
38
|
|
|
(new Process(['git', 'checkout', (string) $revision]))->setWorkingDirectory($checkoutDirectory)->mustRun(); |
|
39
|
|
|
|
|
40
|
|
|
return CheckedOutRepository::fromPath($checkoutDirectory); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
* @throws ProcessRuntimeException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function remove(CheckedOutRepository $checkedOutRepository) : void |
|
48
|
|
|
{ |
|
49
|
|
|
(new Process(['rm', '-rf', (string) $checkedOutRepository]))->mustRun(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws RuntimeException |
|
54
|
|
|
*/ |
|
55
|
|
|
private function generateTemporaryPathFor(Revision $revision) : string |
|
56
|
|
|
{ |
|
57
|
|
|
$uniquePathGenerator = $this->uniquenessFunction; |
|
58
|
|
|
$checkoutDirectory = sys_get_temp_dir() . '/api-compare-' . $uniquePathGenerator((string) $revision . '_'); |
|
59
|
|
|
|
|
60
|
|
|
Assert::that($checkoutDirectory)->not |
|
61
|
|
|
if (file_exists($checkoutDirectory) || is_dir($checkoutDirectory)) { |
|
|
|
|
|
|
62
|
|
|
throw new RuntimeException(sprintf( |
|
63
|
|
|
'Tried to check out revision "%s" to directory "%s" which already exists', |
|
64
|
|
|
(string) $revision, |
|
65
|
|
|
$checkoutDirectory |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $checkoutDirectory; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|