/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: Marius Kruger
  • Date: 2008-10-03 20:57:44 UTC
  • mto: This revision was merged to the branch mainline in revision 3809.
  • Revision ID: amanic@gmail.com-20081003205744-o0cdopyj7mum2dkw
* checkouts now use master nick when no explicit nick is set.
* switch updates only explicit nicks.

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
    wants_revision_history = True
 
139
 
 
140
    @staticmethod
 
141
    def from_string(spec):
 
142
        """Parse a revision spec string into a RevisionSpec object.
 
143
 
 
144
        :param spec: A string specified by the user
 
145
        :return: A RevisionSpec object that understands how to parse the
 
146
            supplied notation.
 
147
        """
 
148
        if not isinstance(spec, (type(None), basestring)):
 
149
            raise TypeError('error')
 
150
 
 
151
        if spec is None:
 
152
            return RevisionSpec(None, _internal=True)
 
153
        for spectype in SPEC_TYPES:
 
154
            if spec.startswith(spectype.prefix):
 
155
                trace.mutter('Returning RevisionSpec %s for %s',
 
156
                             spectype.__name__, spec)
 
157
                return spectype(spec, _internal=True)
 
158
        else:
 
159
            # RevisionSpec_revno is special cased, because it is the only
 
160
            # one that directly handles plain integers
 
161
            # TODO: This should not be special cased rather it should be
 
162
            # a method invocation on spectype.canparse()
 
163
            global _revno_regex
 
164
            if _revno_regex is None:
 
165
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
166
            if _revno_regex.match(spec) is not None:
 
167
                return RevisionSpec_revno(spec, _internal=True)
 
168
 
 
169
            raise errors.NoSuchRevisionSpec(spec)
 
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
            # XXX: Update this after 0.10 is released
 
180
            symbol_versioning.warn('Creating a RevisionSpec directly has'
 
181
                                   ' been deprecated in version 0.11. Use'
 
182
                                   ' RevisionSpec.from_string()'
 
183
                                   ' instead.',
 
184
                                   DeprecationWarning, stacklevel=2)
 
185
        self.user_spec = spec
 
186
        if self.prefix and spec.startswith(self.prefix):
 
187
            spec = spec[len(self.prefix):]
 
188
        self.spec = spec
 
189
 
 
190
    def _match_on(self, branch, revs):
 
191
        trace.mutter('Returning RevisionSpec._match_on: None')
 
192
        return RevisionInfo(branch, None, None)
 
193
 
 
194
    def _match_on_and_check(self, branch, revs):
 
195
        info = self._match_on(branch, revs)
 
196
        if info:
 
197
            return info
 
198
        elif info == (None, None):
 
199
            # special case - nothing supplied
 
200
            return info
 
201
        elif self.prefix:
 
202
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
203
        else:
 
204
            raise errors.InvalidRevisionSpec(self.spec, branch)
 
205
 
 
206
    def in_history(self, branch):
 
207
        if branch:
 
208
            if self.wants_revision_history:
 
209
                revs = branch.revision_history()
 
210
            else:
 
211
                revs = None
 
212
        else:
 
213
            # this should never trigger.
 
214
            # TODO: make it a deprecated code path. RBC 20060928
 
215
            revs = None
 
216
        return self._match_on_and_check(branch, revs)
 
217
 
 
218
        # FIXME: in_history is somewhat broken,
 
219
        # it will return non-history revisions in many
 
220
        # circumstances. The expected facility is that
 
221
        # in_history only returns revision-history revs,
 
222
        # in_store returns any rev. RBC 20051010
 
223
    # aliases for now, when we fix the core logic, then they
 
224
    # will do what you expect.
 
225
    in_store = in_history
 
226
    in_branch = in_store
 
227
 
 
228
    def as_revision_id(self, context_branch):
 
229
        """Return just the revision_id for this revisions spec.
 
230
 
 
231
        Some revision specs require a context_branch to be able to determine
 
232
        their value. Not all specs will make use of it.
 
233
        """
 
