Move path code into filesystemobject class and path template to a component template
Move path code into filesystemobject class and path template to a component template

--- a/include/controller/Controller_Blame.class.php
+++ b/include/controller/Controller_Blame.class.php
@@ -9,8 +9,6 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-require_once(GITPHP_INCLUDEDIR . 'gitutil.git_path_trees.php');
 
 /**
  * Blame controller class
@@ -110,9 +108,6 @@
 
 		$this->tpl->assign('tree', $hashbase->GetTree());
 
-		$paths = git_path_trees($this->params['hashbase'], $this->params['file']);
-		$this->tpl->assign("paths",$paths);
-
 		$blame = $hash->GetBlame();
 		$this->tpl->assign('blame', $hash->GetBlame());
 	}

--- a/include/controller/Controller_Blob.class.php
+++ b/include/controller/Controller_Blob.class.php
@@ -9,8 +9,6 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-require_once(GITPHP_INCLUDEDIR . 'gitutil.git_path_trees.php');
 
 /**
  * Blob controller class
@@ -158,9 +156,6 @@
 
 		$this->tpl->assign('tree', $hashbase->GetTree());
 
-		$paths = git_path_trees($this->params['hashbase'], $this->params['file']);
-		$this->tpl->assign("paths",$paths);
-
 		if (GitPHP_Config::GetInstance()->GetValue('filemimetype', true)) {
 			$mime = $hash->FileMime();
 			if ($mime)

--- a/include/controller/Controller_Blobdiff.class.php
+++ b/include/controller/Controller_Blobdiff.class.php
@@ -9,8 +9,6 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-require_once(GITPHP_INCLUDEDIR . 'gitutil.git_path_trees.php');
 
 /**
  * Blobdiff controller class
@@ -118,20 +116,18 @@
 			return;
 		}
 
+		$hashbase = $this->project->GetCommit($this->params['hashbase']);
+		$this->tpl->assign('hashbase', $hashbase);
+
+		$hashparent = $this->project->GetBlob($this->params['hashparent']);
+		$hashparent->SetCommit($hashbase);
+		$this->tpl->assign('hashparent', $hashparent);
+
 		$hash = $this->project->GetBlob($this->params['hash']);
 		$this->tpl->assign('hash', $hash);
 
-		$hashparent = $this->project->GetBlob($this->params['hashparent']);
-		$this->tpl->assign('hashparent', $hashparent);
-
-		$hashbase = $this->project->GetCommit($this->params['hashbase']);
-		$this->tpl->assign('hashbase', $hashbase);
-
 		$tree = $hashbase->GetTree();
 		$this->tpl->assign('tree', $tree);
-
-		$paths = git_path_trees($this->params['hashbase'], $this->params['file']);
-		$this->tpl->assign("paths",$paths);
 	}
 
 }

--- a/include/controller/Controller_History.class.php
+++ b/include/controller/Controller_History.class.php
@@ -9,8 +9,6 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-require_once(GITPHP_INCLUDEDIR . 'gitutil.git_path_trees.php');
 
 /**
  * History controller class
@@ -98,9 +96,6 @@
 		$blob = $this->project->GetBlob($blobhash);
 		$blob->SetCommit($co);
 		$this->tpl->assign('blob', $blob);
-
-		$paths = git_path_trees($this->params['hash'], $this->params['file']);
-		$this->tpl->assign("paths",$paths);
 	}
 
 }

--- a/include/controller/Controller_Tree.class.php
+++ b/include/controller/Controller_Tree.class.php
@@ -9,8 +9,6 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-require_once(GITPHP_INCLUDEDIR . 'gitutil.git_path_trees.php');
 
 /**
  * Tree controller class
@@ -118,9 +116,6 @@
 
 		if (!isset($this->params['file']))
 			$this->params['file'] = $tree->GetPath();
-
-		$paths = git_path_trees($this->params['hashbase'], $this->params['file']);
-		$this->tpl->assign("paths",$paths);
 	}
 
 }

--- a/include/git/FilesystemObject.class.php
+++ b/include/git/FilesystemObject.class.php
@@ -58,6 +58,24 @@
 	protected $commit;
 
 	/**
+	 * pathTree
+	 *
+	 * Stores the trees of this object's base path
+	 *
+	 * @access protected
+	 */
+	protected $pathTree;
+
+	/**
+	 * pathTreeRead
+	 *
+	 * Stores whether the trees of the object's base path have been read
+	 *
+	 * @access protected
+	 */
+	protected $pathTreeRead = false;
+
+	/**
 	 * __construct
 	 *
 	 * Instantiates object
@@ -83,7 +101,13 @@
 	 */
 	public function GetName()
 	{
-		return $this->name;
+		if (!empty($this->name))
+			return $this->name;
+
+		if ($this->commit)
+			return basename($this->commit->HashToPath($this->hash));
+
+		return '';
 	}
 
 	/**
@@ -239,5 +263,57 @@
 		$this->commit = $commit;
 	}
 
+	/**
+	 * GetPathTree
+	 *
+	 * Gets the objects of the base path
+	 *
+	 * @access public
+	 * @return array array of tree objects
+	 */
+	public function GetPathTree()
+	{
+		if (!$this->pathTreeRead)
+			$this->ReadPathTree();
+
+		return $this->pathTree;
+	}
+
+	/**
+	 * ReadPathTree
+	 *
+	 * Reads the objects of the base path
+	 *
+	 * @access private
+	 */
+	private function ReadPathTree()
+	{
+		$this->pathTreeRead = true;
+
+		$path = $this->GetPath();
+		
+		if (empty($path)) {
+			/* this is a top level tree, it has no subpath */
+			$this->SetName('[' . $this->project->GetProject() . ']');
+			return;
+		}
+
+		while (($pos = strrpos($path, '/')) !== false) {
+			$path = substr($path, 0, $pos);
+			$pathhash = $this->commit->PathToHash($path);
+			if (!empty($pathhash)) {
+				$parent = $this->project->GetTree($pathhash);
+				$parent->SetName(basename($path));
+				$this->pathTree[] = $parent;
+			}
+		}
+
+		$parent = $this->commit->GetTree();
+		$parent->SetName('[' . $this->project->GetProject() . ']');
+		$this->pathTree[] = $parent;
+
+		$this->pathTree = array_reverse($this->pathTree);
+	}
+
 }
 

