~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Aaron Bentley
  • Date: 2007-08-06 22:26:54 UTC
  • mfrom: (2665 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2733.
  • Revision ID: aaron.bentley@utoronto.ca-20070806222654-a96j4mysnih1ha8x
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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(), """
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
22
18
import bisect
23
19
import datetime
24
 
""")
 
20
import re
25
21
 
26
22
from bzrlib import (
27
23
    errors,
28
24
    osutils,
29
 
    registry,
30
25
    revision,
31
26
    symbol_versioning,
32
27
    trace,
 
28
    tsort,
33
29
    )
34
30
 
35
31
 
62
58
                self.rev_id = None
63
59
            else:
64
60
                self.rev_id = branch.get_rev_id(self.revno)
 
61
        elif rev_id is None:
 
62
            self.rev_id = None
65
63
        else:
66
64
            self.rev_id = rev_id
67
65
 
104
102
 
105
103
        Use this if you don't know or care what the revno is.
106
104
        """
107
 
        if revision_id == revision.NULL_REVISION:
108
 
            return RevisionInfo(branch, 0, revision_id)
109
105
        try:
110
106
            revno = revs.index(revision_id) + 1
111
107
        except ValueError:
113
109
        return RevisionInfo(branch, revno, revision_id)
114
110
 
115
111
 
 
112
# classes in this list should have a "prefix" attribute, against which
 
113
# string specs are matched
 
114
SPEC_TYPES = []
116
115
_revno_regex = None
117
116
 
118
117
 
121
120
 
122
121
    help_txt = """A parsed revision specification.
123
122
 
124
 
    A revision specification is a string, which may be unambiguous about
125
 
    what it represents by giving a prefix like 'date:' or 'revid:' etc,
126
 
    or it may have no prefix, in which case it's tried against several
127
 
    specifier types in sequence to determine what the user meant.
 
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.
128
127
 
129
128
    Revision specs are an UI element, and they have been moved out
130
129
    of the branch class to leave "back-end" classes unaware of such
136
135
    """
137
136
 
138
137
    prefix = None
139
 
    wants_revision_history = True
140
 
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
141
 
    """Exceptions that RevisionSpec_dwim._match_on will catch.
142
 
 
143
 
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
144
 
    invalid revspec and raises some exception. The exceptions mentioned here
145
 
    will not be reported to the user but simply ignored without stopping the
146
 
    dwim processing.
147
 
    """
 
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)
148
149
 
149
150
    @staticmethod
150
151
    def from_string(spec):
159
160
 
160
161
        if spec is None:
161
162
            return RevisionSpec(None, _internal=True)
162
 
        match = revspec_registry.get_prefix(spec)
163
 
        if match is not None:
164
 
            spectype, specsuffix = match
165
 
            trace.mutter('Returning RevisionSpec %s for %s',
166
 
                         spectype.__name__, spec)
167
 
            return spectype(spec, _internal=True)
 
163
 
 
164
        assert isinstance(spec, basestring), \
 
165
            "You should only supply strings not %s" % (type(spec),)
 
166
 
 
167
        for spectype in SPEC_TYPES:
 
168
            if spec.startswith(spectype.prefix):
 
169
                trace.mutter('Returning RevisionSpec %s for %s',
 
170
                             spectype.__name__, spec)
 
171
                return spectype(spec, _internal=True)
168
172
        else:
169
 
            for spectype in SPEC_TYPES:
170
 
                if spec.startswith(spectype.prefix):
171
 
                    trace.mutter('Returning RevisionSpec %s for %s',
172
 
                                 spectype.__name__, spec)
173
 
                    return spectype(spec, _internal=True)
174
 
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
175
 
            # wait for _match_on to be called.
176
 
            return RevisionSpec_dwim(spec, _internal=True)
 
173
            # RevisionSpec_revno is special cased, because it is the only
 
174
            # one that directly handles plain integers
 
175
            # TODO: This should not be special cased rather it should be
 
176
            # a method invocation on spectype.canparse()
 
177
            global _revno_regex
 
178
            if _revno_regex is None:
 
179
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
180
            if _revno_regex.match(spec) is not None:
 
181
                return RevisionSpec_revno(spec, _internal=True)
 
182
 
 
183
            raise errors.NoSuchRevisionSpec(spec)
177
184
 
178
185
    def __init__(self, spec, _internal=False):
179
186
        """Create a RevisionSpec referring to the Null revision.
