/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
1
#!/usr/bin/env python
2
"""\
3
Read in a changeset output, and process it into a Changeset object.
4
"""
5
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
6
from bzrlib.tree import Tree
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
7
import pprint
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
8
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
9
from bzrlib.trace import mutter
0.5.62 by John Arbash Meinel
Doing some internal validation before allowing processing to continue, additional checks at the command level.
10
from bzrlib.errors import BzrError
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
11
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
12
class BadChangeset(Exception): pass
13
class MalformedHeader(BadChangeset): pass
14
class MalformedPatches(BadChangeset): pass
15
class MalformedFooter(BadChangeset): pass
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
16
0.5.11 by John Arbash Meinel
Working on properly representing renames.
17
def _unescape(name):
18
    """Now we want to find the filename effected.
19
    Unfortunately the filename is written out as
20
    repr(filename), which means that it surrounds
21
    the name with quotes which may be single or double
22
    (single is preferred unless there is a single quote in
23
    the filename). And some characters will be escaped.
24
25
    TODO:   There has to be some pythonic way of undo-ing the
26
            representation of a string rather than using eval.
27
    """
28
    delimiter = name[0]
29
    if name[-1] != delimiter:
30
        raise BadChangeset('Could not properly parse the'
31
                ' filename: %r' % name)
32
    # We need to handle escaped hexadecimals too.
33
    return name[1:-1].replace('\"', '"').replace("\'", "'")
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
34
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
35
class RevisionInfo(object):
36
    """Gets filled out for each revision object that is read.
37
    """
38
    def __init__(self, rev_id):
39
        self.rev_id = rev_id
40
        self.sha1 = None
41
        self.committer = None
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
42
        self.date = None
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
43
        self.timestamp = None
44
        self.timezone = None
45
        self.inventory_id = None
46
        self.inventory_sha1 = None
47
48
        self.parents = None
49
        self.message = None
50
51
    def __str__(self):
52
        return pprint.pformat(self.__dict__)
53
0.5.37 by John Arbash Meinel
Made read_changeset able to spit out 'Revision' entities.
54
    def as_revision(self):
55
        from bzrlib.revision import Revision, RevisionReference
56
        rev = Revision(revision_id=self.rev_id,
57
            committer=self.committer,
58
            timestamp=float(self.timestamp),
59
            timezone=int(self.timezone),
60
            inventory_id=self.inventory_id,
61
            inventory_sha1=self.inventory_sha1,
62
            message='\n'.join(self.message))
63
0.5.59 by John Arbash Meinel
Several fixes for handling the case where you are doing a changeset against revno=0 (Null base)
64
        if self.parents:
65
            for parent in self.parents:
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
66
                rev_id, sha1 = parent.split()
0.5.59 by John Arbash Meinel
Several fixes for handling the case where you are doing a changeset against revno=0 (Null base)
67
                rev.parents.append(RevisionReference(rev_id, sha1))
0.5.37 by John Arbash Meinel
Made read_changeset able to spit out 'Revision' entities.
68
69
        return rev
70
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
71
class ChangesetInfo(object):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
72
    """This contains the meta information. Stuff that allows you to
73
    recreate the revision or inventory XML.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
74
    """
75
    def __init__(self):
76
        self.committer = None
77
        self.date = None
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
78
        self.message = None
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
79
        self.base = None
80
        self.base_sha1 = None
81
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
82
        # A list of RevisionInfo objects
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
83
        self.revisions = []
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
84
85
        self.actions = []
86
87
        # The next entries are created during complete_info() and
88
        # other post-read functions.
89
90
        # A list of real Revision objects
91
        self.real_revisions = []
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
92
93
        self.timestamp = None
94
        self.timezone = None
0.5.15 by John Arbash Meinel
Created an apply-changeset function, and modified output for better parsing.
95
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
96
    def __str__(self):
97
        return pprint.pformat(self.__dict__)
98
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
99
    def complete_info(self):
100
        """This makes sure that all information is properly
101
        split up, based on the assumptions that can be made
102
        when information is missing.
103
        """
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
104
        from common import unpack_highres_date
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
105
        # Put in all of the guessable information.
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
106
        if not self.timestamp and self.date:
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
107
            self.timestamp, self.timezone = unpack_highres_date(self.date)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
108
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
109
        self.real_revisions = []
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
110
        for rev in self.revisions:
0.5.60 by John Arbash Meinel
read_changeset now parses the date: subheader of revisions correctly.
111
            if rev.timestamp is None:
112
                if rev.date is not None:
113
                    rev.timestamp, rev.timezone = \
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
114
                            unpack_highres_date(rev.date)
0.5.60 by John Arbash Meinel
read_changeset now parses the date: subheader of revisions correctly.
115
                else:
116
                    rev.timestamp = self.timestamp
117
                    rev.timezone = self.timezone
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
118
            if rev.message is None and self.message:
