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\FileStaged; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Git\Diff\FilterUtil; |
16
|
|
|
use CaptainHook\App\Hook\Condition; |
17
|
|
|
use CaptainHook\App\Hook\Constrained; |
18
|
|
|
use CaptainHook\App\Hook\Restriction; |
19
|
|
|
use CaptainHook\App\Hooks; |
20
|
|
|
use SebastianFeldmann\Git\Repository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class InDirectory |
24
|
|
|
* |
25
|
|
|
* All FileStaged conditions are only applicable for `pre-commit` hooks. |
26
|
|
|
* |
27
|
|
|
* Example configuration: |
28
|
|
|
* |
29
|
|
|
* "action": "some-action" |
30
|
|
|
* "conditions": [ |
31
|
|
|
* {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\InDirectory", |
32
|
|
|
* "args": [ |
33
|
|
|
* "src/" |
34
|
|
|
* ]} |
35
|
|
|
* ] |
36
|
|
|
* |
37
|
|
|
* @package CaptainHook |
38
|
|
|
* @author Sebastian Feldmann <[email protected]> |
39
|
|
|
* @link https://github.com/captainhook-git/captainhook |
40
|
|
|
* @since Class available since Release 5.6.1 |
41
|
|
|
*/ |
42
|
|
|
class InDirectory implements Condition, Constrained |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Directory path to check e.g. 'src/' or 'path/To/Custom/Directory/' |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private string $directory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* --diff-filter options |
53
|
|
|
* |
54
|
|
|
* @var array<int, string> |
55
|
|
|
*/ |
56
|
|
|
private array $diffFilter; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* InDirectory constructor |
60
|
|
|
* |
61
|
|
|
* @param mixed $directory |
62
|
|
|
* @param array<int, string>|string $diffFilter |
63
|
|
|
*/ |
64
|
2 |
|
public function __construct($directory, $diffFilter = []) |
65
|
|
|
{ |
66
|
2 |
|
$this->directory = (string) $directory; |
67
|
2 |
|
$this->diffFilter = FilterUtil::filterFromConfigValue($diffFilter); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return the hook restriction information |
72
|
|
|
* |
73
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
74
|
|
|
*/ |
75
|
1 |
|
public static function getRestriction(): Restriction |
76
|
|
|
{ |
77
|
1 |
|
return Restriction::fromArray([Hooks::PRE_COMMIT]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Evaluates the condition |
82
|
|
|
* |
83
|
|
|
* @param \CaptainHook\App\Console\IO $io |
84
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
2 |
|
public function isTrue(IO $io, Repository $repository): bool |
88
|
|
|
{ |
89
|
2 |
|
$files = $repository->getIndexOperator()->getStagedFiles($this->diffFilter); |
90
|
|
|
|
91
|
2 |
|
$filtered = []; |
92
|
2 |
|
foreach ($files as $file) { |
93
|
2 |
|
if (strpos($file, $this->directory) === 0) { |
94
|
1 |
|
$filtered[] = $file; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return count($filtered) > 0; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|