From a4d11ef49055a61e8ee1c6a8fb9fed033cab9bae Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 5 May 2012 23:34:24 +0200 Subject: [PATCH] php-libgit path searching --- public_html/libgit/base.php | 3 ++ public_html/libgit/class.repository.php | 48 +++++++++++++++++++++++++ public_html/test.php | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/public_html/libgit/base.php b/public_html/libgit/base.php index 1524b66..52f974c 100644 --- a/public_html/libgit/base.php +++ b/public_html/libgit/base.php @@ -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"); diff --git a/public_html/libgit/class.repository.php b/public_html/libgit/class.repository.php index b35c032..2ba563e 100644 --- a/public_html/libgit/class.repository.php +++ b/public_html/libgit/class.repository.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."); + } + } } diff --git a/public_html/test.php b/public_html/test.php index 479fb25..7def26f 100644 --- a/public_html/test.php +++ b/public_html/test.php @@ -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"));