119
                rev.message = self.message
120
            if rev.committer is None and self.committer:
121
                rev.committer = self.committer
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
122
            if rev.inventory_id is None:
123
                rev.inventory_id = rev.rev_id
124
            self.real_revisions.append(rev.as_revision())
125
126
        if self.base is None:
127
            # When we don't have a base, then the real base
128
            # is the first parent of the first revision listed
129
            rev = self.real_revisions[0]
0.5.59 by John Arbash Meinel
Several fixes for handling the case where you are doing a changeset against revno=0 (Null base)
130
            if len(rev.parents) == 0:
131
                # There is no base listed, and
132
                # the lowest revision doesn't have a parent
133
                # so this is probably against the empty tree
134
                # and thus base truly is None
135
                self.base = None
136
                self.base_sha1 = None
137
            else:
138
                self.base = rev.parents[0].revision_id
139
                # In general, if self.base is None, self.base_sha1 should
140
                # also be None
141
                if self.base_sha1 is not None:
142
                    assert self.base_sha1 == rev.parents[0].revision_sha1
143
                self.base_sha1 = rev.parents[0].revision_sha1
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
144
0.5.67 by John Arbash Meinel
Working on apply_changeset
145
    def _get_target(self):
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
146
        """Return the target revision."""
0.5.67 by John Arbash Meinel
Working on apply_changeset
147
        if len(self.real_revisions) > 0:
148
            return self.real_revisions[-1].revision_id
149
        elif len(self.revisions) > 0:
150
            return self.revisions[-1].rev_id
151
        return None
152
153
    target = property(_get_target, doc='The target revision id')
154
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
155
class ChangesetReader(object):
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
156
    """This class reads in a changeset from a file, and returns
157
    a Changeset object, which can then be applied against a tree.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
158
    """
159
    def __init__(self, from_file):
160
        """Read in the changeset from the file.
161
162
        :param from_file: A file-like object (must have iterator support).
163
        """
164
        object.__init__(self)
165
        self.from_file = from_file
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
166
        self._next_line = None
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
167
        
168
        self.info = ChangesetInfo()
169
        # We put the actual inventory ids in the footer, so that the patch
170
        # is easier to read for humans.
171
        # Unfortunately, that means we need to read everything before we
172
        # can create a proper changeset.
0.5.62 by John Arbash Meinel
Doing some internal validation before allowing processing to continue, additional checks at the command level.
173
        self._read()
174
        self._validate()
175
176
    def _read(self):
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
177
        self._read_header()
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
178
        self._read_patches()
179
        self._read_footer()
180
0.5.62 by John Arbash Meinel
Doing some internal validation before allowing processing to continue, additional checks at the command level.
181
    def _validate(self):
182
        """Make sure that the information read in makes sense
183
        and passes appropriate checksums.
184
        """
185
        # Fill in all the missing blanks for the revisions
186
        # and generate the real_revisions list.
187
        self.info.complete_info()
188
        self._validate_revisions()
189
190
    def _validate_revisions(self):
191
        """Make sure all revision entries match their checksum."""
192
        from bzrlib.xml import pack_xml
193
        from cStringIO import StringIO
194
        from bzrlib.osutils import sha_file
195
196
        # This is a mapping from each revision id to it's sha hash
197
        rev_to_sha1 = {}
198
199
        for rev, rev_info in zip(self.info.real_revisions, self.info.revisions):
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
200
            assert rev.revision_id == rev_info.rev_id
0.5.62 by John Arbash Meinel
Doing some internal validation before allowing processing to continue, additional checks at the command level.
201
            sio = StringIO()
202
            pack_xml(rev, sio)
203
            sio.seek(0)
204
            sha1 = sha_file(sio)
205
            if sha1 != rev_info.sha1:
206
                raise BzrError('Revision checksum mismatch.'
207
                    ' For rev_id {%s} supplied sha1 (%s) != measured (%s)'
208
                    % (rev.revision_id, rev_info.sha1, sha1))
209
            if rev_to_sha1.has_key(rev.revision_id):
210
                raise BzrError('Revision {%s} given twice in the list'
211
                        % (rev.revision_id))
212
            rev_to_sha1[rev.revision_id] = sha1
213
214
        # Now that we've checked all the sha1 sums, we can make sure that
215
        # at least for the small list we have, all of the references are
216
        # valid.
217
        for rev in self.info.real_revisions:
218
            for parent in rev.parents:
219
                if parent.revision_id in rev_to_sha1:
220
                    if parent.revision_sha1 != rev_to_sha1[parent.revision_id]:
221
                        raise BzrError('Parent revision checksum mismatch.'
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
222
                                ' A parent was referenced with an'
223
                                ' incorrect checksum'
0.5.62 by John Arbash Meinel
Doing some internal validation before allowing processing to continue, additional checks at the command level.
224
                                ': {%r} %s != %s' % (parent.revision_id,
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
225
                                            parent.revision_sha1,
226
                                            rev_to_sha1[parent.revision_id]))