183
190
            called directly. Only from RevisionSpec.from_string()
184
191
        """
185
192
        if not _internal:
 
193
            # XXX: Update this after 0.10 is released
186
194
            symbol_versioning.warn('Creating a RevisionSpec directly has'
187
195
                                   ' been deprecated in version 0.11. Use'
188
196
                                   ' RevisionSpec.from_string()'
195
203
 
196
204
    def _match_on(self, branch, revs):
197
205
        trace.mutter('Returning RevisionSpec._match_on: None')
198
 
        return RevisionInfo(branch, None, None)
 
206
        return RevisionInfo(branch, 0, None)
199
207
 
200
208
    def _match_on_and_check(self, branch, revs):
201
209
        info = self._match_on(branch, revs)
202
210
        if info:
203
211
            return info
204
 
        elif info == (None, None):
205
 
            # special case - nothing supplied
 
212
        elif info == (0, None):
 
213
            # special case - the empty tree
206
214
            return info
207
215
        elif self.prefix:
208
216
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
211
219
 
212
220
    def in_history(self, branch):
213
221
        if branch:
214
 
            if self.wants_revision_history:
215
 
                revs = branch.revision_history()
216
 
            else:
217
 
                revs = None
 
222
            revs = branch.revision_history()
218
223
        else:
219
224
            # this should never trigger.
220
225
            # TODO: make it a deprecated code path. RBC 20060928
230
235
    # will do what you expect.
231
236
    in_store = in_history
232
237
    in_branch = in_store
233
 
 
234
 
    def as_revision_id(self, context_branch):
235
 
        """Return just the revision_id for this revisions spec.
236
 
 
237
 
        Some revision specs require a context_branch to be able to determine
238
 
        their value. Not all specs will make use of it.
239
 
        """
240
 
        return self._as_revision_id(context_branch)
241
 
 
242
 
    def _as_revision_id(self, context_branch):
243
 
        """Implementation of as_revision_id()
244
 
 
245
 
        Classes should override this function to provide appropriate
246
 
        functionality. The default is to just call '.in_history().rev_id'
247
 
        """
248
 
        return self.in_history(context_branch).rev_id
249
 
 
250
 
    def as_tree(self, context_branch):
251
 
        """Return the tree object for this revisions spec.
252
 
 
253
 
        Some revision specs require a context_branch to be able to determine
254
 
        the revision id and access the repository. Not all specs will make
255
 
        use of it.
256
 
        """
257
 
        return self._as_tree(context_branch)
258
 
 
259
 
    def _as_tree(self, context_branch):
260
 
        """Implementation of as_tree().
261
 
 
262
 
        Classes should override this function to provide appropriate
263
 
        functionality. The default is to just call '.as_revision_id()'
264
 
        and get the revision tree from context_branch's repository.
265
 
        """
266
 
        revision_id = self.as_revision_id(context_branch)
267
 
        return context_branch.repository.revision_tree(revision_id)
268
 
 
 
238
        
269
239
    def __repr__(self):
270
240
        # this is mostly for helping with testing
271
241
        return '<%s %s>' % (self.__class__.__name__,
272
242
                              self.user_spec)
273
 
 
 
243
    
274
244
    def needs_branch(self):
275
245
        """Whether this revision spec needs a branch.
276
246
 
280
250
 
281
251
    def get_branch(self):
282
252
        """When the revision specifier contains a branch location, return it.
283
 
 
 
253
        
284
254
        Otherwise, return None.
285
255
        """
286
256
        return None
288
258
 
289
259
# private API
290
260
 
291
 
class RevisionSpec_dwim(RevisionSpec):
292
 
    """Provides a DWIMish revision specifier lookup.
293
 
 
294
 
    Note that this does not go in the revspec_registry because by definition
295
 
    there is no prefix to identify it.  It's solely called from
296
 
    RevisionSpec.from_string() because the DWIMification happen when _match_on
297
 
    is called so the string describing the revision is kept here until needed.
298
 
    """
299
 
 
300
 
    help_txt = None
301
 
    # We don't need to build the revision history ourself, that's delegated to
302
 
    # each revspec we try.
303
 
    wants_revision_history = False
304
 
 
305
 
    def _try_spectype(self, rstype, branch):
