@@ -90,7 +90,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
90
90
$ this ->handleOptions ($ action ->getOptions ());
91
91
92
92
foreach ($ refsToPush as $ range ) {
93
- $ commits = $ this ->getInvalidCommits ($ repository , $ range ->from ()->id (), $ range ->to ()->id ());
93
+ $ commits = $ this ->getBlockedCommits ($ repository , $ range ->from ()->id (), $ range ->to ()->id ());
94
94
95
95
if (count ($ commits ) > 0 ) {
96
96
$ this ->handleFailure ($ commits , $ range ->from ()->branch ());
@@ -99,7 +99,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
99
99
}
100
100
101
101
/**
102
- * Check if fixup and squash should be blocked
102
+ * Check if fixup or squash should be blocked
103
103
*
104
104
* @param \CaptainHook\App\Config\Options $options
105
105
* @return void
@@ -119,19 +119,52 @@ private function handleOptions(Config\Options $options): void
119
119
* @return array<\SebastianFeldmann\Git\Log\Commit>
120
120
* @throws \Exception
121
121
*/
122
- private function getInvalidCommits (Repository $ repository , string $ remoteHash , string $ localHash ): array
122
+ private function getBlockedCommits (Repository $ repository , string $ remoteHash , string $ localHash ): array
123
123
{
124
- $ invalid = [];
124
+ $ typesToCheck = $ this ->getTypesToBlock ();
125
+ $ blocked = [];
125
126
foreach ($ repository ->getLogOperator ()->getCommitsBetween ($ remoteHash , $ localHash ) as $ commit ) {
126
- if ($ this ->blockFixupCommits && strpos ($ commit ->getSubject (), 'fixup! ' ) === 0 ) {
127
- $ invalid [] = $ commit ;
128
- continue ;
127
+ if ($ this ->hasToBeBlocked ($ commit ->getSubject (), $ typesToCheck )) {
128
+ $ blocked [] = $ commit ;
129
129
}
130
- if ($ this ->blockSquashCommits && strpos ($ commit ->getSubject (), 'squash! ' ) === 0 ) {
131
- $ invalid [] = $ commit ;
130
+ }
131
+ return $ blocked ;
132
+ }
133
+
134
+ /**
135
+ * Returns a list of strings to look for in commit messages
136
+ *
137
+ * Will most likely return ['fixup!', 'squash!']
138
+ *
139
+ * @return array<string>
140
+ */
141
+ private function getTypesToBlock (): array
142
+ {
143
+ $ strings = [];
144
+ if ($ this ->blockFixupCommits ) {
145
+ $ strings [] = 'fixup! ' ;
146
+ }
147
+ if ($ this ->blockSquashCommits ) {
148
+ $ strings [] = 'squash! ' ;
149
+ }
150
+ return $ strings ;
151
+ }
152
+
153
+ /**
154
+ * Checks if the commit message starts with any of the given strings
155
+ *
156
+ * @param string $message
157
+ * @param array<string> $typesToCheck
158
+ * @return bool
159
+ */
160
+ private function hasToBeBlocked (string $ message , array $ typesToCheck ): bool
161
+ {
162
+ foreach ($ typesToCheck as $ type ) {
163
+ if (strpos ($ message , $ type ) === 0 ) {
164
+ return true ;
132
165
}
133
166
}
134
- return $ invalid ;
167
+ return false ;
135
168
}
136
169
137
170
/**
0 commit comments