From 4bc9c82c81c36b7c39aab38189551edc499d02bf Mon Sep 17 00:00:00 2001
From: Correl Roush <correl@gmail.com>
Date: Wed, 22 Dec 2010 16:41:21 -0500
Subject: [PATCH] Added models for storing commit info and an update method for
 loading it

---
 dashboard/models.py | 57 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/dashboard/models.py b/dashboard/models.py
index 077b366..1edd6be 100755
--- a/dashboard/models.py
+++ b/dashboard/models.py
@@ -1,4 +1,5 @@
 from django.db import models
+from codereview.browser import vcs
 
 class Repository(models.Model):
     name = models.CharField(max_length=200, unique=True)
@@ -10,5 +11,61 @@ class Repository(models.Model):
             ("browse", "Browse repositories"),
         )
 
+    def update(self):
+        repo = vcs.create(self.type, self.path)
+        branches = repo.branches()
+        for branch, commit in branches.iteritems():
+            try:
+                head = Head.objects.get(repository=self, name=branch)
+            except:
+                head = Head(repository=self, name=branch)
+            try:
+                c = Commit.objects.get(repository=self, ref=commit.id)
+            except:
+                c = Commit(repository=self, ref=commit.id)
+            c.load(repo)
+            head.commit = c
+            head.save()
+    def __unicode__(self):
+        return self.name
+
+class Commit(models.Model):
+    ref = models.CharField(max_length=40)
+    repository = models.ForeignKey(Repository)
+    message = models.TextField()
+    author = models.CharField(max_length=255)
+    author_email = models.CharField(max_length=255)
+    committer = models.CharField(max_length=255)
+    committer_email = models.CharField(max_length=255)
+    authored_date = models.DateTimeField()
+    committed_date = models.DateTimeField()
+    parents = models.ManyToManyField('self')
+
+    def load(self, repo):
+        commit = repo.commit(self.ref)
+        self.message = commit.message
+        self.author = commit.author
+        self.author_email = commit.author_email
+        self.committer = commit.committer
+        self.committer_email = commit.committer_email
+        self.authored_date = commit.authored_date
+        self.committed_date = commit.committed_date
+        self.save()
+        for parent in commit.parents:
+            try:
+                p = Commit.objects.get(ref=parent, repository=self.repository)
+            except:
+                p = Commit(ref=parent, repository=self.repository)
+                p.load(repo)
+            self.parents.add(p)
+        self.save()
+    def __unicode__(self):
+        return self.ref
+
+class Head(models.Model):
+    repository = models.ForeignKey(Repository)
+    commit = models.ForeignKey(Commit)
+    name = models.CharField(max_length=255)
+
     def __unicode__(self):
         return self.name