Abstract exec functions into platform-specific versions
Abstract exec functions into platform-specific versions

--- a/config/gitphp.conf.php
+++ b/config/gitphp.conf.php
@@ -22,10 +22,13 @@
 
 /*
  * gitbin
- * Path to git binaries (don't forget trailing slash!)
- * (Leave blank or comment to just use $PATH)
+ * Path to git binary
+ * For example, /usr/bin/git on Linux
+ * or C:\\Program Files\\Git\\cmd\\git.cmd on Windows
+ * with msysgit.  You can also omit the full path and just
+ * use the executable name to search the user's $PATH.
  */
-$gitphp_conf['gitbin'] = "/usr/bin/";
+$gitphp_conf['gitbin'] = "/usr/bin/git";
 
 /*
  * gittmp

--- a/include/defs.commands.php
+++ b/include/defs.commands.php
@@ -7,13 +7,13 @@
  *  Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
  */
 
-define('GIT_CAT_FILE','git-cat-file');
-define('GIT_DIFF_TREE','git-diff-tree');
-define('GIT_LS_TREE','git-ls-tree');
-define('GIT_REV_LIST','git-rev-list');
-define('GIT_REV_PARSE','git-rev-parse');
-define('GIT_SHOW_REF','git-show-ref');
-define('GIT_TAR_TREE','git-tar-tree');
+define('GIT_CAT_FILE','cat-file');
+define('GIT_DIFF_TREE','diff-tree');
+define('GIT_LS_TREE','ls-tree');
+define('GIT_REV_LIST','rev-list');
+define('GIT_REV_PARSE','rev-parse');
+define('GIT_SHOW_REF','show-ref');
+define('GIT_TAR_TREE','tar-tree');
 
 ?>
 

--- a/include/display.git_history.php
+++ b/include/display.git_history.php
@@ -30,8 +30,7 @@
 	$tpl->display("history_header.tpl");
 	$cmdout = git_history_list($projectroot . $project, $hash, $file);
 	$alternate = FALSE;