234
        return self._as_revision_id(context_branch)
 
235
 
 
236
    def _as_revision_id(self, context_branch):
 
237
        """Implementation of as_revision_id()
 
238
 
 
239
        Classes should override this function to provide appropriate
 
240
        functionality. The default is to just call '.in_history().rev_id'
 
241
        """
 
242
        return self.in_history(context_branch).rev_id
 
243
 
 
244
    def as_tree(self, context_branch):
 
245
        """Return the tree object for this revisions spec.
 
246
 
 
247
        Some revision specs require a context_branch to be able to determine
 
248
        the revision id and access the repository. Not all specs will make
 
249
        use of it.
 
250
        """
 
251
        return self._as_tree(context_branch)
 
252
 
 
253
    def _as_tree(self, context_branch):
 
254
        """Implementation of as_tree().
 
255
 
 
256
        Classes should override this function to provide appropriate
 
257
        functionality. The default is to just call '.as_revision_id()'
 
258
        and get the revision tree from context_branch's repository.
 
259
        """
 
260
        revision_id = self.as_revision_id(context_branch)
 
261
        return context_branch.repository.revision_tree(revision_id)
 
262
 
 
263
    def __repr__(self):
 
264
        # this is mostly for helping with testing
 
265
        return '<%s %s>' % (self.__class__.__name__,
 
266
                              self.user_spec)
 
267
    
 
268
    def needs_branch(self):
 
269
        """Whether this revision spec needs a branch.
 
270
 
 
271
        Set this to False the branch argument of _match_on is not used.
 
272
        """
 
273
        return True
 
274
 
 
275
    def get_branch(self):
 
276
        """When the revision specifier contains a branch location, return it.
 
277
        
 
278
        Otherwise, return None.
 
279
        """
 
280
        return None
 
281
 
 
282
 
 
283
# private API
 
284
 
 
285
class RevisionSpec_revno(RevisionSpec):
 
286
    """Selects a revision using a number."""
 
287
 
 
288
    help_txt = """Selects a revision using a number.
 
289
 
 
290
    Use an integer to specify a revision in the history of the branch.
 
291
    Optionally a branch can be specified. The 'revno:' prefix is optional.
 
292
    A negative number will count from the end of the branch (-1 is the
 
293
    last revision, -2 the previous one). If the negative number is larger
 
294
    than the branch's history, the first revision is returned.
 
295
    Examples::
 
296
 
 
297
      revno:1                   -> return the first revision of this branch
 
298
      revno:3:/path/to/branch   -> return the 3rd revision of
 
299
                                   the branch '/path/to/branch'
 
300
      revno:-1                  -> The last revision in a branch.
 
301
      -2:http://other/branch    -> The second to last revision in the
 
302
                                   remote branch.
 
303
      -1000000                  -> Most likely the first revision, unless
 
304
                                   your history is very long.
 
305
    """
 
306
    prefix = 'revno:'
 
307
    wants_revision_history = False
 
308
 
 
309
    def _match_on(self, branch, revs):
 
310
        """Lookup a revision by revision number"""
 
311
        branch, revno, revision_id = self._lookup(branch, revs)
 
312
        return RevisionInfo(branch, revno, revision_id)
 
313
 
 
314
    def _lookup(self, branch, revs_or_none):
 
315
        loc = self.spec.find(':')
 
316
        if loc == -1:
 
317
            revno_spec = self.spec
 
318
            branch_spec = None
 
319
        else:
 
320
            revno_spec = self.spec[:loc]
 
321
            branch_spec = self.spec[loc+1:]
 
322
 
 
323
        if revno_spec == '':
 
324
            if not branch_spec:
 
325
                raise errors.InvalidRevisionSpec(self.user_spec,
 
326
                        branch, 'cannot have an empty revno and no branch')
 
327
            revno = None
 
328
        else:
 
329
            try:
 
330
                revno = int(revno_spec)
 
331
                dotted = False
 
332
            except ValueError:
 
333
                # dotted decimal. This arguably should not be here
 
