1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Configuration\Infrastructure\Hook; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
|
7
|
|
|
class HookCopier |
8
|
|
|
{ |
9
|
|
|
public const DEFAULT_GIT_HOOKS_DIR = '.git/hooks'; |
10
|
|
|
|
11
|
|
|
protected $hooksDir = self::DEFAULT_GIT_HOOKS_DIR; |
12
|
|
|
protected $sourceHooksDir = __DIR__ . '/../../../../Infrastructure/Hook'; |
13
|
|
|
|
14
|
|
|
public function copyPreCommitHook(): void |
15
|
|
|
{ |
16
|
|
|
$this->copyHookFile('pre-commit'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function copyCommitMsgHook(): void |
20
|
|
|
{ |
21
|
|
|
$this->copyHookFile('commit-msg'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function copyPrePushHook(): void |
25
|
|
|
{ |
26
|
|
|
$this->copyHookFile('pre-push'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function copyPrepareCommitMsgHook(): void |
30
|
|
|
{ |
31
|
|
|
$this->copyHookFile('prepare-commit-msg'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function hookExists(string $hookFile): bool |
35
|
|
|
{ |
36
|
|
|
return \file_exists($this->hooksDir . "/$hookFile"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function copyFile(string $hookFile): void |
40
|
|
|
{ |
41
|
|
|
$this->createHooksDir(); |
42
|
|
|
|
43
|
|
|
$copy = new Process(['cp', $hookFile, $this->hooksDir]); |
44
|
|
|
$copy->run(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function createHooksDir(): void |
48
|
|
|
{ |
49
|
|
|
$mkdir = new Process(['mkdir', '-p', $this->hooksDir]); |
50
|
|
|
$mkdir->run(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function setPermissions(string $hookFile): void |
54
|
|
|
{ |
55
|
|
|
$permissions = new Process(['chmod', '775', $this->hooksDir . "/$hookFile"]); |
56
|
|
|
$permissions->run(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function copyHookFile(string $file): void |
60
|
|
|
{ |
61
|
|
|
if (false === $this->hookExists($file)) { |
62
|
|
|
$this->copyFile($this->sourceHooksDir . "/$file"); |
63
|
|
|
$this->setPermissions($file); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|