--- a/include/gitutil.git_get_hash_by_path.php
+++ /dev/null
@@ -1,35 +1,1 @@
-<?php
-/*
- *  gitutil.git_get_hash_by_path.php
- *  gitphp: A PHP git repository browser
- *  Component: Git utility - get hash from a path
- *
- *  Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
- */
 
-require_once('gitutil.git_ls_tree.php');
-
-function git_get_hash_by_path($base,$path,$type = null)
-{
-	$tree = $base;
-	$parts = explode("/",$path);
-	$partcount = count($parts);
-	foreach ($parts as $i => $part) {
-		$lsout = git_ls_tree($tree);
-		$entries = explode("\n",$lsout);
-		foreach ($entries as $j => $line) {
-			if (preg_match("/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/",$line,$regs)) {
-				if ($regs[4] == $part) {
-					if ($i == ($partcount)-1)
-						return $regs[3];
-					if ($regs[2] == "tree")
-						$tree = $regs[3];
-					break;
-				}
-			}
-		}
-	}
-}
-
-?>
-

--- a/include/gitutil.git_path_tree.php
+++ /dev/null
@@ -1,28 +1,1 @@
-<?php
-/*
- *  gitutil.git_path_tree.php
- *  gitphp: A PHP git repository browser
- *  Component: Git utility - path tree
- *
- *  Copyright (C) 2009 Christopher Han <xiphux@gmail.com>
- */
 
-require_once('gitutil.git_get_hash_by_path.php');
-
-function git_path_tree($base,$filename)
-{
-	if (strlen($filename) < 1)
-		return null;
-	$path = array();
-	$path['full'] = $filename;
-	$spath = $filename;
-	$spos = strrpos($spath, "/");
-	if ($spos !== false)
-		$spath = substr($spath, $spos+1);
-	$path['short'] = $spath;
-	$path['tree'] = git_get_hash_by_path($base, $filename);
-	return $path;
-}
-
-?>
-

--- a/include/gitutil.git_path_trees.php
+++ /dev/null
@@ -1,33 +1,1 @@
-<?php
-/*
- *  gitutil.git_path_trees.php
- *  gitphp: A PHP git repository browser
- *  Component: Git utility - path trees
- *
- *  Copyright (C) 2009 Christopher Han <xiphux@gmail.com>
- */
 
-require_once('gitutil.git_path_tree.php');
-require_once('gitutil.git_get_hash_by_path.php');
-
-function git_path_trees($base,$filename)
-{
-	$paths = array();
-	$path = git_path_tree($base,$filename);
-	if ($path != null)
-		$paths[] = $path;
-	$pos = strrpos($filename, "/");
-	while ($pos !== false) {
-		$filename = substr($filename,0,$pos);
-		
-		$path = git_path_tree($base, $filename);
-		if ($path != null)
-			$paths[] = $path;
-
-		$pos = strrpos($filename, "/");
-	}
-	return array_reverse($paths);
-}
-
-?>
-

--- a/templates/blame.tpl
+++ b/templates/blame.tpl
@@ -21,20 +21,9 @@
  </div>
 
  {include file='title.tpl' titlecommit=$hashbase}
+
+ {include file='path.tpl' pathobject=$hash target='blob'}
  
- <div class="page_path">
-   {* The path to the file, with directories broken into tree links *}
-   <b>
-     <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$hashbase->GetHash()}">[{$project->GetProject()}]</a> / 
-     {foreach from=$paths item=path name=paths}
-       {if $smarty.foreach.paths.last}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob_plain&h={$path.tree}&f={$path.full}">{$path.short}</a>
-       {else}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$path.tree}&f={$path.full}">{$path.short}</a> / 
-       {/if}
-     {/foreach}
-   </b>
- </div>
  <div class="page_body">
  	<table class="code">
 	{foreach from=$hash->GetData(true) item=blobline name=blob}

