Turn git exe into a singleton
Turn git exe into a singleton

--- a/include/git/Archive.class.php
+++ b/include/git/Archive.class.php
@@ -390,8 +390,6 @@
 			return true;
 		}
 
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 
 		switch ($this->format) {
@@ -408,8 +406,7 @@
 		$args[] = '--prefix=' . $this->GetPrefix();
 		$args[] = $this->objectHash;
 
-		$this->handle = $exe->Open(GIT_ARCHIVE, $args);
-		unset($exe);
+		$this->handle = GitPHP_GitExe::GetInstance()->Open($this->GetProject()->GetPath(), GIT_ARCHIVE, $args);
 
 		if ($this->format == GITPHP_COMPRESS_GZ) {
 			// hack to get around the fact that gzip files

--- a/include/git/Blob.class.php
+++ b/include/git/Blob.class.php
@@ -133,13 +133,11 @@
 		$this->dataRead = true;
 
 		if ($this->GetProject()->GetCompat()) {
-			$exe = new GitPHP_GitExe($this->GetProject());
-
 			$args = array();
 			$args[] = 'blob';
 			$args[] = $this->hash;
 
-			$this->data = $exe->Execute(GIT_CAT_FILE, $args);
+			$this->data = GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_CAT_FILE, $args);
 		} else {
 			$this->data = $this->GetProject()->GetObject($this->hash);
 		}
@@ -417,15 +415,13 @@
 	{
 		$this->historyRead = true;
 
-		$exe = new GitPHP_GitExe($this->GetProject());
-		
 		$args = array();
 		if ($this->commitHash)
 			$args[] = $this->commitHash;
 		else
 			$args[] = 'HEAD';
 		$args[] = '|';
-		$args[] = $exe->GetBinary();
+		$args[] = GitPHP_GitExe::GetInstance()->GetBinary();
 		$args[] = '--git-dir=' . $this->GetProject()->GetPath();
 		$args[] = GIT_DIFF_TREE;
 		$args[] = '-r';
@@ -433,7 +429,7 @@
 		$args[] = '--';
 		$args[] = $this->GetPath();
 		
-		$historylines = explode("\n", $exe->Execute(GIT_REV_LIST, $args));
+		$historylines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_REV_LIST, $args));
 
 		$commitHash = null;
 		foreach ($historylines as $line) {
@@ -478,8 +474,6 @@
 	{
 		$this->blameRead = true;
 
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 		$args[] = '-s';
 		$args[] = '-l';
@@ -490,7 +484,7 @@
 		$args[] = '--';
 		$args[] = $this->GetPath();
 
-		$blamelines = explode("\n", $exe->Execute(GIT_BLAME, $args));
+		$blamelines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_BLAME, $args));
 
 		$lastcommit = '';
 		foreach ($blamelines as $line) {

--- a/include/git/Commit.class.php
+++ b/include/git/Commit.class.php
@@ -557,15 +557,13 @@
 		if ($this->GetProject()->GetCompat()) {
 
 			/* get data from git_rev_list */
-			$exe = new GitPHP_GitExe($this->GetProject());
 			$args = array();
 			$args[] = '--header';
 			$args[] = '--parents';
 			$args[] = '--max-count=1';
 			$args[] = '--abbrev-commit';
 			$args[] = $this->hash;
-			$ret = $exe->Execute(GIT_REV_LIST, $args);
-			unset($exe);
+			$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_REV_LIST, $args);
 
 			$lines = explode("\n", $ret);
 
@@ -727,11 +725,10 @@
 	{
 		$this->containingTagRead = true;
 
-		$exe = new GitPHP_GitExe($this->GetProject());
 		$args = array();
 		$args[] = '--tags';
 		$args[] = $this->hash;
-		$revs = explode("\n", $exe->Execute(GIT_NAME_REV, $args));
+		$revs = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_NAME_REV, $args));
 
 		foreach ($revs as $revline) {
 			if (preg_match('/^([0-9a-fA-F]{40})\s+tags\/(.+)(\^[0-9]+|\~[0-9]+)$/', $revline, $regs)) {
@@ -815,15 +812,13 @@
 	 */
 	private function ReadHashPathsGit()
 	{
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 		$args[] = '--full-name';
 		$args[] = '-r';
 		$args[] = '-t';
 		$args[] = $this->hash;
 
-		$lines = explode("\n", $exe->Execute(GIT_LS_TREE, $args));
+		$lines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_LS_TREE, $args));
 
 		foreach ($lines as $line) {
 			if (preg_match("/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/", $line, $regs)) {
@@ -936,8 +931,6 @@
 		if (empty($pattern))
 			return;
 
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 		$args[] = '-I';
 		$args[] = '--full-name';
@@ -947,7 +940,7 @@
 		$args[] = '\'' . preg_quote($pattern) . '\'';
 		$args[] = $this->hash;
 
-		$lines = explode("\n", $exe->Execute(GIT_GREP, $args));
+		$lines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_GREP, $args));
 
 		$results = array();
 

--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -34,17 +34,19 @@
  */
 class GitPHP_GitExe
 {
-	/**
-	 * project
-	 *
-	 * Stores the project internally
+
+	/**
+	 * instance
+	 *
+	 * Stores the singleton instance
 	 *
 	 * @access protected
-	 */
-	protected $project;
-
-	/**
-	 * bin
+	 * @static
+	 */
+	protected static $instance;
+
+	/**
+	 * binary
 	 *
 	 * Stores the binary path internally
 	 *
@@ -53,52 +55,51 @@
 	protected $binary;
 
 	/**
+	 * GetInstance
+	 *
+	 * Returns the singleton instance
+	 *
+	 * @access public
+	 * @static
+	 * @return mixed instance of git exe classe
+	 */
+	public static function GetInstance()
+	{
+		if (!self::$instance) {
+			self::$instance = new GitPHP_GitExe();
+		}
+		return self::$instance;
+	}
+
+	/**
+	 * DestroyInstance
+	 *
+	 * Releases the singleton instance
+	 *
+	 * @access public
+	 * @static
+	 */
+	public static function DestroyInstance()
+	{
+		self::$instance = null;
+	}
+
+	/**
 	 * __construct
 	 *
 	 * Constructor
 	 *
+	 * @access protected
 	 * @param string $binary path to git binary
-	 * @param mixed $project project to operate on
 	 * @return mixed git executable class
 	 */
-	public function __construct($project = null)
+	protected function __construct()
 	{
 		$binary = GitPHP_Config::GetInstance()->GetValue('gitbin');
 		if (empty($binary)) {
-			$this->binary = GitPHP_GitExe::DefaultBinary();
-		} else {
-			$this->binary = $binary;
-		}
-
-		$this->SetProject($project);
-	}
-
-	/**
-	 * GetProject
-	 *
-	 * Gets the project
-	 *
-	 * @access public
-	 * @return mixed project
-	 */
-	public function GetProject()
-	{
-		return GitPHP_ProjectList::GetInstance()->GetProject($this->project);
-	}
-
-	/**
-	 * SetProject
-	 *
-	 * Sets the project for this executable
-	 *
-	 * @param mixed $project project to set
-	 */
-	public function SetProject($project = null)
-	{
-		if ($project)
-			$this->project = $project->GetProject();
-		else
-			$this->project = null;
+			$binary = GitPHP_GitExe::DefaultBinary();
+		}
+		$this->binary = $binary;
 	}
 
 	/**
@@ -106,13 +107,14 @@
 	 *
 	 * Executes a command
 	 *
+	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
 	 * @param array $args arguments
 	 * @return string result of command
 	 */
-	public function Execute($command, $args)
-	{
-		$fullCommand = $this->CreateCommand($command, $args);
+	public function Execute($projectPath, $command, $args)
+	{
+		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
 		GitPHP_Log::GetInstance()->Log('Begin executing "' . $fullCommand . '"');
 
@@ -129,13 +131,14 @@
 	 *
 	 * Opens a resource to a command
 	 *
+	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
 	 * @param array $args arguments
 	 * @return resource process handle
 	 */
-	public function Open($command, $args, $mode = 'r')
-	{
-		$fullCommand = $this->CreateCommand($command, $args);
+	public function Open($projectPath, $command, $args, $mode = 'r')
+	{
+		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
 		return popen($fullCommand, $mode);
 	}
@@ -147,15 +150,16 @@
 	 *
 	 * @access protected
 	 *
+	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
 	 * @param array $args arguments
 	 * @return string result of command
 	 */
-	protected function CreateCommand($command, $args)
+	protected function CreateCommand($projectPath, $command, $args)
 	{
 		$gitDir = '';
-		if ($this->project) {
-			$gitDir = '--git-dir=' . $this->GetProject()->GetPath();
+		if (!empty($projectPath)) {
+			$gitDir = '--git-dir=' . $projectPath;
 		}
 		
 		return $this->binary . ' ' . $gitDir . ' ' . $command . ' ' . implode(' ', $args);

--- a/include/git/Project.class.php
+++ b/include/git/Project.class.php
@@ -790,11 +790,10 @@
 	 */
 	private function ReadHeadCommitGit()
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '--verify';
 		$args[] = 'HEAD';
-		$this->head = trim($exe->Execute(GIT_REV_PARSE, $args));
+		$this->head = trim(GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_REV_PARSE, $args));
 	}
 
 	/**
@@ -889,21 +888,17 @@
 	 */
 	private function ReadEpochGit()
 	{
-		$exe = new GitPHP_GitExe($this);
-
 		$args = array();
 		$args[] = '--format="%(committer)"';
 		$args[] = '--sort=-committerdate';
 		$args[] = '--count=1';
 		$args[] = 'refs/heads';
 
-		$epochstr = trim($exe->Execute(GIT_FOR_EACH_REF, $args));
+		$epochstr = trim(GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_FOR_EACH_REF, $args));
 
 		if (preg_match('/ (\d+) [-+][01]\d\d\d$/', $epochstr, $regs)) {
 			$this->epoch = $regs[1];
 		}
-
-		unset($exe);
 	}
 
 	/**
@@ -1135,13 +1130,11 @@
 	 */
 	private function ReadRefListGit()
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '--heads';
 		$args[] = '--tags';
 		$args[] = '--dereference';
-		$ret = $exe->Execute(GIT_SHOW_REF, $args);
-		unset($exe);
+		$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_SHOW_REF, $args);
 
 		$lines = explode("\n", $ret);
 
@@ -1316,7 +1309,6 @@
 	 */
 	private function GetTagsGit($count = 0)
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '--sort=-creatordate';
 		$args[] = '--format="%(refname)"';
@@ -1325,8 +1317,7 @@
 		}
 		$args[] = '--';
 		$args[] = 'refs/tags';
-		$ret = $exe->Execute(GIT_FOR_EACH_REF, $args);
-		unset($exe);
+		$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_FOR_EACH_REF, $args);
 
 		$lines = explode("\n", $ret);
 
