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

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 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
 
 
20
from .lazy_import import lazy_import
 
21
lazy_import(globals(), """
 
22
import bisect
 
23
import datetime
 
24
 
 
25
from breezy import (
 
26
    branch as _mod_branch,
 
27
    cache_utf8,
 
28
    revision,
 
29
    workingtree,
 
30
    )
 
31
from breezy.i18n import gettext
 
32
""")
 
33
 
 
34
from . import (
 
35
    errors,
 
36
    lazy_regex,
 
37
    registry,
 
38
    trace,
 
39
    )
 
40
from .sixish import (
 
41
    text_type,
 
42
    )
 
43
 
 
44
 
 
45
class InvalidRevisionSpec(errors.BzrError):
 
46
 
 
47
    _fmt = ("Requested revision: '%(spec)s' does not exist in branch:"
 
48
            " %(branch_url)s%(extra)s")
 
49
 
 
50
    def __init__(self, spec, branch, extra=None):
 
51
        errors.BzrError.__init__(self, branch=branch, spec=spec)
 
52
        self.branch_url = getattr(branch, 'user_url', str(branch))
 
53
        if extra:
 
54
            self.extra = '\n' + str(extra)
 
55
        else:
 
56
            self.extra = ''
 
57
 
 
58
 
 
59
class RevisionInfo(object):
 
60
    """The results of applying a revision specification to a branch."""
 
61
 
 
62
    help_txt = """The results of applying a revision specification to a branch.
 
63
 
 
64
    An instance has two useful attributes: revno, and rev_id.
 
65
 
 
66
    They can also be accessed as spec[0] and spec[1] respectively,
 
67
    so that you can write code like:
 
68
    revno, rev_id = RevisionSpec(branch, spec)
 
69
    although this is probably going to be deprecated later.
 
70
 
 
71
    This class exists mostly to be the return value of a RevisionSpec,
 
72
    so that you can access the member you're interested in (number or id)
 
73
    or treat the result as a tuple.
 
74
    """
 
75
 
 
76
    def __init__(self, branch, revno=None, rev_id=None):
 
77
        self.branch = branch
 
78
        self._has_revno = (revno is not None)
 
79
        self._revno = revno
 
80
        self.rev_id = rev_id
 
81
        if self.rev_id is None and self._revno is not None:
 
82
            # allow caller to be lazy
 
83
            self.rev_id = branch.get_rev_id(self._revno)
 
84
 
 
85
    @property
 
86
    def revno(self):
 
87
        if not self._has_revno and self.rev_id is not None:
 
88
            try:
 
89
                self._revno = self.branch.revision_id_to_revno(self.rev_id)
 
90
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
 
91
                self._revno = None
 
92
            self._has_revno = True
 
93
        return self._revno
 
94
 
 
95
    def __bool__(self):
 
96
        if self.rev_id is None:
 
97
            return False
 
98
        # TODO: otherwise, it should depend on how I was built -
 
99
        # if it's in_history(branch), then check revision_history(),
 
100
        # if it's in_store(branch), do the check below
 
101
        return self.branch.repository.has_revision(self.rev_id)
 
102
 
 
103
    __nonzero__ = __bool__
 
104
 
 
105
    def __len__(self):
 
106
        return 2
 
107
 
 
108
    def __getitem__(self, index):
 
109
        if index == 0:
 
110
            return self.revno
 
111
        if index == 1:
 
112
            return self.rev_id
 
113
        raise IndexError(index)
 
114
 
 
115
    def get(self):
 
116
        return self.branch.repository.get_revision(self.rev_id)
 
117
 
 
118
    def __eq__(self, other):
 
119
        if type(other) not in (tuple, list, type(self)):
 
120
            return False
 
121
        if isinstance(other, type(self)) and self.branch is not other.branch:
 
122
            return False
 
123
        return tuple(self) == tuple(other)
 
124
 
 
125
    def __repr__(self):
 
126
        return '<breezy.revisionspec.RevisionInfo object %s, %s for %r>' % (
 
127
            self.revno, self.rev_id, self.branch)
 
128
 
 
129
    @staticmethod
 
130
    def from_revision_id(branch, revision_id):
 
131
        """Construct a RevisionInfo given just the id.
 
132
 
 
133
        Use this if you don't know or care what the revno is.
 
134
        """
 
135
        return RevisionInfo(branch, revno=None, rev_id=revision_id)
 
136
 
 
137
 
 
138
class RevisionSpec(object):
 
139
    """A parsed revision specification."""
 
