Smarter way of getting project age for caching and display
Smarter way of getting project age for caching and display

--- a/css/gitphp.css
+++ b/css/gitphp.css
@@ -1,6 +1,7 @@
 .italic {font-style:italic;}
 .bold {font-weight:bold;}
 .underline {text-decoration:underline;}
+.empty {color: gray;}
 body {
 	font-family: sans-serif; font-size: 12px; border:solid #d9d8d1; border-width:1px;
 	margin:10px; background-color:#ffffff; color:#000000;

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -337,14 +337,14 @@
 		if (!$this->project)
 			return;
 
-		$headList = $this->project->GetHeads();
-
-		if (count($headList) > 0) {
-			$age = $headList[0]->GetCommit()->GetAge();
-
-			$this->tpl->clear_cache(null, $this->GetCacheKeyPrefix(), null, $age);
-			$this->tpl->clear_cache('projectlist.tpl', $this->GetCacheKeyPrefix(false), null, $age);
-		}
+		$epoch = $this->project->GetEpoch();
+		if (empty($epoch))
+			return;
+
+		$age = $this->project->GetAge();
+
+		$this->tpl->clear_cache(null, $this->GetCacheKeyPrefix(), null, $age);
+		$this->tpl->clear_cache('projectlist.tpl', $this->GetCacheKeyPrefix(false), null, $age);
 	}
 
 }

--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -20,6 +20,7 @@
 define('GIT_GREP','grep');
 define('GIT_BLAME','blame');
 define('GIT_NAME_REV','name-rev');
+define('GIT_FOR_EACH_REF','for-each-ref');
 
 /**
  * Git Executable class

--- a/include/git/Project.class.php
+++ b/include/git/Project.class.php
@@ -78,6 +78,24 @@
 	 * @access protected
 	 */
 	protected $category = '';
+
+	/**
+	 * epoch
+	 *
+	 * Stores the project epoch internally
+	 *
+	 * @access protected
+	 */
+	protected $epoch;
+
+	/**
+	 * epochRead
+	 *
+	 * Stores whether the project epoch has been read yet
+	 *
+	 * @access protected
+	 */
+	protected $epochRead = false;
 
 	/**
 	 * head
@@ -670,19 +688,9 @@
 		if ($catCmp !== 0)
 			return $catCmp;
 
-		$aCommit = $a->GetHeadCommit();
-		$bCommit = $b->GetHeadCommit();
-
-		if (($aCommit == null) && ($bCommit == null))
+		if ($a->GetAge() === $b->GetAge())
 			return 0;
-		else if ($aCommit == null)
-			return 1;
-		else if ($bCommit == null)
-			return -1;
-
-		if ($aCommit->GetAge() === $bCommit->GetAge())
-			return 0;
-		return ($aCommit->GetAge() < $bCommit->GetAge() ? -1 : 1);
+		return ($a->GetAge() < $b->GetAge() ? -1 : 1);
 	}
 
 	/**
@@ -1099,5 +1107,68 @@
 		return $revlist;
 	}
 
+	/**
+	 * GetEpoch
+	 *
+	 * Gets this project's epoch
+	 * (time of last change)
+	 *
+	 * @access public
+	 * @return integer timestamp
+	 */
+	public function GetEpoch()
+	{
+		if (!$this->epochRead)
+			$this->ReadEpoch();
+
+		return $this->epoch;
+	}
+
+	/**
+	 * GetAge
+	 *
+	 * Gets this project's age
+	 * (time since most recent change)
+	 *
+	 * @access public
+	 * @return integer age
+	 */
+	public function GetAge()
+	{
+		if (!$this->epochRead)
+			$this->ReadEpoch();
+
+		return time() - $this->epoch;
+	}
+
+	/**
+	 * ReadEpoch
+	 *
+	 * Reads this project's epoch
+	 * (timestamp of most recent change)
+	 *
+	 * @access private
+	 */
+	private function ReadEpoch()
+	{
+		$this->epochRead = true;
+
+		$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));
+
+		if (preg_match('/ (\d+) [-+][01]\d\d\d$/', $epochstr, $regs)) {
+			$this->epoch = $regs[1];
+		}
+
+		unset($exe);
+	}
+
 }
 

--- a/templates/projectlist.tpl
+++ b/templates/projectlist.tpl
@@ -72,16 +72,18 @@
       </td>
       <td class="projectDescription"><a href="{$SCRIPT_NAME}?p={$proj->GetProject()|urlencode}&amp;a=summary" class="list">{$proj->GetDescription()}</a></td>
       <td class="projectOwner"><em>{$proj->GetOwner()}</em></td>
+      {assign var=projecthead value=$proj->GetHeadCommit()}
       <td class="projectAge">
-        {assign var=projecthead value=$proj->GetHeadCommit()}
-	{if $projecthead}
-          {if $projecthead->GetAge() < 7200}   {* 60*60*2, or 2 hours *}
-            <span class="agehighlight"><strong><em>{$projecthead->GetAge()|agestring}</em></strong></span>
-          {elseif $projecthead->GetAge() < 172800}   {* 60*60*24*2, or 2 days *}
-            <span class="agehighlight"><em>{$projecthead->GetAge()|agestring}</em></span>
+        {if $projecthead}
+          {if $proj->GetAge() < 7200}   {* 60*60*2, or 2 hours *}
+            <span class="agehighlight"><strong><em>{$proj->GetAge()|agestring}</em></strong></span>
+          {elseif $proj->GetAge() < 172800}   {* 60*60*24*2, or 2 days *}
+            <span class="agehighlight"><em>{$proj->GetAge()|agestring}</em></span>
           {else}
-            <em>{$projecthead->GetAge()|agestring}</em>
+            <em>{$proj->GetAge()|agestring}</em>
           {/if}
+	{else}
+	  <em class="empty">{t}No commits{/t}</em>
 	{/if}
       </td>
       <td class="link">

comments