Skip to content

Commit 5448d79

Browse files
Get real commit for annotated tag (#171)
1 parent 07ea495 commit 5448d79

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Gitonomy/Git/Reference/Tag.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
use Gitonomy\Git\Exception\ProcessException;
1616
use Gitonomy\Git\Exception\RuntimeException;
17+
use Gitonomy\Git\Parser\ReferenceParser;
1718
use Gitonomy\Git\Parser\TagParser;
1819
use Gitonomy\Git\Reference;
1920

2021
/**
2122
* Representation of a tag reference.
2223
*
2324
* @author Alexandre Salomé <alexandre.salome@gmail.com>
25+
* @author Bruce Wells <brucekwells@gmail.com>
2426
*/
2527
class Tag extends Reference
2628
{
@@ -49,6 +51,32 @@ public function isAnnotated()
4951
return true;
5052
}
5153

54+
/**
55+
* Returns the actual commit associated with the tag, and not the hash of the tag if annotated.
56+
*
57+
* @return Commit
58+
*/
59+
public function getCommit()
60+
{
61+
if ($this->isAnnotated()) {
62+
try {
63+
$output = $this->repository->run('show-ref', ['-d', '--tag', $this->revision]);
64+
$parser = new ReferenceParser();
65+
$parser->parse($output);
66+
67+
foreach ($parser->references as $row) {
68+
list($commitHash, $fullname) = $row;
69+
}
70+
71+
return $this->repository->getCommit($commitHash);
72+
} catch (ProcessException $e) {
73+
// ignore the exception
74+
}
75+
}
76+
77+
return parent::getCommit();
78+
}
79+
5280
/**
5381
* Returns the tagger name.
5482
*

tests/Gitonomy/Git/Tests/ReferenceTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ public function testAnnotatedTag($repository)
9797

9898
$this->assertEquals('heading', $tag->getSubjectMessage(), 'Message heading is correct');
9999
$this->assertEquals("body\nbody", $tag->getBodyMessage(), 'Message body is correct');
100+
101+
$closure = function () {
102+
return parent::getCommit();
103+
};
104+
$parentCommit = $closure->bindTo($tag, Tag::class);
105+
$this->assertNotEquals($parentCommit()->getHash(), $tag->getCommit()->getHash(), 'Tag commit is not the same as main commit');
106+
$this->assertEquals('fbde681b329a39e08b63dc54b341a3274c0380c0', $tag->getCommit()->getHash(), 'Tag commit is correct');
100107
}
101108

102109
/**

0 commit comments

Comments
 (0)