Git name-rev to find the tag on a commit is a whole lot faster than walking the tree ourselves
Git name-rev to find the tag on a commit is a whole lot faster than walking the tree ourselves

--- a/include/git/Commit.class.php
+++ b/include/git/Commit.class.php
@@ -654,25 +654,19 @@
 	{
 		$this->containingTagRead = true;
 
-		$tags = array();
-
-		$projectTags = $this->project->GetTags();
-
-		foreach ($projectTags as $pTag) {
-			$tags[$pTag->GetObject()->GetHash()] = $pTag;
-		}
-
 		$exe = new GitPHP_GitExe($this->project);
 		$args = array();
-		$args[] = 'HEAD';
-		$revs = explode("\n", $exe->Execute(GIT_REV_LIST, $args));
-
-		$this->containingTag = null;
-		foreach ($revs as $rev) {
-			if (isset($tags[$rev]))
-				$this->containingTag = $tags[$rev];
-			if ($rev == $this->hash)
-				break;
+		$args[] = '--tags';
+		$args[] = $this->hash;
+		$revs = explode("\n", $exe->Execute(GIT_NAME_REV, $args));
+
+		foreach ($revs as $revline) {
+			if (preg_match('/^([0-9a-fA-F]{40})\s+tags\/(.+)(\^[0-9]+|\~[0-9]+)$/', $revline, $regs)) {
+				if ($regs[1] == $this->hash) {
+					$this->containingTag = $this->project->GetTag($regs[2]);
+					break;
+				}
+			}
 		}
 	}
 

--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -19,6 +19,7 @@
 define('GIT_ARCHIVE','archive');
 define('GIT_GREP','grep');
 define('GIT_BLAME','blame');
+define('GIT_NAME_REV','name-rev');
 
 /**
  * Git Executable class

comments