140
 
 
141
    help_txt = """A parsed revision specification.
 
142
 
 
143
    A revision specification is a string, which may be unambiguous about
 
144
    what it represents by giving a prefix like 'date:' or 'revid:' etc,
 
145
    or it may have no prefix, in which case it's tried against several
 
146
    specifier types in sequence to determine what the user meant.
 
147
 
 
148
    Revision specs are an UI element, and they have been moved out
 
149
    of the branch class to leave "back-end" classes unaware of such
 
150
    details.  Code that gets a revno or rev_id from other code should
 
151
    not be using revision specs - revnos and revision ids are the
 
152
    accepted ways to refer to revisions internally.
 
153
 
 
154
    (Equivalent to the old Branch method get_revision_info())
 
155
    """
 
156
 
 
157
    prefix = None
 
158
    dwim_catchable_exceptions = (InvalidRevisionSpec,)
 
159
    """Exceptions that RevisionSpec_dwim._match_on will catch.
 
160
 
 
161
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
 
162
    invalid revspec and raises some exception. The exceptions mentioned here
 
163
    will not be reported to the user but simply ignored without stopping the
 
164
    dwim processing.
 
165
    """
 
166
 
 
167
    @staticmethod
 
168
    def from_string(spec):
 
169
        """Parse a revision spec string into a RevisionSpec object.
 
170
 
 
171
        :param spec: A string specified by the user
 
172
        :return: A RevisionSpec object that understands how to parse the
 
173
            supplied notation.
 
174
        """
 
175
        if spec is None:
 
176
            return RevisionSpec(None, _internal=True)
 
177
        if not isinstance(spec, (str, text_type)):
 
178
            raise TypeError("revision spec needs to be text")
 
179
        match = revspec_registry.get_prefix(spec)
 
180
        if match is not None:
 
181
            spectype, specsuffix = match
 
182
            trace.mutter('Returning RevisionSpec %s for %s',
 
183
                         spectype.__name__, spec)
 
184
            return spectype(spec, _internal=True)
 
185
        else:
 
186
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
 
187
            # wait for _match_on to be called.
 
188
            return RevisionSpec_dwim(spec, _internal=True)
 
189
 
 
190
    def __init__(self, spec, _internal=False):
 
191
        """Create a RevisionSpec referring to the Null revision.
 
192
 
 
193
        :param spec: The original spec supplied by the user
 
194
        :param _internal: Used to ensure that RevisionSpec is not being
 
195
            called directly. Only from RevisionSpec.from_string()
 
196
        """
 
197
        if not _internal:
 
198
            raise AssertionError(
 
199
                'Creating a RevisionSpec directly is not supported. '
 
200
                'Use RevisionSpec.from_string() instead.')
 
201
        self.user_spec = spec
 
202
        if self.prefix and spec.startswith(self.prefix):
 
203
            spec = spec[len(self.prefix):]
 
204
        self.spec = spec
 
205
 
 
206
    def _match_on(self, branch, revs):
 
207
        trace.mutter('Returning RevisionSpec._match_on: None')
 
208
        return RevisionInfo(branch, None, None)
 
209
 
 
210
    def _match_on_and_check(self, branch, revs):
 
211
        info = self._match_on(branch, revs)
 
212
        if info:
 
213
            return info
 
214
        elif info == (None, None):
 
215
            # special case - nothing supplied
 
216
            return info
 
217
        elif self.prefix:
 
218
            raise InvalidRevisionSpec(self.user_spec, branch)
 
219
        else:
 
220
            raise InvalidRevisionSpec(self.spec, branch)
 
221
 
 
222
    def in_history(self, branch):
 
223
        return self._match_on_and_check(branch, revs=None)
 
224
 
 
225
        # FIXME: in_history is somewhat broken,
 
226
        # it will return non-history revisions in many
 
227
        # circumstances. The expected facility is that
 
228
        # in_history only returns revision-history revs,
 
229
        # in_store returns any rev. RBC 20051010
 
230
    # aliases for now, when we fix the core logic, then they
 
231
    # will do what you expect.
 
232
    in_store = in_history
 
233
    in_branch = in_store
 
234
 
 
235
    def as_revision_id(self, context_branch):
 
236
        """Return just the revision_id for this revisions spec.
 
237
 
 
238
        Some revision specs require a context_branch to be able to determine
 
239
        their value. Not all specs will make use of it.
 
240
        """
 
241
        return self._as_revision_id(context_branch)
 
242
 
 
243
    def _as_revision_id(self, context_branch):
 
244
        """Implementation of as_revision_id()
 
245
 
 
246
        Classes should override this function to provide appropriate
 
247
        functionality. The default is to just call '.in_history().rev_id'
 
248
        """
 
249
        return self.in_history(context_branch).rev_id
 
250
 
 
251
    def as_tree(self, context_branch):
 
252
        """Return the tree object for this revisions spec.
 
253
 
 
254
        Some revision specs require a context_branch to be able to determine
 
255
        the revision id and access the repository. Not all specs will make
 
256
        use of it.
 
257
        """
 
258
        return self._as_tree(context_branch)
 
259
 
 
260
    def _as_tree(self, context_branch):
 
