Skip to content

Commit 2fb5930

Browse files
Improve readability
1 parent da60aac commit 2fb5930

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/Hook/Branch/Action/BlockFixupAndSquashCommits.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
9090
$this->handleOptions($action->getOptions());
9191

9292
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());
9494

9595
if (count($commits) > 0) {
9696
$this->handleFailure($commits, $range->from()->branch());
@@ -99,7 +99,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
9999
}
100100

101101
/**
102-
* Check if fixup and squash should be blocked
102+
* Check if fixup or squash should be blocked
103103
*
104104
* @param \CaptainHook\App\Config\Options $options
105105
* @return void
@@ -119,19 +119,52 @@ private function handleOptions(Config\Options $options): void
119119
* @return array<\SebastianFeldmann\Git\Log\Commit>
120120
* @throws \Exception
121121
*/
122-
private function getInvalidCommits(Repository $repository, string $remoteHash, string $localHash): array
122+
private function getBlockedCommits(Repository $repository, string $remoteHash, string $localHash): array
123123
{
124-
$invalid = [];
124+
$typesToCheck = $this->getTypesToBlock();
125+
$blocked = [];
125126
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;
129129
}
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;
132165
}
133166
}
134-
return $invalid;
167+
return false;
135168
}
136169

137170
/**

0 commit comments

Comments
 (0)