Parent: [3abb2e] (diff)

Child: [b8ce0c] (diff)

Download this file

repository.py    164 lines (143 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
from urllib import unquote
from pylons import c, g, request, response
import tg
from tg import redirect, expose, url, override_template
from allura.lib import patience
from allura.lib.widgets.file_browser import TreeWidget
from allura import model
from .base import BaseController
def on_import():
BranchBrowser.CommitBrowserClass = CommitBrowser
CommitBrowser.TreeBrowserClass = TreeBrowser
TreeBrowser.FileBrowserClass = FileBrowser
class BranchBrowser(BaseController):
CommitBrowserClass=None
def __init__(self, branch):
self._branch = branch
def index(self, limit=None, page=0, count=0, **kw):
limit, page, start = g.handle_paging(limit, page)
count = c.app.repo.count(branch=self._branch)
revisions = c.app.repo.log(
branch=self._branch,
offset=start,
limit=limit)
revisions = [ dict(value=r) for r in revisions ]
for r in revisions:
r.update(r['value'].context())
return dict(
username=c.user._id and c.user.username,
branch=self._branch,
log=revisions,
page=page,
limit=limit,
count=count,
**kw)
@expose()
def _lookup(self, commit, *rest):
commit=unquote(commit)
return self.CommitBrowserClass(commit), rest
class CommitBrowser(BaseController):
TreeBrowserClass=None
revision_widget = None
def __init__(self, revision):
self._revision = revision
self._commit = c.app.repo.commit(revision)
self.tree = self.TreeBrowserClass(self._commit)
def index(self):
c.revision_widget = self.revision_widget
result = dict(commit=self._commit)
if self._commit:
result.update(self._commit.context())
return result
class TreeBrowser(BaseController):
FileBrowserClass=None
tree_widget=TreeWidget()
def __init__(self, commit, parent=None, name=None):
self._commit = commit
self._parent = parent
self._name = name
if parent:
self._tree = parent.get_tree(name)
elif self._commit:
self._tree = self._commit.tree()
else:
self._tree = None
@expose('allura.templates.repo.tree')
def index(self):
c.tree_widget = self.tree_widget
return dict(
commit=self._commit,
tree=self._tree,
parent=self._parent)
@expose()
def _lookup(self, next, *rest):
next=unquote(next)
if not rest:
# Might be a file rather than a dir
filename = request.environ['PATH_INFO'].rsplit('/')[-1]
if filename and self._tree.is_blob(filename):
return self.FileBrowserClass(
self._commit,
self._tree,
filename), rest
return self.__class__(
self._commit,
self._tree,
next), rest
class FileBrowser(BaseController):
def __init__(self, commit, tree, filename):
self._commit = commit
self._tree = tree
self._filename = filename
self._blob = self._tree.get_blob(filename)
@expose('allura.templates.repo.file')
def index(self, **kw):
self._blob.context()
if kw.pop('format', 'html') == 'raw':
return self.raw()
elif 'diff' in kw:
override_template(self.index, 'genshi:allura.templates.repo.diff')
return self.diff(kw['diff'])
else:
force_display = 'force' in kw
context = self._blob.context()
return dict(
blob=self._blob,
prev=context.get('prev', None),
next=context.get('next', None),
force_display=force_display
)
@expose()
def raw(self):
content_type = self._blob.content_type.encode('utf-8')
filename = self._blob.filename.encode('utf-8')
response.headers['Content-Type'] = ''
response.content_type = content_type
if self._blob.content_encoding is not None:
content_encoding = self._blob.content_encoding.encode('utf-8')
response.headers['Content-Encoding'] = ''
response.content_encoding = content_encoding
response.headers.add(
'Content-Disposition', 'attachment;filename=' + filename)
return iter(self._blob)
def diff(self, commit):
try:
path, filename = os.path.split(self._blob.path())
a_ci = c.app.repo.commit(commit)
a_tree = a_ci.tree()
a = a_tree.get_blob(filename, path[1:].split('/'))
apath = a.path()
except:
a = []
apath = ''
b = self._blob
la = list(a)
lb = list(b)
diff = ''.join(patience.unified_diff(
la, lb,
'a' + apath, 'b' + b.path()))
return dict(
a=a, b=b,
diff=diff)
on_import()