306
 
        rs = rstype(self.spec, _internal=True)
307
 
        # Hit in_history to find out if it exists, or we need to try the
308
 
        # next type.
309
 
        return rs.in_history(branch)
310
 
 
311
 
    def _match_on(self, branch, revs):
312
 
        """Run the lookup and see what we can get."""
313
 
 
314
 
        # First, see if it's a revno
315
 
        global _revno_regex
316
 
        if _revno_regex is None:
317
 
            _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
318
 
        if _revno_regex.match(self.spec) is not None:
319
 
            try:
320
 
                return self._try_spectype(RevisionSpec_revno, branch)
321
 
            except RevisionSpec_revno.dwim_catchable_exceptions:
322
 
                pass
323
 
 
324
 
        # Next see what has been registered
325
 
        for rs_class in dwim_revspecs:
326
 
            try:
327
 
                return self._try_spectype(rs_class, branch)
328
 
            except rs_class.dwim_catchable_exceptions:
329
 
                pass
330
 
 
331
 
        # Well, I dunno what it is. Note that we don't try to keep track of the
332
 
        # first of last exception raised during the DWIM tries as none seems
333
 
        # really relevant.
334
 
        raise errors.InvalidRevisionSpec(self.spec, branch)
335
 
 
336
 
 
337
261
class RevisionSpec_revno(RevisionSpec):
338
262
    """Selects a revision using a number."""
339
263
 
340
264
    help_txt = """Selects a revision using a number.
341
265
 
342
266
    Use an integer to specify a revision in the history of the branch.
343
 
    Optionally a branch can be specified.  A negative number will count
344
 
    from the end of the branch (-1 is the last revision, -2 the previous
345
 
    one). If the negative number is larger than the branch's history, the
346
 
    first revision is returned.
347
 
    Examples::
348
 
 
349
 
      revno:1                   -> return the first revision of this branch
 
267
    Optionally a branch can be specified. The 'revno:' prefix is optional.
 
268
    A negative number will count from the end of the branch (-1 is the
 
269
    last revision, -2 the previous one). If the negative number is larger
 
270
    than the branch's history, the first revision is returned.
 
271
    examples:
 
272
      revno:1                   -> return the first revision
350
273
      revno:3:/path/to/branch   -> return the 3rd revision of
351
274
                                   the branch '/path/to/branch'
352
275
      revno:-1                  -> The last revision in a branch.
356
279
                                   your history is very long.
357
280
    """
358
281
    prefix = 'revno:'
359
 
    wants_revision_history = False
360
282
 
361
283
    def _match_on(self, branch, revs):
362
284
        """Lookup a revision by revision number"""
363
 
        branch, revno, revision_id = self._lookup(branch, revs)
364
 
        return RevisionInfo(branch, revno, revision_id)
365
 
 
366
 
    def _lookup(self, branch, revs_or_none):
367
285
        loc = self.spec.find(':')
368
286
        if loc == -1:
369
287
            revno_spec = self.spec
383
301
                dotted = False
384
302
            except ValueError:
385
303
                # dotted decimal. This arguably should not be here
386
 
                # but the from_string method is a little primitive
 
304
                # but the from_string method is a little primitive 
387
305
                # right now - RBC 20060928
388
306
                try:
389
307
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
398
316
            # the branch object.
399
317
            from bzrlib.branch import Branch
400
318
            branch = Branch.open(branch_spec)
401
 
            revs_or_none = None
 
319
            # Need to use a new revision history
 
320
            # because we are using a specific branch
 
321
            revs = branch.revision_history()
402
322
 
403
323
        if dotted:
 
324
            branch.lock_read()
404
325
            try:
405
 
                revision_id = branch.dotted_revno_to_revision_id(match_revno,
406
 
                    _cache_reverse=True)
407
 
            except errors.NoSuchRevision:
408
 
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
326
                revision_id_to_revno = branch.get_revision_id_to_revno_map()
 
327
                revisions = [revision_id for revision_id, revno
 
328
                             in revision_id_to_revno.iteritems()
 
329
                             if revno == match_revno]
 
330
            finally:
 
331
                branch.unlock()
 
332
            if len(revisions) != 1:
 
333
                return RevisionInfo(branch, None, None)
409
334
            else:
410
335
                # there is no traditional 'revno' for dotted-decimal revnos.
