/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/revision.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
 
18
 
 
19
# TODO: Some kind of command-line display of revision properties:
 
20
# perhaps show them in log -v and allow them as options to the commit command.
 
21
 
 
22
 
 
23
from .lazy_import import lazy_import
 
24
lazy_import(globals(), """
 
25
from breezy import bugtracker
 
26
""")
 
27
from . import (
 
28
    errors,
 
29
    osutils,
 
30
    )
 
31
from .sixish import (
 
32
    text_type,
 
33
    )
 
34
 
 
35
NULL_REVISION = b"null:"
 
36
CURRENT_REVISION = b"current:"
 
37
 
 
38
 
 
39
class Revision(object):
 
40
    """Single revision on a branch.
 
41
 
 
42
    Revisions may know their revision_hash, but only once they've been
 
43
    written out.  This is not stored because you cannot write the hash
 
44
    into the file it describes.
 
45
 
 
46
    After bzr 0.0.5 revisions are allowed to have multiple parents.
 
47
 
 
48
    parent_ids
 
49
        List of parent revision_ids
 
50
 
 
51
    properties
 
52
        Dictionary of revision properties.  These are attached to the
 
53
        revision as extra metadata.  The name must be a single
 
54
        word; the value can be an arbitrary string.
 
55
    """
 
56
 
 
57
    def __init__(self, revision_id, properties=None, **args):
 
58
        self.revision_id = revision_id
 
59
        if properties is None:
 
60
            self.properties = {}
 
61
        else:
 
62
            self.properties = properties
 
63
            self._check_properties()
 
64
        self.committer = None
 
65
        self.parent_ids = []
 
66
        self.parent_sha1s = []
 
67
        """Not used anymore - legacy from for 4."""
 
68
        self.__dict__.update(args)
 
69
 
 
70
    def __repr__(self):
 
71
        return "<Revision id %s>" % self.revision_id
 
72
 
 
73
    def __eq__(self, other):
 
74
        if not isinstance(other, Revision):
 
75
            return False
 
76
        return (
 
77
            self.inventory_sha1 == other.inventory_sha1
 
78
            and self.revision_id == other.revision_id
 
79
            and self.timestamp == other.timestamp
 
80
            and self.message == other.message
 
81
            and self.timezone == other.timezone
 
82
            and self.committer == other.committer
 
83
            and self.properties == other.properties
 
84
            and self.parent_ids == other.parent_ids)
 
85
 
 
86
    def __ne__(self, other):
 
87
        return not self.__eq__(other)
 
88
 
 
89
    def _check_properties(self):
 
90
        """Verify that all revision properties are OK."""
 
91
        for name, value in self.properties.items():
 
92
            # GZ 2017-06-10: What sort of string are properties exactly?
 
93
            not_text = not isinstance(name, (text_type, str))
 
94
            if not_text or osutils.contains_whitespace(name):
 
95
                raise ValueError("invalid property name %r" % name)
 
96
            if not isinstance(value, (text_type, bytes)):
 
97
                raise ValueError("invalid property value %r for %r" %
 
98
                                 (value, name))
 
99
 
 
100
    def get_history(self, repository):
 
101
        """Return the canonical line-of-history for this revision.
 
102
 
 
103
        If ghosts are present this may differ in result from a ghost-free
 
104
        repository.
 
105
        """
 
106
        current_revision = self
 
107
        reversed_result = []
 
108
        while current_revision is not None:
 
109
            reversed_result.append(current_revision.revision_id)
 
110
            if not len(current_revision.parent_ids):
 
111
                reversed_result.append(None)
 
112
                current_revision = None
 
113
            else:
 
114
                next_revision_id = current_revision.parent_ids[0]
 
115
                current_revision = repository.get_revision(next_revision_id)
 
116
        reversed_result.reverse()
 
117
        return reversed_result
 
118
 
 
119
    def get_summary(self):
 
120
        """Get the first line of the log message for this revision.
 
121
 
 
122
        Return an empty string if message is None.
 
123
        """
 
