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

GitExecutor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 59
ccs 24
cts 24
cp 1
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setVersionTag() 0 3 1
A getLastTag() 0 5 1
A runCommand() 0 8 2
A addFile() 0 3 1
A status() 0 3 1
A commit() 0 3 1
A getCurrentBranch() 0 5 1
A getCommitDescription() 0 3 1
A getCommitsSinceLastTag() 0 5 2
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