~bzr/bzr-webserve/webserve-dev

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
# cmd_content.py - web interface for bazaar
# this code is derived by the hgweb server of the Matt Mackall's mercurial
# project
#
# Copyright 2006 Goffredo Baroncelli <kreijack@inwind.it>
#
# This file is part of bazaar-webserve.
#
# bazaar-webserve is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bazaar-webserve is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bazaar-webserve; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from bzrlib.annotate import _annotate_file

import hgweb
from hgweb import (
    WebServeErrorRevisionNotFound,
    WebServeErrorPathNotFound
)


def _file_annotate(hgweb, file_id, file_version):
    count = 0
    parity = 1
    oldvalues = ['-1', 'unknow', 'unknow']

    for (revno_str, author, date_str, line_rev_id, text) in \
            _annotate_file(hgweb.branch, file_version, file_id):

        if revno_str != "":
            parity = 1- parity

        count = count + 1

        if revno_str == "":
            tmpl = "contentline"
            (revision, author, line_rev_id) = oldvalues
        else:
            tmpl = "revidannotateline"
            revno = hgweb.revision_history.revid2revno(line_rev_id)
            oldvalues = (revno, author, line_rev_id)

        yield hgweb.t(tmpl,
            parity = parity,
            linenumber = count,
            author = author[:7],
            revno = revno,
            labelrevno = len(revno) > 10 and revno[:9]+"..." or revno,
            revid = line_rev_id,
            line = text.decode(hgweb.encoding, 'replace'))


def _file_contents(hgweb, file_id, file_version):
    count = 0
    parity = 1

    revtree = hgweb.branch.repository.revision_tree(file_version)
    for line in revtree.get_file_lines(file_id):
        parity = 1- parity
        count = count + 1
        yield hgweb.t("contentline",
            parity = parity,
            linenumber = count,
            line = line.decode(hgweb.encoding, 'replace'))


def content(hgweb, revid, path, pathrevid, annotate=False):

    revid = hgweb.get_revid(revid)
    pathrevid = hgweb.get_revid(pathrevid, revid)

    if not path:
        raise WebServeErrorPathNotFound(revid, path, pathrevid)
    if not hgweb.revision_history.exist_revision(revid):
        raise WebServeErrorRevisionNotFound(revid)
    if not hgweb.revision_history.exist_revision(pathrevid):
        raise WebServeErrorRevisionNotFound(pathrevid)

    rev = hgweb.branch.repository.get_revision(revid)

    file_id = hgweb.get_fileid(path, pathrevid)
    inv = hgweb.branch.repository.get_revision_inventory(revid)

    if file_id == None or not inv.has_id(file_id):
        raise WebServeErrorPathNotFound(revid, path, pathrevid)

    finc = inv[file_id]
    revid = finc.revision

    if finc.kind == "symlink":
        entries = hgweb.t("linkline",
            name=finc.name,
            value=finc.symlink_target)
    elif annotate:
        entries = _file_annotate(hgweb, file_id, revid)
    else:
        entries = _file_contents(hgweb, file_id, revid)

    yield hgweb.t(annotate and 'fileannotate' or 'filecontent',
        entries = entries,
        cntnav = lambda **map: hgweb.template_nav_revid(
            "cntnaventry",
            rev.revision_id,
            path=path,
            pathrevid=pathrevid),
        node = rev.revision_id,
        author = rev.committer,
        desc = rev.message,
        path = path,
        fileid = file_id,
        date = rev.timestamp,
        pathrevid = pathrevid,
        linkrev = hgweb.template_revid(rev.revision_id),
        linkpath = hgweb.template_path(rev.revision_id, path, pathrevid),
    )


def dump_content(hgweb, revid, path, pathrevid):

    revid = hgweb.get_revid(revid)
    pathrevid = hgweb.get_revid(pathrevid, revid)

    if not hgweb.revision_history.exist_revision(revid):
        raise WebServeErrorRevisionNotFound(revid)
    if not hgweb.revision_history.exist_revision(pathrevid):
        raise WebServeErrorRevisionNotFound(pathrevid)

    rev = hgweb.branch.repository.get_revision(revid)

    file_id = hgweb.get_fileid(path, pathrevid)
    inv = hgweb.branch.repository.get_revision_inventory(revid)

    if file_id == None or not inv.has_id(file_id):
        raise WebServeErrorPathNotFound(revid, path, pathrevid)

    finc = inv[file_id]
    revid = finc.revision

    hgweb.raw_write('Content-Type: application/force-download\n'  \
              'Content-Disposition: attachment; filename=%s\n\n'%(
              path.encode("utf_8","replace")))
    if finc.kind == "symlink":
        hgweb.raw_write("symlink: %s => %s\n"%(finc.name, finc.symlink_target))
    else:
        rev_tree = hgweb.branch.repository.revision_tree(revid)
        hgweb.raw_write(rev_tree.get_file(file_id).read())