261
        """Implementation of as_tree().
 
262
 
 
263
        Classes should override this function to provide appropriate
 
264
        functionality. The default is to just call '.as_revision_id()'
 
265
        and get the revision tree from context_branch's repository.
 
266
        """
 
267
        revision_id = self.as_revision_id(context_branch)
 
268
        return context_branch.repository.revision_tree(revision_id)
 
269
 
 
270
    def __repr__(self):
 
271
        # this is mostly for helping with testing
 
272
        return '<%s %s>' % (self.__class__.__name__,
 
273
                            self.user_spec)
 
274
 
 
275
    def needs_branch(self):
 
276
        """Whether this revision spec needs a branch.
 
277
 
 
278
        Set this to False the branch argument of _match_on is not used.
 
279
        """
 
280
        return True
 
281
 
 
282
    def get_branch(self):
 
283
        """When the revision specifier contains a branch location, return it.
 
284
 
 
285
        Otherwise, return None.
 
286
        """
 
287
        return None
 
288
 
 
289
 
 
290
# private API
 
291
 
 
292
class RevisionSpec_dwim(RevisionSpec):
 
293
    """Provides a DWIMish revision specifier lookup.
 
294
 
 
295
    Note that this does not go in the revspec_registry because by definition
 
296
    there is no prefix to identify it.  It's solely called from
 
297
    RevisionSpec.from_string() because the DWIMification happen when _match_on
 
298
    is called so the string describing the revision is kept here until needed.
 
299
    """
 
300
 
 
301
    help_txt = None
 
302
 
 
303
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
304
 
 
305
    # The revspecs to try
 
306
    _possible_revspecs = []
 
307
 
 
308
    def _try_spectype(self, rstype, branch):
 
309
        rs = rstype(self.spec, _internal=True)
 
310
        # Hit in_history to find out if it exists, or we need to try the
 
311
        # next type.
 
312
        return rs.in_history(branch)
 
313
 
 
314
    def _match_on(self, branch, revs):
 
315
        """Run the lookup and see what we can get."""
 
316
 
 
317
        # First, see if it's a revno
 
318
        if self._revno_regex.match(self.spec) is not None:
 
319
            try:
 
320
                return self._try_spectype(RevisionSpec_revno, branch)
 
321
            except RevisionSpec_revno.dwim_catchable_exceptions:
 
322
                pass
 
323
 
 
324
        # Next see what has been registered
 
325
        for objgetter in self._possible_revspecs:
 
326
            rs_class = objgetter.get_obj()
 
327
            try:
 
328
                return self._try_spectype(rs_class, branch)
 
329
            except rs_class.dwim_catchable_exceptions:
 
330
                pass
 
331
 
 
332
        # Well, I dunno what it is. Note that we don't try to keep track of the
 
333
        # first of last exception raised during the DWIM tries as none seems
 
334
        # really relevant.
 
335
        raise InvalidRevisionSpec(self.spec, branch)
 
336
 
 
337
    @classmethod
 
338
    def append_possible_revspec(cls, revspec):
 
339
        """Append a possible DWIM revspec.
 
340
 
 
341
        :param revspec: Revision spec to try.
 
342
        """
 
343
        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
 
344
 
 
345
    @classmethod
 
346
    def append_possible_lazy_revspec(cls, module_name, member_name):
 
347
        """Append a possible lazily loaded DWIM revspec.
 
348
 
 
349
        :param module_name: Name of the module with the revspec
 
350
        :param member_name: Name of the revspec within the module
 
351
        """
 
352
        cls._possible_revspecs.append(
 
353
            registry._LazyObjectGetter(module_name, member_name))
 
354
 
 
355
 
 
356
class RevisionSpec_revno(RevisionSpec):
 
357
    """Selects a revision using a number."""
 
358
 
 
359
    help_txt = """Selects a revision using a number.
 
360
 
 
361
    Use an integer to specify a revision in the history of the branch.
 
362
    Optionally a branch can be specified.  A negative number will count
 
363
    from the end of the branch (-1 is the last revision, -2 the previous
 
364
    one). If the negative number is larger than the branch's history, the
 
365
    first revision is returned.
 
366
    Examples::
 
367
 
 
368
      revno:1                   -> return the first revision of this branch
 
369
      revno:3:/path/to/branch   -> return the 3rd revision of
 
370
                                   the branch '/path/to/branch'
 
371
      revno:-1                  -> The last revision in a branch.
 
372
      -2:http://other/branch    -> The second to last revision in the
 
373
                                   remote branch.
 
374
      -1000000                  -> Most likely the first revision, unless
 
375
                                   your history is very long.
 
376
    """
 
377
    prefix = 'revno:'
 
378
 
 
379
    def _match_on(self, branch, revs):
 
380
        """Lookup a revision by revision number"""
 
381
        branch, revno, revision_id = self._lookup(branch)
 
382
        return RevisionInfo(branch, revno, revision_id)
 
