Passed
Push — main ( e485ec...2a6706 )
by Sebastian
04:37
created

PrePush::useReflogFallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Git\ChangedFiles\Detector;
13
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Git\ChangedFiles\Detector;
16
use CaptainHook\App\Git\Range\Detector\PrePush as RangeDetector;
17
use CaptainHook\App\Git\Range\PrePush as Range;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Class PrePush
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhookphp/captainhook
26
 * @since   Class available since Release 5.20.0
27
 */
28
class PrePush extends Detector
29
{
30
    /**
31
     * Reflog fallback switch
32
     *
33
     * @var bool
34
     */
35
    private bool $reflogFallback = false;
36
37
    /**
38
     * Activate the reflog fallback file detection
39
     *
40
     * @param bool $bool
41
     * @return void
42
     */
43 2
    public function useReflogFallback(bool $bool): void
44
    {
45 2
        $this->reflogFallback = $bool;
46
    }
47
48
    /**
49
     * Return list of changed files
50
     *
51
     * @param  array<string> $filter
52
     * @return array<string>
53
     */
54 7
    public function getChangedFiles(array $filter = []): array
55
    {
56 7
        $ranges = $this->getRanges();
57 7
        if (empty($ranges)) {
58 1
            return [];
59
        }
60 6
        $files = $this->collectChangedFiles($ranges, $filter);
61 6
        if (count($files) > 0 || !$this->reflogFallback) {
62 5
            return $files;
63
        }
64
        // by now we should have found something but if the "branch: created" entry is gone from the reflog
65
        // try to find as many commits belonging to this branch as possible
66 1
        $branch    = $ranges[0]->to()->branch();
67 1
        $revisions = $this->repository->getLogOperator()->getBranchRevsFromRefLog($branch);
68 1
        return $this->repository->getLogOperator()->getChangedFilesInRevisions($revisions);
69
    }
70
71
    /**
72
     * Create ranges from stdIn
73
     *
74
     * @return array<\CaptainHook\App\Git\Range\PrePush>
75
     */
76 7
    private function getRanges(): array
77
    {
78 7
        $detector = new RangeDetector();
79 7
        return $detector->getRanges($this->io);
80
    }
81
82
    /**
83
     * Collect all changed files from all ranges
84
     *
85
     * @param  array<\CaptainHook\App\Git\Range\PrePush> $ranges
86
     * @param  array<string> $filter
87
     * @return array<string>
88
     */
89 6
    private function collectChangedFiles(array $ranges, array $filter): array
90
    {
91 6
        $files = [];
92 6
        foreach ($ranges as $range) {
93 6
            if ($this->isKnownBranch($range)) {
94 3
                $oldHash = $range->from()->id();
95 3
                $newHash = $range->to()->id();
96
            } else {
97 3
                if (!$this->reflogFallback) {
98 1
                    continue;
99
                }
100
                // remote branch does not exist
101
                // try to find the branch starting point with the reflog
102 2
                $oldHash = $this->repository->getLogOperator()->getBranchRevFromRefLog($range->to()->branch());
103 2
                $newHash = 'HEAD';
104
            }
105 5
            if (!empty($oldHash)) {
106 4
                $files = array_merge(
107 4
                    $files,
108 4
                    $this->repository->getDiffOperator()->getChangedFiles($oldHash, $newHash, $filter)
109 4
                );
110
            }
111
        }
112 6
        return array_unique($files);
113
    }
114
115
    /**
116
     * If the remote branch is known the diff can  be easily determined
117
     *
118
     * @param  \CaptainHook\App\Git\Range\PrePush $range
119
     * @return bool
120
     */
121 6
    private function isKnownBranch(Range $range): bool
122
    {
123 6
        return !$range->from()->isZeroRev();
124
    }
125
}
126