Passed
Push — master ( 6073e7...d981da )
by Alexander
02:02
created

GitExecutor::getCommitDescription()   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
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement;
6
7
use Vasoft\VersionIncrement\Contract\GetExecutorInterface;
8
use Vasoft\VersionIncrement\Exceptions\GitCommandException;
9
10
class GitExecutor implements GetExecutorInterface
11
{
12 1
    public function status(): array
13
    {
14 1
        return $this->runCommand('status --porcelain');
15
    }
16
17 1
    public function getCommitDescription(string $commitId): array
18
    {
19 1
        return $this->runCommand('log -1 --pretty=format:%b ' . $commitId);
20
    }
21
22 1
    public function addFile(string $file): void
23
    {
24 1
        $this->runCommand('add ' . $file);
25
    }
26
27 1
    public function setVersionTag(string $version): void
28
    {
29 1
        $this->runCommand('tag v' . $version);
30
    }
31
32 1
    public function commit(string $message): void
33
    {
34 1
        $this->runCommand("commit -am '" . $message . "'");
35
    }
36
37 2
    public function getCurrentBranch(): string
38
    {
39 2
        $branch = $this->runCommand('rev-parse --abbrev-ref HEAD');
40
41 1
        return trim($branch[0] ?? '');
42
    }
43
44 2
    public function getLastTag(): ?string
45
    {
46 2
        $tags = $this->runCommand('tag --sort=-creatordate');
47
48 2
        return $tags[0] ?? null;
49
    }
50
51 2
    public function getCommitsSinceLastTag(?string $lastTag): array
52
    {
53 2
        $command = $lastTag ? "log {$lastTag}..HEAD --pretty=format:\"%H %s\"" : 'log --pretty=format:"%H %s"';
54
55 2
        return $this->runCommand($command);
56
    }
57
58
    /**
59
     * @throws GitCommandException
60
     */
61 11
    private function runCommand(string $command): array
62
    {
63 11
        exec("git {$command} 2>&1", $output, $returnCode);
64 11
        if (0 !== $returnCode) {
65 1
            throw new GitCommandException($command, $output);
66
        }
67
68 10
        return $output;
69
    }
70
}
71