2012-05-05 20:06:53 +02:00
|
|
|
<?php
|
|
|
|
class GitRepository
|
|
|
|
{
|
|
|
|
public $path = "";
|
|
|
|
|
|
|
|
function __construct($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetObjectRaw($sha)
|
|
|
|
{
|
2012-05-06 01:55:54 +02:00
|
|
|
$filename = sprintf("{$this->path}/objects/%s/%s", substr($sha, 0, 2), substr($sha, 2));
|
|
|
|
return gzuncompress(file_get_contents($filename));
|
2012-05-05 20:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function GetObject($sha)
|
|
|
|
{
|
2012-05-06 01:55:54 +02:00
|
|
|
$filename = sprintf("{$this->path}/objects/%s/%s", substr($sha, 0, 2), substr($sha, 2));
|
|
|
|
|
|
|
|
if(file_exists($filename))
|
|
|
|
{
|
|
|
|
return $this->CreateObject($this->GetObjectRaw($sha), null, null, $sha);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->FindPackedObject($sha);
|
|
|
|
}
|
2012-05-06 01:20:51 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 01:55:54 +02:00
|
|
|
function CreateObject($data, $type = null, $size = null, $sha)
|
2012-05-06 01:20:51 +02:00
|
|
|
{
|
|
|
|
if($type == null && $size == null)
|
|
|
|
{
|
|
|
|
list($header, $data) = explode("\0", $data, 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case OBJ_BLOB:
|
|
|
|
$typestring = "blob";
|
|
|
|
break;
|
|
|
|
case OBJ_TREE:
|
|
|
|
$typestring = "tree";
|
|
|
|
break;
|
|
|
|
case OBJ_TAG:
|
|
|
|
$typestring = "tag";
|
|
|
|
break;
|
|
|
|
case OBJ_COMMIT:
|
|
|
|
$typestring = "commit";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new GitUnknownTypeException("The specified type is not valid for this function.");
|
|
|
|
}
|
|
|
|
$header = "{$typestring} {$size}";
|
|
|
|
}
|
2012-05-05 20:06:53 +02:00
|
|
|
|
|
|
|
if(strpos($header, " ") !== false)
|
|
|
|
{
|
|
|
|
list($type, $headerdata) = explode(" ", $header, 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$type = $header;
|
|
|
|
$headerdata = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case "commit":
|
2012-05-06 01:55:54 +02:00
|
|
|
return new GitCommit($this, $headerdata, $data, $sha);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "blob":
|
2012-05-06 01:55:54 +02:00
|
|
|
return new GitBlob($this, $headerdata, $data, $sha);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "tree":
|
2012-05-06 01:55:54 +02:00
|
|
|
return new GitTree($this, $headerdata, $data, $sha);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
case "tag":
|
2012-05-06 01:55:54 +02:00
|
|
|
return new GitTag($this, $headerdata, $data, $sha);
|
2012-05-05 20:06:53 +02:00
|
|
|
break;
|
|
|
|
default:
|
2012-05-06 01:55:54 +02:00
|
|
|
return new GitObject($this, $headerdata, $data, $sha);
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 01:26:59 +02:00
|
|
|
function GetBranches()
|
|
|
|
{
|
|
|
|
$branches = array();
|
|
|
|
|
|
|
|
foreach(scandir("{$this->path}/refs/heads/") as $branch)
|
|
|
|
{
|
|
|
|
if($branch != "." && $branch != "..")
|
|
|
|
{
|
|
|
|
$branches[$branch] = $this->GetBranch($branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $branches;
|
|
|
|
}
|
|
|
|
|
2012-05-05 23:01:03 +02:00
|
|
|
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
|
|
|
|
2012-05-06 01:26:59 +02:00
|
|
|
function GetTags()
|
|
|
|
{
|
|
|
|
$tags = array();
|
|
|
|
|
|
|
|
foreach(scandir("{$this->path}/refs/tags/") as $tag)
|
|
|
|
{
|
|
|
|
if($tag != "." && $tag != "..")
|
|
|
|
{
|
|
|
|
$tags[$tag] = $this->GetTag($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2012-05-06 01:55:54 +02:00
|
|
|
function GetPacks()
|
|
|
|
{
|
|
|
|
$packs = array();
|
|
|
|
|
|
|
|
foreach(scandir("{$this->path}/objects/pack/") as $pack)
|
|
|
|
{
|
|
|
|
if($pack != "." && $pack != ".." && substr($pack, strlen($pack) - 5) != ".pack")
|
|
|
|
{
|
|
|
|
$pack_name = substr($pack, 0, strlen($pack) - 4);
|
|
|
|
$packs[] = $pack_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $packs;
|
|
|
|
}
|
|
|
|
|
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-06 01:55:54 +02:00
|
|
|
|
|
|
|
function FindPackedObject($sha)
|
|
|
|
{
|
|
|
|
foreach($this->GetPacks() as $item)
|
|
|
|
{
|
|
|
|
$pack = new GitPack($this, $item);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return $pack->UnpackObject($sha);
|
|
|
|
}
|
|
|
|
catch (GitObjectNotFoundException $e)
|
|
|
|
{
|
|
|
|
// Silently ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 20:06:53 +02:00
|
|
|
}
|