/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/testament.py

  • Committer: Jelmer Vernooij
  • Date: 2018-06-19 18:31:46 UTC
  • mto: (6973.12.2 python3-k)
  • mto: This revision was merged to the branch mainline in revision 7005.
  • Revision ID: jelmer@jelmer.uk-20180619183146-vswp38w2zexg9u94
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
* the testament uses unix line-endings (\n)
60
60
"""
61
61
 
 
62
from __future__ import absolute_import
 
63
 
62
64
# XXX: At the moment, clients trust that the graph described in a weave
63
65
# is accurate, but that's not covered by the testament.  Perhaps the best
64
66
# fix is when verifying a revision to make sure that every file mentioned
71
73
 
72
74
from copy import copy
73
75
 
74
 
from bzrlib.osutils import (
 
76
from .osutils import (
75
77
    contains_whitespace,
76
78
    contains_linebreaks,
77
 
    sha,
 
79
    sha_strings,
78
80
    )
 
81
from .sixish import text_type
 
82
from .tree import Tree
79
83
 
80
84
 
81
85
class Testament(object):
90
94
    """
91
95
 
92
96
    long_header = 'bazaar-ng testament version 1\n'
93
 
    short_header = 'bazaar-ng testament short form 1\n'
 
97
    short_header = b'bazaar-ng testament short form 1\n'
 
98
    include_root = False
94
99
 
95
100
    @classmethod
96
101
    def from_revision(cls, repository, revision_id):
97
 
        """Produce a new testament from a historical revision"""
 
102
        """Produce a new testament from a historical revision."""
98
103
        rev = repository.get_revision(revision_id)
99
 
        inventory = repository.get_inventory(revision_id)
100
 
        return cls(rev, inventory)
101
 
 
102
 
    def __init__(self, rev, inventory):
103
 
        """Create a new testament for rev using inventory."""
 
104
        tree = repository.revision_tree(revision_id)
 
105
        return cls(rev, tree)
 
106
 
 
107
    @classmethod
 
108
    def from_revision_tree(cls, tree):
 
109
        """Produce a new testament from a revision tree."""
 
110
        rev = tree._repository.get_revision(tree.get_revision_id())
 
111
        return cls(rev, tree)
 
112
 
 
113
    def __init__(self, rev, tree):
 
114
        """Create a new testament for rev using tree."""
104
115
        self.revision_id = rev.revision_id
105
116
        self.committer = rev.committer
106
117
        self.timezone = rev.timezone or 0
107
118
        self.timestamp = rev.timestamp
108
119
        self.message = rev.message
109
120
        self.parent_ids = rev.parent_ids[:]
110
 
        self.inventory = inventory
 
121
        if not isinstance(tree, Tree):
 
122
            raise TypeError("As of bzr 2.4 Testament.__init__() takes a "
 
123
                "Revision and a Tree.")
 
124
        self.tree = tree
111
125
        self.revprops = copy(rev.properties)
112
126
        if contains_whitespace(self.revision_id):
113
127
            raise ValueError(self.revision_id)
143
157
        return [line.encode('utf-8') for line in r]
144
158
 
145
159
    def _get_entries(self):
146
 
        entries = self.inventory.iter_entries()
147
 
        entries.next()
148
 
        return entries
 
160
        return ((path, ie) for (path, versioned, kind, file_id, ie) in
 
161
                self.tree.list_files(include_root=self.include_root))
149
162
 
150
163
    def _escape_path(self, path):
151
164
        if contains_linebreaks(path):
152
165
            raise ValueError(path)
153
 
        return unicode(path.replace('\\', '/').replace(' ', '\ '))
 
166
        return text_type(path.replace('\\', '/').replace(' ', '\\ '))
154
167
 
155
168
    def _entry_to_line(self, path, ie):
156
169
        """Turn an inventory entry into a testament line"""
176
189
        return l
177
190
 
178
191
    def as_text(self):
179
 
        return ''.join(self.as_text_lines())
 
192
        return b''.join(self.as_text_lines())
180
193
 
181
194
    def as_short_text(self):
182
195
        """Return short digest-based testament."""
183
196
        return (self.short_header +
184
 
                'revision-id: %s\n'
185
 
                'sha1: %s\n'
 
197
                b'revision-id: %s\n'
 
198
                b'sha1: %s\n'
186
199
                % (self.revision_id, self.as_sha1()))
187
200
 
188
201
    def _revprops_to_lines(self):
199
212
        return r
200
213
 
201
214
    def as_sha1(self):
202
 
        s = sha()
203
 
        map(s.update, self.as_text_lines())
204
 
        return s.hexdigest()
 
215
        return sha_strings(self.as_text_lines())
205
216
 
206
217
 
207
218
class StrictTestament(Testament):
209
220
 
210
221
    long_header = 'bazaar-ng testament version 2.1\n'
211
222
    short_header = 'bazaar-ng testament short form 2.1\n'
 
223
    include_root = False
212
224
    def _entry_to_line(self, path, ie):
213
225
        l = Testament._entry_to_line(self, path, ie)[:-1]
214
 
        l += ' ' + ie.revision
 
226
        l += ' ' + ie.revision.decode('ascii')
215
227
        l += {True: ' yes\n', False: ' no\n'}[ie.executable]
216
228
        return l
217
229
 
224
236
 
225
237
    long_header = 'bazaar testament version 3 strict\n'
226
238
    short_header = 'bazaar testament short form 3 strict\n'
227
 
    def _get_entries(self):
228
 
        return self.inventory.iter_entries()
 
239
    include_root = True
229
240
 
230
241
    def _escape_path(self, path):
231
242
        if contains_linebreaks(path):
232
243
            raise ValueError(path)
233
244
        if path == '':
234
245
            path = '.'
235
 
        return unicode(path.replace('\\', '/').replace(' ', '\ '))
 
246
        return text_type(path.replace('\\', '/').replace(' ', '\\ '))