411
336
                # so for  API compatability we return None.
412
 
                return branch, None, revision_id
 
337
                return RevisionInfo(branch, None, revisions[0])
413
338
        else:
414
 
            last_revno, last_revision_id = branch.last_revision_info()
415
339
            if revno < 0:
416
340
                # if get_rev_id supported negative revnos, there would not be a
417
341
                # need for this special case.
418
 
                if (-revno) >= last_revno:
 
342
                if (-revno) >= len(revs):
419
343
                    revno = 1
420
344
                else:
421
 
                    revno = last_revno + revno + 1
 
345
                    revno = len(revs) + revno + 1
422
346
            try:
423
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
347
                revision_id = branch.get_rev_id(revno, revs)
424
348
            except errors.NoSuchRevision:
425
349
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
 
        return branch, revno, revision_id
427
 
 
428
 
    def _as_revision_id(self, context_branch):
429
 
        # We would have the revno here, but we don't really care
430
 
        branch, revno, revision_id = self._lookup(context_branch, None)
431
 
        return revision_id
432
 
 
 
350
        return RevisionInfo(branch, revno, revision_id)
 
351
        
433
352
    def needs_branch(self):
434
353
        return self.spec.find(':') == -1
435
354
 
439
358
        else:
440
359
            return self.spec[self.spec.find(':')+1:]
441
360
 
442
 
# Old compatibility
 
361
# Old compatibility 
443
362
RevisionSpec_int = RevisionSpec_revno
444
363
 
 
364
SPEC_TYPES.append(RevisionSpec_revno)
445
365
 
446
366
 
447
367
class RevisionSpec_revid(RevisionSpec):
450
370
    help_txt = """Selects a revision using the revision id.
451
371
 
452
372
    Supply a specific revision id, that can be used to specify any
453
 
    revision id in the ancestry of the branch.
 
373
    revision id in the ancestry of the branch. 
454
374
    Including merges, and pending merges.
455
 
    Examples::
456
 
 
 
375
    examples:
457
376
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
458
 
    """
459
 
 
 
377
    """    
460
378
    prefix = 'revid:'
461
379
 
462
380
    def _match_on(self, branch, revs):
466
384
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
467
385
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
468
386
 
469
 
    def _as_revision_id(self, context_branch):
470
 
        return osutils.safe_revision_id(self.spec, warn=False)
471
 
 
 
387
SPEC_TYPES.append(RevisionSpec_revid)
472
388
 
473
389
 
474
390
class RevisionSpec_last(RevisionSpec):
478
394
 
479
395
    Supply a positive number to get the nth revision from the end.
480
396
    This is the same as supplying negative numbers to the 'revno:' spec.
481
 
    Examples::
482
 
 
 
397
    examples:
483
398
      last:1        -> return the last revision
484
399
      last:3        -> return the revision 2 before the end.
485
 
    """
 
400
    """    
486
401
 
487
402
    prefix = 'last:'
488
403
 
489
404
    def _match_on(self, branch, revs):
490
 
        revno, revision_id = self._revno_and_revision_id(branch, revs)
491
 
        return RevisionInfo(branch, revno, revision_id)
492
 
 
493
 
    def _revno_and_revision_id(self, context_branch, revs_or_none):
494
 
        last_revno, last_revision_id = context_branch.last_revision_info()
495
 
 
496
405
        if self.spec == '':
497
 
            if not last_revno:
498
 
                raise errors.NoCommits(context_branch)
499
 
            return last_revno, last_revision_id
 
406
            if not revs:
 
407
                raise errors.NoCommits(branch)
 
408
            return RevisionInfo(branch, len(revs), revs[-1])
500
409
 
501
410
        try:
502
411
            offset = int(self.spec)
503
412
        except ValueError, e:
504
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
 
413
            raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
505
414
 
506
415
        if offset <= 0:
507
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
416
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
508
417
                                             'you must supply a positive value')
509
 
 
510
 
        revno = last_revno - offset + 1
 
418
        revno = len(revs) - offset + 1
511
419
        try:
512
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
420
            revision_id = branch.get_rev_id(revno, revs)
513
421
        except errors.NoSuchRevision:
514
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
515
 
        return revno, revision_id
516
 
 
517
 
    def _as_revision_id(self, context_branch):
518
 
        # We compute the revno as part of the process, but we don't really care
519
 
        # about it.
520
 
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
521
 
        return revision_id
522
 
 
 
422
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
423
        return RevisionInfo(branch, revno, revision_id)
 
424
 
 
425
SPEC_TYPES.append(RevisionSpec_last)
523
426
 
524
427
 
525
428
class RevisionSpec_before(RevisionSpec):
527
430
 
528
431
    help_txt = """Selects the parent of the revision specified.
