Initial commit

This commit is contained in:
Correl Roush 2010-11-19 00:12:29 -05:00
commit 95477c9dc1
25 changed files with 694 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.py[oc]

0
__init__.py Executable file
View file

0
browser/__init__.py Executable file
View file

3
browser/models.py Executable file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

View file

@ -0,0 +1,16 @@
import hashlib
from django.template import Library
from django.template.defaultfilters import stringfilter
register = Library()
@register.filter
@stringfilter
def gravatar(value, size):
email = value.strip().lower()
hash = hashlib.md5(email).hexdigest()
url = "http://www.gravatar.com/avatar/{hash}?s={size}&r={rating}".format(
hash=hash,
size=size,
rating='g')
return url

View file

@ -0,0 +1,10 @@
from django.template import Library
from django.template.defaultfilters import stringfilter
register = Library()
@register.filter
@stringfilter
def oneline(value):
line = value.split('\n')[0].strip()
return line

23
browser/tests.py Executable file
View file

@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

6
browser/urls.py Normal file
View file

@ -0,0 +1,6 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^(?P<repository>.*?)/log/$', 'codereview.browser.views.log'),
(r'^(?P<repository>.*?)/view/(?P<ref>.*?)/$', 'codereview.browser.views.view'),
)

48
browser/views.py Executable file
View file

@ -0,0 +1,48 @@
from django.http import Http404
from django.shortcuts import render_to_response
from codereview.dashboard.models import Repository
def log(request,repository):
try:
repo = Repository.objects.get(name=repository)
except:
raise Http404
vcs = repo.get_vcs()
ref = request.GET['c'] if 'c' in request.GET else vcs.ref()
offset = int(request.GET['o']) if 'o' in request.GET else 0
limit = 20
log = vcs.log(ref, max=limit, offset=offset)
newer = offset - limit if offset > limit else 0
# Inspect the last commit. If it has no parents, we can't go any further
# back.
last = log[-1]
older = offset + limit if last.parents else 0
return render_to_response('browser/log.html',
{
'repository': repository,
'vcs': vcs,
'log': log,
'ref': ref,
'offset': offset,
'newer': newer,
'older': older,
})
def view(request, repository, ref):
try:
repo = Repository.objects.get(name=repository)
except:
raise Http404
vcs = repo.get_vcs()
commit = vcs.commit(ref)
diffs = vcs.diff(ref)
return render_to_response('browser/view.html',
{
'repository': repository,
'vcs': vcs,
'ref': ref,
'commit': commit,
'diffs': diffs,
})

0
dashboard/__init__.py Executable file
View file

View file

@ -0,0 +1,20 @@
[
{
"pk": 1,
"model": "dashboard.repository",
"fields": {
"path": "/home/correlr/code/voiceaxis",
"type": 0,
"name": "VoiceAxis"
}
},
{
"pk": 2,
"model": "dashboard.repository",
"fields": {
"path": "/srv/git/mtg.git",
"type": 0,
"name": "MTG"
}
}
]

16
dashboard/models.py Executable file
View file

@ -0,0 +1,16 @@
from django.db import models
from codereview.lib import vcs
class Repository(models.Model):
Types = {
'Git': 0,
}
name = models.CharField(max_length=200, unique=True)
path = models.CharField(max_length=255)
type = models.IntegerField(default=0)
def get_vcs(self):
if self.type == 0:
return vcs.Git(self.path)
else:
raise Exception('Invalid VCS type')

23
dashboard/tests.py Executable file
View file

@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

9
dashboard/views.py Executable file
View file

@ -0,0 +1,9 @@
from django.shortcuts import render_to_response
from codereview.dashboard.models import Repository
def index(request):
""" List available repositories
"""
repositories = Repository.objects.all()
return render_to_response('dashboard/index.html',
{'repositories': repositories})

0
lib/__init__.py Normal file
View file

183
lib/vcs.py Normal file
View file

