<?php
namespace Gitonomy\Git\Reference;
use Gitonomy\Git\Commit;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\RuntimeException;
use Gitonomy\Git\Parser\ReferenceParser;
use Gitonomy\Git\Parser\TagParser;
use Gitonomy\Git\Reference;
class Tag extends Reference
{
protected $data;
public function getName()
{
if (!preg_match('#^refs/tags/(.*)$#', $this->revision, $vars)) {
throw new RuntimeException(sprintf('Cannot extract tag name from "%s"', $this->revision));
}
return $vars[1];
}
public function isAnnotated()
{
try {
$this->repository->run('cat-file', ['tag', $this->revision]);
} catch (ProcessException $e) {
return false;
}
return true;
}
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 list($row)) {
$commitHash = $row;
}
return $this->repository->getCommit($commitHash);
} catch (ProcessException $e) {
}
}
return parent::getCommit();
}
public function getTaggerName()
{
return $this->getData('taggerName');
}
public function getTaggerEmail()
{
return $this->getData('taggerEmail');
}
public function getTaggerDate()
{
return $this->getData('taggerDate');
}
public function getMessage()
{
return $this->getData('message');
}
public function getSubjectMessage()
{
return $this->getData('subjectMessage');
}
public function getBodyMessage()
{
return $this->getData('bodyMessage');
}
public function getGPGSignature()
{
return $this->getData('gpgSignature');
}
public function isSigned()
{
try {
$this->getGPGSignature();
return true;
} catch (\InvalidArgumentException $e) {
return false;
}
}
private function getData($name)
{
if (!$this->isAnnotated()) {
return false;
}
if (isset($this->data[$name])) {
return $this->data[$name];
}
if ($name === 'subjectMessage') {
$lines = explode("\n", $this->getData('message'));
$this->data['subjectMessage'] = reset($lines);
return $this->data['subjectMessage'];
}
if ($name === 'bodyMessage') {
$message = $this->getData('message');
$lines = explode("\n", $message);
array_shift($lines);
array_pop($lines);
$this->data['bodyMessage'] = implode("\n", $lines);
return $this->data['bodyMessage'];
}
$parser = new TagParser();
$result = $this->repository->run('cat-file', ['tag', $this->revision]);
$parser->parse($result);
$this->data['taggerName'] = $parser->taggerName;
$this->data['taggerEmail'] = $parser->taggerEmail;
$this->data['taggerDate'] = $parser->taggerDate;
$this->data['message'] = $parser->message;
$this->data['gpgSignature'] = $parser->gpgSignature;
if (!isset($this->data[$name])) {
throw new \InvalidArgumentException(sprintf('No data named "%s" in Tag.', $name));
}
return $this->data[$name];
}
}