383
 
 
384
    def _lookup(self, branch):
 
385
        loc = self.spec.find(':')
 
386
        if loc == -1:
 
387
            revno_spec = self.spec
 
388
            branch_spec = None
 
389
        else:
 
390
            revno_spec = self.spec[:loc]
 
391
            branch_spec = self.spec[loc + 1:]
 
392
 
 
393
        if revno_spec == '':
 
394
            if not branch_spec:
 
395
                raise InvalidRevisionSpec(
 
396
                    self.user_spec, branch, 'cannot have an empty revno and no branch')
 
397
            revno = None
 
398
        else:
 
399
            try:
 
400
                revno = int(revno_spec)
 
401
                dotted = False
 
402
            except ValueError:
 
403
                # dotted decimal. This arguably should not be here
 
404
                # but the from_string method is a little primitive
 
405
                # right now - RBC 20060928
 
406
                try:
 
407
                    match_revno = tuple((int(number)
 
408
                                         for number in revno_spec.split('.')))
 
409
                except ValueError as e:
 
410
                    raise InvalidRevisionSpec(self.user_spec, branch, e)
 
411
 
 
412
                dotted = True
 
413
 
 
414
        if branch_spec:
 
415
            # the user has overriden the branch to look in.
 
416
            branch = _mod_branch.Branch.open(branch_spec)
 
417
 
 
418
        if dotted:
 
419
            try:
 
420
                revision_id = branch.dotted_revno_to_revision_id(match_revno,
 
421
                                                                 _cache_reverse=True)
 
422
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
 
423
                raise InvalidRevisionSpec(self.user_spec, branch)
 
424
            else:
 
425
                # there is no traditional 'revno' for dotted-decimal revnos.
 
426
                # so for API compatibility we return None.
 
427
                return branch, None, revision_id
 
428
        else:
 
429
            last_revno, last_revision_id = branch.last_revision_info()
 
430
            if revno < 0:
 
431
                # if get_rev_id supported negative revnos, there would not be a
 
432
                # need for this special case.
 
433
                if (-revno) >= last_revno:
 
434
                    revno = 1
 
435
                else:
 
436
                    revno = last_revno + revno + 1
 
437
            try:
 
438
                revision_id = branch.get_rev_id(revno)
 
439
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
 
440
                raise InvalidRevisionSpec(self.user_spec, branch)
 
441
        return branch, revno, revision_id
 
442
 
 
443
    def _as_revision_id(self, context_branch):
 
444
        # We would have the revno here, but we don't really care
 
445
        branch, revno, revision_id = self._lookup(context_branch)
 
446
        return revision_id
 
447
 
 
448
    def needs_branch(self):
 
449
        return self.spec.find(':') == -1
 
450
 
 
451
    def get_branch(self):
 
452
        if self.spec.find(':') == -1:
 
453
            return None
 
454
        else:
 
455
            return self.spec[self.spec.find(':') + 1:]
 
456
 
 
457
 
 
458
# Old compatibility
 
459
RevisionSpec_int = RevisionSpec_revno
 
460
 
 
461
 
 
462
class RevisionIDSpec(RevisionSpec):
 
463
 
 
464
    def _match_on(self, branch, revs):
 
465
        revision_id = self.as_revision_id(branch)
 
466
        return RevisionInfo.from_revision_id(branch, revision_id)
 
467
 
 
468
 
 
469
class RevisionSpec_revid(RevisionIDSpec):
 
470
    """Selects a revision using the revision id."""
 
471
 
 
472
    help_txt = """Selects a revision using the revision id.
 
473
 
 
474
    Supply a specific revision id, that can be used to specify any
 
475
    revision id in the ancestry of the branch.
 
476
    Including merges, and pending merges.
 
477
    Examples::
 
478
 
 
479
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
 
480
    """
 
481
 
 
482
    prefix = 'revid:'
 
483
 
 
484
    def _as_revision_id(self, context_branch):
 
485
        # self.spec comes straight from parsing the command line arguments,
 
486
        # so we expect it to be a Unicode string. Switch it to the internal
 
487
        # representation.
 
488
        if isinstance(self.spec, text_type):
 
489
            return cache_utf8.encode(self.spec)
 
490
        return self.spec
 
491
 
 
492
 
 
493
class RevisionSpec_last(RevisionSpec):
 
494
    """Selects the nth revision from the end."""
 
495
 
 
496
    help_txt = """Selects the nth revision from the end.
 
497
 
 
498
    Supply a positive number to get the nth revision from the end.
 
499
    This is the same as supplying negative numbers to the 'revno:' spec.
 
500
    Examples::
 
501
 
 
502
      last:1        -> return the last revision
 
503
      last:3        -> return the revision 2 before the end.
 
504
    """
 
505
 
 
506
    prefix = 'last:'
 
507
 
 
508
    def _match_on(self, branch, revs):
 