@@ -1440,7 +1431,6 @@
 	 */
 	private function GetHeadsGit($count = 0)
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '--sort=-committerdate';
 		$args[] = '--format="%(refname)"';
@@ -1449,8 +1439,7 @@
 		}
 		$args[] = '--';
 		$args[] = 'refs/heads';
-		$ret = $exe->Execute(GIT_FOR_EACH_REF, $args);
-		unset($exe);
+		$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_FOR_EACH_REF, $args);
 
 		$lines = explode("\n", $ret);
 
@@ -1819,13 +1808,12 @@
 	 */
 	private function AbbreviateHashGit($hash)
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '-1';
 		$args[] = '--format=format:%h';
 		$args[] = $hash;
 
-		$abbrevData = explode("\n", $exe->Execute(GIT_REV_LIST, $args));
+		$abbrevData = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_REV_LIST, $args));
 		if (empty($abbrevData[0])) {
 			return $hash;
 		}
@@ -1936,13 +1924,12 @@
 	 */
 	private function ExpandHashGit($abbrevHash)
 	{
-		$exe = new GitPHP_GitExe($this);
 		$args = array();
 		$args[] = '-1';
 		$args[] = '--format=format:%H';
 		$args[] = $abbrevHash;
 
-		$fullData = explode("\n", $exe->Execute(GIT_REV_LIST, $args));
+		$fullData = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_REV_LIST, $args));
 		if (empty($fullData[0])) {
 			return $abbrevHash;
 		}
