• 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

92.5
/src/Repository/LocalDirectory/CommandHandler/GetLatestTagCommandHandler.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\GetLatestTagCommand;
9
use SixtyEightPublishers\TracyGitVersion\Repository\Entity\CommitHash;
10
use SixtyEightPublishers\TracyGitVersion\Repository\Entity\Tag;
11
use SixtyEightPublishers\TracyGitVersion\Repository\LocalDirectory\GitDirectory;
12
use function current;
13
use function file_exists;
14
use function file_get_contents;
15
use function filectime;
16
use function in_array;
17
use function is_readable;
18
use function key;
19
use function krsort;
20
use function scandir;
21
use function sprintf;
22
use function trim;
23

24
final class GetLatestTagCommandHandler extends AbstractLocalDirectoryCommandHandler
25
{
26
    private bool $useBinary;
27

28
    public function __construct(?GitDirectory $gitDirectory = null, bool $useBinary = false)
29
    {
30
        parent::__construct($gitDirectory);
1✔
31

32
        $this->useBinary = $useBinary;
1✔
33
    }
1✔
34

35
    /**
36
     * @throws GitDirectoryException
37
     */
38
    public function __invoke(GetLatestTagCommand $getLatestTag): ?Tag
39
    {
40
        return $this->useBinary ? $this->readUsingBinary() : $this->readFromDirectory();
1✔
41
    }
42

43
    /**
44
     * @throws GitDirectoryException
45
     */
46
    private function readFromDirectory(): ?Tag
47
    {
48
        $tagsDirectory = sprintf('%s%srefs%stags', $this->getGitDirectory(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
1✔
49

50
        if (!file_exists($tagsDirectory)) {
1✔
51
            return null;
1✔
52
        }
53

54
        $latestTagNames = [];
1✔
55
        $latestTimestamp = 0;
1✔
56
        $tagNames = scandir($tagsDirectory);
1✔
57

58
        foreach ($tagNames ?: [] as $tagName) {
1✔
59
            if (in_array($tagName, ['.', '..'], true)) {
1✔
60
                continue;
1✔
61
            }
62

63
            $filename = $tagsDirectory . DIRECTORY_SEPARATOR . $tagName;
1✔
64

65
            if (!is_readable($filename)) {
1✔
66
                continue;
×
67
            }
68

69
            $creationTime = @filectime($filename);
1✔
70

71
            if (false !== $creationTime && $creationTime >= $latestTimestamp) {
1✔
72
                $latestTimestamp = $creationTime;
1✔
73
                $latestTagNames[$tagName] = $filename;
1✔
74
            }
75
        }
76

77
        if (empty($latestTagNames)) {
1✔
78
            return null;
×
79
        }
80

81
        krsort($latestTagNames);
1✔
82

83
        return new Tag((string) key($latestTagNames), new CommitHash(trim((string) @file_get_contents(current($latestTagNames)))));
1✔
84
    }
85

86
    /**
87
     * @throws GitDirectoryException
88
     */
89
    private function readUsingBinary(): ?Tag
90
    {
91
        $tagOutput = $this->getGitDirectory()->executeGitCommand([
1✔
92
            'describe',
1✔
93
            '--tags',
1✔
94
            '$(' . $this->getGitDirectory()->createGitCommand([
1✔
95
                'rev-list',
1✔
96
                '--tags',
97
                '--max-count',
98
                '1',
99
            ]) . ')',
1✔
100
        ]);
101

102
        if (0 !== $tagOutput['code']) {
1✔
103
            return null;
1✔
104
        }
105

106
        $tag = $tagOutput['out'];
1✔
107

108
        $commitOutput = $this->getGitDirectory()->executeGitCommand([
1✔
109
            'show-ref',
1✔
110
            '-s',
1✔
111
            $tag,
1✔
112
        ]);
113

114
        if (0 !== $commitOutput['code']) {
1✔
115
            return null;
×
116
        }
117

118
        return new Tag($tag, new CommitHash($commitOutput['out']));
1✔
119
    }
120
}
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