509
        revno, revision_id = self._revno_and_revision_id(branch)
 
510
        return RevisionInfo(branch, revno, revision_id)
 
511
 
 
512
    def _revno_and_revision_id(self, context_branch):
 
513
        last_revno, last_revision_id = context_branch.last_revision_info()
 
514
 
 
515
        if self.spec == '':
 
516
            if not last_revno:
 
517
                raise errors.NoCommits(context_branch)
 
518
            return last_revno, last_revision_id
 
519
 
 
520
        try:
 
521
            offset = int(self.spec)
 
522
        except ValueError as e:
 
523
            raise InvalidRevisionSpec(self.user_spec, context_branch, e)
 
524
 
 
525
        if offset <= 0:
 
526
            raise InvalidRevisionSpec(self.user_spec, context_branch,
 
527
                                             'you must supply a positive value')
 
528
 
 
529
        revno = last_revno - offset + 1
 
530
        try:
 
531
            revision_id = context_branch.get_rev_id(revno)
 
532
        except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
 
533
            raise InvalidRevisionSpec(self.user_spec, context_branch)
 
534
        return revno, revision_id
 
535
 
 
536
    def _as_revision_id(self, context_branch):
 
537
        # We compute the revno as part of the process, but we don't really care
 
538
        # about it.
 
539
        revno, revision_id = self._revno_and_revision_id(context_branch)
 
540
        return revision_id
 
541
 
 
542
 
 
543
class RevisionSpec_before(RevisionSpec):
 
544
    """Selects the parent of the revision specified."""
 
545
 
 
546
    help_txt = """Selects the parent of the revision specified.
 
547
 
 
548
    Supply any revision spec to return the parent of that revision.  This is
 
549
    mostly useful when inspecting revisions that are not in the revision history
 
550
    of a branch.
 
551
 
 
552
    It is an error to request the parent of the null revision (before:0).
 
553
 
 
554
    Examples::
 
555
 
 
556
      before:1913    -> Return the parent of revno 1913 (revno 1912)
 
557
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
 
558
                                            aaaa@bbbb-1234567890
 
559
      bzr diff -r before:1913..1913
 
560
            -> Find the changes between revision 1913 and its parent (1912).
 
561
               (What changes did revision 1913 introduce).
 
562
               This is equivalent to:  bzr diff -c 1913
 
563
    """
 
564
 
 
565
    prefix = 'before:'
 
566
 
 
567
    def _match_on(self, branch, revs):
 
568
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
 
569
        if r.revno == 0:
 
570
            raise InvalidRevisionSpec(self.user_spec, branch, 'cannot go before the null: revision')
 
571
        if r.revno is None:
 
572
            # We need to use the repository history here
 
573
            rev = branch.repository.get_revision(r.rev_id)
 
574
            if not rev.parent_ids:
 
575
                revision_id = revision.NULL_REVISION
 
576
            else:
 
577
                revision_id = rev.parent_ids[0]
 
578
            revno = None
 
579
        else:
 
580
            revno = r.revno - 1
 
581
            try:
 
582
                revision_id = branch.get_rev_id(revno, revs)
 
583
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
 
584
                raise InvalidRevisionSpec(self.user_spec, branch)
 
585
        return RevisionInfo(branch, revno, revision_id)
 
586
 
 
587
    def _as_revision_id(self, context_branch):
 
588
        base_revision_id = RevisionSpec.from_string(
 
589
            self.spec)._as_revision_id(context_branch)
 
590
        if base_revision_id == revision.NULL_REVISION:
 
591
            raise InvalidRevisionSpec(
 
592
                self.user_spec, context_branch,
 
593
                'cannot go before the null: revision')
 
594
        context_repo = context_branch.repository
 
595
        with context_repo.lock_read():
 
596
            parent_map = context_repo.get_parent_map([base_revision_id])
 
597
        if base_revision_id not in parent_map:
 
598
            # Ghost, or unknown revision id
 
599
            raise InvalidRevisionSpec(
 
600
                self.user_spec, context_branch, 'cannot find the matching revision')
 
601
        parents = parent_map[base_revision_id]
 
602
        if len(parents) < 1:
 
603
            raise InvalidRevisionSpec(
 
604
                self.user_spec, context_branch, 'No parents for revision.')
 
605
        return parents[0]
 
606
 
 
607
 
 
608
class RevisionSpec_tag(RevisionSpec):
 
609
    """Select a revision identified by tag name"""
 
610
 
 
611
    help_txt = """Selects a revision identified by a tag name.
 
612
 
 
613
    Tags are stored in the branch and created by the 'tag' command.
 
614
    """
 
615
 
 
616
    prefix = 'tag:'
 
617
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
 
618
 
 
619
    def _match_on(self, branch, revs):
 
620
        # Can raise tags not supported, NoSuchTag, etc
 
