1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace PurpleBooth\GitLintValidators\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
6
|
|
|
|
7
|
|
|
class Scripts |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Installs and activates the Git commit message |
12
|
|
|
* hook when confirmed by the user. An existing hook |
13
|
|
|
* is backed with the .bak extension. |
14
|
|
|
* |
15
|
|
|
* |
16
|
|
|
* @param Event $event The script event. |
17
|
|
|
* @return boolean |
18
|
|
|
*/ |
19
|
|
|
public static function installGitMessageHook(Event $event) |
20
|
|
|
{ |
21
|
|
|
$gitHookContent = <<<CONTENT |
22
|
|
|
#!/bin/sh |
23
|
|
|
|
24
|
|
|
vendor/bin/git-lint-validators git-lint-validator:hook $1 |
25
|
|
|
CONTENT; |
26
|
|
|
|
27
|
|
|
$io = $event->getIO(); |
|
|
|
|
28
|
|
|
$question = "Do you want to install and activate the Git " |
29
|
|
|
. "commit message hook? "; |
30
|
|
|
|
31
|
|
|
if ($io->askConfirmation($question, false)) { |
32
|
|
|
$gitDirectory = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . '.git'; |
33
|
|
|
if (!is_dir($gitDirectory)) { |
34
|
|
|
$errorMessage = "Couldn't locate the .git directory. " |
35
|
|
|
. "Aborting the Git hook installation."; |
36
|
|
|
$io->error($errorMessage); |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$gitCommitMessageHookFile = $gitDirectory |
|
|
|
|
42
|
|
|
. DIRECTORY_SEPARATOR . 'hooks' |
43
|
|
|
. DIRECTORY_SEPARATOR . 'commit-msg'; |
44
|
|
|
|
45
|
|
|
$backedExistingGitCommitMessageHookFile = false; |
|
|
|
|
46
|
|
|
if (file_exists($gitCommitMessageHookFile)) { |
47
|
|
|
$backedExistingGitCommitMessageHookFile = copy( |
48
|
|
|
$gitCommitMessageHookFile, |
49
|
|
|
$gitCommitMessageHookFile . '.bak' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
file_put_contents($gitCommitMessageHookFile, $gitHookContent); |
54
|
|
|
chmod($gitCommitMessageHookFile, 0751); |
55
|
|
|
|
56
|
|
|
$io->write("Installed and activated the Git commit message hook."); |
57
|
|
|
|
58
|
|
|
if ($backedExistingGitCommitMessageHookFile) { |
59
|
|
|
$io->write("Backed previous Git commit message hook."); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($io->isVerbose()) { |
63
|
|
|
$io->write("Wrote"); |
64
|
|
|
$io->write("<comment>$gitHookContent</comment>"); |
65
|
|
|
$io->write("into <info>$gitCommitMessageHookFile</info> and made it executable."); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$io->write("Aborted installation and activation of the Git commit message hook."); |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.