Added a blob view

This commit is contained in:
Correl Roush 2010-11-22 15:12:09 -05:00
parent 0e5e36698f
commit d5312f2361
8 changed files with 128 additions and 67 deletions

View file

@ -10,6 +10,11 @@ def oneline(value):
line = value.split('\n')[0].strip()
return line
@register.filter
@stringfilter
def lines(value):
return value.split('\n')
@register.filter
@stringfilter
def dirname(value):

View file

@ -1,7 +1,12 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('',
# Root log
(r'^(?P<repository>.*?)/log/$', 'codereview.browser.views.log'),
(r'^(?P<repository>.*?)/log/(?P<path>.*?/?)$', 'codereview.browser.views.log'),
# File (Blob) view
(r'^(?P<repository>.*?)/blob/(?P<path>.*?)$', 'codereview.browser.views.blob'),
# Path log
(r'^(?P<repository>.*?)/log/(?P<path>.*?)/$', 'codereview.browser.views.log'),
# Commit view
(r'^(?P<repository>.*?)/view/(?P<ref>.*?)/$', 'codereview.browser.views.view'),
)

View file

@ -1,3 +1,4 @@
import os
import re
import difflib
from datetime import datetime
@ -197,5 +198,18 @@ class Git(VCS):
elif type(node) == git.objects.Tree:
dirs.append(node.path)
return dirs, files
def blob(self, commit, path):
tree = self._repo.commit(commit).tree
dir = os.path.dirname(path)
if dir:
for i in tree.traverse():
if type(i) == git.objects.Tree and i.path == dir:
tree = i
if dir != tree.path:
raise Exception('Path not found')
for node in tree:
if type(node) == git.objects.Blob and node.path == path:
return Blob(node.path, node.data_stream.read())
raise Exception('Blob Path not found')
if __name__ == '__main__':
g = Git('/home/correlr/code/voiceaxis')

View file

@ -4,38 +4,45 @@ from django.shortcuts import render_to_response
from codereview.dashboard.models import Repository
from codereview.browser import vcs
def log(request,repository, path=None):
def _repo(request, name):
try:
repository = Repository.objects.get(name=repository)
repository = Repository.objects.get(name=name)
except:
raise Http404
repo = vcs.create(repository.type, repository.path)
ref = request.GET['c'] if 'c' in request.GET else repo.ref()
return repo, ref
def _nav_data(request, repo, ref, path=None):
path = path if path else ''
navigation = dict(zip(('dirs', 'files'), repo.browse(ref, path)))
return {'navigation': navigation}
def _log_data(request, repo, ref, path=None):
offset = int(request.GET['o']) if 'o' in request.GET else 0
limit = 20
path = path if path else ''
log = repo.log(ref, path=path, max=limit, offset=offset)
navigation = dict(zip(('dirs', 'files'), repo.browse(ref, os.path.dirname(path))))
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,
return {
'path': path,
'repo': repo,
'log': log,
'navigation': navigation,
'ref': ref,
'offset': offset,
'newer': newer,
'older': older,
})
}
def log(request, repository, path=None):
repo, ref = _repo(request, repository)
data = {'repository': repository}
data.update(_log_data(request, repo, ref, path))
data.update(_nav_data(request, repo, ref, path))
return render_to_response('browser/log.html', data)
def view(request, repository, ref):
try:
repository = Repository.objects.get(name=repository)
@ -53,3 +60,12 @@ def view(request, repository, ref):
'commit': commit,
'diffs': diffs,
})
def blob(request, repository, path):
repo, ref = _repo(request, repository)
data = {
'repository': repository,
'blob': repo.blob(ref, path),
}
data.update(_log_data(request, repo, ref, path))
data.update(_nav_data(request, repo, ref, os.path.dirname(path)))
return render_to_response('browser/blob.html', data)

View file