227
228
    def _validate_references_from_branch(self, branch):
229
        """Now that we have a branch which should have some of the
230
        revisions we care about, go through and validate all of them
231
        that we can.
232
        """
233
        rev_to_sha = {}
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
234
        inv_to_sha = {}
235
        def add_sha(d, rev_id, sha1):
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
236
            if rev_id is None:
237
                if sha1 is not None:
238
                    raise BzrError('A Null revision should always'
239
                        'have a null sha1 hash')
240
                return
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
241
            if rev_id in d:
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
242
                # This really should have been validated as part
243
                # of _validate_revisions but lets do it again
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
244
                if sha1 != d[rev_id]:
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
245
                    raise BzrError('** Revision %r referenced with 2 different'
246
                            ' sha hashes %s != %s' % (rev_id,
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
247
                                sha1, d[rev_id]))
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
248
            else:
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
249
                d[rev_id] = sha1
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
250
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
251
        add_sha(rev_to_sha, self.info.base, self.info.base_sha1)
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
252
        # All of the contained revisions were checked
253
        # in _validate_revisions
254
        checked = {}
255
        for rev_info in self.info.revisions:
256
            checked[rev_info.rev_id] = True
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
257
            add_sha(rev_to_sha, rev_info.rev_id, rev_info.sha1)
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
258
                
259
        for rev in self.info.real_revisions:
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
260
            add_sha(inv_to_sha, rev_info.inventory_id, rev_info.inventory_sha1)
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
261
            for parent in rev.parents:
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
262
                add_sha(rev_to_sha, parent.revision_id, parent.revision_sha1)
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
263
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
264
        count = 0
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
265
        missing = {}
266
        for rev_id, sha1 in rev_to_sha.iteritems():
267
            if rev_id in branch.revision_store:
268
                local_sha1 = branch.get_revision_sha1(rev_id)
269
                if sha1 != local_sha1:
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
270
                    raise BzrError('sha1 mismatch. For revision id {%s}' 
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
271
                            'local: %s, cset: %s' % (rev_id, local_sha1, sha1))
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
272
                else:
273
                    count += 1
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
274
            elif rev_id not in checked:
275
                missing[rev_id] = sha1
276
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
277
        for inv_id, sha1 in inv_to_sha.iteritems():
278
            if inv_id in branch.inventory_store:
279
                local_sha1 = branch.get_inventory_sha1(inv_id)
280
                if sha1 != local_sha1:
281
                    raise BzrError('sha1 mismatch. For inventory id {%s}' 
282
                            'local: %s, cset: %s' % (inv_id, local_sha1, sha1))
283
                else:
284
                    count += 1
285
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
286
        if len(missing) > 0:
287
            # I don't know if this is an error yet
288
            from bzrlib.trace import warning
289
            warning('Not all revision hashes could be validated.'
290
                    ' Unable validate %d hashes' % len(missing))
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
291
        mutter('Verified %d sha hashes for the changeset.' % count)
292
293
    def _validate_inventory(self, inv):
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
294
        """At this point we should have generated the ChangesetTree,
295
        so build up an inventory, and make sure the hashes match.
296
        """
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
297
        from bzrlib.xml import pack_xml
298
        from cStringIO import StringIO
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
299
        from bzrlib.osutils import sha_file
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
300
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
301
        assert inv is not None
302
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
303
        # Now we should have a complete inventory entry.
304
        sio = StringIO()
305
        pack_xml(inv, sio)
306
        sio.seek(0)
307
        sha1 = sha_file(sio)
308
        # Target revision is the last entry in the real_revisions list
309
        rev = self.info.real_revisions[-1]
310
        if sha1 != rev.inventory_sha1:
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
311
            open(',,bogus-inv', 'wb').write(sio.getvalue())
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
312
            raise BzrError('Inventory sha hash mismatch.')
313
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
314
        
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
315
    def get_changeset(self, branch):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
316
        """Return the meta information, and a Changeset tree which can
317
        be used to populate the local stores and working tree, respectively.
318
        """
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
319
        self._validate_references_from_branch(branch)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
320
        tree = ChangesetTree(branch.revision_tree(self.info.base))
321
        self._update_tree(tree)
322
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
323
        inv = tree.inventory
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
324
        self._validate_inventory(inv)
0.5.63 by John Arbash Meinel
Moving the validation into part of the reading.
325
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
326
        return self.info, tree
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
327
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
328
    def _next(self):
329
        """yield the next line, but secretly
330
        keep 1 extra line for peeking.
331
        """
332
        for line in self.from_file:
333
            last = self._next_line
334
            self._next_line = line
335
            if last is not None:
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
336
                #mutter('yielding line: %r' % last)
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
337
                yield last
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
338
        last = self._next_line
