2010-11-22 06:13:50 +00:00
|
|
|
import os
|
2010-11-19 05:12:29 +00:00
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import render_to_response
|
2010-12-08 22:01:09 +00:00
|
|
|
from django.template import RequestContext
|
2010-12-08 13:28:57 +00:00
|
|
|
from django.contrib.auth.decorators import permission_required
|
2010-11-19 05:12:29 +00:00
|
|
|
from codereview.dashboard.models import Repository
|
2010-12-17 20:25:05 +00:00
|
|
|
from codereview.review.forms import NewReviewForm
|
2010-11-19 21:16:35 +00:00
|
|
|
from codereview.browser import vcs
|
2010-11-19 05:12:29 +00:00
|
|
|
|
2010-11-22 20:12:09 +00:00
|
|
|
def _repo(request, name):
|
2010-11-19 05:12:29 +00:00
|
|
|
try:
|
2010-11-22 20:12:09 +00:00
|
|
|
repository = Repository.objects.get(name=name)
|
2010-11-19 05:12:29 +00:00
|
|
|
except:
|
|
|
|
raise Http404
|
2010-11-19 21:16:35 +00:00
|
|
|
repo = vcs.create(repository.type, repository.path)
|
|
|
|
ref = request.GET['c'] if 'c' in request.GET else repo.ref()
|
2010-11-22 20:12:09 +00:00
|
|
|
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):
|
2010-11-19 05:12:29 +00:00
|
|
|
offset = int(request.GET['o']) if 'o' in request.GET else 0
|
|
|
|
limit = 20
|
2010-11-22 06:13:50 +00:00
|
|
|
|
2010-11-22 14:50:41 +00:00
|
|
|
path = path if path else ''
|
2010-11-22 06:13:50 +00:00
|
|
|
log = repo.log(ref, path=path, max=limit, offset=offset)
|
2010-11-19 05:12:29 +00:00
|
|
|
|
|
|
|
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
|
2010-11-22 20:12:09 +00:00
|
|
|
return {
|
|
|
|
'path': path,
|
|
|
|
'repo': repo,
|
|
|
|
'log': log,
|
|
|
|
'ref': ref,
|
|
|
|
'offset': offset,
|
|
|
|
'newer': newer,
|
|
|
|
'older': older,
|
|
|
|
}
|
2010-12-08 13:28:57 +00:00
|
|
|
@permission_required('dashboard.browse')
|
2010-11-22 20:12:09 +00:00
|
|
|
def log(request, repository, path=None):
|
|
|
|
repo, ref = _repo(request, repository)
|
2010-12-08 22:01:09 +00:00
|
|
|
data = RequestContext(request, {
|
|
|
|
'repository': repository
|
|
|
|
})
|
2010-11-22 20:12:09 +00:00
|
|
|
data.update(_log_data(request, repo, ref, path))
|
|
|
|
data.update(_nav_data(request, repo, ref, path))
|
|
|
|
return render_to_response('browser/log.html', data)
|
2010-12-08 13:28:57 +00:00
|
|
|
@permission_required('dashboard.browse')
|
2010-11-22 21:10:07 +00:00
|
|
|
def commit(request, repository, ref):
|
2010-11-19 05:12:29 +00:00
|
|
|
try:
|
2010-11-19 21:16:35 +00:00
|
|
|
repository = Repository.objects.get(name=repository)
|
2010-11-19 05:12:29 +00:00
|
|
|
except:
|
|
|
|
raise Http404
|
2010-11-19 21:16:35 +00:00
|
|
|
repo = vcs.create(repository.type, repository.path)
|
|
|
|
commit = repo.commit(ref)
|
|
|
|
diffs = repo.diff(ref)
|
2010-12-17 20:25:05 +00:00
|
|
|
form = NewReviewForm({
|
2010-12-09 05:06:17 +00:00
|
|
|
'author': request.user.pk,
|
|
|
|
'repo': repository.pk,
|
|
|
|
'ref': ref
|
|
|
|
})
|
2010-11-19 05:12:29 +00:00
|
|
|
|
2010-12-08 22:01:09 +00:00
|
|
|
data = RequestContext(request, {
|
|
|
|
'repository': repository,
|
|
|
|
'repo': repo,
|
|
|
|
'ref': ref,
|
|
|
|
'commit': commit,
|
|
|
|
'diffs': diffs,
|
2010-12-09 05:06:17 +00:00
|
|
|
'form': form,
|
2010-12-08 22:01:09 +00:00
|
|
|
})
|
|
|
|
return render_to_response('browser/view.html', data)
|
2010-12-08 13:28:57 +00:00
|
|
|
@permission_required('dashboard.browse')
|
2010-12-17 19:50:26 +00:00
|
|
|
def diff(request, repository):
|
|
|
|
if not request.GET:
|
|
|
|
raise Http404
|
2010-12-17 20:25:05 +00:00
|
|
|
if not request.GET['b']:
|
2010-12-17 19:50:26 +00:00
|
|
|
raise Http404
|
|
|
|
try:
|
|
|
|
repository = Repository.objects.get(name=repository)
|
|
|
|
repo = vcs.create(repository.type, repository.path)
|
2010-12-17 20:25:05 +00:00
|
|
|
b = request.GET['b']
|
2010-12-17 19:50:26 +00:00
|
|
|
commit_b = repo.commit(b)
|
2010-12-17 20:25:05 +00:00
|
|
|
a = None
|
|
|
|
if request.GET['a']:
|
|
|
|
a = request.GET['a']
|
|
|
|
elif commit_a.parents:
|
|
|
|
a = commit_b.parents[0]
|
|
|
|
commit_a = repo.commit(a) if a else None
|
2010-12-17 19:50:26 +00:00
|
|
|
diffs = repo.diff(b, a)
|
|
|
|
except:
|
|
|
|
raise Http404
|
2010-12-17 20:25:05 +00:00
|
|
|
form = NewReviewForm({
|
|
|
|
'author': request.user.pk,
|
|
|
|
'repo': repository.pk,
|
|
|
|
'ref': b,
|
|
|
|
'parent': a,
|
|
|
|
'description': 'Diff from {0} => {1}'.format(a, b),
|
|
|
|
})
|
2010-12-17 19:50:26 +00:00
|
|
|
data = RequestContext(request, {
|
|
|
|
'repository': repository,
|
|
|
|
'repo': repo,
|
|
|
|
'a': commit_a,
|
|
|
|
'b': commit_b,
|
|
|
|
'diffs': diffs,
|
2010-12-17 20:25:05 +00:00
|
|
|
'form': form,
|
2010-12-17 19:50:26 +00:00
|
|
|
})
|
|
|
|
return render_to_response('browser/diff.html', data)
|
|
|
|
@permission_required('dashboard.browse')
|
2010-11-22 20:12:09 +00:00
|
|
|
def blob(request, repository, path):
|
|
|
|
repo, ref = _repo(request, repository)
|
2010-12-08 22:01:09 +00:00
|
|
|
data = RequestContext(request, {
|
2010-11-22 20:12:09 +00:00
|
|
|
'repository': repository,
|
|
|
|
'blob': repo.blob(ref, path),
|
2010-12-08 22:01:09 +00:00
|
|
|
})
|
2010-11-22 20:12:09 +00:00
|
|
|
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)
|