334
                # but the from_string method is a little primitive 
 
335
                # right now - RBC 20060928
 
336
                try:
 
337
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
 
338
                except ValueError, e:
 
339
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
 
340
 
 
341
                dotted = True
 
342
 
 
343
        if branch_spec:
 
344
            # the user has override the branch to look in.
 
345
            # we need to refresh the revision_history map and
 
346
            # the branch object.
 
347
            from bzrlib.branch import Branch
 
348
            branch = Branch.open(branch_spec)
 
349
            revs_or_none = None
 
350
 
 
351
        if dotted:
 
352
            branch.lock_read()
 
353
            try:
 
354
                revision_id_to_revno = branch.get_revision_id_to_revno_map()
 
355
                revisions = [revision_id for revision_id, revno
 
356
                             in revision_id_to_revno.iteritems()
 
357
                             if revno == match_revno]
 
358
            finally:
 
359
                branch.unlock()
 
360
            if len(revisions) != 1:
 
361
                return branch, None, None
 
362
            else:
 
363
                # there is no traditional 'revno' for dotted-decimal revnos.
 
364
                # so for  API compatability we return None.
 
365
                return branch, None, revisions[0]
 
366
        else:
 
367
            last_revno, last_revision_id = branch.last_revision_info()
 
368
            if revno < 0:
 
369
                # if get_rev_id supported negative revnos, there would not be a
 
370
                # need for this special case.
 
371
                if (-revno) >= last_revno:
 
372
                    revno = 1
 
373
                else:
 
374
                    revno = last_revno + revno + 1
 
375
            try:
 
376
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
377
            except errors.NoSuchRevision:
 
378
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
379
        return branch, revno, revision_id
 
380
 
 
381
    def _as_revision_id(self, context_branch):
 
382
        # We would have the revno here, but we don't really care
 
383
        branch, revno, revision_id = self._lookup(context_branch, None)
 
384
        return revision_id
 
385
 
 
386
    def needs_branch(self):
 
387
        return self.spec.find(':') == -1
 
388
 
 
389
    def get_branch(self):
 
390
        if self.spec.find(':') == -1:
 
391
            return None
 
392
        else:
 
393
            return self.spec[self.spec.find(':')+1:]
 
394
 
 
395
# Old compatibility 
 
396
RevisionSpec_int = RevisionSpec_revno
 
397
 
 
398
SPEC_TYPES.append(RevisionSpec_revno)
 
399
 
 
400
 
 
401
class RevisionSpec_revid(RevisionSpec):
 
402
    """Selects a revision using the revision id."""
 
403
 
 
404
    help_txt = """Selects a revision using the revision id.
 
405
 
 
406
    Supply a specific revision id, that can be used to specify any
 
407
    revision id in the ancestry of the branch. 
 
408
    Including merges, and pending merges.
 
409
    Examples::
 
410
 
 
411
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
 
412
    """
 
413
 
 
414
    prefix = 'revid:'
 
415
 
 
416
    def _match_on(self, branch, revs):
 
417
        # self.spec comes straight from parsing the command line arguments,
 
418
        # so we expect it to be a Unicode string. Switch it to the internal
 
419
        # representation.
 
420
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
 
421
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
 
422
 
 
423
    def _as_revision_id(self, context_branch):
 
424
        return osutils.safe_revision_id(self.spec, warn=False)
 
425
 
 
426
SPEC_TYPES.append(RevisionSpec_revid)
 
427
 
 
428
 
 
429
class RevisionSpec_last(RevisionSpec):
 
430
    """Selects the nth revision from the end."""
 
431
 
 
432
    help_txt = """Selects the nth revision from the end.
 
433
 
 
434
    Supply a positive number to get the nth revision from the end.
 
435
    This is the same as supplying negative numbers to the 'revno:' spec.
 
436
    Examples::
 
437
 
 
438
      last:1        -> return the last revision
 
439
      last:3        -> return the revision 2 before the end.
 
440
    """
 
441
 
 
442
    prefix = 'last:'
 