339
        self._next_line = None
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
340
        #mutter('yielding line: %r' % last)
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
341
        yield last
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
342
343
    def _read_header(self):
344
        """Read the bzr header"""
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
345
        import common
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
346
        header = common.get_header()
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
347
        found = False
348
        for line in self._next():
349
            if found:
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
350
                # not all mailers will keep trailing whitespace
351
                if line == '#\n':
352
                    line = '# \n'
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
353
                if (line[:2] != '# ' or line[-1:] != '\n'
354
                        or line[2:-1] != header[0]):
355
                    raise MalformedHeader('Found a header, but it'
356
                        ' was improperly formatted')
357
                header.pop(0) # We read this line.
358
                if not header:
359
                    break # We found everything.
360
            elif (line[:1] == '#' and line[-1:] == '\n'):
361
                line = line[1:-1].strip()
362
                if line[:len(common.header_str)] == common.header_str:
363
                    if line == header[0]:
364
                        found = True
365
                    else:
366
                        raise MalformedHeader('Found what looks like'
367
                                ' a header, but did not match')
368
                    header.pop(0)
369
        else:
370
            raise MalformedHeader('Did not find an opening header')
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
371
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
372
        for line in self._next():
373
            # The bzr header is terminated with a blank line
374
            # which does not start with '#'
375
            if line == '\n':
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
376
                break
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
377
            self._handle_next(line)
378
379
    def _read_next_entry(self, line, indent=1):
380
        """Read in a key-value pair
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
381
        """
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
382
        if line[:1] != '#':
383
            raise MalformedHeader('Bzr header did not start with #')
384
        line = line[1:-1] # Remove the '#' and '\n'
385
        if line[:indent] == ' '*indent:
386
            line = line[indent:]
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
387
        if not line:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
388
            return None, None# Ignore blank lines
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
389
390
        loc = line.find(': ')
391
        if loc != -1:
392
            key = line[:loc]
393
            value = line[loc+2:]
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
394
            if not value:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
395
                value = self._read_many(indent=indent+3)
396
        elif line[-1:] == ':':
397
            key = line[:-1]
398
            value = self._read_many(indent=indent+3)
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
399
        else:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
400
            raise MalformedHeader('While looking for key: value pairs,'
401
                    ' did not find the colon %r' % (line))
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
402
403
        key = key.replace(' ', '_')
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
404
        #mutter('found %s: %s' % (key, value))
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
405
        return key, value
406
407
    def _handle_next(self, line):
408
        key, value = self._read_next_entry(line, indent=1)
409
        if key is None:
410
            return
411
412
        if key == 'revision':
413
            self._read_revision(value)
414
        elif hasattr(self.info, key):
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
415
            if getattr(self.info, key) is None:
416
                setattr(self.info, key, value)
417
            else:
418
                raise MalformedHeader('Duplicated Key: %s' % key)
419
        else:
420
            # What do we do with a key we don't recognize
421
            raise MalformedHeader('Unknown Key: %s' % key)
422
        
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
423
    def _read_many(self, indent):
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
424
        """If a line ends with no entry, that means that it should be
425
        followed with multiple lines of values.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
426
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
427
        This detects the end of the list, because it will be a line that
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
428
        does not start properly indented.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
429
        """
430
        values = []
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
431
        start = '#' + (' '*indent)
432
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
433
        if self._next_line is None or self._next_line[:len(start)] != start:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
434
            return values
435
436
        for line in self._next():
437
            values.append(line[len(start):-1])
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
438
            if self._next_line is None or self._next_line[:len(start)] != start:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
439
                break
440
        return values
441
442
    def _read_one_patch(self):
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
443
        """Read in one patch, return the complete patch, along with
444
        the next line.
445
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
446
        :return: action, lines, do_continue
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
447
        """
0.5.57 by John Arbash Meinel
Simplified the header, only output base if it is not the expected one.
448
        #mutter('_read_one_patch: %r' % self._next_line)
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
449
        # Peek and see if there are no patches
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
450
        if self._next_line is None or self._next_line[:1] == '#':
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
451
            return None, [], False
452
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
453
        first = True
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
454
        lines = []
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
455
        for line in self._next():
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
456
            if first:
457
                if line[:3] != '***':
458
                    raise MalformedPatches('The first line of all patches'
459
                        ' should be a bzr meta line "***"'
460
                        ': %r' % line)
461
                action = line[4:-1]
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
462
            if self._next_line is not None and self._next_line[:3] == '***':
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
463
                return action, lines, True
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
464
            elif self._next_line is None or self._next_line[:1] == '#':
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
465
                return action, lines, False
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
466
467
            if first:
468
                first = False
469
            else:
470
                lines.append(line)
471
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
472
        return action, lines, False
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
473
            
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
474
    def _read_patches(self):
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
475
        do_continue = True
476
        while do_continue:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
