Add blob and blob_plain support
Add blob and blob_plain support

--- a/gitphp.lib.php
+++ b/gitphp.lib.php
@@ -242,13 +242,46 @@
 	return "unknown";
 }
 
+function git_get_hash_by_path($project,$base,$path,$type)
+{
+	global $gitphp_conf;
+	$tree = $base;
+	$parts = array();
+	$tok = strtok($path,"/");
+	while ($tok !== null) {
+		$parts[] = $tok;
+		$tok = strtok("/");
+	}
+	$partcount = count($parts);
+	foreach ($parts as $i => $part) {
+		$entries = array();
+		$lsout = shell_exec("env GIT_DIR=" . $project . " " . $gitphp_conf['gitbin'] . "git-ls-tree " . $tree);
+		$tok = strtok($lsout,"\n");
+		while ($tok !== false) {
+			$entries[] = $tok;
+			$tok = strtok("\n");
+		}
+		foreach ($entries as $j => $line) {
+			if (ereg("^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$",$tok,$regs)) {
+				if ($regs[4] == $part) {
+					if ($i == ($partcount)-1)
+						return $regs[3];
+					if ($regs[2] == "tree")
+						$tree = $regs[3];
+					break;
+				}
+			}
+		}
+	}
+}
+
 function git_tree($projectroot,$project,$hash,$file,$hashbase)
 {
 	global $gitphp_conf,$tpl;
 	if (!isset($hash)) {
 		$hash = git_read_head($projectroot . $project);
 		if (isset($file))
-			$hash = git_get_hash_by_path(($hashbase?$hashbase:$hash),$file,"tree");
+			$hash = git_get_hash_by_path($projectroot . $project, ($hashbase?$hashbase:$hash),$file,"tree");
 			if (!isset($hashbase))
 				$hashbase = $hash;
 	}
@@ -1110,5 +1143,61 @@
 	$tpl->display("rss_footer.tpl");
 }
 
+function git_blob($projectroot, $project, $hash, $file, $hashbase)
+{
+	global $gitphp_conf,$tpl;
+	if (!isset($hash) && isset($file)) {
+		$base = $hashbase ? $hashbase : git_read_head($projectroot . $project);
+		$hash = git_get_hash_by_path($projectroot . $project, $base,$file,"blob");
+	}
+	$catout = shell_exec("env GIT_DIR=" . $projectroot . $project . " " . $gitphp_conf['gitbin'] . "git-cat-file blob " . $hash);
+	if (isset($hashbase) && ($co = git_read_commit($projectroot . $project, $hashbase))) {
+		$tpl->clear_all_assign();
+		$tpl->assign("project",$project);
+		$tpl->assign("hashbase",$hashbase);
+		$tpl->assign("tree",$co['tree']);
+		$tpl->assign("hash",$hash);
+		if (isset($file))
+			$tpl->assign("file",$file);
+		$tpl->assign("title",$co['title']);
+		$tpl->display("blob_nav.tpl");
+	} else {
+		$tpl->clear_all_assign();
+		$tpl->assign("hash",$hash);
+		$tpl->display("blob_emptynav.tpl");
+	}
+	$tpl->clear_all_assign();
+	if (isset($file))
+		$tpl->assign("file",$file);
+	$tpl->display("blob_header.tpl");
+	$nr = 0;
+	$tok = strtok($catout,"\n");
+	while ($tok !== false) {
+		$nr++;
+		/*
+		 * TODO: Convert tabs to spaces
+		 */
+		$tpl->clear_all_assign();
+		$tpl->assign("nr",$nr);
+		$tpl->assign("line",htmlentities($tok));
+		$tpl->display("blob_line.tpl");
+		$tok = strtok("\n");
+	}
+	$tpl->clear_all_assign();
+	$tpl->display("blob_footer.tpl");
+}
+
+function git_blob_plain($projectroot,$project,$hash,$file)
+{
+	global $gitphp_conf;
+	if ($file)
+		$saveas = $file;
+	else
+		$saveas = $hash . ".txt";
+	header("Content-type: text/plain; charset=UTF-8");
+	header("Content-disposition: inline; filename=\"" . $saveas . "\"");
+	echo shell_exec("env GIT_DIR=" . $projectroot . $project . " " . $gitphp_conf['gitbin'] . "git-cat-file blob " . $hash);
+}
+
 ?>
 

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -91,6 +91,13 @@
 					$suppress_headers = TRUE;
 					git_rss($gitphp_conf['projectroot'],$_GET['p']);
 					break;
