<?php
namespace Gitonomy\Git;
use Gitonomy\Git\Exception\LogicException;
class PushReference
{
const ZERO = '0000000000000000000000000000000000000000';
protected $repository;
protected $reference;
protected $before;
protected $after;
protected $isForce;
public function __construct(Repository $repository, $reference, $before, $after)
{
$this->repository = $repository;
$this->reference = $reference;
$this->before = $before;
$this->after = $after;
$this->isForce = $this->getForce();
}
public function getRepository()
{
return $this->repository;
}
public function getReference()
{
return $this->reference;
}
public function getBefore()
{
return $this->before;
}
public function getAfter()
{
return $this->after;
}
public function getLog($excludes = [])
{
return $this->repository->getLog(array_merge(
[$this->getRevision()],
array_map(function ($e) {
return '^'.$e;
}, $excludes)
));
}
public function getRevision()
{
if ($this->isDelete()) {
throw new LogicException('No revision for deletion');
}
if ($this->isCreate()) {
return $this->getAfter();
}
return $this->getBefore().'..'.$this->getAfter();
}
public function isCreate()
{
return $this->isZero($this->before);
}
public function isDelete()
{
return $this->isZero($this->after);
}
public function isForce()
{
return $this->isForce;
}
public function isFastForward()
{
return !$this->isDelete() && !$this->isCreate() && !$this->isForce();
}
protected function isZero($reference)
{
return self::ZERO === $reference;
}
protected function getForce()
{
if ($this->isDelete() || $this->isCreate()) {
return false;
}
$result = $this->repository->run('merge-base', [
$this->before,
$this->after,
]);
return $this->before !== trim($result);
}
}