477
            action, lines, do_continue = self._read_one_patch()
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
478
            if action is not None:
479
                self.info.actions.append((action, lines))
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
480
481
    def _read_revision(self, rev_id):
482
        """Revision entries have extra information associated.
483
        """
484
        rev_info = RevisionInfo(rev_id)
485
        start = '#    '
486
        for line in self._next():
487
            key,value = self._read_next_entry(line, indent=4)
488
            #if key is None:
489
            #    continue
490
            if hasattr(rev_info, key):
491
                if getattr(rev_info, key) is None:
492
                    setattr(rev_info, key, value)
493
                else:
494
                    raise MalformedHeader('Duplicated Key: %s' % key)
495
            else:
496
                # What do we do with a key we don't recognize
497
                raise MalformedHeader('Unknown Key: %s' % key)
498
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
499
            if self._next_line is None or self._next_line[:len(start)] != start:
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
500
                break
501
502
        self.info.revisions.append(rev_info)
503
504
    def _read_footer(self):
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
505
        """Read the rest of the meta information.
506
0.5.9 by John Arbash Meinel
Now adding the patch information to the ChangesetInfo
507
        :param first_line:  The previous step iterates past what it
508
                            can handle. That extra line is given here.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
509
        """
0.5.36 by John Arbash Meinel
Updated so that read_changeset is able to parse the output
510
        for line in self._next():
511
            self._handle_next(line)
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
512
            if self._next_line is None or self._next_line[:1] != '#':
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
513
                break
514
515
    def _update_tree(self, tree):
516
        """This fills out a ChangesetTree based on the information
517
        that was read in.
518
519
        :param tree: A ChangesetTree to update with the new information.
520
        """
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
521
        from common import decode, guess_text_id
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
522
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
523
        def get_text_id(info, file_id, kind):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
524
            if info is not None:
525
                if info[:8] != 'text-id:':
526
                    raise BzrError("Text ids should be prefixed with 'text-id:'"
527
                        ': %r' % info)
528
                text_id = decode(info[8:])
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
529
            elif tree._text_ids.has_key(file_id):
530
                return tree._text_ids[file_id]
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
531
            else:
532
                # If text_id was not explicitly supplied
533
                # then it should be whatever we would guess it to be
534
                # based on the base revision, and what we know about
535
                # the target revision
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
536
                text_id = guess_text_id(tree.base_tree, 
537
                        file_id, self.info.base, kind, modified=True)
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
538
            tree.note_text_id(file_id, text_id)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
539
            return text_id
540
541
        def renamed(kind, extra, lines):
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
542
            info = extra.split(' // ')
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
543
            if len(info) < 2:
544
                raise BzrError('renamed action lines need both a from and to'
545
                        ': %r' % extra)
546
            old_path = decode(info[0])
547
            if info[1][:3] == '=> ':
548
                new_path = decode(info[1][3:])
549
            else:
550
                new_path = decode(info[1][3:])
551
552
            file_id = tree.path2id(new_path)
553
            if len(info) > 2:
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
554
                text_id = get_text_id(info[2], file_id, kind)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
555
            else:
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
556
                text_id = get_text_id(None, file_id, kind)
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
557
            tree.note_rename(old_path, new_path, text_id)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
558
            if lines:
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
559
                tree.note_patch(new_path, ''.join(lines), text_id)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
560
561
        def removed(kind, extra, lines):
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
562
            info = extra.split(' // ')
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
563
            if len(info) > 1:
564
                # TODO: in the future we might allow file ids to be
565
                # given for removed entries
566
                raise BzrError('removed action lines should only have the path'
567
                        ': %r' % extra)
568
            path = decode(info[0])
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
569
            tree.note_deletion(path, text_id)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
570
571
        def added(kind, extra, lines):
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
572
            info = extra.split(' // ')
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
573
            if len(info) <= 1:
574
                raise BzrError('add action lines require the path and file id'
575
                        ': %r' % extra)
576
            elif len(info) > 3:
577
                raise BzrError('add action lines have fewer than 3 entries.'
578
                        ': %r' % extra)
579
            path = decode(info[0])
0.5.59 by John Arbash Meinel
Several fixes for handling the case where you are doing a changeset against revno=0 (Null base)
580
            if info[1][:8] != 'file-id:':
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
581
                raise BzrError('The file-id should follow the path for an add'
582
                        ': %r' % extra)
583
            file_id = decode(info[1][8:])
584
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
585
            tree.note_id(file_id, path, kind)
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
586
            if kind == 'directory':
587
                return
588
            if len(info) > 2:
589
                text_id = get_text_id(info[2], file_id, kind)
590
            else:
591
                text_id = get_text_id(None, file_id, kind)
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
592
            tree.note_patch(path, ''.join(lines))
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
593
594
        def modified(kind, extra, lines):
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
595
            info = extra.split(' // ')
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
596
            if len(info) < 1:
