php-libgit path searching

master
Sven Slootweg 12 years ago
parent 1a3a81adad
commit a4d11ef490

@ -25,6 +25,9 @@ function sha1_from_bin($bin)
class GitBranchNotFoundException extends Exception {}
class GitTagNotFoundException extends Exception {}
class GitInvalidOriginException extends Exception {}
class GitInvalidElementException extends Exception {}
class GitPathNotFoundException extends Exception {}
require(dirname(__FILE__) . "/class.repository.php");
require(dirname(__FILE__) . "/class.branch.php");

@ -81,4 +81,52 @@ class GitRepository
throw new GitTagNotFoundException("The '{$name}' tag does not exist.");
}
}
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.");
}
}
}

@ -2,4 +2,4 @@
require("libgit/base.php");
$repo = new GitRepository("/home/occupy/testrepo.git");
pretty_dump($repo->GetBranch("master")->GetTree());
pretty_dump($repo->GetObjectForPath($repo->GetBranch("master")->GetTree(), "public_html/css/images/derp"));

Loading…
Cancel
Save