/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 bzrlib/merge_directive.py

  • Committer: Aaron Bentley
  • Date: 2007-06-27 16:11:12 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070627161112-t1r8d32qifq7ox07
Implement patch verification

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
from email import Message
19
19
from StringIO import StringIO
 
20
import re
20
21
 
21
22
from bzrlib import (
22
23
    branch as _mod_branch,
60
61
        self.source_branch = source_branch
61
62
        self.message = message
62
63
 
63
 
    def _to_lines(self):
 
64
    def _to_lines(self, base_revision=False):
64
65
        """Serialize as a list of lines
65
66
 
66
67
        :return: a list of lines
72
73
        for key in ('source_branch', 'message'):
73
74
            if self.__dict__[key] is not None:
74
75
                stanza.add(key, self.__dict__[key])
 
76
        if base_revision:
 
77
            stanza.add('base_revision_id', self.base_revision_id)
75
78
        lines = ['# ' + self._format_string + '\n']
76
79
        lines.extend(rio.to_patch_lines(stanza))
77
80
        lines.append('# \n')
194
197
            else:
195
198
                source_branch = _mod_branch.Branch.open(self.source_branch)
196
199
                target_repo.fetch(source_branch.repository, self.revision_id)
 
200
        if self.patch is not None:
 
201
            self._verify_patch(target_repo)
197
202
        return self.revision_id
198
203
 
199
204
 
248
253
    def get_raw_bundle(self):
249
254
        return self.bundle
250
255
 
 
256
    def _verify_patch(self, repository):
 
257
        pass
 
258
 
251
259
    def _bundle(self):
252
260
        if self.patch_type == 'bundle':
253
261
            return self.patch
322
330
 
323
331
    def __init__(self, revision_id, testament_sha1, time, timezone,
324
332
                 target_branch, patch=None, source_branch=None, message=None,
325
 
                 bundle=None):
 
333
                 bundle=None, base_revision_id=None):
326
334
        if source_branch is None and bundle is None:
327
335
            raise errors.NoMergeSource()
328
336
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
329
337
            timezone, target_branch, patch, source_branch, message)
330
338
        self.bundle = bundle
 
339
        self.base_revision_id = base_revision_id
331
340
 
332
341
    def _patch_type(self):
333
342
        if self.bundle is not None:
377
386
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
378
387
        kwargs = {}
379
388
        for key in ('revision_id', 'testament_sha1', 'target_branch',
380
 
                    'source_branch', 'message'):
 
389
                    'source_branch', 'message', 'base_revision_id'):
381
390
            try:
382
391
                kwargs[key] = stanza.get(key)
383
392
            except KeyError:
384
393
                pass
385
394
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
 
395
        kwargs['base_revision_id'] =\
 
396
            kwargs['base_revision_id'].encode('utf-8')
386
397
        return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
387
398
                     **kwargs)
388
399
 
389
400
    def to_lines(self):
390
 
        lines = self._to_lines()
 
401
        lines = self._to_lines(base_revision=True)
391
402
        if self.patch is not None:
392
403
            lines.append('# Begin patch\n')
393
404
            lines.extend(self.patch.splitlines(True))
435
446
            locked.append(submit_branch)
436
447
            if submit_branch.get_public_branch() is not None:
437
448
                target_branch = submit_branch.get_public_branch()
 
449
            submit_revision_id = submit_branch.last_revision()
 
450
            submit_revision_id = _mod_revision.ensure_null(submit_revision_id)
 
451
            graph = repository.get_graph(submit_branch.repository)
 
452
            ancestor_id = graph.find_unique_lca(revision_id,
 
453
                                                submit_revision_id)
438
454
            if patch_type is None:
439
455
                patch = None
440
456
                bundle = None
441
457
            else:
442
 
                submit_revision_id = submit_branch.last_revision()
443
 
                submit_revision_id = _mod_revision.ensure_null(
444
 
                    submit_revision_id)
445
458
                repository.fetch(submit_branch.repository, submit_revision_id)
446
 
                graph = repository.get_graph()
447
 
                ancestor_id = graph.find_unique_lca(revision_id,
448
 
                                                    submit_revision_id)
449
459
                if patch_type in ('bundle', 'diff'):
450
460
                    patch = klass._generate_diff(repository, revision_id,
451
461
                                                 ancestor_id)
467
477
            for entry in reversed(locked):
468
478
                entry.unlock()
469
479
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
470
 
            patch, public_branch, message, bundle)
 
480
            patch, public_branch, message, bundle, ancestor_id)
 
481
 
 
482
    def _verify_patch(self, repository):
 
483
        calculated_patch = self._generate_diff(repository, self.revision_id,
 
484
                                               self.base_revision_id)
 
485
        # Convert line-endings to UNIX
 
486
        stored_patch = re.sub('\r\n?', '\n', self.patch)
 
487
        # Strip trailing whitespace
 
488
        calculated_patch = re.sub(' *\n', '\n', calculated_patch)
 
489
        stored_patch = re.sub(' *\n', '\n', stored_patch)
 
490
        if calculated_patch != stored_patch:
 
491
            raise errors.PatchVerificationFailed()
 
492
 
471
493
 
472
494
class MergeDirectiveFormatRegistry(registry.Registry):
473
495