• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / tracy-git-version / 5982422976

26 Aug 2023 02:55AM UTC coverage: 90.041% (-2.2%) from 92.276%
5982422976

Pull #4

github

web-flow
Merge 0c60a3907 into fa82db0dd
Pull Request #4: Feature/git executable

465 of 465 new or added lines in 32 files covered. (100.0%)

434 of 482 relevant lines covered (90.04%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/Repository/LocalDirectory/CommandHandler/GetHeadCommandHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\TracyGitVersion\Repository\LocalDirectory\CommandHandler;
6

7
use SixtyEightPublishers\TracyGitVersion\Exception\GitDirectoryException;
8
use SixtyEightPublishers\TracyGitVersion\Repository\Command\GetHeadCommand;
9
use SixtyEightPublishers\TracyGitVersion\Repository\Entity\CommitHash;
10
use SixtyEightPublishers\TracyGitVersion\Repository\Entity\Head;
11
use SixtyEightPublishers\TracyGitVersion\Repository\LocalDirectory\GitDirectory;
12
use function explode;
13
use function file_get_contents;
14
use function is_readable;
15
use function strlen;
16
use function strpos;
17
use function substr;
18
use function trim;
19

20
final class GetHeadCommandHandler extends AbstractLocalDirectoryCommandHandler
21
{
22
    private bool $useBinary;
23

24
    public function __construct(?GitDirectory $gitDirectory = null, bool $useBinary = false)
25
    {
26
        parent::__construct($gitDirectory);
1✔
27

28
        $this->useBinary = $useBinary;
1✔
29
    }
1✔
30

31
    /**
32
     * @throws GitDirectoryException
33
     */
34
    public function __invoke(GetHeadCommand $command): Head
35
    {
36
        return $this->useBinary ? $this->readUsingBinary() : $this->readFromDirectory();
1✔
37
    }
38

39
    /**
40
     * @throws GitDirectoryException
41
     */
42
    private function readFromDirectory(): Head
43
    {
44
        $headFile = $this->getGitDirectory() . DIRECTORY_SEPARATOR . 'HEAD';
1✔
45

46
        # not versioned
47
        if (!is_readable($headFile) || false === ($content = @file_get_contents($headFile))) {
1✔
48
            return new Head(null, null);
×
49
        }
50

51
        # detached head
52
        if (0 !== strpos($content, 'ref:')) {
1✔
53
            return new Head(null, new CommitHash(trim($content)));
1✔
54
        }
55

56
        $branchParts = explode('/', $content, 3);
1✔
57
        $commitFile = $this->getGitDirectory() . DIRECTORY_SEPARATOR . trim(substr($content, 5, strlen($content)));
1✔
58

59
        return new Head(
1✔
60
            isset($branchParts[2]) ? trim($branchParts[2]) : null,
1✔
61
            is_readable($commitFile) && false !== ($commitHash = @file_get_contents($commitFile)) ? new CommitHash(trim($commitHash)) : null,
1✔
62
        );
63
    }
64

65
    /**
66
     * @throws GitDirectoryException
67
     */
68
    private function readUsingBinary(): Head
69
    {
70
        $commitOutput = $this->getGitDirectory()->executeGitCommand([
1✔
71
            'rev-parse',
1✔
72
            'HEAD',
73
        ]);
74

75
        if (0 !== $commitOutput['code']) {
1✔
76
            return new Head(null, null);
1✔
77
        }
78

79
        $branchOutput = $this->getGitDirectory()->executeGitCommand([
1✔
80
            'rev-parse',
1✔
81
            '--abbrev-ref',
82
            'HEAD',
83
        ]);
84

85
        if (0 !== $branchOutput['code'] || 'HEAD' === $branchOutput['out']) {
1✔
86
            return new Head(null, new CommitHash($commitOutput['out']));
1✔
87
        }
88

89
        return new Head($branchOutput['out'], new CommitHash($commitOutput['out']));
1✔
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc