Add exception for ambiguous abbreviated hash
Add exception for ambiguous abbreviated hash

--- a/include/controller/Controller_Message.class.php
+++ b/include/controller/Controller_Message.class.php
@@ -231,6 +231,13 @@
 			return sprintf('Could not load config file %1$s', $exception->File);
 		}
 
+		if ($exception instanceof GitPHP_AmbiguousHashException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Ambiguous abbreviated hash %1$s'), $exception->Hash);
+			
+			return sprintf('Ambiguous abbreviated hash %1$s', $exception->Hash);
+		}
+
 		return $exception->getMessage();
 	}
 

--- /dev/null
+++ b/include/exception/AmbiguousHashException.class.php
@@ -1,1 +1,34 @@
+<?php
+/**
+ * Custom exception when an ambiguous abbreviated hash is specified
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Exception
+ */
+class GitPHP_AmbiguousHashException extends GitPHP_MessageException
+{
+	/**
+	 * Hash
+	 *
+	 * @var string
+	 */
+	public $Hash;
 
+	/** 
+	 * Constructor
+	 *
+	 * @param string $hash hash
+	 * @param string $message message
+	 * @param integer $code exception code
+	 */
+	public function __construct($hash, $message = '', $code = 0)
+	{
+		$this->Hash = $hash;
+		if (empty($message))
+			$message = 'Ambiguous abbreviated hash %1$s';
+		parent::__construct($message, true, 200, $code);
+	}
+}
+

--- a/include/git/GitObjectLoader.class.php
+++ b/include/git/GitObjectLoader.class.php
@@ -192,22 +192,22 @@
 		}
 
 		$matches = $this->FindHashObjects($abbrevHash);
-		if (count($matches) > 0) {
-			return $matches[0];
-		}
 
 		if (!$this->packsRead) {
 			$this->ReadPacks();
 		}
 
 		foreach ($this->packs as $pack) {
-			$matches = $pack->FindHashes($abbrevHash);
-			if (count($matches) > 0) {
-				return $matches[0];
-			}
-		}
-
-		return $abbrevHash;
+			$matches = array_merge($matches, $pack->FindHashes($abbrevHash));
+		}
+
+		if (count($matches) < 1)
+			return $abbrevHash;
+
+		if (count($matches) > 1)
+			throw new GitPHP_AmbiguousHashException($abbrevHash);
+
+		return $matches[0];
 	}
 
 	/**

comments