@ -0,0 +1,183 @@
import difflib
from datetime import datetime
class VCS(object):
def __init__(self, path):
self._path = path
self._ref = None
def branches(self):
return []
def tags(self):
return []
def ref(self):
return self._ref
def log(self, commit=None, path=None, max=50, offset=0):
return []
class Blob(object):
def __init__(self, path, data):
self.path = path
self.data = data
class Diff(object):
Types = {
'Add': 0,
'Delete': 1,
'Rename': 2,
'Modify': 3,
}
def __init__(self):
self.a = None
self.b = None
self.type = 0
def unified(self):
a = self.a.data.split('\n') if self.a else []
b = self.b.data.split('\n') if self.b else []
diff = difflib.unified_diff(
a,
b,
fromfile=self.a.path,
tofile=self.b.path)
return '\n'.join(diff)
def changes(self, context=3):
a = self.a.data.split('\n') if self.a else []
b = self.b.data.split('\n') if self.b else []
differ = difflib.Differ()
# Locate changes so we can mark context lines
i = 0
changes = []
for change in differ.compare(a, b):
if change[0] in ['+', '-']:
changes.append(i)
i += 1
i = 0
line_a = 0
line_b = 0
for change in differ.compare(a, b):
type = change[:2].strip()
text = change[2:]
if type == '?':
# Change information. Discard it for now.
i += 1
print 'skip ?'
continue
if type == '+':
line_b += 1
elif type == '-':
line_a += 1
else:
line_a += 1
line_b += 1
if context and not type:
# Check to see if we're in range of a change
nearby = [c for c in changes if abs(i - c) <= context + 1]
if not nearby:
i += 1
print 'skip nc'
continue
result = {
'type': type,
'line_a': line_a,
'line_b': line_b,
'text': text,
}
yield result
i += 1
def html(self):
a = self.a.data.split('\n') if self.a.data else []
b = self.b.data.split('\n') if self.b.data else []
h = difflib.HtmlDiff()
diff = h.make_table(
a,
b,
fromdesc=self.a.path,
todesc=self.b.path,
context=True)
return diff
class Commit(object):
def __init__(self):
self.id = None
self.tree = None
self.message = None
self.author = None
self.author_email = None
self.committer = None
self.committer_email = None
self.authored_date = None
self.committed_date = None
self.parents = []
import git
class Git(VCS):
def __init__(self, path):
super(Git, self).__init__(path);
self._repo = git.Repo(self._path)
self._branches = None
self._tags = None
# Set default branch ref
if 'master' in self.branches():
self._ref = 'master'
else:
self.ref = self.branches()[0]
def branches(self):
if not self._branches:
self._branches = dict([(head.name, self.commit(head.commit)) for head in self._repo.heads])
return self._branches
def tags(self):
if not self._tags:
self._tags = dict([(tag.name, self.commit(tag.commit)) for tag in self._repo.tags])
return self._tags
def commit(self, commit):
if type(commit) in [str, unicode]:
commit = self._repo.commit(commit)
c = Commit()
c.id = commit.hexsha
c.tree = commit.tree.hexsha
c.message = commit.message
c.author = commit.author.name
c.author_email = commit.author.email
c.committer = commit.committer.name
c.committer_email = commit.committer.email
c.authored_date = datetime.fromtimestamp(commit.authored_date)
c.committed_date = datetime.fromtimestamp(commit.committed_date)
c.parents = [parent.hexsha for parent in commit.parents]
return c
def log(self, commit=None, path=None, max=50, offset=0):
commit = commit if commit else self._ref
result = []
for c in self._repo.iter_commits(commit, path, max_count=max,
skip=offset):
result.append(self.commit(c))
return result
def diff(self, b, a=None):
"""Get the diff for ref b from ref a
If ref a is not provided, b's parent refs will be used.
*** Note: The parameter order is backwards, since we default the origin
ref to the target's parents.
"""
result = []
b = self._repo.commit(b)
if not a:
a = b.parents
else:
a = self._repo.commit(a)
for diff in b.diff(a):
# b and a are swapped so the parent diff will work as a list of
# parents. Therefore, we'll swap them back when we put them into our
# Diff object.
d = Diff()
if diff.a_blob: d.b = Blob(diff.a_blob.path,
diff.a_blob.data_stream.read())
if diff.b_blob: d.a = Blob(diff.b_blob.path,
diff.b_blob.data_stream.read())
result.append(d)
return result
if __name__ == '__main__':
g = Git('/home/correlr/code/voiceaxis')

11
manage.py Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)

72
media/default.css Normal file
View file

@ -0,0 +1,72 @@
body, th, td {
font-family: sans-serif;
font-size: 10pt;
}
a {
color: black;
}
span.marker {
display: block;
float: left;
border: 1px solid black;
padding-right: 0.2em;
padding-left: 0.2em;
margin-right: 0.2em;
}
span.marker a {
text-decoration: none;
color: black;
}
span.marker a:hover {
text-decoration: underline;
}
span.marker.branch {
background-color: #88ff88;
}
span.marker.tag {
background-color: #ffff88;
}
table.vcs-log {
width: 100%;
}
table.vcs-log th {
text-align: left;
}
table.vcs-log .date,
table.vcs-log .author {
width: 15%;
}
div.vcs-nav {
text-align: center;
}
table.diff {
width: 100%;
border: 1px solid black;
border-collapse: collapse;
}
table.diff td {
padding-left: .3em;
padding-right: .3em;
}
table.diff .number {
width: 3em;
}
table.diff .add .number {
background-color: #baeeba;
}
table.diff .add .text {
border-left: 1px solid green;
background-color: #e0ffe0;
}
table.diff .del .number {
background-color: #eebaba;
}
table.diff .del .text {
border-left: 1px solid red;
background-color: #ffe0e0;
}
table.diff td {
font-family: monospace;
}

