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\File\Action; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IO; |
16
|
|
|
use CaptainHook\App\Exception\ActionFailed; |
17
|
|
|
use CaptainHook\App\Hook\Action; |
18
|
|
|
use CaptainHook\App\Hook\Constrained; |
19
|
|
|
use CaptainHook\App\Hook\Restriction; |
20
|
|
|
use Exception; |
21
|
|
|
use SebastianFeldmann\Git\Repository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Check |
25
|
|
|
* |
26
|
|
|
* @package CaptainHook |
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
29
|
|
|
* @since Class available since Release 5.4.1 |
30
|
|
|
*/ |
31
|
|
|
abstract class Check implements Action, Constrained |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Actual action name |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $actionName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Make sure this action is only used pro pre-commit hooks |
42
|
|
|
* |
43
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
44
|
|
|
*/ |
45
|
|
|
public static function getRestriction(): Restriction |
46
|
|
|
{ |
47
|
|
|
return new Restriction('pre-commit'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Executes the action |
52
|
|
|
* |
53
|
|
|
* @param \CaptainHook\App\Config $config |
54
|
|
|
* @param \CaptainHook\App\Console\IO $io |
55
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
56
|
|
|
* @param \CaptainHook\App\Config\Action $action |
57
|
|
|
* @return void |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
abstract public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \CaptainHook\App\Config\Options $options |
64
|
|
|
* @param string[] $stagedFiles |
65
|
|
|
* @return array |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
protected function getFilesToCheck(Config\Options $options, array $stagedFiles): array |
69
|
|
|
{ |
70
|
|
|
return $this->extractFilesToCheck($this->getFilesToWatch($options), $stagedFiles); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return a list of files to watch |
75
|
|
|
* |
76
|
|
|
* ['pattern1' => ['file1', 'file2'], 'pattern2' => ['file3']...] |
77
|
|
|
* |
78
|
|
|
* @param \CaptainHook\App\Config\Options $options |
79
|
|
|
* @return array |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
private function getFilesToWatch(Config\Options $options): array |
83
|
|
|
{ |
84
|
|
|
$filesToWatch = []; |
85
|
|
|
$filePatterns = $options->get('files'); |
86
|
|
|
if (!is_array($filePatterns)) { |
|
|
|
|
87
|
|
|
throw new Exception('Missing option "files" for ' . $this->actionName . ' action'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// collect all files that should be watched |
91
|
|
|
foreach ($filePatterns as $glob) { |
92
|
|
|
$filesToWatch[$glob] = glob($glob); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $filesToWatch; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Extract files list from the action configuration |
100
|
|
|
* |
101
|
|
|
* @param array $filesToWatch ['pattern1' => ['file1', 'file2'], 'pattern2' => ['file3']...] |
102
|
|
|
* @param string[] $stagedFiles |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
private function extractFilesToCheck(array $filesToWatch, array $stagedFiles): array |
106
|
|
|
{ |
107
|
|
|
$filesToCheck = []; |
108
|
|
|
// check if any staged file should be watched |
109
|
|
|
foreach ($stagedFiles as $stagedFile) { |
110
|
|
|
if ($this->isFileUnderWatch($stagedFile, $filesToWatch)) { |
111
|
|
|
$filesToCheck[] = $stagedFile; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return $filesToCheck; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if a file is in the list of watched files |
119
|
|
|
* |
120
|
|
|
* @param string $stagedFile |
121
|
|
|
* @param array $filesToWatch |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function isFileUnderWatch(string $stagedFile, array $filesToWatch): bool |
125
|
|
|
{ |
126
|
|
|
// check the list of files found for each pattern |
127
|
|
|
foreach ($filesToWatch as $pattern => $files) { |
128
|
|
|
foreach ($files as $fileToWatch) { |
129
|
|
|
if ($fileToWatch === $stagedFile) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns true if the file has no contents |
139
|
|
|
* |
140
|
|
|
* @param string $file |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
protected function isEmpty(string $file): bool |
144
|
|
|
{ |
145
|
|
|
return filesize($file) === 0; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|