-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathDisallowActionChanges.php
More file actions
81 lines (68 loc) · 2.32 KB
/
Copy pathDisallowActionChanges.php
File metadata and controls
81 lines (68 loc) · 2.32 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
<?php
/**
* This file is part of CaptainHook
*
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CaptainHook\App\Plugin\Hook;
use CaptainHook\App\Config;
use CaptainHook\App\Plugin;
use CaptainHook\App\Plugin\Hook\Exception\ActionChangedFiles;
use CaptainHook\App\Runner\Hook as RunnerHook;
use SebastianFeldmann\Git\Diff\File;
/**
* DisallowActionChanges runner plugin
*
* @package CaptainHook
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
* @link https://github.com/captainhookphp/captainhook
* @since Class available since Release 5.11.0.
*/
class DisallowActionChanges extends PreserveWorkingTree implements Plugin\Hook
{
/**
* @var iterable
*/
private $priorDiff = [];
/**
* An array of actions that made changes to files. Each action name is the
* key for an array of file changes made by that action.
*
* @var array<string, File[]>
*/
private $actionChanges = [];
public function beforeHook(RunnerHook $hook): void
{
parent::beforeHook($hook);
// Get a diff of the current state of the working tree. Since we ran
// the parent beforeHook(), which moves changes out of the working
// tree, this should be an empty diff.
$this->priorDiff = $this->repository->getDiffOperator()->compareTo();
}
public function afterAction(RunnerHook $hook, Config\Action $action): void
{
$afterDiff = $this->repository->getDiffOperator()->compareTo();
// Did this action make any changes?
if ($afterDiff != $this->priorDiff) {
$this->actionChanges[$action->getAction()] = $afterDiff;
}
$this->priorDiff = $afterDiff;
}
public function afterHook(RunnerHook $hook): void
{
parent::afterHook($hook);
if (count($this->actionChanges) > 0) {
$message = '';
foreach ($this->actionChanges as $action => $changes) {
$message .= '<error>Action \'' . $action
. '\' on hook ' . $hook->getName()
. ' modified files</error>'
. PHP_EOL;
}
throw new ActionChangedFiles($message);
}
}
}