/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: John Arbash Meinel
  • Date: 2008-06-02 16:09:09 UTC
  • mto: This revision was merged to the branch mainline in revision 3472.
  • Revision ID: john@arbash-meinel.com-20080602160909-u5q4mzn2ou6kz2r7
Change WT.set_parent_(ids/trees) to filter out ancestors.

This makes it impossible to trigger bug #235407, since we can't have repeated
ancestors given.

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