Skip to content

Commit

Permalink
Properly check the detected ranges
Browse files Browse the repository at this point in the history
Since the range detector can return zero revs we have to check on
the outside to make sure to not run commands with any zero revisions.
  • Loading branch information
sebastianfeldmann committed Jan 22, 2024
1 parent 6e1fcae commit be5608c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/Hook/Branch/Action/BlockFixupAndSquashCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CaptainHook\App\Exception\ActionFailed;
use CaptainHook\App\Git\Range\Detector\PrePush;
use CaptainHook\App\Hook\Action;
use CaptainHook\App\Hook\Input;
use CaptainHook\App\Hook\Restriction;
use CaptainHook\App\Hooks;
use SebastianFeldmann\Git\Repository;
Expand Down Expand Up @@ -91,16 +90,16 @@ public static function getRestriction(): Restriction
*/
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$refDetector = new PrePush();
$refsToPush = $refDetector->getRanges($io);
$rangeDetector = new PrePush();
$rangesToPush = $rangeDetector->getRanges($io);

if (empty($refsToPush)) {
if (!$this->hasFoundRangesToCheck($rangesToPush)) {
return;
}

$this->handleOptions($action->getOptions());

foreach ($refsToPush as $range) {
foreach ($rangesToPush as $range) {
if (!empty($this->protectedBranches) && !in_array($range->from()->branch(), $this->protectedBranches)) {
return;
}
Expand Down Expand Up @@ -175,7 +174,7 @@ private function getTypesToBlock(): array
private function hasToBeBlocked(string $message, array $typesToCheck): bool
{
foreach ($typesToCheck as $type) {
if (strpos($message, $type) === 0) {
if (str_starts_with($message, $type)) {
return true;
}
}
Expand Down Expand Up @@ -203,4 +202,24 @@ private function handleFailure(array $commits, string $branch): void
. implode(PHP_EOL, $out)
);
}

/**
* Checks if we found valid ranges to check
*
* @param array<\CaptainHook\App\Git\Range\PrePush> $rangesToPush
* @return bool
*/
private function hasFoundRangesToCheck(array $rangesToPush): bool
{
if (empty($rangesToPush)) {
return false;
}
if ($rangesToPush[0]->from()->isZeroRev()) {
return false;
}
if ($rangesToPush[0]->to()->isZeroRev()) {
return false;
}
return true;
}
}

0 comments on commit be5608c

Please sign in to comment.