443
 
 
444
    def _match_on(self, branch, revs):
 
445
        revno, revision_id = self._revno_and_revision_id(branch, revs)
 
446
        return RevisionInfo(branch, revno, revision_id)
 
447
 
 
448
    def _revno_and_revision_id(self, context_branch, revs_or_none):
 
449
        last_revno, last_revision_id = context_branch.last_revision_info()
 
450
 
 
451
        if self.spec == '':
 
452
            if not last_revno:
 
453
                raise errors.NoCommits(context_branch)
 
454
            return last_revno, last_revision_id
 
455
 
 
456
        try:
 
457
            offset = int(self.spec)
 
458
        except ValueError, e:
 
459
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
 
460
 
 
461
        if offset <= 0:
 
462
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
463
                                             'you must supply a positive value')
 
464
 
 
465
        revno = last_revno - offset + 1
 
466
        try:
 
467
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
468
        except errors.NoSuchRevision:
 
469
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
470
        return revno, revision_id
 
471
 
 
472
    def _as_revision_id(self, context_branch):
 
473
        # We compute the revno as part of the process, but we don't really care
 
474
        # about it.
 
475
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
 
476
        return revision_id
 
477
 
 
478
SPEC_TYPES.append(RevisionSpec_last)
 
479
 
 
480
 
 
481
class RevisionSpec_before(RevisionSpec):
 
482
    """Selects the parent of the revision specified."""
 
483
 
 
484
    help_txt = """Selects the parent of the revision specified.
 
485
 
 
486
    Supply any revision spec to return the parent of that revision.  This is
 
487
    mostly useful when inspecting revisions that are not in the revision history
 
488
    of a branch.
 
489
 
 
490
    It is an error to request the parent of the null revision (before:0).
 
491
 
 
492
    Examples::
 
493
 
 
494
      before:1913    -> Return the parent of revno 1913 (revno 1912)
 
495
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
 
496
                                            aaaa@bbbb-1234567890
 
497
      bzr diff -r before:1913..1913
 
498
            -> Find the changes between revision 1913 and its parent (1912).
 
499
               (What changes did revision 1913 introduce).
 
500
               This is equivalent to:  bzr diff -c 1913
 
501
    """
 
502
 
 
503
    prefix = 'before:'
 
504
    
 
505
    def _match_on(self, branch, revs):
 
506
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
 
507
        if r.revno == 0:
 
508
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
 
509
                                         'cannot go before the null: revision')
 
510
        if r.revno is None:
 
511
            # We need to use the repository history here
 
512
            rev = branch.repository.get_revision(r.rev_id)
 
513
            if not rev.parent_ids:
 
514
                revno = 0
 
515
                revision_id = revision.NULL_REVISION
 
516
            else:
 
517
                revision_id = rev.parent_ids[0]
 
518
                try:
 
519
                    revno = revs.index(revision_id) + 1
 
520
                except ValueError:
 
521
                    revno = None
 
522
        else:
 
523
            revno = r.revno - 1
 
524
            try:
 
525
                revision_id = branch.get_rev_id(revno, revs)
 
526
            except errors.NoSuchRevision:
 
527
                raise errors.InvalidRevisionSpec(self.user_spec,
 
528
                                                 branch)
 
529
        return RevisionInfo(branch, revno, revision_id)
 
530
 
 
531
    def _as_revision_id(self, context_branch):
 
532
        base_revspec = RevisionSpec.from_string(self.spec)
 
533
        base_revision_id = base_revspec.as_revision_id(context_branch)
 
534
        if base_revision_id == revision.NULL_REVISION:
 
535
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
536
                                         'cannot go before the null: revision')
 
537
        context_repo = context_branch.repository
 
538
        context_repo.lock_read()
 
539
        try:
 
540
            parent_map = context_repo.get_parent_map([base_revision_id])
 
541
        finally:
 
542
            context_repo.unlock()
 
543
        if base_revision_id not in parent_map:
 
544
            # Ghost, or unknown revision id
 
