Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Gitonomy/Git/Reference/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\RuntimeException;
use Gitonomy\Git\Parser\ReferenceParser;
use Gitonomy\Git\Parser\TagParser;
use Gitonomy\Git\Reference;

/**
* Representation of a tag reference.
*
* @author Alexandre Salomé <[email protected]>
* @author Bruce Wells <[email protected]>
*/
class Tag extends Reference
{
Expand Down Expand Up @@ -49,6 +51,32 @@ public function isAnnotated()
return true;
}

/**
* Returns the actual commit associated with the tag, and not the hash of the tag if annotated.
*
* @return Commit
*/
public function getCommit()
{
if ($this->isAnnotated()) {
try {
$output = $this->repository->run('show-ref', ['-d', '--tag', $this->revision]);
$parser = new ReferenceParser();
$parser->parse($output);

foreach ($parser->references as $row) {
list($commitHash, $fullname) = $row;
}

return $this->repository->getCommit($commitHash);
} catch (ProcessException $e) {
// ignore the exception
}
}

return parent::getCommit();
}

/**
* Returns the tagger name.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Gitonomy/Git/Tests/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public function testAnnotatedTag($repository)

$this->assertEquals('heading', $tag->getSubjectMessage(), 'Message heading is correct');
$this->assertEquals("body\nbody", $tag->getBodyMessage(), 'Message body is correct');

$closure = function () {
return parent::getCommit();
};
$parentCommit = $closure->bindTo($tag, Tag::class);
$this->assertNotEquals($parentCommit()->getHash(), $tag->getCommit()->getHash(), 'Tag commit is not the same as main commit');
$this->assertEquals('fbde681b329a39e08b63dc54b341a3274c0380c0', $tag->getCommit()->getHash(), 'Tag commit is correct');
}

/**
Expand Down