124
        if self.message:
 
125
            return self.message.lstrip().split('\n', 1)[0]
 
126
        else:
 
127
            return ''
 
128
 
 
129
    def get_apparent_authors(self):
 
130
        """Return the apparent authors of this revision.
 
131
 
 
132
        If the revision properties contain the names of the authors,
 
133
        return them. Otherwise return the committer name.
 
134
 
 
135
        The return value will be a list containing at least one element.
 
136
        """
 
137
        authors = self.properties.get('authors', None)
 
138
        if authors is None:
 
139
            author = self.properties.get('author', self.committer)
 
140
            if author is None:
 
141
                return []
 
142
            return [author]
 
143
        else:
 
144
            return authors.split("\n")
 
145
 
 
146
    def iter_bugs(self):
 
147
        """Iterate over the bugs associated with this revision."""
 
148
        bug_property = self.properties.get('bugs', None)
 
149
        if bug_property is None:
 
150
            return iter([])
 
151
        return bugtracker.decode_bug_urls(bug_property)
 
152
 
 
153
 
 
154
def iter_ancestors(revision_id, revision_source, only_present=False):
 
155
    ancestors = (revision_id,)
 
156
    distance = 0
 
157
    while len(ancestors) > 0:
 
158
        new_ancestors = []
 
159
        for ancestor in ancestors:
 
160
            if not only_present:
 
161
                yield ancestor, distance
 
162
            try:
 
163
                revision = revision_source.get_revision(ancestor)
 
164
            except errors.NoSuchRevision as e:
 
165
                if e.revision == revision_id:
 
166
                    raise
 
167
                else:
 
168
                    continue
 
169
            if only_present:
 
170
                yield ancestor, distance
 
171
            new_ancestors.extend(revision.parent_ids)
 
172
        ancestors = new_ancestors
 
173
        distance += 1
 
174
 
 
175
 
 
176
def find_present_ancestors(revision_id, revision_source):
 
177
    """Return the ancestors of a revision present in a branch.
 
178
 
 
179
    It's possible that a branch won't have the complete ancestry of
 
180
    one of its revisions.
 
181
 
 
182
    """
 
183
    found_ancestors = {}
 
184
    anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
 
185
                                        only_present=True))
 
186
    for anc_order, (anc_id, anc_distance) in anc_iter:
 
187
        if anc_id not in found_ancestors:
 
188
            found_ancestors[anc_id] = (anc_order, anc_distance)
 
189
    return found_ancestors
 
190
 
 
191
 
 
192
def __get_closest(intersection):
 
193
    intersection.sort()
 
194
    matches = []
 
195
    for entry in intersection:
 
196
        if entry[0] == intersection[0][0]:
 
197
            matches.append(entry[2])
 
198
    return matches
 
199
 
 
200
 
 
201
def is_reserved_id(revision_id):
 
202
    """Determine whether a revision id is reserved
 
203
 
 
204
    :return: True if the revision is reserved, False otherwise
 
205
    """
 
206
    return isinstance(revision_id, bytes) and revision_id.endswith(b':')
 
207
 
 
208
 
 
209
def check_not_reserved_id(revision_id):
 
210
    """Raise ReservedId if the supplied revision_id is reserved"""
 
211
    if is_reserved_id(revision_id):
 
212
        raise errors.ReservedId(revision_id)
 
213
 
 
214
 
 
215
def ensure_null(revision_id):
 
216
    """Ensure only NULL_REVISION is used to represent the null revision"""
 
217
    if revision_id is None:
 
218
        raise ValueError(
 
219
            'NULL_REVISION should be used for the null'
 
220
            ' revision instead of None.')
 
221
    return revision_id
 
222
 
 
223
 
 
224
def is_null(revision_id):
 
225
    if revision_id is None:
 
226
        raise ValueError('NULL_REVISION should be used for the null'
 
227
                         ' revision instead of None.')
 
228
    return (revision_id == NULL_REVISION)