545
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
546
                'cannot find the matching revision')
 
547
        parents = parent_map[base_revision_id]
 
548
        if len(parents) < 1:
 
549
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
550
                'No parents for revision.')
 
551
        return parents[0]
 
552
 
 
553
SPEC_TYPES.append(RevisionSpec_before)
 
554
 
 
555
 
 
556
class RevisionSpec_tag(RevisionSpec):
 
557
    """Select a revision identified by tag name"""
 
558
 
 
559
    help_txt = """Selects a revision identified by a tag name.
 
560
 
 
561
    Tags are stored in the branch and created by the 'tag' command.
 
562
    """
 
563
 
 
564
    prefix = 'tag:'
 
565
 
 
566
    def _match_on(self, branch, revs):
 
567
        # Can raise tags not supported, NoSuchTag, etc
 
568
        return RevisionInfo.from_revision_id(branch,
 
569
            branch.tags.lookup_tag(self.spec),
 
570
            revs)
 
571
 
 
572
    def _as_revision_id(self, context_branch):
 
573
        return context_branch.tags.lookup_tag(self.spec)
 
574
 
 
575
SPEC_TYPES.append(RevisionSpec_tag)
 
576
 
 
577
 
 
578
class _RevListToTimestamps(object):
 
579
    """This takes a list of revisions, and allows you to bisect by date"""
 
580
 
 
581
    __slots__ = ['revs', 'branch']
 
582
 
 
583
    def __init__(self, revs, branch):
 
584
        self.revs = revs
 
585
        self.branch = branch
 
586
 
 
587
    def __getitem__(self, index):
 
588
        """Get the date of the index'd item"""
 
589
        r = self.branch.repository.get_revision(self.revs[index])
 
590
        # TODO: Handle timezone.
 
591
        return datetime.datetime.fromtimestamp(r.timestamp)
 
592
 
 
593
    def __len__(self):
 
594
        return len(self.revs)
 
595
 
 
596
 
 
597
class RevisionSpec_date(RevisionSpec):
 
598
    """Selects a revision on the basis of a datestamp."""
 
599
 
 
600
    help_txt = """Selects a revision on the basis of a datestamp.
 
601
 
 
602
    Supply a datestamp to select the first revision that matches the date.
 
603
    Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
 
604
    Matches the first entry after a given date (either at midnight or
 
605
    at a specified time).
 
606
 
 
607
    One way to display all the changes since yesterday would be::
 
608
 
 
609
        bzr log -r date:yesterday..
 
610
 
 
611
    Examples::
 
612
 
 
613
      date:yesterday            -> select the first revision since yesterday
 
614
      date:2006-08-14,17:10:14  -> select the first revision after
 
615
                                   August 14th, 2006 at 5:10pm.
 
616
    """    
 
617
    prefix = 'date:'
 
618
    _date_re = re.compile(
 
619
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
 
620
            r'(,|T)?\s*'
 
621
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
 
622
        )
 
623
 
 
624
    def _match_on(self, branch, revs):
 
625
        """Spec for date revisions:
 
626
          date:value
 
627
          value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
 
628
          matches the first entry after a given date (either at midnight or
 
629
          at a specified time).
 
630
        """
 
631
        #  XXX: This doesn't actually work
 
632
        #  So the proper way of saying 'give me all entries for today' is:
 
633
        #      -r date:yesterday..date:today
 
634
        today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
 
635
        if self.spec.lower() == 'yesterday':
 
636
            dt = today - datetime.timedelta(days=1)
 
637
        elif self.spec.lower() == 'today':
 
638
            dt = today
 
639
        elif self.spec.lower() == 'tomorrow':
 
640
            dt = today + datetime.timedelta(days=1)
 
641
        else:
 
642
            m = self._date_re.match(self.spec)
 
643
            if not m or (not m.group('date') and not m.group('time')):
 
644
                raise errors.InvalidRevisionSpec(self.user_spec,
 
645
                                                 branch, 'invalid date')
 
646
 
 
647
            try:
 
648
                if m.group('date'):
 
