Cache git exe version
[gitphp.git] / include / git / GitExe.class.php
blob:a/include/git/GitExe.class.php -> blob:b/include/git/GitExe.class.php
--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -1,7 +1,61 @@
 <?php
-/**
- * GitPHP GitExe
- *
+
+/**
+ * git cat-file constant
+ */
+define('GIT_CAT_FILE','cat-file');
+
+/**
+ * git diff-tree constant
+ */
+define('GIT_DIFF_TREE','diff-tree');
+
+/**
+ * git ls-tree constant
+ */
+define('GIT_LS_TREE','ls-tree');
+
+/**
+ * git rev-list constant
+ */
+define('GIT_REV_LIST','rev-list');
+
+/**
+ * git rev-parse constant
+ */
+define('GIT_REV_PARSE','rev-parse');
+
+/**
+ * git show-ref constant
+ */
+define('GIT_SHOW_REF','show-ref');
+
+/**
+ * git archive constant
+ */
+define('GIT_ARCHIVE','archive');
+
+/**
+ * git grep constant
+ */
+define('GIT_GREP','grep');
+
+/**
+ * git blame constant
+ */
+define('GIT_BLAME','blame');
+
+/**
+ * git name-rev constant
+ */
+define('GIT_NAME_REV','name-rev');
+
+/**
+ * git for-each-ref constant
+ */
+define('GIT_FOR_EACH_REF','for-each-ref');
+
+/**
  * Class to wrap git executable
  *
  * @author Christopher Han <xiphux@gmail.com>
@@ -9,111 +63,65 @@
  * @package GitPHP
  * @subpackage Git
  */
