37
from loggerhead.controllers import TemplatedBranchView
40
from ..controllers import TemplatedBranchView
39
from loggerhead.highlight import highlight
42
from ..highlight import highlight
40
43
except ImportError:
42
from loggerhead import util
45
48
class ViewUI(TemplatedBranchView):
47
template_path = 'loggerhead.templates.view'
50
template_name = 'view'
49
52
def tree_for(self, file_id, revid):
50
file_revid = self._history.get_inventory(revid)[file_id].revision
53
if not isinstance(file_id, bytes):
54
raise TypeError(file_id)
55
if not isinstance(revid, bytes):
56
raise TypeError(revid)
57
rev_tree = self._history.revision_tree(revid)
58
file_revid = rev_tree.get_file_revision(rev_tree.id2path(file_id))
51
59
return self._history._branch.repository.revision_tree(file_revid)
53
61
def text_lines(self, file_id, revid):
54
file_name = os.path.basename(self._history.get_path(revid, file_id))
62
path = self._history.get_path(revid, file_id)
63
file_name = os.path.basename(path)
56
65
tree = self.tree_for(file_id, revid)
57
file_text = tree.get_file_text(file_id)
66
file_text = tree.get_file_text(path)
60
file_text = file_text.decode(encoding)
70
file_text.decode(encoding)
61
71
except UnicodeDecodeError:
62
72
encoding = 'iso-8859-15'
63
file_text = file_text.decode(encoding)
65
file_lines = bzrlib.osutils.split_lines(file_text)
66
# This can throw bzrlib.errors.BinaryFile (which our caller catches).
67
bzrlib.textfile.check_text_lines(file_lines)
73
file_text.decode(encoding)
75
file_lines = osutils.split_lines(file_text)
76
# This can throw breezy.errors.BinaryFile (which our caller catches).
77
breezy.textfile.check_text_lines(file_lines)
79
file_text = file_text.decode(encoding)
69
81
if highlight is not None:
70
82
hl_lines = highlight(file_name, file_text, encoding)
71
83
# highlight strips off extra newlines at the end of the file.
89
101
history = self._history
90
102
branch = history._branch
91
103
revid = self.get_revid()
92
revid = history.fix_revid(revid)
93
104
file_id = kwargs.get('file_id', None)
105
if file_id is not None:
106
file_id = urlutils.unquote_to_bytes(osutils.safe_utf8(file_id))
94
107
if (file_id is None) and (path is None):
95
108
raise HTTPBadRequest('No file_id or filename '
96
109
'provided to view')
119
132
self._branch.is_root,
135
tree = history.revision_tree(revid)
122
137
# Create breadcrumb trail for the path within the branch
124
inv = history.get_inventory(revid)
126
self.log.exception('Exception fetching changes')
127
raise HTTPServerError('Could not fetch changes')
128
branch_breadcrumbs = util.branch_breadcrumbs(path, inv, 'files')
138
branch_breadcrumbs = util.branch_breadcrumbs(path, tree, 'files')
140
if not tree.has_id(file_id):
133
141
raise HTTPNotFound()
135
if file.kind == "directory":
143
if tree.kind(path) == "directory":
136
144
raise HTTPMovedPermanently(self._branch.context_url(['/files', revno_url, path]))
138
146
# no navbar for revisions