621
        return RevisionInfo.from_revision_id(branch,
 
622
                                             branch.tags.lookup_tag(self.spec))
 
623
 
 
624
    def _as_revision_id(self, context_branch):
 
625
        return context_branch.tags.lookup_tag(self.spec)
 
626
 
 
627
 
 
628
class _RevListToTimestamps(object):
 
629
    """This takes a list of revisions, and allows you to bisect by date"""
 
630
 
 
631
    __slots__ = ['branch']
 
632
 
 
633
    def __init__(self, branch):
 
634
        self.branch = branch
 
635
 
 
636
    def __getitem__(self, index):
 
637
        """Get the date of the index'd item"""
 
638
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
 
639
        # TODO: Handle timezone.
 
640
        return datetime.datetime.fromtimestamp(r.timestamp)
 
641
 
 
642
    def __len__(self):
 
643
        return self.branch.revno()
 
644
 
 
645
 
 
646
class RevisionSpec_date(RevisionSpec):
 
647
    """Selects a revision on the basis of a datestamp."""
 
648
 
 
649
    help_txt = """Selects a revision on the basis of a datestamp.
 
650
 
 
651
    Supply a datestamp to select the first revision that matches the date.
 
652
    Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
 
653
    Matches the first entry after a given date (either at midnight or
 
654
    at a specified time).
 
655
 
 
656
    One way to display all the changes since yesterday would be::
 
657
 
 
658
        brz log -r date:yesterday..
 
659
 
 
660
    Examples::
 
661
 
 
662
      date:yesterday            -> select the first revision since yesterday
 
663
      date:2006-08-14,17:10:14  -> select the first revision after
 
664
                                   August 14th, 2006 at 5:10pm.
 
665
    """
 
666
    prefix = 'date:'
 
667
    _date_regex = lazy_regex.lazy_compile(
 
668
        r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
 
669
        r'(,|T)?\s*'
 
670
        r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
 
671
        )
 
672
 
 
673
    def _match_on(self, branch, revs):
 
674
        """Spec for date revisions:
 
675
          date:value
 
676
          value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
 
677
          matches the first entry after a given date (either at midnight or
 
678
          at a specified time).
 
679
        """
 
680
        #  XXX: This doesn't actually work
 
681
        #  So the proper way of saying 'give me all entries for today' is:
 
682
        #      -r date:yesterday..date:today
 
683
        today = datetime.datetime.fromordinal(
 
684
            datetime.date.today().toordinal())
 
685
        if self.spec.lower() == 'yesterday':
 
686
            dt = today - datetime.timedelta(days=1)
 
687
        elif self.spec.lower() == 'today':
 
688
            dt = today
 
689
        elif self.spec.lower() == 'tomorrow':
 
690
            dt = today + datetime.timedelta(days=1)
 
691
        else:
 
692
            m = self._date_regex.match(self.spec)
 
693
            if not m or (not m.group('date') and not m.group('time')):
 
694
                raise InvalidRevisionSpec(self.user_spec, branch, 'invalid date')
 
695
 
 
696
            try:
 
697
                if m.group('date'):
 
698
                    year = int(m.group('year'))
 
699
                    month = int(m.group('month'))
 
700
                    day = int(m.group('day'))
 
701
                else:
 
702
                    year = today.year
 
703
                    month = today.month
 
704
                    day = today.day
 
705
 
 
706
                if m.group('time'):
 
707
                    hour = int(m.group('hour'))
 
708
                    minute = int(m.group('minute'))
 
709
                    if m.group('second'):
 
710
                        second = int(m.group('second'))
 
711
                    else:
 
712
                        second = 0
 
713
                else:
 
714
                    hour, minute, second = 0, 0, 0
 
715
            except ValueError:
 
716
                raise InvalidRevisionSpec(self.user_spec, branch, 'invalid date')
 
717
 
 
718
            dt = datetime.datetime(year=year, month=month, day=day,
 
719
                                   hour=hour, minute=minute, second=second)
 
720
        with branch.lock_read():
 
721
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
 
722
        if rev == branch.revno():
 
723
            raise InvalidRevisionSpec(self.user_spec, branch)
 
724
        return RevisionInfo(branch, rev)
 
725
 
 
726
 
 
727
class RevisionSpec_ancestor(RevisionSpec):
 
728
    """Selects a common ancestor with a second branch."""
 
729
 
 
730
    help_txt = """Selects a common ancestor with a second branch.
 
731
 
 
732
    Supply the path to a branch to select the common ancestor.
 
733
 
 
734
    The common ancestor is the last revision that existed in both
 
735
    branches. Usually this is the branch point, but it could also be
 
736
    a revision that was merged.
 
737
 
 
738
    This is frequently used with 'diff' to return all of the changes
 
739
    that your branch introduces, while excluding the changes that you
 
740
    have not merged from the remote branch.
 
741
 
 
742
    Examples::
 
743
 
 
744
      ancestor:/path/to/branch
 
745
      $ bzr diff -r ancestor:../../mainline/branch
 
746
    """
 
