-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathHooks.php
More file actions
188 lines (171 loc) · 5.44 KB
/
Copy pathHooks.php
File metadata and controls
188 lines (171 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* This file is part of CaptainHook
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace CaptainHook\App;
use RuntimeException;
/**
* Class Hooks
*
* Defines the list of hooks that can be handled with captainhook and provides some name constants.
*
* @package CaptainHook
* @author Andrea Heigl <[email protected]>
* @author Sebastian Feldmann <[email protected]>
* @link https://github.com/captainhook-git/captainhook
* @since Class available since Release 3.0.1
*/
final class Hooks
{
public const PRE_COMMIT = 'pre-commit';
public const PRE_PUSH = 'pre-push';
public const COMMIT_MSG = 'commit-msg';
public const PREPARE_COMMIT_MSG = 'prepare-commit-msg';
public const POST_COMMIT = 'post-commit';
public const POST_MERGE = 'post-merge';
public const POST_CHECKOUT = 'post-checkout';
public const POST_REWRITE = 'post-rewrite';
public const POST_CHANGE = 'post-change';
public const ARG_COMMAND = 'command';
public const ARG_GIT_COMMAND = 'git-command';
public const ARG_HASH = 'hash';
public const ARG_MESSAGE_FILE = 'message-file';
public const ARG_MODE = 'mode';
public const ARG_NEW_HEAD = 'new-head';
public const ARG_PREVIOUS_HEAD = 'previous-head';
public const ARG_SQUASH = 'squash';
public const ARG_TARGET = 'target';
public const ARG_URL = 'url';
/**
* This defines which native hook triggers which virtual hook
*
* @var string[]
*/
private static array $virtualHookTriggers = [
self::POST_CHECKOUT => self::POST_CHANGE,
self::POST_MERGE => self::POST_CHANGE,
self::POST_REWRITE => self::POST_CHANGE,
];
/**
* Determines if it is necessary to give the Captain access to the user input
*
* @var array<string, bool>
*/
private static array $hooksReceivingStdInput = [
self::PRE_PUSH => true,
self::POST_REWRITE => true,
];
/**
* Returns the list of valid hooks
*
* @return array<string, string>
*/
public static function getValidHooks(): array
{
return array_merge(self::nativeHooks(), self::virtualHooks());
}
/**
* Returns a list of all natively supported git hooks
*
* @return array<string, string>
*/
public static function nativeHooks(): array
{
return [
self::COMMIT_MSG => 'CommitMsg',
self::PRE_PUSH => 'PrePush',
self::PRE_COMMIT => 'PreCommit',
self::PREPARE_COMMIT_MSG => 'PrepareCommitMsg',
self::POST_COMMIT => 'PostCommit',
self::POST_MERGE => 'PostMerge',
self::POST_CHECKOUT => 'PostCheckout',
self::POST_REWRITE => 'PostRewrite'
];
}
/**
* Return a list of all artificial CaptainHook hooks (virtual hooks)
*
* @return array<string, string>
*/
public static function virtualHooks(): array
{
return [
self::POST_CHANGE => 'PostChange'
];
}
/**
* Returns a list of all native hooks triggered by a given virtual hook
*
* @return array<string>
*/
public static function getNativeHooksForVirtualHook(string $virtualHook): array
{
return array_keys(
array_filter(
self::$virtualHookTriggers,
function ($e) use ($virtualHook) {
return $e === $virtualHook;
}
)
);
}
/**
* Returns the argument placeholders for a given hook
*
* @param string $hook
* @return string
*/
public static function getOriginalHookArguments(string $hook): string
{
static $arguments = [
Hooks::COMMIT_MSG => ' {$FILE}',
Hooks::POST_MERGE => ' {$SQUASH}',
Hooks::PRE_COMMIT => '',
Hooks::POST_COMMIT => '',
Hooks::PRE_PUSH => ' {$TARGET} {$URL}',
Hooks::PREPARE_COMMIT_MSG => ' {$FILE} {$MODE} {$HASH}',
Hooks::POST_CHECKOUT => ' {$PREVIOUSHEAD} {$NEWHEAD} {$MODE}',
Hooks::POST_REWRITE => ' {$GIT-COMMAND}',
];
return $arguments[$hook];
}
/**
* Defines if a given hook allows for user input to be used
*
* @param string $hook
* @return bool
*/
public static function receivesStdIn(string $hook): bool
{
return self::$hooksReceivingStdInput[$hook] ?? false;
}
/**
* Tell if the given hook should trigger a virtual hook
*
* @param string $hook
* @return bool
*/
public static function triggersVirtualHook(string $hook): bool
{
return isset(self::$virtualHookTriggers[$hook]);
}
/**
* Return the virtual hook name that should be triggered by given hook
*
* @param string $hook
* @return string
*/
public static function getVirtualHook(string $hook): string
{
if (!self::triggersVirtualHook($hook)) {
throw new RuntimeException('no virtual hooks for ' . $hook);
}
return self::$virtualHookTriggers[$hook];
}
}