597
                raise BzrError('modified action lines have at least'
598
                        'the path in them: %r' % extra)
599
            path = decode(info[0])
600
601
            file_id = tree.path2id(path)
602
            if len(info) > 1:
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
603
                text_id = get_text_id(info[1], file_id, kind)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
604
            else:
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
605
                text_id = get_text_id(None, file_id, kind)
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
606
            tree.note_patch(path, ''.join(lines))
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
607
            
608
609
        valid_actions = {
610
            'renamed':renamed,
611
            'removed':removed,
612
            'added':added,
613
            'modified':modified
614
        }
615
        for action_line, lines in self.info.actions:
616
            first = action_line.find(' ')
617
            if first == -1:
618
                raise BzrError('Bogus action line'
619
                        ' (no opening space): %r' % action_line)
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
620
            second = action_line.find(' ', first+1)
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
621
            if second == -1:
622
                raise BzrError('Bogus action line'
623
                        ' (missing second space): %r' % action_line)
624
            action = action_line[:first]
625
            kind = action_line[first+1:second]
626
            if kind not in ('file', 'directory'):
627
                raise BzrError('Bogus action line'
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
628
                        ' (invalid object kind %r): %r' % (kind, action_line))
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
629
            extra = action_line[second+1:]
630
631
            if action not in valid_actions:
632
                raise BzrError('Bogus action line'
633
                        ' (unrecognized action): %r' % action_line)
634
            valid_actions[action](kind, extra, lines)
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
635
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
636
def read_changeset(from_file, branch):
637
    """Read in a changeset from a iterable object (such as a file object)
638
639
    :param from_file: A file-like object to read the changeset information.
640
    :param branch: This will be used to build the changeset tree, it needs
641
                   to contain the base of the changeset. (Which you probably
642
                   won't know about until after the changeset is parsed.)
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
643
    """
644
    cr = ChangesetReader(from_file)
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
645
    return cr.get_changeset(branch)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
646
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
647
class ChangesetTree(Tree):
648
    def __init__(self, base_tree):
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
649
        self.base_tree = base_tree
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
650
        self._renamed = {} # Mapping from old_path => new_path
651
        self._renamed_r = {} # new_path => old_path
652
        self._new_id = {} # new_path => new_id
653
        self._new_id_r = {} # new_id => new_path
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
654
        self._kinds = {} # new_id => kind
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
655
        self._text_ids = {} # new_id => text_id
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
656
        self.patches = {}
0.5.48 by aaron.bentley at utoronto
Implemented deletion for ChangesetTrees
657
        self.deleted = []
0.5.52 by aaron.bentley at utoronto
Make contents-addressing configurable
658
        self.contents_by_id = True
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
659
        self._inventory = None
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
660
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
661
    def __str__(self):
662
        return pprint.pformat(self.__dict__)
663
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
664
    def note_rename(self, old_path, new_path):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
665
        """A file/directory has been renamed from old_path => new_path"""
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
666
        assert not self._renamed.has_key(old_path)
667
        assert not self._renamed_r.has_key(new_path)
668
        self._renamed[new_path] = old_path
669
        self._renamed_r[old_path] = new_path
670
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
671
    def note_id(self, new_id, new_path, kind='file'):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
672
        """Files that don't exist in base need a new id."""
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
673
        self._new_id[new_path] = new_id
674
        self._new_id_r[new_id] = new_path
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
675
        self._kinds[new_id] = kind
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
676
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
677
    def note_text_id(self, file_id, text_id):
678
        if (self._text_ids.has_key(file_id)
679
                and self._text_ids[file_id] != text_id):
680
            raise BzrError('Mismatched text_ids for file_id {%s}'
681
                    ': %s != %s' % (file_id,
682
                                    self._text_ids[file_id],
683
                                    text_id))
684
        self._text_ids[file_id] = text_id
685
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
686
    def note_patch(self, new_path, patch):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
687
        """There is a patch for a given filename."""
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
688
        self.patches[new_path] = patch
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
689
0.5.48 by aaron.bentley at utoronto
Implemented deletion for ChangesetTrees
690
    def note_deletion(self, old_path):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
691
        """The file at old_path has been deleted."""
0.5.48 by aaron.bentley at utoronto
Implemented deletion for ChangesetTrees
692
        self.deleted.append(old_path)
693
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
694
    def old_path(self, new_path):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
695
        """Get the old_path (path in the base_tree) for the file at new_path"""
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
696
        import os.path
697
        old_path = self._renamed.get(new_path)
698
        if old_path is not None:
699
            return old_path
700
        dirname,basename = os.path.split(new_path)
0.5.56 by John Arbash Meinel
A couple more fixups, it seems actually capable now of writing out a changeset, and reading it back.
701
        # dirname is not '' doesn't work, because
702
        # dirname may be a unicode entry, and is
703
        # requires the objects to be identical
