35
35
from loggerhead import util
38
class AnnotateUI(TemplatedBranchView):
40
template_path = 'loggerhead.templates.annotate'
42
def annotate_file(self, file_id, revid):
38
class ViewUI(TemplatedBranchView):
40
template_path = 'loggerhead.templates.view'
42
def tree_for(self, file_id, revid):
47
43
file_revid = self._history.get_inventory(revid)[file_id].revision
48
tree = self._history._branch.repository.revision_tree(file_revid)
44
return self._history._branch.repository.revision_tree(file_revid)
46
def text_lines(self, file_id, revid):
50
47
file_name = os.path.basename(self._history.get_path(revid, file_id))
49
tree = self.tree_for(file_id, revid)
52
50
file_text = tree.get_file_text(file_id)
58
56
file_text = file_text.decode(encoding)
60
58
file_lines = bzrlib.osutils.split_lines(file_text)
59
# This can throw bzrlib.errors.BinaryFile (which our caller catches).
60
bzrlib.textfile.check_text_lines(file_lines)
62
if highlight is not None:
63
hl_lines = highlight(file_name, file_text, encoding)
64
# highlight strips off extra newlines at the end of the file.
65
extra_lines = len(file_lines) - len(hl_lines)
66
hl_lines.extend([u''] * extra_lines)
68
hl_lines = map(cgi.escape, file_lines)
72
def file_contents(self, file_id, revid):
63
bzrlib.textfile.check_text_lines(file_lines)
74
file_lines = self.text_lines(file_id, revid)
64
75
except bzrlib.errors.BinaryFile:
65
# bail out; this isn't displayable text
66
yield util.Container(parity=0, lineno=1, status='same',
67
text='(This is a binary file.)',
68
change=util.Container())
70
if highlight is not None:
71
hl_lines = highlight(file_name, file_text, encoding)
72
hl_lines.extend([u''] * (len(file_lines) - len(hl_lines)))
74
hl_lines = map(cgi.escape, file_lines)
78
last_line_revid = None
79
for line_revid, text in tree.annotate_iter(file_id):
80
if line_revid == last_line_revid:
81
# remember which lines have a new revno and which don't
86
last_line_revid = line_revid
87
if line_revid in change_cache:
88
change = change_cache[line_revid]
90
change = self._history.get_changes([line_revid])[0]
91
change_cache[line_revid] = change
94
parity=parity, lineno=lineno, status=status,
95
change=change, text=hl_lines[lineno - 1])
98
self.log.debug('annotate: %r secs' % (time.time() - z))
76
# bail out; this isn't displayable text
77
return ['(This is a binary file.)']
100
81
def get_values(self, path, kwargs, headers):
101
82
history = self._history
139
120
raise HTTPServerError('Could not fetch changes')
140
121
branch_breadcrumbs = util.branch_breadcrumbs(path, inv, 'files')
123
if inv[file_id].kind == "directory":
124
raise HTTPMovedPermanently(self._branch.context_url(['/files', revno_url, path]))
127
# In AnnotateUI, "annotated" is a generator giving revision
128
# numbers per lines, but the template checks if "annotated" is
129
# true or not before using it, so we have to define it here also.
143
131
'revno_url': revno_url,
144
132
'file_id': file_id,
146
134
'filename': filename,
147
135
'navigation': navigation,
148
136
'change': change,
149
'contents': list(self.annotate_file(file_id, revid)),
137
'contents': self.file_contents(file_id, revid),
150
138
'fileview_active': True,
151
139
'directory_breadcrumbs': directory_breadcrumbs,
152
140
'branch_breadcrumbs': branch_breadcrumbs,