747
    prefix = 'ancestor:'
 
748
 
 
749
    def _match_on(self, branch, revs):
 
750
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
751
        return self._find_revision_info(branch, self.spec)
 
752
 
 
753
    def _as_revision_id(self, context_branch):
 
754
        return self._find_revision_id(context_branch, self.spec)
 
755
 
 
756
    @staticmethod
 
757
    def _find_revision_info(branch, other_location):
 
758
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
 
759
                                                              other_location)
 
760
        return RevisionInfo(branch, None, revision_id)
 
761
 
 
762
    @staticmethod
 
763
    def _find_revision_id(branch, other_location):
 
764
        from .branch import Branch
 
765
 
 
766
        with branch.lock_read():
 
767
            revision_a = revision.ensure_null(branch.last_revision())
 
768
            if revision_a == revision.NULL_REVISION:
 
769
                raise errors.NoCommits(branch)
 
770
            if other_location == '':
 
771
                other_location = branch.get_parent()
 
772
            other_branch = Branch.open(other_location)
 
773
            with other_branch.lock_read():
 
774
                revision_b = revision.ensure_null(other_branch.last_revision())
 
775
                if revision_b == revision.NULL_REVISION:
 
776
                    raise errors.NoCommits(other_branch)
 
777
                graph = branch.repository.get_graph(other_branch.repository)
 
778
                rev_id = graph.find_unique_lca(revision_a, revision_b)
 
779
            if rev_id == revision.NULL_REVISION:
 
780
                raise errors.NoCommonAncestor(revision_a, revision_b)
 
781
            return rev_id
 
782
 
 
783
 
 
784
class RevisionSpec_branch(RevisionSpec):
 
785
    """Selects the last revision of a specified branch."""
 
786
 
 
787
    help_txt = """Selects the last revision of a specified branch.
 
788
 
 
789
    Supply the path to a branch to select its last revision.
 
790
 
 
791
    Examples::
 
792
 
 
793
      branch:/path/to/branch
 
794
    """
 
795
    prefix = 'branch:'
 
796
    dwim_catchable_exceptions = (errors.NotBranchError,)
 
797
 
 
798
    def _match_on(self, branch, revs):
 
799
        from .branch import Branch
 
800
        other_branch = Branch.open(self.spec)
 
801
        revision_b = other_branch.last_revision()
 
802
        if revision_b in (None, revision.NULL_REVISION):
 
803
            raise errors.NoCommits(other_branch)
 
804
        if branch is None:
 
805
            branch = other_branch
 
806
        else:
 
807
            try:
 
808
                # pull in the remote revisions so we can diff
 
809
                branch.fetch(other_branch, revision_b)
 
810
            except errors.ReadOnlyError:
 
811
                branch = other_branch
 
812
        return RevisionInfo(branch, None, revision_b)
 
813
 
 
814
    def _as_revision_id(self, context_branch):
 
815
        from .branch import Branch
 
816
        other_branch = Branch.open(self.spec)
 
817
        last_revision = other_branch.last_revision()
 
818
        last_revision = revision.ensure_null(last_revision)
 
819
        context_branch.fetch(other_branch, last_revision)
 
820
        if last_revision == revision.NULL_REVISION:
 
821
            raise errors.NoCommits(other_branch)
 
822
        return last_revision
 
823
 
 
824
    def _as_tree(self, context_branch):
 
825
        from .branch import Branch
 
826
        other_branch = Branch.open(self.spec)
 
827
        last_revision = other_branch.last_revision()
 
828
        last_revision = revision.ensure_null(last_revision)
 
829
        if last_revision == revision.NULL_REVISION:
 
830
            raise errors.NoCommits(other_branch)
 
831
        return other_branch.repository.revision_tree(last_revision)
 
832
 
 
833
    def needs_branch(self):
 
834
        return False
 
835
 
 
836
    def get_branch(self):
 
837
        return self.spec
 
838
 
 
839
 
 
840
class RevisionSpec_submit(RevisionSpec_ancestor):
 
841
    """Selects a common ancestor with a submit branch."""
 
842
 
 
843
    help_txt = """Selects a common ancestor with the submit branch.
 
844
 
 
845
    Diffing against this shows all the changes that were made in this branch,
 
846
    and is a good predictor of what merge will do.  The submit branch is
 
847
    used by the bundle and merge directive commands.  If no submit branch
 
848
    is specified, the parent branch is used instead.
 
849
 
 
850
    The common ancestor is the last revision that existed in both
 
851
    branches. Usually this is the branch point, but it could also be
 
852
    a revision that was merged.
 
853
 
 
854
    Examples::
 
855
 
 
856
      $ bzr diff -r submit:
 
857
    """
 
