diff --git a/browser/templatetags/vcs.py b/browser/templatetags/vcs.py index fd12105..2ba4a76 100644 --- a/browser/templatetags/vcs.py +++ b/browser/templatetags/vcs.py @@ -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): diff --git a/browser/urls.py b/browser/urls.py index b37f950..6da1ea1 100644 --- a/browser/urls.py +++ b/browser/urls.py @@ -1,7 +1,12 @@ from django.conf.urls.defaults import * urlpatterns = patterns('', + # Root log (r'^(?P.*?)/log/$', 'codereview.browser.views.log'), - (r'^(?P.*?)/log/(?P.*?/?)$', 'codereview.browser.views.log'), + # File (Blob) view + (r'^(?P.*?)/blob/(?P.*?)$', 'codereview.browser.views.blob'), + # Path log + (r'^(?P.*?)/log/(?P.*?)/$', 'codereview.browser.views.log'), + # Commit view (r'^(?P.*?)/view/(?P.*?)/$', 'codereview.browser.views.view'), ) diff --git a/browser/vcs.py b/browser/vcs.py index 782da36..9a879a6 100644 --- a/browser/vcs.py +++ b/browser/vcs.py @@ -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') diff --git a/browser/views.py b/browser/views.py index a33510d..fd72de7 100755 --- a/browser/views.py +++ b/browser/views.py @@ -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, - 'path': path, - 'repo': repo, - 'log': log, - 'navigation': navigation, - 'ref': ref, - 'offset': offset, - 'newer': newer, - 'older': older, - }) + return { + 'path': path, + 'repo': repo, + 'log': log, + '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) diff --git a/templates/browser/blob.html b/templates/browser/blob.html new file mode 100644 index 0000000..afcc7ff --- /dev/null +++ b/templates/browser/blob.html @@ -0,0 +1,19 @@ +{% extends "layouts/default.html" %} +{% load vcs %} + +{% block navigation %} +{% include "browser/navigation.html" %} +{% endblock %} + +{% block content %} +

Blob View

+ + {% for line in blob.data|lines %} + + + + + {% endfor %} +
{{ forloop.counter }}{{ line }}
+{% include "browser/commitlog.html" %} +{% endblock %} diff --git a/templates/browser/commitlog.html b/templates/browser/commitlog.html new file mode 100644 index 0000000..860d285 --- /dev/null +++ b/templates/browser/commitlog.html @@ -0,0 +1,44 @@ +{% load gravatar %} +{% load vcs %} + +

Commit Log

+ + + + + + +{% for commit in log %} + + + + + +{% endfor %} +
Commit DateAuthorCommit Message
{{ commit.authored_date }} + + {{ commit.author }} + + {% for branch, c in repo.branches.items %} + {% if commit.id == c.id %} + {{ branch }} + {% endif %} + {% endfor %} + {% for tag, c in repo.tags.items %} + {% if commit.id == c.id %} + {{ tag }} + {% endif %} + {% endfor %} + {{ commit.message|oneline }} +
+
+ {% if older %} + Older + {% endif %} + {% if offset and older %} + · + {% endif %} + {% if offset %} + Newer + {% endif %} +
diff --git a/templates/browser/log.html b/templates/browser/log.html index 3f2ce32..4b11b26 100644 --- a/templates/browser/log.html +++ b/templates/browser/log.html @@ -1,51 +1,9 @@ {% extends "layouts/default.html" %} -{% load gravatar %} -{% load vcs %} {% block navigation %} {% include "browser/navigation.html" %} {% endblock %} {% block content %} -

Commit Log

- - - - - - -{% for commit in log %} - - - - - -{% endfor %} -
Commit DateAuthorCommit Message
{{ commit.authored_date }} - - {{ commit.author }} - - {% for branch, c in repo.branches.items %} - {% if commit.id == c.id %} - {{ branch }} - {% endif %} - {% endfor %} - {% for tag, c in repo.tags.items %} - {% if commit.id == c.id %} - {{ tag }} - {% endif %} - {% endfor %} - {{ commit.message|oneline }} -
-
- {% if older %} - Older - {% endif %} - {% if offset and older %} - · - {% endif %} - {% if offset %} - Newer - {% endif %} -
+{% include "browser/commitlog.html" %} {% endblock %} diff --git a/templates/browser/navigation.html b/templates/browser/navigation.html index a1b689d..1e4ef3b 100644 --- a/templates/browser/navigation.html +++ b/templates/browser/navigation.html @@ -1,5 +1,5 @@ {% load vcs %} -

{{ repository.name }}

+

{{ repository }}

Branches

    {% for branch, commit in repo.branches.items %} @@ -7,20 +7,20 @@ {% if ref == branch or ref == commit.id %} {{ branch }} {% else %} - {{ branch }} + {{ branch }} {% endif %} {% endfor %}

Browse

-{% if path|dirname %} - {{ path|dirname }}/
- Up one +{% if path %} + {{ path }}
+ Up one {% endif %}