@ -0,0 +1,19 @@
{% extends "layouts/default.html" %}
{% load vcs %}
{% block navigation %}
{% include "browser/navigation.html" %}
{% endblock %}
{% block content %}
<h2>Blob View</h2>
<table class="diff">
{% for line in blob.data|lines %}
<tr>
<td class="number">{{ forloop.counter }}</td>
<td class="text">{{ line }}</td>
</tr>
{% endfor %}
</table>
{% include "browser/commitlog.html" %}
{% endblock %}

View file

@ -0,0 +1,44 @@
{% load gravatar %}
{% load vcs %}
<h2>Commit Log</h2>
<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 repo.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 repo.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>

View file

@ -1,51 +1,9 @@
{% extends "layouts/default.html" %}
{% load gravatar %}
{% load vcs %}
{% block navigation %}
{% include "browser/navigation.html" %}
{% endblock %}
{% block content %}
<h2>Commit Log</h2>
<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 repo.branches.items %}
{% if commit.id == c.id %}
<span class="marker branch"><a href="{% url codereview.browser.views.log repository=repository.name %}?c={{ branch }}">{{ branch }}</a></span>
{% endif %}
{% endfor %}
{% for tag, c in repo.tags.items %}
{% if commit.id == c.id %}
<span class="marker tag"><a href="{% url codereview.browser.views.log repository=repository.name %}?c={{ tag }}">{{ tag }}</a></span>
{% endif %}
{% endfor %}
<a href="{% url codereview.browser.views.view repository=repository.name 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.name %}?c={{ ref }}&o={{ older }}">Older</a>
{% endif %}
{% if offset and older %}
&middot;
{% endif %}
{% if offset %}
<a href="{% url codereview.browser.views.log repository=repository.name %}?c={{ ref }}&o={{ newer }}">Newer</a>
{% endif %}
</div>
{% include "browser/commitlog.html" %}
{% endblock %}

View file

@ -1,5 +1,5 @@
{% load vcs %}
<h2><a href="{% url codereview.browser.views.log repository=repository.name %}?c={{ ref }}">{{ repository.name }}</a></h2>
<h2><a href="{% url codereview.browser.views.log repository=repository %}?c={{ ref }}">{{ repository }}</a></h2>
<h3>Branches</h3>
<ul>
{% for branch, commit in repo.branches.items %}
@ -7,20 +7,20 @@
{% if ref == branch or ref == commit.id %}
<strong>{{ branch }}</strong>
{% else %}
<a href="{% url codereview.browser.views.log repository=repository.name %}?c={{branch}}">{{ branch }}</a>
<a href="{% url codereview.browser.views.log repository=repository %}?c={{branch}}">{{ branch }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
<h3>Browse</h3>
{% if path|dirname %}
<strong>{{ path|dirname }}/</strong><br />
<a href="{% url codereview.browser.views.log repository=repository.name path=path|dirname %}?c={{ ref }}">Up one</a>
{% if path %}
<strong>{{ path }}</strong><br />
<a href="{% url codereview.browser.views.log repository=repository path=path|dirname %}?c={{ ref }}">Up one</a>
{% endif %}
<ul class="tree">
{% for dir in navigation.dirs %}
<li class="dir">
<a href="{% url codereview.browser.views.log repository=repository.name path=dir %}/?c={{ ref }}">{{ dir|basename }}</a>
<a href="{% url codereview.browser.views.log repository=repository path=dir %}?c={{ ref }}">{{ dir|basename }}</a>
</li>
{% endfor %}
{% for file in navigation.files %}
@ -28,7 +28,7 @@
{% if file == path %}
<strong>{{ file|basename }}</strong>
{% else %}
<a href="{% url codereview.browser.views.log repository=repository.name path=file %}?c={{ ref }}">{{ file|basename }}</a>
<a href="{% url codereview.browser.views.blob repository=repository path=file %}?c={{ ref }}">{{ file|basename }}</a>
{% endif %}
</li>
{% endfor %}