|
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\Condition; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
|
15
|
|
|
use CaptainHook\App\Hook\Condition; |
|
16
|
|
|
use CaptainHook\App\Hook\Constrained; |
|
17
|
|
|
use CaptainHook\App\Hook\Restriction; |
|
18
|
|
|
use CaptainHook\App\Hooks; |
|
19
|
|
|
use SebastianFeldmann\Git\Repository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class FileChange |
|
23
|
|
|
* |
|
24
|
|
|
* @package CaptainHook |
|
25
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
26
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
27
|
|
|
* @since Class available since Release 4.2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class FileChanged implements Condition, Constrained |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* List of file to watch |
|
33
|
|
|
* |
|
34
|
|
|
* @var array<string> |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $filesToWatch; |
|
37
|
|
|
|
|
38
|
5 |
|
/** |
|
39
|
|
|
* FileChange constructor |
|
40
|
5 |
|
* |
|
41
|
5 |
|
* @param array<string> $files |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(array $files) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->filesToWatch = $files; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Return the hook restriction information |
|
50
|
|
|
* |
|
51
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getRestriction(): Restriction |
|
54
|
|
|
{ |
|
55
|
|
|
return Restriction::fromArray([Hooks::POST_CHECKOUT, Hooks::POST_MERGE]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Evaluates a condition |
|
60
|
|
|
* |
|
61
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
62
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
63
|
|
|
* @return bool |
|
64
|
5 |
|
*/ |
|
65
|
|
|
abstract public function isTrue(IO $io, Repository $repository): bool; |
|
66
|
5 |
|
|
|
67
|
5 |
|
/** |
|
68
|
|
|
* Use 'diff-tree' to find the changed files after this merge or checkout |
|
69
|
5 |
|
* |
|
70
|
|
|
* In case of a checkout it is easy because the arguments 'previousHead' and 'newHead' exist. |
|
71
|
|
|
* In case of a merge determining this hashes is more difficult so we are using the 'ref-log' |
|
72
|
|
|
* to do it and using 'HEAD@{1}' as the last position before the merge and 'HEAD' as the |
|
73
|
|
|
* current position after the merge. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
76
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
77
|
|
|
* @return array<string> |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getChangedFiles(IO $io, Repository $repository) |
|
80
|
|
|
{ |
|
81
|
|
|
$oldHash = $io->getArgument('previousHead', 'HEAD@{1}'); |
|
82
|
|
|
$newHash = $io->getArgument('newHead', 'HEAD'); |
|
83
|
|
|
|
|
84
|
|
|
return $repository->getDiffOperator()->getChangedFiles($oldHash, $newHash); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Check if a file matching a `fnmatch` pattern was changed |
|
89
|
|
|
* |
|
90
|
|
|
* @param array<string> $changedFiles |
|
91
|
|
|
* @param string $pattern |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function didMatchingFileChange(array $changedFiles, string $pattern): bool |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($changedFiles as $file) { |
|
97
|
|
|
if (fnmatch($pattern, $file)) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|