PrePush::isKnownBranch()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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\Git\ChangedFiles\Detector;
15
use CaptainHook\App\Git\Range\Detector\PrePush as RangeDetector;
16
use CaptainHook\App\Git\Range\PrePush as Range;
17
18
/**
19
 * Class PrePush
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhook-git/captainhook
24
 * @since   Class available since Release 5.20.0
25
 */
26
class PrePush extends Detector
27
{
28
    /**
29
     * Reflog fallback switch
30
     *
31
     * @var bool
32
     */
33
    private bool $reflogFallback = false;
34
35
    /**
36
     * Activate the reflog fallback file detection
37
     *
38
     * @param bool $bool
39
     * @return void
40
     */
41 2
    public function useReflogFallback(bool $bool): void
42
    {
43 2
        $this->reflogFallback = $bool;
44
    }
45
46
    /**
47
     * Returns a list of changed files
48
     *
49
     * @param  array<string> $filter
50
     * @return array<string>
51
     * @throws \Exception
52
     */
53 8
    public function getChangedFiles(array $filter = []): array
54
    {
55 8
        $ranges = $this->getRanges();
56 8
        if (empty($ranges)) {
57 1
            return [];
58
        }
59 7
        $files = $this->collectChangedFiles($ranges, $filter);
60 7
        if (count($files) > 0 || !$this->reflogFallback) {
61 6
            return $files;
62
        }
63
        // by now we should have found something, but if the "branch: created" entry is gone from the reflog,
64
        // try to find as many commits belonging to this branch as possible
65 1
        $branch    = $ranges[0]->to()->branch();
66 1
        $revisions = $this->repository->getLogOperator()->getBranchRevsFromRefLog($branch);
67 1
        return $this->repository->getLogOperator()->getChangedFilesInRevisions($revisions);
68
    }
69
70
    /**
71
     * Create ranges from stdIn
72
     *
73
     * @return array<\CaptainHook\App\Git\Range\PrePush>
74
     */
75 8
    private function getRanges(): array
76
    {
77 8
        $detector = new RangeDetector();
78 8
        return $detector->getRanges($this->io);
79
    }
80
81
    /**
82
     * Collect all changed files from all ranges
83
     *
84
     * @param  array<\CaptainHook\App\Git\Range\PrePush> $ranges
85
     * @param  array<string>                             $filter
86
     * @return array<string>
87
     * @throws \Exception
88
     */
89 7
    private function collectChangedFiles(array $ranges, array $filter): array
90
    {
91 7
        $files = [];
92 7
        foreach ($ranges as $range) {
93 7
            if ($this->isKnownBranch($range)) {
94 4
                $oldHash = $range->from()->id();
95 4
                $newHash = $range->to()->id();
96
            } else {
97 3
                if (!$this->reflogFallback) {
98 1
                    continue;
99
                }
100
                // the 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 6
            if (!empty($oldHash)) {
106 5
                $files[] = $this->repository->getDiffOperator()->getChangedFiles($oldHash, $newHash, $filter);
107
            }
108
        }
109 7
        return array_unique(array_merge(...$files));
110
    }
111
112
    /**
113
     * If the remote branch is known, the diff can be easily determined
114
     *
115
     * @param  \CaptainHook\App\Git\Range\PrePush $range
116
     * @return bool
117
     */
118 7
    private function isKnownBranch(Range $range): bool
119
    {
120 7
        return !$range->from()->isZeroRev();
121
    }
122
}
123