Make tree view blob sizes fallback gracefully on older versions
Make tree view blob sizes fallback gracefully on older versions

The tree view used -l indiscriminately to get blob sizes, without
taking into account that the -l option only appeared in git 1.5.3,
in which case the tree would fail to display anything (because the
command returned only the help message because of the invalid -l
parameter). Now it falls back properly depending on the version,
only displaying blob sizes if running on git 1.5.3 or greater.

--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -165,5 +165,32 @@
 		return true;
 	}
 
+	/**
+	 * 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()
+	{
+		$version = $this->GetVersion();
+		if (!empty($version)) {
+			$splitver = explode('.', $version);
+
+			/*
+			 * ls-tree -l only appears in git 1.5.3
+			 * (technically 1.5.3-rc0 but i'm not getting that fancy)
+			 */
+			if (($splitver[0] < 1) || (($splitver[0] == 1) && ($splitver[1] < 5)) || (($splitver[0] == 1) && ($splitver[1] == 5) && ($splitver[2] < 3))) {
+				return false;
+			}
+		}
+
+		return true;
+
+	}
+
 }
 

--- a/include/git/Tree.class.php
+++ b/include/git/Tree.class.php
@@ -87,14 +87,15 @@
 
 		$args = array();
 		$args[] = '--full-name';
-		$args[] = '-l';
+		if ($exe->CanShowSizeInTree())
+			$args[] = '-l';
 		$args[] = '-t';
 		$args[] = $this->hash;
 		
 		$lines = explode("\n", $exe->Execute(GIT_LS_TREE, $args));
 
 		foreach ($lines as $line) {
-			if (preg_match("/^([0-9]+) (.+) ([0-9a-fA-F]{40})\s+([0-9]+|-)\s+(.+)$/", $line, $regs)) {
+			if (preg_match("/^([0-9]+) (.+) ([0-9a-fA-F]{40})(\s+[0-9]+|\s+-)?\t(.+)$/", $line, $regs)) {
 				switch($regs[2]) {
 					case 'tree':
 						$t = $this->project->GetTree($regs[3]);
@@ -109,7 +110,9 @@
 						$b = $this->project->GetBlob($regs[3]);
 						$b->SetMode($regs[1]);
 						$b->SetName($regs[5]);
-						$b->SetSize($regs[4]);
+						$size = trim($regs[4]);
+						if (!empty($size))
+							$b->SetSize($regs[4]);
 						$b->SetParent($this);
 						if ($this->commit)
 							$b->SetCommit($this->commit);

comments