+				case "blob":
+					git_blob($gitphp_conf['projectroot'],$_GET['p'],$_GET['h'],$_GET['f'],$_GET['hb']);
+					break;
+				case "blob_plain":
+					$suppress_headers = TRUE;
+					git_blob_plain($gitphp_conf['projectroot'],$_GET['p'],$_GET['h'],$_GET['f']);
+					break;
 				default:
 					echo "Unknown action";
 					break;

--- /dev/null
+++ b/templates/blob_emptynav.tpl
@@ -1,1 +1,25 @@
+{*
+ *  blob_emptynav.tpl
+ *  gitphp: A PHP git repository browser
+ *  Component: Blob view emptynav template
+ *
+ *  Copyright (C) 2006 Christopher Han <xiphux@gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *}
+ <div class="page_nav">
+ <br /><br /></div>
+ <div class="title">{$hash}</div>
 

--- /dev/null
+++ b/templates/blob_footer.tpl
@@ -1,1 +1,23 @@
+{*
+ *  blob_footer.tpl
+ *  gitphp: A PHP git repository browser
+ *  Component: Blob view footer template
+ *
+ *  Copyright (C) 2006 Christopher Han <xiphux@gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *}
+ </div>
 

--- /dev/null
+++ b/templates/blob_header.tpl
@@ -1,1 +1,26 @@
+{*
+ *  blob_header.tpl
+ *  gitphp: A PHP git repository browser
+ *  Component: Blob view header template
+ *
+ *  Copyright (C) 2006 Christopher Han <xiphux@gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *}
+ {if $file}
+ <div class="page_path"><b>{$file}</b></div>
+ {/if}
+ <div class="page_body">
 

--- /dev/null
+++ b/templates/blob_line.tpl
@@ -1,1 +1,23 @@
+{*
+ *  blob_line.tpl
+ *  gitphp: A PHP git repository browser
+ *  Component: Blob view line template
+ *
+ *  Copyright (C) 2006 Christopher Han <xiphux@gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *}
+ <div class="pre"><a id="l{$nr}" href="#l{$nr}" class="linenr">{$nr}</a> {$line}</div>
 

--- /dev/null
+++ b/templates/blob_nav.tpl
@@ -1,1 +1,31 @@
+{*
+ *  blob_nav.tpl
+ *  gitphp: A PHP git repository browser
+ *  Component: Blob view nav template
+ *
+ *  Copyright (C) 2006 Christopher Han <xiphux@gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *}
+ <div class="page_nav">
+ <a href="{$SCRIPT_NAME}?p={$project}&a=summary">summary</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=shortlog">shortlog</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=log">log</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=commit&h={$hashbase}">commit</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=commitdiff&h={$hashbase}">commitdiff</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=tree&h={$tree}&hb={$hashbase}">tree</a><br />
+ {if $file}
+ <a href="{$SCRIPT_NAME}?p={$project}&a=blob_plain&h={$hash}&f={$file}">plain</a> | <a href="{$SCRIPT_NAME}?p={$project}&a=blob&hb=HEAD&f={$file}">head</a><br />
+ {else}
+ <a href="{$SCRIPT_NAME}?p={$project}&a=blob_plain&h={$hash}">plain</a><br />
+ {/if}
+ </div>
+ <div><a href="{$SCRIPT_NAME}?p={$project}&a=commit&h={$hashbase}" class="title">{$title}</a></div>
 

comments