858
 
 
859
    prefix = 'submit:'
 
860
 
 
861
    def _get_submit_location(self, branch):
 
862
        submit_location = branch.get_submit_branch()
 
863
        location_type = 'submit branch'
 
864
        if submit_location is None:
 
865
            submit_location = branch.get_parent()
 
866
            location_type = 'parent branch'
 
867
        if submit_location is None:
 
868
            raise errors.NoSubmitBranch(branch)
 
869
        trace.note(gettext('Using {0} {1}').format(location_type,
 
870
                                                   submit_location))
 
871
        return submit_location
 
872
 
 
873
    def _match_on(self, branch, revs):
 
874
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
875
        return self._find_revision_info(branch,
 
876
                                        self._get_submit_location(branch))
 
877
 
 
878
    def _as_revision_id(self, context_branch):
 
879
        return self._find_revision_id(context_branch,
 
880
                                      self._get_submit_location(context_branch))
 
881
 
 
882
 
 
883
class RevisionSpec_annotate(RevisionIDSpec):
 
884
 
 
885
    prefix = 'annotate:'
 
886
 
 
887
    help_txt = """Select the revision that last modified the specified line.
 
888
 
 
889
    Select the revision that last modified the specified line.  Line is
 
890
    specified as path:number.  Path is a relative path to the file.  Numbers
 
891
    start at 1, and are relative to the current version, not the last-
 
892
    committed version of the file.
 
893
    """
 
894
 
 
895
    def _raise_invalid(self, numstring, context_branch):
 
896
        raise InvalidRevisionSpec(self.user_spec, context_branch,
 
897
                                  'No such line: %s' % numstring)
 
898
 
 
899
    def _as_revision_id(self, context_branch):
 
900
        path, numstring = self.spec.rsplit(':', 1)
 
901
        try:
 
902
            index = int(numstring) - 1
 
903
        except ValueError:
 
904
            self._raise_invalid(numstring, context_branch)
 
905
        tree, file_path = workingtree.WorkingTree.open_containing(path)
 
906
        with tree.lock_read():
 
907
            if not tree.has_filename(file_path):
 
908
                raise InvalidRevisionSpec(
 
909
                    self.user_spec, context_branch,
 
910
                    "File '%s' is not versioned." % file_path)
 
911
            revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
 
912
        try:
 
913
            revision_id = revision_ids[index]
 
914
        except IndexError:
 
915
            self._raise_invalid(numstring, context_branch)
 
916
        if revision_id == revision.CURRENT_REVISION:
 
917
            raise InvalidRevisionSpec(
 
918
                self.user_spec, context_branch,
 
919
                'Line %s has not been committed.' % numstring)
 
920
        return revision_id
 
921
 
 
922
 
 
923
class RevisionSpec_mainline(RevisionIDSpec):
 
924
 
 
925
    help_txt = """Select mainline revision that merged the specified revision.
 
926
 
 
927
    Select the revision that merged the specified revision into mainline.
 
928
    """
 
929
 
 
930
    prefix = 'mainline:'
 
931
 
 
932
    def _as_revision_id(self, context_branch):
 
933
        revspec = RevisionSpec.from_string(self.spec)
 
934
        if revspec.get_branch() is None:
 
935
            spec_branch = context_branch
 
936
        else:
 
937
            spec_branch = _mod_branch.Branch.open(revspec.get_branch())
 
938
        revision_id = revspec.as_revision_id(spec_branch)
 
939
        graph = context_branch.repository.get_graph()
 
940
        result = graph.find_lefthand_merger(revision_id,
 
941
                                            context_branch.last_revision())
 
942
        if result is None:
 
943
            raise InvalidRevisionSpec(self.user_spec, context_branch)
 
944
        return result
 
945
 
 
946
 
 
947
# The order in which we want to DWIM a revision spec without any prefix.
 
948
# revno is always tried first and isn't listed here, this is used by
 
949
# RevisionSpec_dwim._match_on
 
950
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
 
951
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
 
952
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
 
953
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
 
954
 
 
955
revspec_registry = registry.Registry()
 
956
 
 
957
 
 
958
def _register_revspec(revspec):
 
959
    revspec_registry.register(revspec.prefix, revspec)
 
960
 
 
961
 
 
962
_register_revspec(RevisionSpec_revno)
 
963
_register_revspec(RevisionSpec_revid)
 
964
_register_revspec(RevisionSpec_last)
 
965
_register_revspec(RevisionSpec_before)
 
966
_register_revspec(RevisionSpec_tag)
 
967
_register_revspec(RevisionSpec_date)
 
968
_register_revspec(RevisionSpec_ancestor)
 
969
_register_revspec(RevisionSpec_branch)
 
970
_register_revspec(RevisionSpec_submit)
 
971
_register_revspec(RevisionSpec_annotate)
 
972
_register_revspec(RevisionSpec_mainline)