1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace PurpleBooth\GitLintValidators\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
6
|
|
|
|
7
|
|
|
class Scripts |
8
|
|
|
{ |
9
|
|
|
const BACKUP_EXTENSION = '.bak'; |
10
|
|
|
const GIT_PATH = '.git'; |
11
|
|
|
const HOOKS_PATH = 'hooks'; |
12
|
|
|
const HOOK_FILENAME = 'commit-msg'; |
13
|
|
|
const TEMPLATE_FILENAME = '.gitmessage'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default Permissions (Copied from example hooks) |
17
|
|
|
* |
18
|
|
|
* User: Read, Write, Execute |
19
|
|
|
* Group: Read, Execute |
20
|
|
|
* Other: Execute |
21
|
|
|
*/ |
22
|
|
|
const EXECUTABLE_PERMISSIONS = 0751; |
23
|
|
|
const HOOK_CONTENTS = <<<CONTENT |
24
|
|
|
#!/bin/sh |
25
|
|
|
|
26
|
|
|
vendor/bin/git-lint-validators git-lint-validator:hook $1 |
27
|
|
|
CONTENT; |
28
|
|
|
|
29
|
|
|
const TEMPLATE_CONTENTS = <<<CONTENT |
|
|
|
|
30
|
|
|
Subject line |
31
|
|
|
|
32
|
|
|
# - Capitalise the subject line and do not end it with a period |
33
|
|
|
# - Use the imperative mood in the subject line |
34
|
|
|
# - Summarise changes in around 50 (soft limit, hard limit at 69) |
35
|
|
|
# characters or less in the subject line |
36
|
|
|
# - Separate subject line from body with a blank line |
37
|
|
|
Subject body |
38
|
|
|
# - Use the subject body to explain what and why vs. how |
39
|
|
|
# - Wrap the subject body at 72 characters |
40
|
|
|
|
41
|
|
|
CONTENT; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Installs and activates the Git commit message |
45
|
|
|
* hook when confirmed by the user. An existing hook |
46
|
|
|
* is backed with the .bak extension. |
47
|
|
|
* |
48
|
|
|
* @param Event $event The script event. |
49
|
|
|
* |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
|
|
public static function installGitMessageHook(Event $event) |
53
|
|
|
{ |
54
|
|
|
$hookContent = self::HOOK_CONTENTS; |
55
|
|
|
$inputOutput = $event->getIO(); |
56
|
|
|
$question = "Do you want to install and activate the Git "; |
57
|
|
|
$question .= "commit message hook? "; |
58
|
|
|
|
59
|
|
|
if ($inputOutput->askConfirmation($question, false)) { |
60
|
|
|
$gitDirectory = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 4), self::GIT_PATH]); |
61
|
|
|
|
62
|
|
View Code Duplication |
if (!is_dir($gitDirectory)) { |
|
|
|
|
63
|
|
|
$errorMessage = "Couldn't locate the .git directory. "; |
64
|
|
|
$errorMessage .= "Aborting the Git hook installation."; |
65
|
|
|
|
66
|
|
|
$inputOutput->error($errorMessage); |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$hookFile = implode(DIRECTORY_SEPARATOR, [$gitDirectory, self::HOOKS_PATH, self::HOOK_FILENAME]); |
72
|
|
|
$existingHookFile = false; |
73
|
|
|
|
74
|
|
|
if (file_exists($hookFile)) { |
75
|
|
|
$existingHookFile = copy( |
76
|
|
|
$hookFile, |
77
|
|
|
$hookFile . self::BACKUP_EXTENSION |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
file_put_contents($hookFile, $hookContent); |
82
|
|
|
chmod($hookFile, self::EXECUTABLE_PERMISSIONS); |
83
|
|
|
|
84
|
|
|
$inputOutput->write("Installed and activated the Git commit message hook."); |
85
|
|
|
|
86
|
|
|
if ($existingHookFile) { |
87
|
|
|
$inputOutput->write("Backed previous Git commit message hook."); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($inputOutput->isVerbose()) { |
91
|
|
|
$inputOutput->write("Wrote"); |
92
|
|
|
$inputOutput->write("<comment>$hookContent</comment>"); |
93
|
|
|
$inputOutput->write("into <info>$hookFile</info> and made it executable."); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$inputOutput->write("Aborted installation and activation of the Git commit message hook."); |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Installs and configures the Git commit message |
106
|
|
|
* template when confirmed by the user. |
107
|
|
|
* |
108
|
|
|
* @param Event $event The script event. |
109
|
|
|
* |
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public static function installGitCommitMessageTemplate(Event $event) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$templateContent = self::TEMPLATE_CONTENTS; |
115
|
|
|
$inputOutput = $event->getIO(); |
|
|
|
|
116
|
|
|
$question = "Do you want to install and configure the Git "; |
|
|
|
|
117
|
|
|
$question .= "commit message template? "; |
|
|
|
|
118
|
|
|
|
119
|
|
|
if ($inputOutput->askConfirmation($question, false)) { |
120
|
|
|
$gitDirectory = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 4), self::GIT_PATH]); |
121
|
|
|
|
122
|
|
View Code Duplication |
if (!is_dir($gitDirectory)) { |
|
|
|
|
123
|
|
|
$errorMessage = "Couldn't locate the .git directory. "; |
124
|
|
|
$errorMessage .= "Aborting the Git commit message template installation."; |
125
|
|
|
|
126
|
|
|
$inputOutput->error($errorMessage); |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$templateFile = implode(DIRECTORY_SEPARATOR, [$gitDirectory, self::TEMPLATE_FILENAME]); |
132
|
|
|
|
133
|
|
|
file_put_contents($templateFile, $templateContent); |
134
|
|
|
$gitConfigureCommand = "git config --add commit.template $templateFile"; |
135
|
|
|
exec($gitConfigureCommand); |
136
|
|
|
|
137
|
|
|
$inputOutput->write("Installed and configured the Git commit message template."); |
138
|
|
|
|
139
|
|
|
if ($inputOutput->isVerbose()) { |
140
|
|
|
$inputOutput->write("Wrote"); |
141
|
|
|
$inputOutput->write("<comment>$templateContent</comment>"); |
142
|
|
|
$inputOutput->write("into <info>$templateFile</info> and configured the Git commit message template "); |
143
|
|
|
$inputOutput->write("via <comment>$gitConfigureCommand</comment>."); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$inputOutput->write("Aborted installation and configuration of the Git commit message template."); |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.