@@ -2046,10 +2033,8 @@
 
 		$args = array();
 
-		$exe = new GitPHP_GitExe($this);
-		if ($exe->CanIgnoreRegexpCase())
+		if (GitPHP_GitExe::GetInstance()->CanIgnoreRegexpCase())
 			$args[] = '--regexp-ignore-case';
-		unset($exe);
 
 		$args[] = '--grep=\'' . $pattern . '\'';
 
@@ -2081,10 +2066,8 @@
 
 		$args = array();
 
-		$exe = new GitPHP_GitExe($this);
-		if ($exe->CanIgnoreRegexpCase())
+		if (GitPHP_GitExe::GetInstance()->CanIgnoreRegexpCase())
 			$args[] = '--regexp-ignore-case';
-		unset($exe);
 
 		$args[] = '--author=\'' . $pattern . '\'';
 
@@ -2116,10 +2099,8 @@
 
 		$args = array();
 
-		$exe = new GitPHP_GitExe($this);
-		if ($exe->CanIgnoreRegexpCase())
+		if (GitPHP_GitExe::GetInstance()->CanIgnoreRegexpCase())
 			$args[] = '--regexp-ignore-case';
-		unset($exe);
 
 		$args[] = '--committer=\'' . $pattern . '\'';
 
@@ -2153,12 +2134,10 @@
 		if ($count < 1)
 			return;
 
