/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
1
from StringIO import StringIO
2
3
from bzrlib import (
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
4
    branch as _mod_branch,
5
    diff,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
6
    errors,
1551.12.16 by Aaron Bentley
Enable signing merge directives
7
    gpg,
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
8
    revision as _mod_revision,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
9
    rio,
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
10
    testament,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
11
    )
12
from bzrlib.bundle import serializer as bundle_serializer
13
14
15
class MergeDirective(object):
16
1551.12.12 by Aaron Bentley
Add format header
17
    _format_string = 'Bazaar merge directive format experimental-1'
18
1551.12.4 by Aaron Bentley
Add failing test
19
    def __init__(self, revision_id, testament_sha1, time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
20
                 target_branch, patch=None, patch_type=None,
21
                 source_branch=None):
1551.12.6 by Aaron Bentley
Force times to be floats
22
        assert isinstance(time, float)
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
23
        assert patch_type in (None, 'diff', 'bundle')
1551.12.13 by Aaron Bentley
Rename fields
24
        if patch_type != 'bundle' and source_branch is None:
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
25
            raise errors.NoMergeSource()
26
        if patch_type is not None and patch is None:
27
            raise errors.PatchMissing(patch_type)
1551.12.4 by Aaron Bentley
Add failing test
28
        self.revision_id = revision_id
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
29
        self.testament_sha1 = testament_sha1
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
30
        self.time = time
31
        self.timezone = timezone
1551.12.13 by Aaron Bentley
Rename fields
32
        self.target_branch = target_branch
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
33
        self.patch = patch
34
        self.patch_type = patch_type
1551.12.13 by Aaron Bentley
Rename fields
35
        self.source_branch = source_branch
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
36
1551.12.12 by Aaron Bentley
Add format header
37
    @classmethod
38
    def from_lines(klass, lines):
39
        assert lines[0].startswith('# ' + klass._format_string + '\n')
40
        line_iter = iter(lines[1:])
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
41
        stanza = rio.read_patch_stanza(line_iter)
42
        patch_lines = list(line_iter)
43
        if len(patch_lines) == 0:
44
            patch = None
45
        else:
46
            patch = ''.join(patch_lines)
47
        try:
48
            bundle_serializer.read_bundle(StringIO(patch))
49
        except errors.NotABundle:
50
            patch_type = 'diff'
51
        else:
52
            patch_type = 'bundle'
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
53
        time, timezone = bundle_serializer.unpack_highres_date(
54
            stanza.get('timestamp'))
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
55
        kwargs = {}
1551.12.13 by Aaron Bentley
Rename fields
56
        for key in ('revision_id', 'testament_sha1', 'target_branch',
57
                    'source_branch'):
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
58
            try:
59
                kwargs[key] = stanza.get(key)
60
            except KeyError:
61
                pass
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
62
        return MergeDirective(time=time, timezone=timezone,
63
                              patch_type=patch_type, patch=patch, **kwargs)
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
64
65
    def to_lines(self):
1551.12.3 by Aaron Bentley
Add timestamps to merge directives
66
        timestamp = bundle_serializer.format_highres_date(self.time,
67
                                                          self.timezone)
1551.12.4 by Aaron Bentley
Add failing test
68
        stanza = rio.Stanza(revision_id=self.revision_id, timestamp=timestamp,
1551.12.13 by Aaron Bentley
Rename fields
69
                            target_branch=self.target_branch,
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
70
                            testament_sha1=self.testament_sha1)
1551.12.13 by Aaron Bentley
Rename fields
71
        for key in ('source_branch',):
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
72
            if self.__dict__[key] is not None:
73
                stanza.add(key, self.__dict__[key])
1551.12.12 by Aaron Bentley
Add format header
74
        lines = ['# ' + self._format_string + '\n']
75
        lines.extend(rio.to_patch_lines(stanza))
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
76
        lines.append('# \n')
77
        if self.patch is not None:
78
            lines.extend(self.patch.splitlines(True))
79
        return lines
80
1551.12.16 by Aaron Bentley
Enable signing merge directives
81
    def to_signed(self, branch):
82
        my_gpg = gpg.GPGStrategy(branch.get_config())
83
        return my_gpg.sign(''.join(self.to_lines()))
84
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
85
    @classmethod
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
86
    def from_objects(klass, repository, revision_id, time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
87
                 target_branch, patch_type='bundle',
88
                 local_target_branch=None, public_branch=None):
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
89
        if public_branch is not None:
1551.12.13 by Aaron Bentley
Rename fields
90
            source_branch = public_branch.base
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
91
            if not public_branch.repository.has_revision(revision_id):
1551.12.13 by Aaron Bentley
Rename fields
92
                raise errors.PublicBranchOutOfDate(source_branch,
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
93
                                                   revision_id)
1551.12.7 by Aaron Bentley
Fix use of public location/branch
94
        else:
1551.12.13 by Aaron Bentley
Rename fields
95
            source_branch = None
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
96
        t = testament.StrictTestament3.from_revision(repository, revision_id)
97
        if patch_type is None:
98
            patch = None
1551.12.2 by Aaron Bentley
Got directives round-tripping, with bundles and everything
99
        else:
1551.12.13 by Aaron Bentley
Rename fields
100
            submit_branch = _mod_branch.Branch.open(target_branch)
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
101
            submit_revision_id = submit_branch.last_revision()
102
            repository.fetch(submit_branch.repository, submit_revision_id)
103
            ancestor_id = _mod_revision.common_ancestor(revision_id,
104
                                                        submit_revision_id,
105
                                                        repository)
106
            if patch_type == 'bundle':
107
                s = StringIO()
108
                bundle_serializer.write_bundle(repository, revision_id,
109
                                               ancestor_id, s)
110
                patch = s.getvalue()
111
            elif patch_type == 'diff':
112
                patch = klass._generate_diff(repository, revision_id,
113
                                             ancestor_id)
114
        return MergeDirective(revision_id, t.as_sha1(), time, timezone,
1551.12.13 by Aaron Bentley
Rename fields
115
                              target_branch, patch, patch_type, source_branch)
1551.12.5 by Aaron Bentley
Get MergeDirective.from_objects working
116
117
    @staticmethod
118
    def _generate_diff(repository, revision_id, ancestor_id):
119
        tree_1 = repository.revision_tree(ancestor_id)
120
        tree_2 = repository.revision_tree(revision_id)
121
        s = StringIO()
122
        diff.show_diff_trees(tree_1, tree_2, s)
123
        return s.getvalue()