--- a/templates/blob.tpl
+++ b/templates/blob.tpl
@@ -21,20 +21,9 @@
  </div>
 
  {include file='title.tpl' titlecommit=$hashbase}
- 
- <div class="page_path">
-   {* The path to the file, with directories broken into tree links *}
-   <b>
-     <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$hashbase->GetHash()}">[{$project->GetProject()}]</a> / 
-     {foreach from=$paths item=path name=paths}
-       {if $smarty.foreach.paths.last}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob_plain&h={$path.tree}&f={$path.full}">{$path.short}</a>
-       {else}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$path.tree}&f={$path.full}">{$path.short}</a> / 
-       {/if}
-     {/foreach}
-   </b>
- </div>
+
+{include file='path.tpl' pathobject=$hash target='blobplain'}
+
  <div class="page_body">
    {if $datatag}
      {* We're trying to display an image *}

--- a/templates/blobdiff.tpl
+++ b/templates/blobdiff.tpl
@@ -16,20 +16,9 @@
  </div>
 
  {include file='title.tpl' titlecommit=$hashbase}
+
+ {include file='path.tpl' pathobject=$hashparent target='blob'}
  
- <div class="page_path">
-   {* The path to the file, with directories broken into tree links *}
-   <b>
-     <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$hashbase->GetHash()}">[{$project->GetProject()}]</a> / 
-     {foreach from=$paths item=path name=paths}
-       {if $smarty.foreach.paths.last}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob&h={$path.tree}&hb={$hashbase->GetHash()}&f={$path.full}">{$path.short}</a>
-       {else}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$path.tree}&f={$path.full}">{$path.short}</a> / 
-       {/if}
-     {/foreach}
-   </b>
- </div>
  <div class="page_body">
    <div class="diff_info">
      {* Display the from -> to diff header *}

--- a/templates/history.tpl
+++ b/templates/history.tpl
@@ -16,19 +16,8 @@
 
  {include file='title.tpl' titlecommit=$hash}
 
- <div class="page_path">
-   {* File path *}
-   <b>
-     <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hash->GetHash()}&h={$tree->GetHash()}">[{$project->GetProject()}]</a> / 
-     {foreach from=$paths item=path name=paths}
-       {if $smarty.foreach.paths.last}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob&h={$path.tree}&f={$path.full}">{$path.short}</a>
-       {else}
-         <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hash->GetHash()}&h={$path.tree}&f={$path.full}">{$path.short}</a> / 
-       {/if}
-     {/foreach}
-   </b>
- </div>
+ {include file='path.tpl' pathobject=$blob target='blob'}
+ 
  <table cellspacing="0">
    {* Display each history line *}
    {foreach from=$blob->GetHistory() item=historyitem}

--- /dev/null
+++ b/templates/path.tpl
@@ -1,1 +1,36 @@
+{*
+ * Path
+ *
+ * Path template
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2010 Christopher Han
+ * @package GitPHP
+ * @subpackage Template
+ *}
+<div class="page_path">
+	{if $pathobject}
+		{assign var=pathobjectcommit value=$pathobject->GetCommit()}
+		{foreach from=$pathobject->GetPathTree() item=pathtreepiece}
+			<a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$pathobjectcommit->GetHash()}&h={$pathtreepiece->GetHash()}"><strong>{$pathtreepiece->GetName()}</strong></a> / 
+		{/foreach}
+		{if $pathobject instanceof GitPHP_Blob}
+			{if $target == 'blobplain'}
+				<a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob_plain&h={$pathobject->GetHash()}&hb={$pathobjectcommit->GetHash()}&f={$pathobject->GetPath()}"><strong>{$pathobject->GetName()}</strong></a>
+			{elseif $target == 'blob'}
+				<a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=blob&h={$pathobject->GetHash()}&hb={$pathobjectcommit->GetHash()}&f={$pathobject->GetPath()}"><strong>{$pathobject->GetName()}</strong></a>
+			{else}
+				<strong>{$pathobject->GetName()}</strong>
+			{/if}
+		{else}
+			{if $target == 'tree'}
+				<a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$pathobjectcommit->GetHash()}&h={$pathobject->GetHash()}"><strong>{$pathobject->GetName()}</strong></a> / 
+			{else}
+				<strong>{$pathobject->GetName()}</strong> / 
+			{/if}
+		{/if}
+	{else}
+		&nbsp;
+	{/if}
+</div>
 

--- a/templates/tree.tpl
+++ b/templates/tree.tpl
@@ -14,16 +14,9 @@
    </div>
 
  {include file='title.tpl' titlecommit=$hashbase}
+
+ {include file='path.tpl' pathobject=$tree target='tree'}
  
- {* Path *}
- <div class="page_path">
-   <b>
-     <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}">[{$project->GetProject()}]</a> / 
-     {foreach from=$paths item=path}
-       <a href="{$SCRIPT_NAME}?p={$project->GetProject()}&a=tree&hb={$hashbase->GetHash()}&h={$path.tree}&f={$path.full}">{$path.short}</a> / 
-     {/foreach}
-   </b>
- </div>
  <div class="page_body">
    {* List files *}
    <table cellspacing="0">

comments