-	$lines = explode("\n",$cmdout);
-	foreach ($lines as $i => $line) {
+	foreach ($cmdout as $i => $line) {
 		if (ereg("^([0-9a-fA-F]{40})",$line,$regs))
 			$commit = $regs[1];
 		else if (ereg(":([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$",$line,$regs) && isset($commit)) {

--- a/include/display.git_snapshot.php
+++ b/include/display.git_snapshot.php
@@ -15,15 +15,15 @@
 	if (!isset($hash))
 		$hash = "HEAD";
 	$rname = str_replace(array("/",".git"),array("-",""),$project);
-	$cmd = git_tar_tree($projectroot . $project, $hash, $rname);
+	$tar = git_tar_tree($projectroot . $project, $hash, $rname);
 	if ($gitphp_conf['bzsnapshots'] && function_exists("bzcompress")) {
 		header("Content-Type: application/x-bzip2");
 		header("Content-Disposition: attachment; filename=" . $rname . ".tar.bz2");
-		echo bzcompress(shell_exec($cmd),$gitphp_conf['bzblocksize']);
+		echo bzcompress($tar,$gitphp_conf['bzblocksize']);
 	} else {
 		header("Content-Type: application/x-tar");
 		header("Content-Disposition: attachment; filename=" . $rname . ".tar");
-		echo shell_exec($cmd);
+		echo $tar;
 	}
 }
 

--- a/include/gitutil.git_cat_file.php
+++ b/include/gitutil.git_cat_file.php
@@ -8,14 +8,14 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_cat_file($proj,$hash,$pipeto = NULL, $type = "blob")
 {
-	global $gitphp_conf;
-	$cmd = "env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_CAT_FILE . " " . $type . " " . $hash;
+	$cmd = GIT_CAT_FILE . " " . $type . " " . $hash;
+	$out = git_exec($proj, $cmd);
 	if ($pipeto)
 		$cmd .= " > " . $pipeto;
-	return shell_exec($cmd);
 }
 
 ?>

--- a/include/gitutil.git_diff_tree.php
+++ b/include/gitutil.git_diff_tree.php
@@ -8,14 +8,14 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_diff_tree($proj,$hashes,$renames = FALSE)
 {
-	global $gitphp_conf;
-	$cmd = "env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_DIFF_TREE . " -r ";
+	$cmd = GIT_DIFF_TREE . " -r ";
 	if ($renames)
 		$cmd .= "-M ";
-	return shell_exec($cmd . $hashes);
+	return git_exec($proj, $cmd . $hashes);
 }
 
 ?>

--- a/include/gitutil.git_history_list.php
+++ b/include/gitutil.git_history_list.php
@@ -8,11 +8,20 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_history_list($proj,$hash,$file)
 {
-	global $gitphp_conf;
-	return shell_exec("env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_REV_LIST . " " . $hash . " | env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_DIFF_TREE . " -r --stdin '" . $file . "'");
+	$list = array();
+	$cmd = GIT_REV_LIST . " " . $hash;
+	$out = git_exec($proj, $cmd);
+	$outlist = explode("\n",$out);
+	foreach ($outlist as $i => $line) {
+		$out2 = git_exec($proj, GIT_DIFF_TREE . " -r " . $line . " '" . $file . "'");
+		$out2list = explode("\n",$out2);
+		$list = array_merge($list, $out2list);
+	}
+	return $list;
 }
 
 ?>

--- a/include/gitutil.git_ls_tree.php
+++ b/include/gitutil.git_ls_tree.php
@@ -8,14 +8,14 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_ls_tree($proj,$hash,$nullterm = FALSE)
 {
-	global $gitphp_conf;
-	$cmd = "env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_LS_TREE;
+	$cmd = GIT_LS_TREE;
 	if ($nullterm)
 		$cmd .= " -z";
-	return shell_exec($cmd . " " . $hash);
+	return git_exec($proj, $cmd . " " . $hash);
 }
 
 ?>

--- a/include/gitutil.git_read_head.php
+++ b/include/gitutil.git_read_head.php
@@ -8,11 +8,12 @@
  */
 
 include_once('defs.commands.php');
+include_once('gitutil.git_exec.php');
 
 function git_read_head($proj)
 {
-	global $gitphp_conf;
-	return shell_exec("env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_REV_PARSE . " --verify HEAD");
+	$cmd = GIT_REV_PARSE . " --verify HEAD";
+	return git_exec($proj, $cmd);
 }
 
 ?>

--- a/include/gitutil.git_rev_list.php
+++ b/include/gitutil.git_rev_list.php
@@ -8,18 +8,18 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_rev_list($proj,$head,$count = NULL,$header = FALSE,$parents = FALSE)
 {
-	global $gitphp_conf;
-	$cmd = "env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_REV_LIST . " ";
+	$cmd = GIT_REV_LIST . " ";
 	if ($header)
 		$cmd .= "--header ";
 	if ($parents)
 		$cmd .= "--parents ";
 	if ($count)
 		$cmd .= "--max-count=" . $count;
-	return shell_exec($cmd . " " . $head);
+	return git_exec($proj, $cmd . " " . $head);
 }
 
 ?>

--- a/include/gitutil.git_tar_tree.php
+++ b/include/gitutil.git_tar_tree.php
@@ -8,14 +8,14 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function git_tar_tree($proj,$hash,$rname = NULL)
 {
-	global $gitphp_conf;
-	$cmd = "env GIT_DIR=" . $proj . " " . $gitphp_conf['gitbin'] . GIT_TAR_TREE . " " . $hash;
+	$cmd = GIT_TAR_TREE . " " . $hash;
 	if ($rname)
 		$cmd .= " " . $rname;
-	return $cmd;
+	return git_exec($proj, $cmd);
 }
 
 ?>

--- a/include/gitutil.read_info_ref.php
+++ b/include/gitutil.read_info_ref.php
@@ -8,12 +8,13 @@
  */
 
  include_once('defs.commands.php');
+ include_once('gitutil.git_exec.php');
 
 function read_info_ref($project, $type = "")
 {
-	global $gitphp_conf;
 	$refs = array();
-	$showrefs = shell_exec("env GIT_DIR=" . $project . " " . $gitphp_conf['gitbin'] . GIT_SHOW_REF . " --dereference");
+	$cmd = GIT_SHOW_REF . " --dereference";
+	$showrefs = git_exec($project, $cmd);
 	$lines = explode("\n",$showrefs);
 	foreach ($lines as $no => $line) {
 		if (ereg("^([0-9a-fA-F]{40}) .*" . $type . "/([^\^]+)",$line,$regs)) {

comments