Skip to content

Commit e2c1fba

Browse files
Prevent usage of zero hashes for change detection
1 parent be63e79 commit e2c1fba

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/Git/ChangedFiles/Detector/Fallback.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CaptainHook\App\Git\ChangedFiles\Detector;
1313

1414
use CaptainHook\App\Git\ChangedFiles\Detector;
15+
use CaptainHook\App\Git\Rev\Util;
1516
use CaptainHook\App\Hooks;
1617

1718
/**
@@ -35,8 +36,13 @@ class Fallback extends Detector
3536
*/
3637
public function getChangedFiles(array $filter = []): array
3738
{
39+
$previousHead = $this->io->getArgument(Hooks::ARG_PREVIOUS_HEAD, 'HEAD@{1}');
40+
if (Util::isZeroHash($previousHead)) {
41+
return [];
42+
}
43+
3844
return $this->repository->getDiffOperator()->getChangedFiles(
39-
$this->io->getArgument(Hooks::ARG_PREVIOUS_HEAD, 'HEAD@{1}'),
45+
$previousHead,
4046
'HEAD',
4147
$filter
4248
);

src/Git/ChangedFiles/Detector/PostRewrite.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CaptainHook\App\Git\ChangedFiles\Detector;
1515
use CaptainHook\App\Git\Range\Detector\PostRewrite as RangeDetector;
16+
use CaptainHook\App\Git\Rev\Util;
1617

1718
/**
1819
* Class PostRewrite
@@ -37,6 +38,10 @@ public function getChangedFiles(array $filter = []): array
3738
$old = $ranges[0]->from()->id();
3839
$new = $ranges[0]->to()->id();
3940

41+
if (Util::isZeroHash($old) || Util::isZeroHash($new)) {
42+
return [];
43+
}
44+
4045
return $this->repository->getDiffOperator()->getChangedFiles($old, $new, $filter);
4146
}
4247
}

src/Git/ChangedFiles/Detector/PrePush.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CaptainHook\App\Git\ChangedFiles\Detector;
1616
use CaptainHook\App\Git\Range\Detector\PrePush as RangeDetector;
1717
use CaptainHook\App\Git\Range\PrePush as Range;
18+
use CaptainHook\App\Git\Rev\Util;
1819
use SebastianFeldmann\Git\Repository;
1920

2021
/**
@@ -97,11 +98,14 @@ private function collectChangedFiles(array $ranges, array $filter): array
9798
if (!$this->reflogFallback) {
9899
continue;
99100
}
100-
// remote branch does not exist
101+
// the remote branch does not exist
101102
// try to find the branch starting point with the reflog
102103
$oldHash = $this->repository->getLogOperator()->getBranchRevFromRefLog($range->to()->branch());
103104
$newHash = 'HEAD';
104105
}
106+
if (Util::isZeroHash($oldHash) || Util::isZeroHash($newHash)) {
107+
continue;
108+
}
105109
if (!empty($oldHash)) {
106110
$files[] = $this->repository->getDiffOperator()->getChangedFiles($oldHash, $newHash, $filter);
107111
}

0 commit comments

Comments
 (0)