529
432
 
530
 
    Supply any revision spec to return the parent of that revision.  This is
531
 
    mostly useful when inspecting revisions that are not in the revision history
532
 
    of a branch.
533
 
 
 
433
    Supply any revision spec to return the parent of that revision.
534
434
    It is an error to request the parent of the null revision (before:0).
535
 
 
536
 
    Examples::
537
 
 
 
435
    This is mostly useful when inspecting revisions that are not in the
 
436
    revision history of a branch.
 
437
 
 
438
    examples:
538
439
      before:1913    -> Return the parent of revno 1913 (revno 1912)
539
440
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
540
441
                                            aaaa@bbbb-1234567890
541
 
      bzr diff -r before:1913..1913
542
 
            -> Find the changes between revision 1913 and its parent (1912).
543
 
               (What changes did revision 1913 introduce).
544
 
               This is equivalent to:  bzr diff -c 1913
 
442
      bzr diff -r before:revid:aaaa..revid:aaaa
 
443
            -> Find the changes between revision 'aaaa' and its parent.
 
444
               (what changes did 'aaaa' introduce)
545
445
    """
546
446
 
547
447
    prefix = 'before:'
548
 
 
 
448
    
549
449
    def _match_on(self, branch, revs):
550
450
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
551
451
        if r.revno == 0:
572
472
                                                 branch)
573
473
        return RevisionInfo(branch, revno, revision_id)
574
474
 
575
 
    def _as_revision_id(self, context_branch):
576
 
        base_revspec = RevisionSpec.from_string(self.spec)
577
 
        base_revision_id = base_revspec.as_revision_id(context_branch)
578
 
        if base_revision_id == revision.NULL_REVISION:
579
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
 
                                         'cannot go before the null: revision')
581
 
        context_repo = context_branch.repository
582
 
        context_repo.lock_read()
583
 
        try:
584
 
            parent_map = context_repo.get_parent_map([base_revision_id])
585
 
        finally:
586
 
            context_repo.unlock()
587
 
        if base_revision_id not in parent_map:
588
 
            # Ghost, or unknown revision id
589
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
590
 
                'cannot find the matching revision')
591
 
        parents = parent_map[base_revision_id]
592
 
        if len(parents) < 1:
593
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
594
 
                'No parents for revision.')
595
 
        return parents[0]
596
 
 
 
475
SPEC_TYPES.append(RevisionSpec_before)
597
476
 
598
477
 
599
478
class RevisionSpec_tag(RevisionSpec):
605
484
    """
606
485
 
607
486
    prefix = 'tag:'
608
 
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
609
487
 
610
488
    def _match_on(self, branch, revs):
611
489
        # Can raise tags not supported, NoSuchTag, etc
613
491
            branch.tags.lookup_tag(self.spec),
614
492
            revs)
615
493
 
616
 
    def _as_revision_id(self, context_branch):
617
 
        return context_branch.tags.lookup_tag(self.spec)
618
 
 
 
494
SPEC_TYPES.append(RevisionSpec_tag)
619
495
 
620
496
 
621
497
class _RevListToTimestamps(object):
647
523
    Matches the first entry after a given date (either at midnight or
648
524
    at a specified time).
649
525
 
650
 
    One way to display all the changes since yesterday would be::
651
 
 
652
 
        bzr log -r date:yesterday..
653
 
 
654
 
    Examples::
655
 
 
 
526
    One way to display all the changes since yesterday would be:
 
527
        bzr log -r date:yesterday..-1
 
528
 
 
529
    examples:
656
530
      date:yesterday            -> select the first revision since yesterday
657
531
      date:2006-08-14,17:10:14  -> select the first revision after
658
532
                                   August 14th, 2006 at 5:10pm.
659
 
    """
 
533
    """    
660
534
    prefix = 'date:'
