1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Hook\Input; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Git\Range\Detecting; |
16
|
|
|
use CaptainHook\App\Git\Ref\Util; |
17
|
|
|
use CaptainHook\App\Hook\Input\PrePush\Range; |
18
|
|
|
use CaptainHook\App\Hook\Input\PrePush\Ref; |
19
|
|
|
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor; |
20
|
|
|
use SebastianFeldmann\Git\Repository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class to access the pre-push stdIn data |
24
|
|
|
* |
25
|
|
|
* @package CaptainHook |
26
|
|
|
* @author Sebastian Feldmann <[email protected]> |
27
|
|
|
* @link https://github.com/captainhookphp/captainhook |
28
|
|
|
* @since Class available since Release 5.15.0 |
29
|
|
|
*/ |
30
|
|
|
class PrePush implements Detecting |
31
|
|
|
{ |
32
|
|
|
private Repository $repository; |
33
|
|
|
|
34
|
|
|
private string $hashOfBranchOrigin; |
35
|
|
|
|
36
|
11 |
|
/** |
37
|
|
|
* Returns list of refs |
38
|
11 |
|
* |
39
|
|
|
* @param \CaptainHook\App\Console\IO $io |
40
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
41
|
|
|
* |
42
|
|
|
* @return array<\CaptainHook\App\Hook\Input\PrePush\Range> |
43
|
|
|
*/ |
44
|
|
|
public function getRanges(IO $io, Repository $repository): array |
45
|
|
|
{ |
46
|
|
|
$this->repository = $repository; |
47
|
11 |
|
|
48
|
|
|
return $this->createFromStdIn($io->getStandardInput()); |
49
|
11 |
|
} |
50
|
11 |
|
|
51
|
11 |
|
/** |
52
|
1 |
|
* Factory method |
53
|
|
|
* |
54
|
|
|
* @param array<string> $stdIn |
55
|
10 |
|
* |
56
|
|
|
* @return array<\CaptainHook\App\Hook\Input\PrePush\Range> |
57
|
10 |
|
*/ |
58
|
2 |
|
private function createFromStdIn(array $stdIn): array |
59
|
|
|
{ |
60
|
|
|
$ranges = []; |
61
|
8 |
|
foreach ($stdIn as $line) { |
62
|
8 |
|
if (empty($line)) { |
63
|
8 |
|
continue; |
64
|
|
|
} |
65
|
11 |
|
|
66
|
|
|
[$localRef, $localHash, $remoteRef, $remoteHash] = explode(' ', trim($line)); |
67
|
|
|
|
68
|
|
|
if (Util::isZeroHash($remoteHash)) { |
69
|
|
|
$remoteHash = $this->getHashOfBranchOrigin(); |
70
|
|
|
|
71
|
|
|
if (Util::isZeroHash($remoteHash)) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$from = new Ref($remoteRef, $remoteHash, Util::extractBranchFromRefPath($remoteRef)); |
77
|
|
|
$to = new Ref($localRef, $localHash, Util::extractBranchFromRefPath($localRef)); |
78
|
|
|
$ranges[] = new Range($from, $to); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $ranges; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getHashOfBranchOrigin(): string |
85
|
|
|
{ |
86
|
|
|
if (!isset($this->hashOfBranchOrigin)) { |
87
|
|
|
$currentBranch = $this->repository->getInfoOperator() |
88
|
|
|
->getCurrentBranch(); |
89
|
|
|
|
90
|
|
|
$processor = new Processor(); |
91
|
|
|
$reflog = $processor->run(sprintf('git reflog show --no-abbrev %s', $currentBranch)) |
92
|
|
|
->getStdOutAsArray(); |
93
|
|
|
|
94
|
|
|
if (empty($reflog)) { |
95
|
|
|
$this->hashOfBranchOrigin = '0000000000000000000000000000000000000000'; |
96
|
|
|
} else { |
97
|
|
|
$branchCreation = array_pop($reflog); |
98
|
|
|
[$hash] = explode(' ', trim($branchCreation)); |
99
|
|
|
|
100
|
|
|
$this->hashOfBranchOrigin = $hash; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->hashOfBranchOrigin; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|