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

  • Committer: Matthew Fuller
  • Date: 2009-10-10 05:07:35 UTC
  • mto: (4772.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4773.
  • Revision ID: fullermd@over-yonder.net-20091010050735-bz43fgmdm166d4kc
Add a test that we slip past the revno-checking stage when we're
handed something that looks like a revno, but isn't.  Tags are
convenient for this, so use them.

Show diffs side-by-side

added added

removed removed

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