661
535
    _date_re = re.compile(
662
536
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
718
592
        finally:
719
593
            branch.unlock()
720
594
        if rev == len(revs):
721
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
595
            return RevisionInfo(branch, None)
722
596
        else:
723
597
            return RevisionInfo(branch, rev + 1)
724
598
 
 
599
SPEC_TYPES.append(RevisionSpec_date)
725
600
 
726
601
 
727
602
class RevisionSpec_ancestor(RevisionSpec):
739
614
    that your branch introduces, while excluding the changes that you
740
615
    have not merged from the remote branch.
741
616
 
742
 
    Examples::
743
 
 
 
617
    examples:
744
618
      ancestor:/path/to/branch
745
619
      $ bzr diff -r ancestor:../../mainline/branch
746
620
    """
750
624
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
751
625
        return self._find_revision_info(branch, self.spec)
752
626
 
753
 
    def _as_revision_id(self, context_branch):
754
 
        return self._find_revision_id(context_branch, self.spec)
755
 
 
756
627
    @staticmethod
757
628
    def _find_revision_info(branch, other_location):
758
 
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
759
 
                                                              other_location)
760
 
        try:
761
 
            revno = branch.revision_id_to_revno(revision_id)
762
 
        except errors.NoSuchRevision:
763
 
            revno = None
764
 
        return RevisionInfo(branch, revno, revision_id)
765
 
 
766
 
    @staticmethod
767
 
    def _find_revision_id(branch, other_location):
768
629
        from bzrlib.branch import Branch
769
630
 
770
 
        branch.lock_read()
771
 
        try:
772
 
            revision_a = revision.ensure_null(branch.last_revision())
773
 
            if revision_a == revision.NULL_REVISION:
774
 
                raise errors.NoCommits(branch)
775
 
            if other_location == '':
776
 
                other_location = branch.get_parent()
777
 
            other_branch = Branch.open(other_location)
778
 
            other_branch.lock_read()
779
 
            try:
780
 
                revision_b = revision.ensure_null(other_branch.last_revision())
781
 
                if revision_b == revision.NULL_REVISION:
782
 
                    raise errors.NoCommits(other_branch)
783
 
                graph = branch.repository.get_graph(other_branch.repository)
784
 
                rev_id = graph.find_unique_lca(revision_a, revision_b)
785
 
            finally:
786
 
                other_branch.unlock()
 
631
        other_branch = Branch.open(other_location)
 
632
        revision_a = branch.last_revision()
 
633
        revision_b = other_branch.last_revision()
 
634
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
 
635
            if r in (None, revision.NULL_REVISION):
 
636
                raise errors.NoCommits(b)
 
637
        revision_source = revision.MultipleRevisionSources(
 
638
                branch.repository, other_branch.repository)
 
639
        graph = branch.repository.get_graph(other_branch.repository)
 
640
        revision_a = revision.ensure_null(revision_a)
 
641
        revision_b = revision.ensure_null(revision_b)
 
642
        if revision.NULL_REVISION in (revision_a, revision_b):
 
643
            rev_id = revision.NULL_REVISION
 
644
        else:
 
645
            rev_id = graph.find_unique_lca(revision_a, revision_b)
787
646
            if rev_id == revision.NULL_REVISION:
788
647
                raise errors.NoCommonAncestor(revision_a, revision_b)
789
 
            return rev_id
790
 
        finally:
791
 
            branch.unlock()
792
 
 
793
 
 
 
648
        try:
 
649
            revno = branch.revision_id_to_revno(rev_id)
 
650
        except errors.NoSuchRevision:
 
651
            revno = None
 
652
        return RevisionInfo(branch, revno, rev_id)
 
653
 
 
654
 
 
655
SPEC_TYPES.append(RevisionSpec_ancestor)
794
656
 
795
657
 
796
658
class RevisionSpec_branch(RevisionSpec):
800
662
 
801
663
    Supply the path to a branch to select its last revision.
802
664
 
803
 
    Examples::
804
 
 
 
665
    examples:
805
666
      branch:/path/to/branch
806
667
    """
807
668
    prefix = 'branch:'
808
 
    dwim_catchable_exceptions = (errors.NotBranchError,)
809
669
 
810
670
    def _match_on(self, branch, revs):
811
671
        from bzrlib.branch import Branch
820
680
        except errors.NoSuchRevision:
821
681
            revno = None
822
682
        return RevisionInfo(branch, revno, revision_b)
823
 
 
824
 
    def _as_revision_id(self, context_branch):
825
 
        from bzrlib.branch import Branch
826
 
        other_branch = Branch.open(self.spec)
827
 
        last_revision = other_branch.last_revision()
828
 
        last_revision = revision.ensure_null(last_revision)
829
 
        context_branch.fetch(other_branch, last_revision)
830
 
        if last_revision == revision.NULL_REVISION:
831
 
            raise errors.NoCommits(other_branch)
832
 
        return last_revision
833
 
 
834
 
    def _as_tree(self, context_branch):
835
 
        from bzrlib.branch import Branch
836
 
        other_branch = Branch.open(self.spec)
837
 
        last_revision = other_branch.last_revision()
838
 
        last_revision = revision.ensure_null(last_revision)
839
 
        if last_revision == revision.NULL_REVISION:
840
 
            raise errors.NoCommits(other_branch)
841
 
        return other_branch.repository.revision_tree(last_revision)
842
 
 
 
683
        
 
684
SPEC_TYPES.append(RevisionSpec_branch)
843
685
 
844
686
 
845
687
class RevisionSpec_submit(RevisionSpec_ancestor):
849
691
 
850
692
    Diffing against this shows all the changes that were made in this branch,
851
693
    and is a good predictor of what merge will do.  The submit branch is
852
 
    used by the bundle and merge directive commands.  If no submit branch
 
694
    used by the bundle and merge directive comands.  If no submit branch
853
695
    is specified, the parent branch is used instead.
854
696
 
855
697
    The common ancestor is the last revision that existed in both
856
698
    branches. Usually this is the branch point, but it could also be
857
699
    a revision that was merged.
858
700
 
859
 
    Examples::
860
 
 
 
701
    examples:
861
702
      $ bzr diff -r submit:
862
703
    """
863
704
 
864
705
    prefix = 'submit:'
865
706
 
866
 
    def _get_submit_location(self, branch):
 
707
    def _match_on(self, branch, revs):
 
708
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
867
709
        submit_location = branch.get_submit_branch()
868
710
        location_type = 'submit branch'
869
711
        if submit_location is None:
872
714
        if submit_location is None:
873
715
            raise errors.NoSubmitBranch(branch)
874
716
        trace.note('Using %s %s', location_type, submit_location)
875
 
        return submit_location
876
 
 
877
 
    def _match_on(self, branch, revs):
878
 
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
879
 
        return self._find_revision_info(branch,
880
 
            self._get_submit_location(branch))
881
 
 
882
 
    def _as_revision_id(self, context_branch):
883
 
        return self._find_revision_id(context_branch,
884
 
            self._get_submit_location(context_branch))
885
 
 
886
 
 
887
 
# The order in which we want to DWIM a revision spec without any prefix.
888
 
# revno is always tried first and isn't listed here, this is used by
889
 
# RevisionSpec_dwim._match_on
890
 
dwim_revspecs = [
891
 
    RevisionSpec_tag, # Let's try for a tag
892
 
    RevisionSpec_revid, # Maybe it's a revid?
893
 
    RevisionSpec_date, # Perhaps a date?
894
 
    RevisionSpec_branch, # OK, last try, maybe it's a branch
895
 
    ]
896
 
 
897
 
 
898
 
revspec_registry = registry.Registry()
899
 
def _register_revspec(revspec):
900
 
    revspec_registry.register(revspec.prefix, revspec)
901
 
 
902
 
_register_revspec(RevisionSpec_revno)
903
 
_register_revspec(RevisionSpec_revid)
904
 
_register_revspec(RevisionSpec_last)
905
 
_register_revspec(RevisionSpec_before)
906
 
_register_revspec(RevisionSpec_tag)
907
 
_register_revspec(RevisionSpec_date)
908
 
_register_revspec(RevisionSpec_ancestor)
909
 
_register_revspec(RevisionSpec_branch)
910
 
_register_revspec(RevisionSpec_submit)
911
 
 
912
 
# classes in this list should have a "prefix" attribute, against which
913
 
# string specs are matched
914
 
SPEC_TYPES = symbol_versioning.deprecated_list(
915
 
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
 
717
        return self._find_revision_info(branch, submit_location)
 
718
 
 
719
 
 
720
SPEC_TYPES.append(RevisionSpec_submit)