-		$exe = new GitPHP_GitExe($this);
-
 		$canSkip = true;
 		
 		if ($skip > 0)
-			$canSkip = $exe->CanSkip();
+			$canSkip = GitPHP_GitExe::GetInstance()->CanSkip();
 
 		if ($canSkip) {
 			$args[] = '--max-count=' . $count;
@@ -2171,14 +2150,14 @@
 
 		$args[] = $hash;
 
-		$revlist = explode("\n", $exe->Execute(GIT_REV_LIST, $args));
+		$revlist = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetPath(), GIT_REV_LIST, $args));
 
 		if (!$revlist[count($revlist)-1]) {
 			/* the last newline creates a null entry */
 			array_splice($revlist, -1, 1);
 		}
 
-		if (($skip > 0) && (!$exe->CanSkip())) {
+		if (($skip > 0) && (!$canSkip)) {
 			return array_slice($revlist, $skip, $count);
 		}
 

--- a/include/git/Ref.class.php
+++ b/include/git/Ref.class.php
@@ -89,12 +89,11 @@
 	 */
 	protected function FindHash()
 	{
-		$exe = new GitPHP_GitExe($this->GetProject());
 		$args = array();
 		$args[] = '--hash';
 		$args[] = '--verify';
 		$args[] = $this->GetRefPath();
-		$hash = trim($exe->Execute(GIT_SHOW_REF, $args));
+		$hash = trim(GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_SHOW_REF, $args));
 
 		if (empty($hash))
 			throw new Exception('Invalid ref ' . $this->GetRefPath());

--- a/include/git/Tag.class.php
+++ b/include/git/Tag.class.php
@@ -364,11 +364,10 @@
 	 */
 	private function ReadDataGit()
 	{
-		$exe = new GitPHP_GitExe($this->GetProject());
 		$args = array();
 		$args[] = '-t';
 		$args[] = $this->GetHash();
-		$ret = trim($exe->Execute(GIT_CAT_FILE, $args));
+		$ret = trim(GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_CAT_FILE, $args));
 		
 		if ($ret === 'commit') {
 			/* light tag */
@@ -383,8 +382,7 @@
 		$args = array();
 		$args[] = 'tag';
 		$args[] = $this->GetName();
-		$ret = $exe->Execute(GIT_CAT_FILE, $args);
-		unset($exe);
+		$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_CAT_FILE, $args);
 
 		$lines = explode("\n", $ret);
 
@@ -427,12 +425,10 @@
 				$this->commitHash = $objectHash;
 				break;
 			case 'tag':
-				$exe = new GitPHP_GitExe($this->GetProject());
 				$args = array();
 				$args[] = 'tag';
 				$args[] = $objectHash;
-				$ret = $exe->Execute(GIT_CAT_FILE, $args);
-				unset($exe);
+				$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_CAT_FILE, $args);
 				$lines = explode("\n", $ret);
 				foreach ($lines as $i => $line) {
 					if (preg_match('/^tag (.+)$/', $line, $regs)) {
@@ -534,13 +530,11 @@
 	 */
 	private function ReadCommit()
 	{
-		$exe = new GitPHP_GitExe($this->GetProject());
 		$args = array();
 		$args[] = '--tags';
 		$args[] = '--dereference';
 		$args[] = $this->refName;
-		$ret = $exe->Execute(GIT_SHOW_REF, $args);
-		unset($exe);
+		$ret = GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_SHOW_REF, $args);
 
 		$lines = explode("\n", $ret);
 

--- a/include/git/Tree.class.php
+++ b/include/git/Tree.class.php
@@ -146,16 +146,14 @@
 	 */
 	private function ReadContentsGit()
 	{
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 		$args[] = '--full-name';
-		if ($exe->CanShowSizeInTree())
+		if (GitPHP_GitExe::GetInstance()->CanShowSizeInTree())
 			$args[] = '-l';
 		$args[] = '-t';
 		$args[] = $this->hash;
 		
-		$lines = explode("\n", $exe->Execute(GIT_LS_TREE, $args));
+		$lines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_LS_TREE, $args));
 
 		foreach ($lines as $line) {
 			if (preg_match("/^([0-9]+) (.+) ([0-9a-fA-F]{40})(\s+[0-9]+|\s+-)?\t(.+)$/", $line, $regs)) {

--- a/include/git/TreeDiff.class.php
+++ b/include/git/TreeDiff.class.php
@@ -133,8 +133,6 @@
 
 		$this->fileDiffs = array();
 
-		$exe = new GitPHP_GitExe($this->GetProject());
-
 		$args = array();
 
 		$args[] = '-r';
@@ -148,7 +146,7 @@
 
 		$args[] = $this->toHash;
 
-		$diffTreeLines = explode("\n", $exe->Execute(GIT_DIFF_TREE, $args));
+		$diffTreeLines = explode("\n", GitPHP_GitExe::GetInstance()->Execute($this->GetProject()->GetPath(), GIT_DIFF_TREE, $args));
 		foreach ($diffTreeLines as $line) {
 			$trimmed = trim($line);
 			if ((strlen($trimmed) > 0) && (substr_compare($trimmed, ':', 0, 1) === 0)) {
@@ -158,8 +156,6 @@
 				}
 			}
 		}
-
-		unset($exe);
 	}
 
 	/**

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -131,11 +131,9 @@
 	/*
 	 * Check for required executables
 	 */
-	$exe = new GitPHP_GitExe(null);
-	if (!$exe->Valid()) {
-		throw new GitPHP_MessageException(sprintf(__('Could not run the git executable "%1$s".  You may need to set the "%2$s" config value.'), $exe->GetBinary(), 'gitbin'), true, 500);
-	}
-	unset($exe);
+	if (!GitPHP_GitExe::GetInstance()->Valid()) {
+		throw new GitPHP_MessageException(sprintf(__('Could not run the git executable "%1$s".  You may need to set the "%2$s" config value.'), GitPHP_GitExe::GetInstance()->GetBinary(), 'gitbin'), true, 500);
+	}
 
 	/*
 	 * Project list
@@ -185,10 +183,11 @@
 
 GitPHP_Log::GetInstance()->Log('MemoryCache count: ' . GitPHP_MemoryCache::GetInstance()->GetCount());
 
+GitPHP_ProjectList::DestroyInstance();
 GitPHP_MemoryCache::DestroyInstance();
-GitPHP_ProjectList::DestroyInstance();
 GitPHP_Resource::DestroyInstance();
 GitPHP_Config::DestroyInstance();
+GitPHP_GitExe::DestroyInstance();
 
 if (GitPHP_Log::GetInstance()->GetEnabled()) {
 	$entries = GitPHP_Log::GetInstance()->GetEntries();

comments