4 * Represents a tree file structure in Git.
6 class Git_Tree
extends Git_Lazy
{
8 * Instance of Git_Repo.
12 * Treeish identifier of the tree. Can be a SHA-1 sum.
18 public function __construct($repo, $kwargs = array()) {
20 foreach ($kwargs as $k => $v) {
25 * Lazy loads the tree's contents.
27 protected function bake() {
28 $tmp = Git_Tree
::construct($this->repo
, $this->id
);
29 $this->contents
= $tmp->contents
;
32 * Constructs and fully initializes a tree.
34 public static function construct($repo, $treeish, $paths = array()) {
35 $output = $repo->git
->lsTree($treeish, $paths);
36 $tree = new Git_Tree($repo, array('id' => $treeish));
37 $tree->constructInitialize($repo, $treeish, $output);
41 * Initializes a tree object based on the output of the ls-tree command.
43 public function constructInitialize($repo, $id, $text) {
46 $this->contents
= array();
47 foreach (explode("\n", $text) as $line) {
48 $out = self
::contentFromString($repo, $line);
50 $this->contents
[] = $out;
55 * Creates a content object (blob or tree) based on the ls-tree command.
57 public function contentFromString($repo, $text) {
58 $text = str_replace("\t", ' ', $text);
59 $bits = explode(' ', $text);
60 if (count($bits) != 4) return;
61 list($mode, $type, $id, $name) = $bits;
64 return new Git_Tree($repo, compact('id', 'mode', 'name'));
66 return new Git_Blob($repo, compact('id', 'mode', 'name'));
70 throw new Git_Exception('Invalid type: ' . $type);
74 * Retrieves a named object in this tree's contents.
75 * @param Filename of object to return. This function does NOT search
77 * @return Null if file not found, otherwise Git_Tree or Git_Commit file.
79 public function get($file) {
80 foreach ($this->contents
as $c) {
81 if ($c->name
== $file) return $c;
85 * Retrieves the basename of this tree.
87 public function basename() {
88 return basename($this->__get('name'));
90 public function __toString() {
91 return '(Git_Tree "'.$this->id
.'")';