-
-/**
- * Constants for git commands
- */
-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_ARCHIVE','archive');
-define('GIT_GREP','grep');
-define('GIT_BLAME','blame');
-define('GIT_NAME_REV','name-rev');
-define('GIT_FOR_EACH_REF','for-each-ref');
-define('GIT_CONFIG','config');
-
-/**
- * Git Executable class
- *
- * @package GitPHP
- * @subpackage Git
- */
-class GitPHP_GitExe
+class GitPHP_GitExe implements GitPHP_Observable_Interface
 {
 
 	/**
-	 * instance
-	 *
-	 * Stores the singleton instance
-	 *
-	 * @access protected
-	 * @static
-	 */
-	protected static $instance;
-
-	/**
-	 * binary
-	 *
-	 * Stores the binary path internally
-	 *
-	 * @access protected
+	 * The binary path
+	 *
+	 * @var string
 	 */
 	protected $binary;
 	
 	/**
-	 * version
-	 *
-	 * Stores the binary version internally
-	 *
-	 * @access protected
+	 * The binary version
+	 *
+	 * @var string
 	 */
 	protected $version;
 
 	/**
-	 * versionRead
-	 *
-	 * Stores whether the version has been read
-	 *
-	 * @access protected
+	 * Whether the version has been read
+	 *
+	 * @var boolean
 	 */
 	protected $versionRead = false;
 
 	/**
-	 * 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
-	 *
+	 * Observers
+	 *
+	 * @var GitPHP_Observer_Interface[]
+	 */
+	protected $observers = array();
+
+	/**
+	 * Whether the exec function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $execAllowed = null;
+
+	/**
+	 * Whether the shell_exec function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $shellExecAllowed = null;
+
+	/**
+	 * Whether the popen function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $popenAllowed = null;
+
+	/**
 	 * Constructor
 	 *
-	 * @access protected
 	 * @param string $binary path to git binary
-	 * @return mixed git executable class
-	 */
-	protected function __construct()
-	{
-		$binary = GitPHP_Config::GetInstance()->GetValue('gitbin');
+	 */
+	public function __construct($binary = '')
+	{
 		if (empty($binary)) {
 			$binary = GitPHP_GitExe::DefaultBinary();
 		}
@@ -121,75 +129,79 @@
 	}
 
 	/**
-	 * Execute
-	 *
 	 * Executes a command
 	 *
 	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
-	 * @param array $args arguments
+	 * @param string[] $args arguments
 	 * @return string result of command
 	 */
 	public function Execute($projectPath, $command, $args)
 	{
+		if ($this->shellExecAllowed === null) {
+			$this->shellExecAllowed = GitPHP_Util::FunctionAllowed('shell_exec');
+			if (!$this->shellExecAllowed) {
+				throw new GitPHP_DisabledFunctionException('shell_exec');
+			}
+		}
+
 		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
-		GitPHP_Log::GetInstance()->Log('Begin executing "' . $fullCommand . '"');
+		$this->Log('Begin executing "' . $fullCommand . '"');
 
 		$ret = shell_exec($fullCommand);
 
-		GitPHP_Log::GetInstance()->Log('Finish executing "' . $fullCommand . '"' .
+		$this->Log('Finish executing "' . $fullCommand . '"' .
 			"\nwith result: " . $ret);
 
 		return $ret;
 	}
 
 	/**
-	 * Open
-	 *
 	 * Opens a resource to a command
 	 *
 	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
-	 * @param array $args arguments
+	 * @param string[] $args arguments
+	 * @param string $mode process open mode
 	 * @return resource process handle
 	 */
 	public function Open($projectPath, $command, $args, $mode = 'r')
 	{
+		if ($this->popenAllowed === null) {
+			$this->popenAllowed = GitPHP_Util::FunctionAllowed('popen');
+			if (!$this->popenAllowed) {
+				throw new GitPHP_DisabledFunctionException('popen');
+			}
+		}
+
 		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
 		return popen($fullCommand, $mode);
 	}
 
 	/**
-	 * BuildCommand
-	 *
 	 * Creates a command
-	 *
-	 * @access protected
 	 *
 	 * @param string $projectPath path to project
 	 * @param string $command the command to execute
-	 * @param array $args arguments
-	 * @return string result of command
+	 * @param string[] $args arguments
+	 * @return string full executable string
 	 */
 	protected function CreateCommand($projectPath, $command, $args)
 	{
 		$gitDir = '';
 		if (!empty($projectPath)) {
-			$gitDir = '--git-dir=' . $projectPath;
+			$gitDir = '--git-dir=' . escapeshellarg($projectPath);
 		}
 		
 		return $this->binary . ' ' . $gitDir . ' ' . $command . ' ' . implode(' ', $args);
 	}
 
 	/**
-	 * GetBinary
-	 *
 	 * Gets the binary for this executable
 	 *
 	 * @return string binary
-	 * @access public
 	 */
 	public function GetBinary()
 	{
@@ -197,12 +209,9 @@
 	}
 
 	/**
-	 * GetVersion
-	 *
 	 * Gets the version of the git binary
 	 *
 	 * @return string version
-	 * @access public
 	 */
 	public function GetVersion()
 	{
@@ -213,14 +222,17 @@
 	}
 
 	/**
-	 * ReadVersion
-	 *
 	 * Reads the git version
-	 *
-	 * @access protected
 	 */
 	protected function ReadVersion()
 	{
+		if ($this->shellExecAllowed === null) {
+			$this->shellExecAllowed = GitPHP_Util::FunctionAllowed('shell_exec');
+			if (!$this->shellExecAllowed) {
+				throw new GitPHP_DisabledFunctionException('shell_exec');
+			}
+		}
+
 		$this->versionRead = true;
 
 		$this->version = '';
@@ -233,11 +245,8 @@
 	}
 
 	/**
-	 * CanSkip
-	 *
 	 * Tests if this version of git can skip through the revision list
 	 *
-	 * @access public
 	 * @return boolean true if we can skip
 	 */
 	public function CanSkip()
@@ -256,11 +265,8 @@
 	}
 
 	/**
-	 * CanShowSizeInTree
-	 *
 	 * Tests if this version of git can show the size of a blob when listing a tree
 	 *
-	 * @access public
 	 * @return true if we can show sizes
 	 */
 	public function CanShowSizeInTree()
@@ -283,11 +289,8 @@
 	}
 
 	/**
-	 * CanIgnoreRegexpCase
-	 *
 	 * Tests if this version of git has the regexp tuning option to ignore regexp case
 	 *
-	 * @access public
 	 * @return true if we can ignore regexp case
 	 */
 	public function CanIgnoreRegexpCase()
@@ -308,15 +311,19 @@
 	}
 
 	/**
-	 * Valid
-	 *
 	 * Tests if this executable is valid
 	 *
-	 * @access public
 	 * @return boolean true if valid
 	 */
 	public function Valid()
 	{
+		if ($this->execAllowed === null) {
+			$this->execAllowed = GitPHP_Util::FunctionAllowed('exec');
+			if (!$this->execAllowed) {
+				throw new GitPHP_DisabledFunctionException('exec');
+			}
+		}
+
 		if (empty($this->binary))
 			return false;
 
@@ -327,12 +334,57 @@
 	}
 
 	/**
-	 * DefaultBinary
-	 *
+	 * Add a new observer
+	 *
+	 * @param GitPHP_Observer_Interface $observer observer
+	 */
+	public function AddObserver($observer)
+	{
+		if (!$observer)
+			return;
+
+		if (array_search($observer, $this->observers) !== false)
+			return;
+
+		$this->observers[] = $observer;
+	}
+
+	/**
+	 * Remove an observer
+	 *
+	 * @param GitPHP_Observer_Interface $observer observer
+	 */
+	public function RemoveObserver($observer)
+	{
+		if (!$observer)
+			return;
+
+		$key = array_search($observer, $this->observers);
+
+		if ($key === false)
+			return;
+
+		unset($this->observers[$key]);
+	}
+
+	/**
+	 * Log an execution
+	 *
+	 * @param string $message message
+	 */
+	private function Log($message)
+	{
+		if (empty($message))
+			return;
+
+		foreach ($this->observers as $observer) {
+			$observer->ObjectChanged($this, GitPHP_Observer_Interface::LoggableChange, array($message));
+		}
+	}
+
+	/**
 	 * Gets the default binary for the platform
 	 *
-	 * @access public
-	 * @static
 	 * @return string binary
 	 */
 	public static function DefaultBinary()

comments