704
        if dirname != '':
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
705
            old_dir = self.old_path(dirname)
706
            if old_dir is None:
0.5.42 by aaron.bentley at utoronto
Improved rename handling
707
                old_path = None
708
            else:
709
                old_path = os.path.join(old_dir, basename)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
710
        else:
0.5.42 by aaron.bentley at utoronto
Improved rename handling
711
            old_path = new_path
712
        #If the new path wasn't in renamed, the old one shouldn't be in
713
        #renamed_r
714
        if self._renamed_r.has_key(old_path):
715
            return None
716
        return old_path 
717
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
718
    def new_path(self, old_path):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
719
        """Get the new_path (path in the target_tree) for the file at old_path
720
        in the base tree.
721
        """
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
722
        import os.path
723
        new_path = self._renamed_r.get(old_path)
724
        if new_path is not None:
725
            return new_path
726
        if self._renamed.has_key(new_path):
727
            return None
728
        dirname,basename = os.path.split(old_path)
0.5.81 by John Arbash Meinel
Cleaning up from pychecker.
729
        if dirname != '':
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
730
            new_dir = self.new_path(dirname)
731
            if new_dir is None:
0.5.42 by aaron.bentley at utoronto
Improved rename handling
732
                new_path = None
733
            else:
734
                new_path = os.path.join(new_dir, basename)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
735
        else:
0.5.42 by aaron.bentley at utoronto
Improved rename handling
736
            new_path = old_path
737
        #If the old path wasn't in renamed, the new one shouldn't be in
738
        #renamed_r
739
        if self._renamed.has_key(new_path):
740
            return None
741
        return new_path 
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
742
743
    def path2id(self, path):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
744
        """Return the id of the file present at path in the target tree."""
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
745
        file_id = self._new_id.get(path)
746
        if file_id is not None:
747
            return file_id
0.5.43 by aaron.bentley at utoronto
Handled moves and adds properly
748
        old_path = self.old_path(path)
749
        if old_path is None:
750
            return None
0.5.48 by aaron.bentley at utoronto
Implemented deletion for ChangesetTrees
751
        if old_path in self.deleted:
752
            return None
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
753
        if hasattr(self.base_tree, 'path2id'):
754
            return self.base_tree.path2id(old_path)
755
        else:
756
            return self.base_tree.inventory.path2id(old_path)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
757
758
    def id2path(self, file_id):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
759
        """Return the new path in the target tree of the file with id file_id"""
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
760
        path = self._new_id_r.get(file_id)
761
        if path is not None:
762
            return path
0.5.43 by aaron.bentley at utoronto
Handled moves and adds properly
763
        old_path = self.base_tree.id2path(file_id)
764
        if old_path is None:
765
            return None
0.5.48 by aaron.bentley at utoronto
Implemented deletion for ChangesetTrees
766
        if old_path in self.deleted:
767
            return None
0.5.43 by aaron.bentley at utoronto
Handled moves and adds properly
768
        return self.new_path(old_path)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
769
0.5.52 by aaron.bentley at utoronto
Make contents-addressing configurable
770
    def old_contents_id(self, file_id):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
771
        """Return the id in the base_tree for the given file_id,
772
        or None if the file did not exist in base.
773
774
        FIXME:  Something doesn't seem right here. It seems like this function
775
                should always either return None or file_id. Even if
776
                you are doing the by-path lookup, you are doing a
777
                id2path lookup, just to do the reverse path2id lookup.
778
        """
0.5.52 by aaron.bentley at utoronto
Make contents-addressing configurable
779
        if self.contents_by_id:
780
            if self.base_tree.has_id(file_id):
781
                return file_id
782
            else:
783
                return None
784
        new_path = self.id2path(file_id)
785
        return self.base_tree.path2id(new_path)
786
        
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
787
    def get_file(self, file_id):
0.5.55 by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets.
788
        """Return a file-like object containing the new contents of the
789
        file given by file_id.
790
791
        TODO:   It might be nice if this actually generated an entry
792
                in the text-store, so that the file contents would
793
                then be cached.
794
        """
0.5.52 by aaron.bentley at utoronto
Make contents-addressing configurable
795
        base_id = self.old_contents_id(file_id)
0.5.50 by aaron.bentley at utoronto
Evaluate patches against file paths, not file ids
796
        if base_id is not None:
797
            patch_original = self.base_tree.get_file(base_id)
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
798
        else:
799
            patch_original = None
0.5.52 by aaron.bentley at utoronto
Make contents-addressing configurable
800
        file_patch = self.patches.get(self.id2path(file_id))
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
801
        if file_patch is None:
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
802
            return patch_original
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
803
        return patched_file(file_patch, patch_original)
804
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
805
    def get_kind(self, file_id):
806
        if file_id in self._kinds:
807
            return self._kinds[file_id]
808
        return self.base_tree.inventory[file_id].kind
809
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
810
    def get_text_id(self, file_id):
