Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement topological sort in GitCommit::getHistory

  • Loading branch information...
commit c4bc50196e8cabca843d502deedb2cc9f22846f9 1 parent a102696
@patrikf patrikf authored
Showing with 28 additions and 5 deletions.
  1. +28 −5 lib/git_commit.class.php
View
33 lib/git_commit.class.php
@@ -54,16 +54,39 @@ public function _serialize()
return $s;
}
+ /* returns history in topological order */
public function getHistory()
{
- $commits = array($this);
+ /* count incoming edges */
+ $inc = array();
+
+ $queue = array($this);
+ while (($commit = array_shift($queue)) !== NULL)
+ {
+ foreach ($commit->parents as $parent)
+ {
+ if (!isset($inc[$parent]))
+ {
+ $inc[$parent] = 1;
+ array_push($queue, $this->repo->getObject($parent));
+ }
+ else
+ $inc[$parent]++;
+ }
+ }
+
+ $queue = array($this);
$r = array();
- while (($commit = array_shift($commits)) !== NULL)
+ while (($commit = array_pop($queue)) !== NULL)
{
- array_push($r, $commit);
- $commits += array_map(array($this->repo, 'getObject'), $commit->parents);
+ array_unshift($r, $commit);
+ foreach ($commit->parents as $parent)
+ {
+ if (--$inc[$parent] == 0)
+ array_push($queue, $this->repo->getObject($parent));
+ }
}
- usort($r, create_function('$a,$b', 'return ($a->committer->time - $b->committer->time);'));
+
return $r;
}
Please sign in to comment.
Something went wrong with that request. Please try again.