98
settings.py Executable file
View file

@ -0,0 +1,98 @@
# Django settings for codereview project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'codereview', # Or path to database file if using sqlite3.
'USER': 'codereview', # Not used with sqlite3.
'PASSWORD': 'codereview', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/New_York'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'w*cepy#)iiaion7n!^_zwyc3cy_s89!qz!ey5%avrgg($8o_*y'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'codereview.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/correlr/code/codereview/templates",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
'codereview.dashboard',
'codereview.browser',
)

View file

@ -0,0 +1,53 @@
{% extends "layouts/default.html" %}
{% load gravatar %}
{% load vcs %}
{% block content %}
<h2>Commit Log</h2>
Branches:
<ul>
{% for branch, commit in vcs.branches.items %}
<li><a href="{% url codereview.browser.views.log repository=repository %}?c={{branch}}">{{ branch }}</a> ({{commit.id}})</li>
{% endfor %}
</ul>
<table class="vcs-log">
<tr>
<th>Commit Date</th>
<th>Author</th>
<th>Commit Message</th>
</tr>
{% for commit in log %}
<tr>
<td class="date">{{ commit.authored_date }}</td>
<td class="author">
<img src="{{ commit.author_email|gravatar:16 }}" />
{{ commit.author }}
</td>
<td class="message">
{% for branch, c in vcs.branches.items %}
{% if commit.id == c.id %}
<span class="marker branch"><a href="{% url codereview.browser.views.log repository=repository %}?c={{ branch }}">{{ branch }}</a></span>
{% endif %}
{% endfor %}
{% for tag, c in vcs.tags.items %}
{% if commit.id == c.id %}
<span class="marker tag"><a href="{% url codereview.browser.views.log repository=repository %}?c={{ tag }}">{{ tag }}</a></span>
{% endif %}
{% endfor %}
<a href="{% url codereview.browser.views.view repository=repository ref=commit.id %}">{{ commit.message|oneline }}</a>
</td>
</tr>
{% endfor %}
</table>
<div class="vcs-nav">
{% if older %}
<a href="{% url codereview.browser.views.log repository=repository %}?c={{ ref }}&o={{ older }}">Older</a>
{% endif %}
{% if offset and older %}
&middot;
{% endif %}
{% if offset %}
<a href="{% url codereview.browser.views.log repository=repository %}?c={{ ref }}&o={{ newer }}">Newer</a>
{% endif %}
</div>
{% endblock %}

View file

@ -0,0 +1,50 @@
{% extends "layouts/default.html" %}
{% load gravatar %}
{% load vcs %}
{% block content %}
<h2>{{ commit.message|oneline }}</h2>
<dl>
<dt>Committer</dt>
<dd>
<img src="{{ commit.author_email|gravatar:32 }}" />
{{ commit.committer }}
</dd>
<dt>Committed</dt>
<dd>{{ commit.committed_date }}</dd>
<dt>Author</dt>
<dd>
<img src="{{ commit.author_email|gravatar:32 }}" />
{{ commit.author }}
</dd>
<dt>Written</dt>
<dd>{{ commit.authored_date }}</dd>
<dt>Message</dt>
<dd>
{{ commit.message }}
</dd>
</dl>
<div class="diff-container">
<div class="diff-html">
{% for diff in diffs %}
<table class="diff">
<caption>
{% if diff.a %}{{ diff.a.path }}{% else %}(New File){% endif %}
{% if diff.a.path != diff.b.path %}
=>
{% if diff.b %}{{ diff.b.path }}{% else %}(Deleted){% endif %}
{% endif %}
{% for change in diff.changes %}
<tr class="{% if change.type == '+' %}add{% endif %}{% if change.type == '-' %}del{% endif %}">
<td class="number">{% if change.type != '+' %}{{ change.line_a }}{% endif %}</td>
<td class="number">{% if change.type != '-' %}{{ change.line_b }}{% endif %}</td>
<td class="text">{{ change.text }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,11 @@
{% extends "layouts/default.html" %}
{% block content %}
<h2>Repositories</h2>
<ul>
{% for repo in repositories %}
<li><a href="{% url codereview.browser.views.log repository=repo.name %}">{{ repo.name }}</a></li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<link rel="stylesheet" href="/media/default.css" />
<title>{% block title %}CodeReview{% endblock %}</title>
</head>
<body>
<div id="container">
<div class="header">
<h1><a href="{% url codereview.dashboard.views.index %}">CodeReview</a></title>
</div>
<div class="navigation">
</div>
<div class="content">
{% block content %}
This space intentionally left blank.
{% endblock %}
</div>
</div>
</body>
</html>

19
urls.py Executable file
View file

@ -0,0 +1,19 @@
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^codereview/', include('codereview.foo.urls')),
(r'^$', 'codereview.dashboard.views.index'),
(r'^browser/', include('codereview.browser.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
)