649
                    year = int(m.group('year'))
 
650
                    month = int(m.group('month'))
 
651
                    day = int(m.group('day'))
 
652
                else:
 
653
                    year = today.year
 
654
                    month = today.month
 
655
                    day = today.day
 
656
 
 
657
                if m.group('time'):
 
658
                    hour = int(m.group('hour'))
 
659
                    minute = int(m.group('minute'))
 
660
                    if m.group('second'):
 
661
                        second = int(m.group('second'))
 
662
                    else:
 
663
                        second = 0
 
664
                else:
 
665
                    hour, minute, second = 0,0,0
 
666
            except ValueError:
 
667
                raise errors.InvalidRevisionSpec(self.user_spec,
 
668
                                                 branch, 'invalid date')
 
669
 
 
670
            dt = datetime.datetime(year=year, month=month, day=day,
 
671
                    hour=hour, minute=minute, second=second)
 
672
        branch.lock_read()
 
673
        try:
 
674
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
 
675
        finally:
 
676
            branch.unlock()
 
677
        if rev == len(revs):
 
678
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
679
        else:
 
680
            return RevisionInfo(branch, rev + 1)
 
681
 
 
682
SPEC_TYPES.append(RevisionSpec_date)
 
683
 
 
684
 
 
685
class RevisionSpec_ancestor(RevisionSpec):
 
686
    """Selects a common ancestor with a second branch."""
 
687
 
 
688
    help_txt = """Selects a common ancestor with a second branch.
 
689
 
 
690
    Supply the path to a branch to select the common ancestor.
 
691
 
 
692
    The common ancestor is the last revision that existed in both
 
693
    branches. Usually this is the branch point, but it could also be
 
694
    a revision that was merged.
 
695
 
 
696
    This is frequently used with 'diff' to return all of the changes
 
697
    that your branch introduces, while excluding the changes that you
 
698
    have not merged from the remote branch.
 
699
 
 
700
    Examples::
 
701
 
 
702
      ancestor:/path/to/branch
 
703
      $ bzr diff -r ancestor:../../mainline/branch
 
704
    """
 
705
    prefix = 'ancestor:'
 
706
 
 
707
    def _match_on(self, branch, revs):
 
708
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
709
        return self._find_revision_info(branch, self.spec)
 
710
 
 
711
    def _as_revision_id(self, context_branch):
 
712
        return self._find_revision_id(context_branch, self.spec)
 
713
 
 
714
    @staticmethod
 
715
    def _find_revision_info(branch, other_location):
 
716
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
 
717
                                                              other_location)
 
718
        try:
 
719
            revno = branch.revision_id_to_revno(revision_id)
 
720
        except errors.NoSuchRevision:
 
721
            revno = None
 
722
        return RevisionInfo(branch, revno, revision_id)
 
723
 
 
724
    @staticmethod
 
725
    def _find_revision_id(branch, other_location):
 
726
        from bzrlib.branch import Branch
 
727
 
 
728
        branch.lock_read()
 
729
        try:
 
730
            revision_a = revision.ensure_null(branch.last_revision())
 
731
            if revision_a == revision.NULL_REVISION:
 
732
                raise errors.NoCommits(branch)
 
733
            other_branch = Branch.open(other_location)
 
734
            other_branch.lock_read()
 
735
            try:
 
736
                revision_b = revision.ensure_null(other_branch.last_revision())
 
737
                if revision_b == revision.NULL_REVISION:
 
738
                    raise errors.NoCommits(other_branch)
 
739
                graph = branch.repository.get_graph(other_branch.repository)
 
740
                rev_id = graph.find_unique_lca(revision_a, revision_b)
 
741
            finally:
 
742
                other_branch.unlock()
 
743
            if rev_id == revision.NULL_REVISION:
 
744
                raise errors.NoCommonAncestor(revision_a, revision_b)
 
745
            return rev_id
 
746
        finally:
 
747
            branch.unlock()
 
748
 
 
749
 
 
750
SPEC_TYPES.append(RevisionSpec_ancestor)
 