811
        if file_id in self._text_ids:
812
            return self._text_ids[file_id]
813
        return self.base_tree.inventory[file_id].text_id
814
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
815
    def get_size_and_sha1(self, file_id):
816
        """Return the size and sha1 hash of the given file id.
817
        If the file was not locally modified, this is extracted
818
        from the base_tree. Rather than re-reading the file.
819
        """
820
        from bzrlib.osutils import sha_string
821
822
        new_path = self.id2path(file_id)
823
        if new_path is None:
824
            return None, None
825
        if new_path not in self.patches:
826
            # If the entry does not have a patch, then the
827
            # contents must be the same as in the base_tree
828
            ie = self.base_tree.inventory[file_id]
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
829
            if ie.text_size is None:
830
                return ie.text_size, ie.text_sha1
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
831
            return int(ie.text_size), ie.text_sha1
832
        content = self.get_file(file_id).read()
833
        return len(content), sha_string(content)
834
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
835
836
    def _get_inventory(self):
837
        """Build up the inventory entry for the ChangesetTree.
838
839
        This need to be called before ever accessing self.inventory
840
        """
841
        from os.path import dirname, basename
842
        from bzrlib.inventory import Inventory, InventoryEntry
843
844
        assert self.base_tree is not None
845
        base_inv = self.base_tree.inventory
846
        root_id = base_inv.root.file_id
847
        try:
848
            # New inventories have a unique root_id
849
            inv = Inventory(root_id)
850
        except TypeError:
851
            inv = Inventory()
852
853
        def add_entry(file_id):
854
            path = self.id2path(file_id)
855
            if path is None:
856
                return
857
            parent_path = dirname(path)
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
858
            if parent_path == '':
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
859
                parent_id = root_id
860
            else:
861
                parent_id = self.path2id(parent_path)
862
863
            kind = self.get_kind(file_id)
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
864
            if kind == 'directory':
865
                text_id = None
866
            else:
867
                text_id = self.get_text_id(file_id)
868
869
            name = basename(path)
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
870
            ie = InventoryEntry(file_id, name, kind, parent_id, text_id=text_id)
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
871
            if kind == 'directory':
872
                ie.text_size, ie.text_sha1 = None, None
873
            else:
874
                ie.text_size, ie.text_sha1 = self.get_size_and_sha1(file_id)
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
875
            if (ie.text_size is None) and (kind != 'directory'):
876
                raise BzrError('Got a text_size of None for file_id %r' % file_id)
877
            inv.add(ie)
878
879
        for path, ie in base_inv.iter_entries():
880
            add_entry(ie.file_id)
881
        for file_id in self._new_id_r.iterkeys():
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
882
            if file_id in inv:
883
                continue
884
            path = self.id2path(file_id)
885
            parent_path = dirname(path)
886
            if parent_path != '':
887
                parent_id = self.path2id(parent_path)
888
                if parent_id not in inv:
889
                    add_entry(parent_id)
890
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
891
            add_entry(file_id)
892
893
        return inv
894
895
    # Have to overload the inherited inventory property
896
    # because _get_inventory is only called in the parent.
897
    # Reading the docs, property() objects do not use
898
    # overloading, they use the function as it was defined
899
    # at that instant
900
    inventory = property(_get_inventory)
0.5.64 by John Arbash Meinel
SUCCESS, we now are able to validate the inventory XML.
901
0.5.49 by aaron.bentley at utoronto
Implemented iteration over ids
902
    def __iter__(self):
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
903
        for path, entry in self.inventory.iter_entries():
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
904
            yield entry.file_id
0.5.49 by aaron.bentley at utoronto
Implemented iteration over ids
905
906
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
907
def patched_file(file_patch, original):
908
    from bzrlib.patch import patch
909
    from tempfile import mkdtemp
910
    from shutil import rmtree
911
    from StringIO import StringIO
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
912
    from bzrlib.osutils import pumpfile
913
    import os.path
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
914
    temp_dir = mkdtemp()
915
    try:
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
916
        original_path = os.path.join(temp_dir, "originalfile")
917
        temp_original = file(original_path, "wb")
918
        if original is not None:
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
919
            pumpfile(original, temp_original)
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
920
        temp_original.close()
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
921
        patched_path = os.path.join(temp_dir, "patchfile")
0.5.47 by aaron.bentley at utoronto
Added safety check to patch call
922
        assert patch(file_patch, original_path, patched_path) == 0
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
923
        result = StringIO()
0.5.44 by aaron.bentley at utoronto
Got get_file working for new files
924
        temp_patched = file(patched_path, "rb")
0.5.41 by aaron.bentley at utoronto
Added non-working ChangesetTree
925
        pumpfile(temp_patched, result)
926
        temp_patched.close()
927
        result.seek(0,0)
928
929
    finally:
930
        rmtree(temp_dir)
931
932
    return result
933