|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use GitElephant\Objects\Remote; |
|
8
|
|
|
use GitElephant\Repository; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use Yiisoft\Aliases\Aliases; |
|
14
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
|
15
|
|
|
use Yiisoft\VarDumper\VarDumper; |
|
16
|
|
|
|
|
17
|
|
|
final class GitController |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct( |
|
20
|
|
|
private DataResponseFactoryInterface $responseFactory, |
|
21
|
|
|
private Aliases $aliases, |
|
22
|
|
|
) { |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function summary(): ResponseInterface |
|
26
|
|
|
{ |
|
27
|
|
|
$git = $this->getGit(); |
|
28
|
|
|
|
|
29
|
|
|
$branch = $git->getMainBranch(); |
|
30
|
|
|
$result = [ |
|
31
|
|
|
'currentBranch' => $branch->getName(), |
|
32
|
|
|
'remotes' => array_map(fn (Remote $repo) => [ |
|
33
|
|
|
'name' => $repo->getName(), |
|
34
|
|
|
'branches' => array_keys($repo->getBranches()), |
|
35
|
|
|
'url' => $repo->getFetchURL(), |
|
36
|
|
|
], $git->getRemotes(false)), |
|
37
|
|
|
'branches' => $git->getBranches(true), |
|
38
|
|
|
'lastCommit' => [ |
|
39
|
|
|
'ref' => $branch->getLastCommit()->getSha(true), |
|
40
|
|
|
'message' => $branch->getLastCommit()->getMessage()->getShortMessage(), |
|
41
|
|
|
'author' => [ |
|
42
|
|
|
'name' => $branch->getLastCommit()->getAuthor()->getName(), |
|
43
|
|
|
'email' => $branch->getLastCommit()->getAuthor()->getEmail(), |
|
44
|
|
|
], |
|
45
|
|
|
], |
|
46
|
|
|
'status' => $git->getStatusOutput(), |
|
47
|
|
|
]; |
|
48
|
|
|
$response = VarDumper::create($result)->asJson(false, 255); |
|
49
|
|
|
return $this->responseFactory->createResponse(json_decode($response, null, 512, JSON_THROW_ON_ERROR)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function checkout(ServerRequestInterface $request): ResponseInterface |
|
53
|
|
|
{ |
|
54
|
|
|
$git = $this->getGit(); |
|
55
|
|
|
|
|
56
|
|
|
$branch = $request->getParsedBody()['branch'] ?? null; |
|
57
|
|
|
|
|
58
|
|
|
if ($branch === null) { |
|
59
|
|
|
throw new InvalidArgumentException('Branch should not be empty.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$git->checkout($branch); |
|
63
|
|
|
return $this->responseFactory->createResponse([]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getGit(): Repository |
|
67
|
|
|
{ |
|
68
|
|
|
$projectPath = $this->aliases->get('@root'); |
|
69
|
|
|
|
|
70
|
|
|
while ($projectPath !== '/') { |
|
71
|
|
|
try { |
|
72
|
|
|
$git = new Repository($projectPath); |
|
73
|
|
|
$git->getStatus(); |
|
74
|
|
|
return $git; |
|
75
|
|
|
} catch (Throwable) { |
|
76
|
|
|
$projectPath = dirname($projectPath); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
throw new InvalidArgumentException( |
|
81
|
|
|
sprintf( |
|
82
|
|
|
'Could find any repositories up from "%s" directory.', |
|
83
|
|
|
$projectPath, |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|