mirror of
https://github.com/php/karma.git
synced 2026-03-26 02:02:19 +01:00
92 lines
2.2 KiB
PHP
92 lines
2.2 KiB
PHP
<?php
|
|
namespace Git;
|
|
|
|
class PushInformation
|
|
{
|
|
|
|
private $hook = null;
|
|
|
|
public function __construct(ReceiveHook $hook)
|
|
{
|
|
$this->hook = $hook;
|
|
}
|
|
|
|
/**
|
|
* Returns the common ancestor revision for two given revisions
|
|
*
|
|
* Returns false if no sha1 was returned. Throws an exception if calling
|
|
* git fails.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
protected function mergeBase($oldrev, $newrev)
|
|
{
|
|
$baserev = \Git::gitExec('merge-base %s %s', escapeshellarg($oldrev), escapeshellarg($newrev));
|
|
|
|
$baserev = trim($baserev);
|
|
|
|
|
|
if (40 != strlen($baserev)) {
|
|
return false;
|
|
}
|
|
|
|
return $baserev;
|
|
}
|
|
|
|
/**
|
|
* Returns true if merging $newrev would be fast forward
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isFastForward()
|
|
{
|
|
$result = $this->hook->mapInput(
|
|
function ($oldrev, $newrev) {
|
|
if ($oldrev == \Git::NULLREV) {
|
|
return true;
|
|
}
|
|
return $oldrev == $this->mergeBase($oldrev, $newrev);
|
|
});
|
|
|
|
return array_reduce($result, function($a, $b) { return $a && $b; }, true);
|
|
}
|
|
|
|
/**
|
|
* Returns true if updating the refs would fail if push is not forced.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isForced()
|
|
{
|
|
return !$this->isFastForward();
|
|
}
|
|
|
|
public function isNewBranch()
|
|
{
|
|
$result = $this->hook->mapInput(
|
|
function($oldrev, $newrev) {
|
|
return $oldrev == \Git::NULLREV;
|
|
});
|
|
return array_reduce($result, function($a, $b) { return $a || $b; }, false);
|
|
}
|
|
|
|
public function isTag()
|
|
{
|
|
$result = $this->hook->mapInput(
|
|
function($oldrev, $newrev, $refname) {
|
|
if (preg_match('@^refs/tags/.+@i', $refname)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
return array_reduce($result, function($a, $b) { return $a || $b; }, false);
|
|
}
|
|
|
|
public function getBranches () {
|
|
return $this->hook->mapInput(function ($oldrev, $newrev, $refname) {
|
|
return $refname;
|
|
});
|
|
}
|
|
}
|