/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Copyright (C) 2007-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tag strategies.

These are contained within a branch and normally constructed
when the branch is opened.  Clients should typically do

  Branch.tags.add('name', 'value')
"""

from collections import defaultdict
import contextlib
import itertools
import re
import sys

# NOTE: I was going to call this tags.py, but vim seems to think all files
# called tags* are ctags files... mbp 20070220.

from .inter import InterObject
from .registry import Registry

from . import (
    errors,
    )


def _reconcile_tags(source_dict, dest_dict, overwrite, selector):
    """Do a two-way merge of two tag dictionaries.

    * only in source => source value
    * only in destination => destination value
    * same definitions => that
    * different definitions => if overwrite is False, keep destination
      value and add to conflict list, otherwise use the source value

    :returns: (result_dict, updates,
        [(conflicting_tag, source_target, dest_target)])
    """
    conflicts = []
    updates = {}
    result = dict(dest_dict)  # copy
    for name, target in source_dict.items():
        if selector and not selector(name):
            continue
        if result.get(name) == target:
            pass
        elif name not in result or overwrite:
            updates[name] = target
            result[name] = target
        else:
            conflicts.append((name, target, result[name]))
    return result, updates, conflicts


class Tags(object):

    def __init__(self, branch):
        self.branch = branch

    def get_tag_dict(self):
        """Return a dictionary mapping tags to revision ids.
        """
        raise NotImplementedError(self.get_tag_dict)

    def get_reverse_tag_dict(self):
        """Returns a dict with revisions as keys
           and a list of tags for that revision as value"""
        d = self.get_tag_dict()
        rev = defaultdict(set)
        for key in d:
            rev[d[key]].add(key)
        return rev

    def merge_to(self, to_tags, overwrite=False, ignore_master=False, selector=None):
        """Copy tags between repositories if necessary and possible.

        This method has common command-line behaviour about handling
        error cases.

        All new definitions are copied across, except that tags that already
        exist keep their existing definitions.

        :param to_tags: Branch to receive these tags
        :param overwrite: Overwrite conflicting tags in the target branch
        :param ignore_master: Do not modify the tags in the target's master
            branch (if any).  Default is false (so the master will be updated).

        :returns: Tuple with tag_updates and tag_conflicts.
            tag_updates is a dictionary with new tags, None is used for
            removed tags
            tag_conflicts is a set of tags that conflicted, each of which is
            (tagname, source_target, dest_target), or None if no copying was
            done.
        """
        intertags = InterTags.get(self, to_tags)
        return intertags.merge(
            overwrite=overwrite, ignore_master=ignore_master,
            selector=selector)

    def set_tag(self, tag_name, revision):
        """Set a tag.

        :param tag_name: Tag name
        :param revision: Revision id
        :raise GhostTagsNotSupported: if revision is not present in
            the branch repository
        """
        raise NotImplementedError(self.set_tag)

    def lookup_tag(self, tag_name):
        """Look up a tag.

        :param tag_name: Tag to look up
        :raise NoSuchTag: Raised when tag does not exist
        :return: Matching revision id
        """
        raise NotImplementedError(self.lookup_tag)

    def delete_tag(self, tag_name):
        """Delete a tag.

        :param tag_name: Tag to delete
        :raise NoSuchTag: Raised when tag does not exist
        """
        raise NotImplementedError(self.delete_tag)

    def rename_revisions(self, rename_map):
        """Rename revisions in this tags dictionary.

        :param rename_map: Dictionary mapping old revids to new revids
        """
        reverse_tags = self.get_reverse_tag_dict()
        for revid, names in reverse_tags.items():
            if revid in rename_map:
                for name in names:
                    self.set_tag(name, rename_map[revid])

    def has_tag(self, tag_name):
        return tag_name in self.get_tag_dict()


class DisabledTags(Tags):
    """Tag storage that refuses to store anything.

    This is used by older formats that can't store tags.
    """

    def _not_supported(self, *a, **k):
        raise errors.TagsNotSupported(self.branch)

    set_tag = _not_supported
    get_tag_dict = _not_supported
    _set_tag_dict = _not_supported
    lookup_tag = _not_supported
    delete_tag = _not_supported

    def merge_to(self, to_tags, overwrite=False, ignore_master=False, selector=None):
        # we never have anything to copy
        return {}, []

    def rename_revisions(self, rename_map):
        # No tags, so nothing to rename
        pass

    def get_reverse_tag_dict(self):
        # There aren't any tags, so the reverse mapping is empty.
        return {}


class InterTags(InterObject):
    """Operations between sets of tags.
    """

    _optimisers = []
    """The available optimised InterTags types."""

    @classmethod
    def is_compatible(klass, source, target):
        # This is the default implementation
        return True

    def merge(self, overwrite=False, ignore_master=False, selector=None):
        """Copy tags between repositories if necessary and possible.

        This method has common command-line behaviour about handling
        error cases.

        All new definitions are copied across, except that tags that already
        exist keep their existing definitions.

        :param to_tags: Branch to receive these tags
        :param overwrite: Overwrite conflicting tags in the target branch
        :param ignore_master: Do not modify the tags in the target's master
            branch (if any).  Default is false (so the master will be updated).
        :param selector: Callback that determines whether a tag should be
            copied. It should take a tag name and as argument and return a
            boolean.

        :returns: Tuple with tag_updates and tag_conflicts.
            tag_updates is a dictionary with new tags, None is used for
            removed tags
            tag_conflicts is a set of tags that conflicted, each of which is
            (tagname, source_target, dest_target), or None if no copying was
            done.
        """
        with contextlib.ExitStack() as stack:
            if self.source.branch == self.target.branch:
                return {}, []
            if not self.source.branch.supports_tags():
                # obviously nothing to copy
                return {}, []
            source_dict = self.source.get_tag_dict()
            if not source_dict:
                # no tags in the source, and we don't want to clobber anything
                # that's in the destination
                return {}, []
            # We merge_to both master and child individually.
            #
            # It's possible for master and child to have differing sets of
            # tags, in which case it's possible to have different sets of
            # conflicts.  We report the union of both conflict sets.  In
            # that case it's likely the child and master have accepted
            # different tags from the source, which may be a surprising result, but
            # the best we can do in the circumstances.
            #
            # Ideally we'd improve this API to report the different conflicts
            # more clearly to the caller, but we don't want to break plugins
            # such as bzr-builddeb that use this API.
            stack.enter_context(self.target.branch.lock_write())
            if ignore_master:
                master = None
            else:
                master = self.target.branch.get_master_branch()
            if master is not None:
                stack.enter_context(master.lock_write())
            updates, conflicts = self._merge_to(
                self.target, source_dict, overwrite, selector=selector)
            if master is not None:
                extra_updates, extra_conflicts = self._merge_to(
                    master.tags, source_dict, overwrite, selector=selector)
                updates.update(extra_updates)
                conflicts += extra_conflicts
            # We use set() to remove any duplicate conflicts from the master
            # branch.
            return updates, set(conflicts)

    @classmethod
    def _merge_to(cls, to_tags, source_dict, overwrite, selector):
        dest_dict = to_tags.get_tag_dict()
        result, updates, conflicts = _reconcile_tags(
            source_dict, dest_dict, overwrite, selector)
        if result != dest_dict:
            to_tags._set_tag_dict(result)
        return updates, conflicts


class MemoryTags(Tags):

    def __init__(self, tag_dict):
        self._tag_dict = tag_dict

    def get_tag_dict(self):
        return self._tag_dict

    def lookup_tag(self, tag_name):
        """Return the referent string of a tag"""
        td = self.get_tag_dict()
        try:
            return td[tag_name]
        except KeyError:
            raise errors.NoSuchTag(tag_name)

    def set_tag(self, name, revid):
        self._tag_dict[name] = revid

    def delete_tag(self, name):
        try:
            del self._tag_dict[name]
        except KeyError:
            raise errors.NoSuchTag(name)

    def rename_revisions(self, revid_map):
        self._tag_dict = {
            name: revid_map.get(revid, revid)
            for name, revid in self._tag_dict.items()}

    def _set_tag_dict(self, result):
        self._tag_dict = dict(result.items())

    def merge_to(self, to_tags, overwrite=False, ignore_master=False, selector=None):
        source_dict = self.get_tag_dict()
        dest_dict = to_tags.get_tag_dict()
        result, updates, conflicts = _reconcile_tags(
            source_dict, dest_dict, overwrite, selector)
        if result != dest_dict:
            to_tags._set_tag_dict(result)
        return updates, conflicts


def sort_natural(branch, tags):
    """Sort tags, with numeric substrings as numbers.

    :param branch: Branch
    :param tags: List of tuples with tag name and revision id.
    """
    def natural_sort_key(tag):
        return [f(s) for f, s in
                zip(itertools.cycle((str.lower, int)),
                    re.split('([0-9]+)', tag[0]))]
    tags.sort(key=natural_sort_key)


def sort_alpha(branch, tags):
    """Sort tags lexicographically, in place.

    :param branch: Branch
    :param tags: List of tuples with tag name and revision id.
    """
    tags.sort()


def sort_time(branch, tags):
    """Sort tags by time inline.

    :param branch: Branch
    :param tags: List of tuples with tag name and revision id.
    """
    timestamps = {}
    for tag, revid in tags:
        try:
            revobj = branch.repository.get_revision(revid)
        except errors.NoSuchRevision:
            timestamp = sys.maxsize  # place them at the end
        else:
            timestamp = revobj.timestamp
        timestamps[revid] = timestamp
    tags.sort(key=lambda x: timestamps[x[1]])


tag_sort_methods = Registry()
tag_sort_methods.register("natural", sort_natural,
                          'Sort numeric substrings as numbers. (default)')
tag_sort_methods.register("alpha", sort_alpha, 'Sort tags lexicographically.')
tag_sort_methods.register("time", sort_time, 'Sort tags chronologically.')
tag_sort_methods.default_key = "natural"


InterTags.register_optimiser(InterTags)