751
 
 
752
 
 
753
class RevisionSpec_branch(RevisionSpec):
 
754
    """Selects the last revision of a specified branch."""
 
755
 
 
756
    help_txt = """Selects the last revision of a specified branch.
 
757
 
 
758
    Supply the path to a branch to select its last revision.
 
759
 
 
760
    Examples::
 
761
 
 
762
      branch:/path/to/branch
 
763
    """
 
764
    prefix = 'branch:'
 
765
 
 
766
    def _match_on(self, branch, revs):
 
767
        from bzrlib.branch import Branch
 
768
        other_branch = Branch.open(self.spec)
 
769
        revision_b = other_branch.last_revision()
 
770
        if revision_b in (None, revision.NULL_REVISION):
 
771
            raise errors.NoCommits(other_branch)
 
772
        # pull in the remote revisions so we can diff
 
773
        branch.fetch(other_branch, revision_b)
 
774
        try:
 
775
            revno = branch.revision_id_to_revno(revision_b)
 
776
        except errors.NoSuchRevision:
 
777
            revno = None
 
778
        return RevisionInfo(branch, revno, revision_b)
 
779
 
 
780
    def _as_revision_id(self, context_branch):
 
781
        from bzrlib.branch import Branch
 
782
        other_branch = Branch.open(self.spec)
 
783
        last_revision = other_branch.last_revision()
 
784
        last_revision = revision.ensure_null(last_revision)
 
785
        context_branch.fetch(other_branch, last_revision)
 
786
        if last_revision == revision.NULL_REVISION:
 
787
            raise errors.NoCommits(other_branch)
 
788
        return last_revision
 
789
 
 
790
    def _as_tree(self, context_branch):
 
791
        from bzrlib.branch import Branch
 
792
        other_branch = Branch.open(self.spec)
 
793
        last_revision = other_branch.last_revision()
 
794
        last_revision = revision.ensure_null(last_revision)
 
795
        if last_revision == revision.NULL_REVISION:
 
796
            raise errors.NoCommits(other_branch)
 
797
        return other_branch.repository.revision_tree(last_revision)
 
798
 
 
799
SPEC_TYPES.append(RevisionSpec_branch)
 
800
 
 
801
 
 
802
class RevisionSpec_submit(RevisionSpec_ancestor):
 
803
    """Selects a common ancestor with a submit branch."""
 
804
 
 
805
    help_txt = """Selects a common ancestor with the submit branch.
 
806
 
 
807
    Diffing against this shows all the changes that were made in this branch,
 
808
    and is a good predictor of what merge will do.  The submit branch is
 
809
    used by the bundle and merge directive commands.  If no submit branch
 
810
    is specified, the parent branch is used instead.
 
811
 
 
812
    The common ancestor is the last revision that existed in both
 
813
    branches. Usually this is the branch point, but it could also be
 
814
    a revision that was merged.
 
815
 
 
816
    Examples::
 
817
 
 
818
      $ bzr diff -r submit:
 
819
    """
 
820
 
 
821
    prefix = 'submit:'
 
822
 
 
823
    def _get_submit_location(self, branch):
 
824
        submit_location = branch.get_submit_branch()
 
825
        location_type = 'submit branch'
 
826
        if submit_location is None:
 
827
            submit_location = branch.get_parent()
 
828
            location_type = 'parent branch'
 
829
        if submit_location is None:
 
830
            raise errors.NoSubmitBranch(branch)
 
831
        trace.note('Using %s %s', location_type, submit_location)
 
832
        return submit_location
 
833
 
 
834
    def _match_on(self, branch, revs):
 
835
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
836
        return self._find_revision_info(branch,
 
837
            self._get_submit_location(branch))
 
838
 
 
839
    def _as_revision_id(self, context_branch):
 
840
        return self._find_revision_id(context_branch,
 
841
            self._get_submit_location(context_branch))
 
842
 
 
843
 
 
844
SPEC_TYPES.append(RevisionSpec_submit)