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()) {
36 $output = $repo->git
->lsTree($treeish, $paths);
37 $tree = new Git_Tree($repo, array('id' => $treeish));
38 $tree->constructInitialize($repo, $treeish, $output);
42 * Initializes a tree object based on the output of the ls-tree command.
44 public function constructInitialize($repo, $id, $text) {
47 $this->contents
= array();
48 foreach (explode("\n", $text) as $line) {
49 $out = self
::contentFromString($repo, $line);
51 $this->contents
[] = $out;
56 * Creates a content object (blob or tree) based on the ls-tree command.
58 public function contentFromString($repo, $text) {
59 $text = str_replace("\t", ' ', $text);
60 $bits = explode(' ', $text);
61 if (count($bits) != 4) return;
62 list($mode, $type, $id, $name) = $bits;
65 return new Git_Tree($repo, compact('id', 'mode', 'name'));
67 return new Git_Blob($repo, compact('id', 'mode', 'name'));
71 throw new Git_Exception('Invalid type: ' . $type);
75 * Retrieves a named object in this tree's contents.
76 * @param Filename of object to return. This function does NOT search
78 * @return Null if file not found, otherwise Git_Tree or Git_Commit file.
80 public function get($file) {
81 foreach ($this->contents
as $c) {
82 if ($c->name
== $file) return $c;
86 * Retrieves the basename of this tree.
88 public function basename() {
89 return basename($this->__get('name'));
91 public function __toString() {
92 return '(Git_Tree "'.$this->id
.'")';