2012-05-05 20:06:53 +02:00
|
|
|
<?php
|
|
|
|
class GitRepository
|
|
|
|
{
|
|
|
|
public $path = "";
|
|
|
|
|
|
|
|
function __construct($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetObjectRaw($sha)
|
|
|
|
{
|
|
|
|
return gzuncompress(
|
|
|
|
file_get_contents(
|
|
|
|
sprintf("{$this->path}/objects/%s/%s",
|
|
|
|
substr($sha, 0, 2),
|
|
|
|
substr($sha, 2)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetObject($sha)
|
|
|
|
{
|
|
|
|
list($header, $data) = explode("\0", $this->GetObjectRaw($sha), 2);
|
|
|
|
|
|
|
|
if(strpos($header, " ") !== false)
|
|
|
|
{
|
|
|
|
list($type, $headerdata) = explode(" ", $header, 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$type = $header;
|
|
|
|
$headerdata = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case "commit":
|
2012-05-05 20:21:21 +02:00
|
|
|
return new GitCommit($this, $headerdata, $data);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "blob":
|
2012-05-05 20:21:21 +02:00
|
|
|
return new GitBlob($this, $headerdata, $data);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "tree":
|
2012-05-05 20:21:21 +02:00
|
|
|
return new GitTree($this, $headerdata, $data);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "tag":
|
2012-05-05 20:21:21 +02:00
|
|
|
return new GitTag($this, $headerdata, $data);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
default:
|
2012-05-05 20:21:21 +02:00
|
|
|
return new GitObject($this, $headerdata, $data);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 21:40:16 +02:00
|
|
|
|
|
|
|
function GetBranch($name)
|
|
|
|
{
|
2012-05-05 22:19:35 +02:00
|
|
|
$filename = "{$this->path}/refs/heads/{$name}";
|
|
|
|
|
|
|
|
if(file_exists($filename))
|
|
|
|
{
|
|
|
|
$sha = trim(file_get_contents($filename));
|
|
|
|
return new GitBranch($this, $sha);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-05 23:01:03 +02:00
|
|
|
throw new GitBranchNotFoundException("The '{$name}' branch does not exist.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetTag($name)
|
|
|
|
{
|
|
|
|
$filename = "{$this->path}/refs/tags/{$name}";
|
|
|
|
|
|
|
|
if(file_exists($filename))
|
|
|
|
{
|
|
|
|
$sha = trim(file_get_contents($filename));
|
|
|
|
return $this->GetObject($sha);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new GitTagNotFoundException("The '{$name}' tag does not exist.");
|
2012-05-05 22:19:35 +02:00
|
|
|
}
|
2012-05-05 21:40:16 +02:00
|
|
|
}
|
2012-05-05 23:34:24 +02:00
|
|
|
|
|
|
|
function GetObjectForPath($origin, $path)
|
|
|
|
{
|
|
|
|
$path_parts = explode("/", $path);
|
|
|
|
$total_parts = count($path_parts);
|
|
|
|
$current_part = 0;
|
|
|
|
|
|
|
|
if(!($origin instanceof GitTree))
|
|
|
|
{
|
|
|
|
$origin = $this->GetObject($origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($origin instanceof GitTree)
|
|
|
|
{
|
|
|
|
$current_tree = $origin;
|
|
|
|
|
|
|
|
for($i = 0; $i < $total_parts; $i++)
|
|
|
|
{
|
|
|
|
foreach($current_tree->elements as $element)
|
|
|
|
{
|
|
|
|
if($element->filename == $path_parts[$current_part])
|
|
|
|
{
|
|
|
|
$current_tree = $this->GetObject($element->hash);
|
|
|
|
pretty_dump($current_tree);
|
|
|
|
|
|
|
|
if($current_part != ($total_parts - 1) && !($current_tree instanceof GitTree))
|
|
|
|
{
|
|
|
|
throw new GitInvalidElementException("Encountered a non-GitTree object while walking the specified path.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_part += 1;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
pretty_dump($path_parts[$current_part]);
|
|
|
|
pretty_dump($current_tree);
|
|
|
|
throw new GitPathNotFoundException("The specified path was not found in the specified origin.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $current_tree;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new GitInvalidOriginException("You can only use a GitTree hash as origin.");
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 20:06:53 +02:00
|
|
|
}
|