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; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Hooks; |
16
|
|
|
use RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Util |
20
|
|
|
* |
21
|
|
|
* @package CaptainHook |
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
23
|
|
|
* @link https://github.com/captainhookphp/captainhook |
24
|
|
|
* @since Class available since Release 0.9.0 |
25
|
|
|
*/ |
26
|
|
|
final class Util |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Checks if a hook name is valid |
30
|
|
|
* |
31
|
|
|
* @param string $hook |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
92 |
|
public static function isValid(string $hook): bool |
35
|
|
|
{ |
36
|
92 |
|
return isset(Hooks::getValidHooks()[$hook]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Answers if a hook is installable |
41
|
|
|
* |
42
|
|
|
* Only native hooks are installable, virtual hooks used by the Cap'n should not be installed. |
43
|
|
|
* |
44
|
|
|
* @param string $hook |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
25 |
|
public static function isInstallable(string $hook): bool |
48
|
|
|
{ |
49
|
25 |
|
return isset(Hooks::nativeHooks()[$hook]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns list of valid hooks |
54
|
|
|
* |
55
|
|
|
* @return array<string> |
56
|
|
|
*/ |
57
|
60 |
|
public static function getValidHooks(): array |
58
|
|
|
{ |
59
|
60 |
|
return Hooks::getValidHooks(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns hooks command class |
64
|
|
|
* |
65
|
|
|
* @param string $hook |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
16 |
|
public static function getHookCommand(string $hook): string |
69
|
|
|
{ |
70
|
16 |
|
if (!self::isValid($hook)) { |
71
|
2 |
|
throw new RuntimeException(sprintf('Hook \'%s\' is not supported', $hook)); |
72
|
|
|
} |
73
|
14 |
|
return Hooks::getValidHooks()[$hook]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get a list of all supported hooks |
78
|
|
|
* |
79
|
|
|
* @return array<string> |
80
|
|
|
*/ |
81
|
5 |
|
public static function getHooks(): array |
82
|
|
|
{ |
83
|
5 |
|
return array_keys(Hooks::getValidHooks()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Checks if a given hook was executed |
88
|
|
|
* |
89
|
|
|
* @param \CaptainHook\App\Console\IO $io |
90
|
|
|
* @param string $hook |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
6 |
|
public static function isRunningHook(IO $io, string $hook): bool |
94
|
|
|
{ |
95
|
6 |
|
return str_contains($io->getArgument(Hooks::ARG_COMMAND), $hook); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Detects the previous head commit hash |
100
|
|
|
* |
101
|
|
|
* @param \CaptainHook\App\Console\IO $io |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
5 |
|
public static function findPreviousHead(IO $io): string |
105
|
|
|
{ |
106
|
|
|
// Check if a list of rewritten commits is supplied via stdIn. |
107
|
|
|
// This happens if the 'post-rewrite' hook is triggered. |
108
|
|
|
// The stdIn is formatted like this: |
109
|
|
|
// |
110
|
|
|
// old-hash new-hash extra-info |
111
|
|
|
// old-hash new-hash extra-info |
112
|
|
|
// ... |
113
|
5 |
|
$stdIn = $io->getStandardInput(); |
114
|
5 |
|
if (!empty($stdIn)) { |
115
|
1 |
|
$info = explode(' ', $stdIn[0]); |
116
|
|
|
// If we find a rewritten commit, we return the first commit before the rewritten one. |
117
|
|
|
// If we do not find any rewritten commits (awkward) we use the last ref-log position. |
118
|
1 |
|
return isset($info[1]) ? trim($info[1]) . '^' : 'HEAD@{1}'; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
return $io->getArgument(Hooks::ARG_PREVIOUS_HEAD, 'HEAD@{1}'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|