~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: John Arbash Meinel
  • Date: 2010-08-02 17:16:12 UTC
  • mto: This revision was merged to the branch mainline in revision 5369.
  • Revision ID: john@arbash-meinel.com-20100802171612-rdh5ods70w2bl3j7
We also have to re-implement it for _simple_set_pyx.pyx

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
 
 
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(), """
18
22
import bisect
19
23
import datetime
20
 
import re
 
24
""")
21
25
 
22
26
from bzrlib import (
23
27
    errors,
24
28
    osutils,
 
29
    registry,
25
30
    revision,
26
31
    symbol_versioning,
27
32
    trace,
28
 
    tsort,
29
33
    )
30
34
 
31
35
 
100
104
 
101
105
        Use this if you don't know or care what the revno is.
102
106
        """
 
107
        if revision_id == revision.NULL_REVISION:
 
108
            return RevisionInfo(branch, 0, revision_id)
103
109
        try:
104
110
            revno = revs.index(revision_id) + 1
105
111
        except ValueError:
107
113
        return RevisionInfo(branch, revno, revision_id)
108
114
 
109
115
 
110
 
# classes in this list should have a "prefix" attribute, against which
111
 
# string specs are matched
112
 
SPEC_TYPES = []
113
116
_revno_regex = None
114
117
 
115
118
 
118
121
 
119
122
    help_txt = """A parsed revision specification.
120
123
 
121
 
    A revision specification can be an integer, in which case it is
122
 
    assumed to be a revno (though this will translate negative values
123
 
    into positive ones); or it can be a string, in which case it is
124
 
    parsed for something like 'date:' or 'revid:' etc.
 
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.
125
128
 
126
129
    Revision specs are an UI element, and they have been moved out
127
130
    of the branch class to leave "back-end" classes unaware of such
133
136
    """
134
137
 
135
138
    prefix = None
136
 
 
137
 
    def __new__(cls, spec, _internal=False):
138
 
        if _internal:
139
 
            return object.__new__(cls, spec, _internal=_internal)
140
 
 
141
 
        symbol_versioning.warn('Creating a RevisionSpec directly has'
142
 
                               ' been deprecated in version 0.11. Use'
143
 
                               ' RevisionSpec.from_string()'
144
 
                               ' instead.',
145
 
                               DeprecationWarning, stacklevel=2)
146
 
        return RevisionSpec.from_string(spec)
 
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
    """
147
148
 
148
149
    @staticmethod
149
150
    def from_string(spec):
158
159
 
159
160
        if spec is None:
160
161
            return RevisionSpec(None, _internal=True)
161
 
 
162
 
        assert isinstance(spec, basestring), \
163
 
            "You should only supply strings not %s" % (type(spec),)
164
 
 
165
 
        for spectype in SPEC_TYPES:
166
 
            if spec.startswith(spectype.prefix):
167
 
                trace.mutter('Returning RevisionSpec %s for %s',
168
 
                             spectype.__name__, spec)
169
 
                return spectype(spec, _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)
170
168
        else:
171
 
            # RevisionSpec_revno is special cased, because it is the only
172
 
            # one that directly handles plain integers
173
 
            # TODO: This should not be special cased rather it should be
174
 
            # a method invocation on spectype.canparse()
175
 
            global _revno_regex
176
 
            if _revno_regex is None:
177
 
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
178
 
            if _revno_regex.match(spec) is not None:
179
 
                return RevisionSpec_revno(spec, _internal=True)
180
 
 
181
 
            raise errors.NoSuchRevisionSpec(spec)
 
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)
182
177
 
183
178
    def __init__(self, spec, _internal=False):
184
179
        """Create a RevisionSpec referring to the Null revision.
188
183
            called directly. Only from RevisionSpec.from_string()
189
184
        """
190
185
        if not _internal:
191
 
            # XXX: Update this after 0.10 is released
192
186
            symbol_versioning.warn('Creating a RevisionSpec directly has'
193
187
                                   ' been deprecated in version 0.11. Use'
194
188
                                   ' RevisionSpec.from_string()'
201
195
 
202
196
    def _match_on(self, branch, revs):
203
197
        trace.mutter('Returning RevisionSpec._match_on: None')
204
 
        return RevisionInfo(branch, 0, None)
 
198
        return RevisionInfo(branch, None, None)
205
199
 
206
200
    def _match_on_and_check(self, branch, revs):
207
201
        info = self._match_on(branch, revs)
208
202
        if info:
209
203
            return info
210
 
        elif info == (0, None):
211
 
            # special case - the empty tree
 
204
        elif info == (None, None):
 
205
            # special case - nothing supplied
212
206
            return info
213
207
        elif self.prefix:
214
208
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
217
211
 
218
212
    def in_history(self, branch):
219
213
        if branch:
220
 
            revs = branch.revision_history()
 
214
            if self.wants_revision_history:
 
215
                revs = branch.revision_history()
 
216
            else:
 
217
                revs = None
221
218
        else:
222
219
            # this should never trigger.
223
220
            # TODO: make it a deprecated code path. RBC 20060928
233
230
    # will do what you expect.
234
231
    in_store = in_history
235
232
    in_branch = in_store
236
 
        
 
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
 
237
269
    def __repr__(self):
238
270
        # this is mostly for helping with testing
239
271
        return '<%s %s>' % (self.__class__.__name__,
240
272
                              self.user_spec)
241
 
    
 
273
 
242
274
    def needs_branch(self):
243
275
        """Whether this revision spec needs a branch.
244
276
 
248
280
 
249
281
    def get_branch(self):
250
282
        """When the revision specifier contains a branch location, return it.
251
 
        
 
283
 
252
284
        Otherwise, return None.
253
285
        """
254
286
        return None
256
288
 
257
289
# private API
258
290
 
 
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
 
259
337
class RevisionSpec_revno(RevisionSpec):
260
338
    """Selects a revision using a number."""
261
339
 
262
340
    help_txt = """Selects a revision using a number.
263
341
 
264
342
    Use an integer to specify a revision in the history of the branch.
265
 
    Optionally a branch can be specified. The 'revno:' prefix is optional.
266
 
    A negative number will count from the end of the branch (-1 is the
267
 
    last revision, -2 the previous one). If the negative number is larger
268
 
    than the branch's history, the first revision is returned.
 
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.
269
347
    Examples::
270
348
 
271
 
      revno:1                   -> return the first revision
 
349
      revno:1                   -> return the first revision of this branch
272
350
      revno:3:/path/to/branch   -> return the 3rd revision of
273
351
                                   the branch '/path/to/branch'
274
352
      revno:-1                  -> The last revision in a branch.
278
356
                                   your history is very long.
279
357
    """
280
358
    prefix = 'revno:'
 
359
    wants_revision_history = False
281
360
 
282
361
    def _match_on(self, branch, revs):
283
362
        """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):
284
367
        loc = self.spec.find(':')
285
368
        if loc == -1:
286
369
            revno_spec = self.spec
300
383
                dotted = False
301
384
            except ValueError:
302
385
                # dotted decimal. This arguably should not be here
303
 
                # but the from_string method is a little primitive 
 
386
                # but the from_string method is a little primitive
304
387
                # right now - RBC 20060928
305
388
                try:
306
389
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
315
398
            # the branch object.
316
399
            from bzrlib.branch import Branch
317
400
            branch = Branch.open(branch_spec)
318
 
            # Need to use a new revision history
319
 
            # because we are using a specific branch
320
 
            revs = branch.revision_history()
 
401
            revs_or_none = None
321
402
 
322
403
        if dotted:
323
 
            branch.lock_read()
324
404
            try:
325
 
                revision_id_to_revno = branch.get_revision_id_to_revno_map()
326
 
                revisions = [revision_id for revision_id, revno
327
 
                             in revision_id_to_revno.iteritems()
328
 
                             if revno == match_revno]
329
 
            finally:
330
 
                branch.unlock()
331
 
            if len(revisions) != 1:
332
 
                return RevisionInfo(branch, None, None)
 
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)
333
409
            else:
334
410
                # there is no traditional 'revno' for dotted-decimal revnos.
335
411
                # so for  API compatability we return None.
336
 
                return RevisionInfo(branch, None, revisions[0])
 
412
                return branch, None, revision_id
337
413
        else:
 
414
            last_revno, last_revision_id = branch.last_revision_info()
338
415
            if revno < 0:
339
416
                # if get_rev_id supported negative revnos, there would not be a
340
417
                # need for this special case.
341
 
                if (-revno) >= len(revs):
 
418
                if (-revno) >= last_revno:
342
419
                    revno = 1
343
420
                else:
344
 
                    revno = len(revs) + revno + 1
 
421
                    revno = last_revno + revno + 1
345
422
            try:
346
 
                revision_id = branch.get_rev_id(revno, revs)
 
423
                revision_id = branch.get_rev_id(revno, revs_or_none)
347
424
            except errors.NoSuchRevision:
348
425
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
349
 
        return RevisionInfo(branch, revno, revision_id)
350
 
        
 
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
 
351
433
    def needs_branch(self):
352
434
        return self.spec.find(':') == -1
353
435
 
357
439
        else:
358
440
            return self.spec[self.spec.find(':')+1:]
359
441
 
360
 
# Old compatibility 
 
442
# Old compatibility
361
443
RevisionSpec_int = RevisionSpec_revno
362
444
 
363
 
SPEC_TYPES.append(RevisionSpec_revno)
364
445
 
365
446
 
366
447
class RevisionSpec_revid(RevisionSpec):
369
450
    help_txt = """Selects a revision using the revision id.
370
451
 
371
452
    Supply a specific revision id, that can be used to specify any
372
 
    revision id in the ancestry of the branch. 
 
453
    revision id in the ancestry of the branch.
373
454
    Including merges, and pending merges.
374
455
    Examples::
375
456
 
376
457
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
377
 
    """    
 
458
    """
 
459
 
378
460
    prefix = 'revid:'
379
461
 
380
462
    def _match_on(self, branch, revs):
384
466
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
385
467
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
386
468
 
387
 
SPEC_TYPES.append(RevisionSpec_revid)
 
469
    def _as_revision_id(self, context_branch):
 
470
        return osutils.safe_revision_id(self.spec, warn=False)
 
471
 
388
472
 
389
473
 
390
474
class RevisionSpec_last(RevisionSpec):
398
482
 
399
483
      last:1        -> return the last revision
400
484
      last:3        -> return the revision 2 before the end.
401
 
    """    
 
485
    """
402
486
 
403
487
    prefix = 'last:'
404
488
 
405
489
    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
 
406
496
        if self.spec == '':
407
 
            if not revs:
408
 
                raise errors.NoCommits(branch)
409
 
            return RevisionInfo(branch, len(revs), revs[-1])
 
497
            if not last_revno:
 
498
                raise errors.NoCommits(context_branch)
 
499
            return last_revno, last_revision_id
410
500
 
411
501
        try:
412
502
            offset = int(self.spec)
413
503
        except ValueError, e:
414
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
 
504
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
415
505
 
416
506
        if offset <= 0:
417
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
 
507
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
418
508
                                             'you must supply a positive value')
419
 
        revno = len(revs) - offset + 1
 
509
 
 
510
        revno = last_revno - offset + 1
420
511
        try:
421
 
            revision_id = branch.get_rev_id(revno, revs)
 
512
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
422
513
        except errors.NoSuchRevision:
423
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
424
 
        return RevisionInfo(branch, revno, revision_id)
425
 
 
426
 
SPEC_TYPES.append(RevisionSpec_last)
 
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
 
427
523
 
428
524
 
429
525
class RevisionSpec_before(RevisionSpec):
431
527
 
432
528
    help_txt = """Selects the parent of the revision specified.
433
529
 
434
 
    Supply any revision spec to return the parent of that revision.
 
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
 
435
534
    It is an error to request the parent of the null revision (before:0).
436
 
    This is mostly useful when inspecting revisions that are not in the
437
 
    revision history of a branch.
438
535
 
439
536
    Examples::
440
537
 
441
538
      before:1913    -> Return the parent of revno 1913 (revno 1912)
442
539
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
443
540
                                            aaaa@bbbb-1234567890
444
 
      bzr diff -r before:revid:aaaa..revid:aaaa
445
 
            -> Find the changes between revision 'aaaa' and its parent.
446
 
               (what changes did 'aaaa' introduce)
 
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
447
545
    """
448
546
 
449
547
    prefix = 'before:'
450
 
    
 
548
 
451
549
    def _match_on(self, branch, revs):
452
550
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
453
551
        if r.revno == 0:
474
572
                                                 branch)
475
573
        return RevisionInfo(branch, revno, revision_id)
476
574
 
477
 
SPEC_TYPES.append(RevisionSpec_before)
 
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
 
478
597
 
479
598
 
480
599
class RevisionSpec_tag(RevisionSpec):
486
605
    """
487
606
 
488
607
    prefix = 'tag:'
 
608
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
489
609
 
490
610
    def _match_on(self, branch, revs):
491
611
        # Can raise tags not supported, NoSuchTag, etc
493
613
            branch.tags.lookup_tag(self.spec),
494
614
            revs)
495
615
 
496
 
SPEC_TYPES.append(RevisionSpec_tag)
 
616
    def _as_revision_id(self, context_branch):
 
617
        return context_branch.tags.lookup_tag(self.spec)
 
618
 
497
619
 
498
620
 
499
621
class _RevListToTimestamps(object):
527
649
 
528
650
    One way to display all the changes since yesterday would be::
529
651
 
530
 
        bzr log -r date:yesterday..-1
 
652
        bzr log -r date:yesterday..
531
653
 
532
654
    Examples::
533
655
 
534
656
      date:yesterday            -> select the first revision since yesterday
535
657
      date:2006-08-14,17:10:14  -> select the first revision after
536
658
                                   August 14th, 2006 at 5:10pm.
537
 
    """    
 
659
    """
538
660
    prefix = 'date:'
539
661
    _date_re = re.compile(
540
662
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
596
718
        finally:
597
719
            branch.unlock()
598
720
        if rev == len(revs):
599
 
            return RevisionInfo(branch, None)
 
721
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
600
722
        else:
601
723
            return RevisionInfo(branch, rev + 1)
602
724
 
603
 
SPEC_TYPES.append(RevisionSpec_date)
604
725
 
605
726
 
606
727
class RevisionSpec_ancestor(RevisionSpec):
629
750
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
630
751
        return self._find_revision_info(branch, self.spec)
631
752
 
 
753
    def _as_revision_id(self, context_branch):
 
754
        return self._find_revision_id(context_branch, self.spec)
 
755
 
632
756
    @staticmethod
633
757
    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):
634
768
        from bzrlib.branch import Branch
635
769
 
636
 
        other_branch = Branch.open(other_location)
637
 
        revision_a = branch.last_revision()
638
 
        revision_b = other_branch.last_revision()
639
 
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
640
 
            if r in (None, revision.NULL_REVISION):
641
 
                raise errors.NoCommits(b)
642
 
        revision_source = revision.MultipleRevisionSources(
643
 
                branch.repository, other_branch.repository)
644
 
        graph = branch.repository.get_graph(other_branch.repository)
645
 
        revision_a = revision.ensure_null(revision_a)
646
 
        revision_b = revision.ensure_null(revision_b)
647
 
        if revision.NULL_REVISION in (revision_a, revision_b):
648
 
            rev_id = revision.NULL_REVISION
649
 
        else:
650
 
            rev_id = graph.find_unique_lca(revision_a, revision_b)
 
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()
651
787
            if rev_id == revision.NULL_REVISION:
652
788
                raise errors.NoCommonAncestor(revision_a, revision_b)
653
 
        try:
654
 
            revno = branch.revision_id_to_revno(rev_id)
655
 
        except errors.NoSuchRevision:
656
 
            revno = None
657
 
        return RevisionInfo(branch, revno, rev_id)
658
 
 
659
 
 
660
 
SPEC_TYPES.append(RevisionSpec_ancestor)
 
789
            return rev_id
 
790
        finally:
 
791
            branch.unlock()
 
792
 
 
793
 
661
794
 
662
795
 
663
796
class RevisionSpec_branch(RevisionSpec):
672
805
      branch:/path/to/branch
673
806
    """
674
807
    prefix = 'branch:'
 
808
    dwim_catchable_exceptions = (errors.NotBranchError,)
675
809
 
676
810
    def _match_on(self, branch, revs):
677
811
        from bzrlib.branch import Branch
679
813
        revision_b = other_branch.last_revision()
680
814
        if revision_b in (None, revision.NULL_REVISION):
681
815
            raise errors.NoCommits(other_branch)
682
 
        # pull in the remote revisions so we can diff
683
 
        branch.fetch(other_branch, revision_b)
 
816
        if branch is None:
 
817
            branch = other_branch
 
818
        else:
 
819
            try:
 
820
                # pull in the remote revisions so we can diff
 
821
                branch.fetch(other_branch, revision_b)
 
822
            except errors.ReadOnlyError:
 
823
                branch = other_branch
684
824
        try:
685
825
            revno = branch.revision_id_to_revno(revision_b)
686
826
        except errors.NoSuchRevision:
687
827
            revno = None
688
828
        return RevisionInfo(branch, revno, revision_b)
689
 
        
690
 
SPEC_TYPES.append(RevisionSpec_branch)
 
829
 
 
830
    def _as_revision_id(self, context_branch):
 
831
        from bzrlib.branch import Branch
 
832
        other_branch = Branch.open(self.spec)
 
833
        last_revision = other_branch.last_revision()
 
834
        last_revision = revision.ensure_null(last_revision)
 
835
        context_branch.fetch(other_branch, last_revision)
 
836
        if last_revision == revision.NULL_REVISION:
 
837
            raise errors.NoCommits(other_branch)
 
838
        return last_revision
 
839
 
 
840
    def _as_tree(self, context_branch):
 
841
        from bzrlib.branch import Branch
 
842
        other_branch = Branch.open(self.spec)
 
843
        last_revision = other_branch.last_revision()
 
844
        last_revision = revision.ensure_null(last_revision)
 
845
        if last_revision == revision.NULL_REVISION:
 
846
            raise errors.NoCommits(other_branch)
 
847
        return other_branch.repository.revision_tree(last_revision)
 
848
 
 
849
    def needs_branch(self):
 
850
        return False
 
851
 
 
852
    def get_branch(self):
 
853
        return self.spec
 
854
 
691
855
 
692
856
 
693
857
class RevisionSpec_submit(RevisionSpec_ancestor):
697
861
 
698
862
    Diffing against this shows all the changes that were made in this branch,
699
863
    and is a good predictor of what merge will do.  The submit branch is
700
 
    used by the bundle and merge directive comands.  If no submit branch
 
864
    used by the bundle and merge directive commands.  If no submit branch
701
865
    is specified, the parent branch is used instead.
702
866
 
703
867
    The common ancestor is the last revision that existed in both
711
875
 
712
876
    prefix = 'submit:'
713
877
 
714
 
    def _match_on(self, branch, revs):
715
 
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
878
    def _get_submit_location(self, branch):
716
879
        submit_location = branch.get_submit_branch()
717
880
        location_type = 'submit branch'
718
881
        if submit_location is None:
721
884
        if submit_location is None:
722
885
            raise errors.NoSubmitBranch(branch)
723
886
        trace.note('Using %s %s', location_type, submit_location)
724
 
        return self._find_revision_info(branch, submit_location)
725
 
 
726
 
 
727
 
SPEC_TYPES.append(RevisionSpec_submit)
 
887
        return submit_location
 
888
 
 
889
    def _match_on(self, branch, revs):
 
890
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
 
891
        return self._find_revision_info(branch,
 
892
            self._get_submit_location(branch))
 
893
 
 
894
    def _as_revision_id(self, context_branch):
 
895
        return self._find_revision_id(context_branch,
 
896
            self._get_submit_location(context_branch))
 
897
 
 
898
 
 
899
# The order in which we want to DWIM a revision spec without any prefix.
 
900
# revno is always tried first and isn't listed here, this is used by
 
901
# RevisionSpec_dwim._match_on
 
902
dwim_revspecs = [
 
903
    RevisionSpec_tag, # Let's try for a tag
 
904
    RevisionSpec_revid, # Maybe it's a revid?
 
905
    RevisionSpec_date, # Perhaps a date?
 
906
    RevisionSpec_branch, # OK, last try, maybe it's a branch
 
907
    ]
 
908
 
 
909
 
 
910
revspec_registry = registry.Registry()
 
911
def _register_revspec(revspec):
 
912
    revspec_registry.register(revspec.prefix, revspec)
 
913
 
 
914
_register_revspec(RevisionSpec_revno)
 
915
_register_revspec(RevisionSpec_revid)
 
916
_register_revspec(RevisionSpec_last)
 
917
_register_revspec(RevisionSpec_before)
 
918
_register_revspec(RevisionSpec_tag)
 
919
_register_revspec(RevisionSpec_date)
 
920
_register_revspec(RevisionSpec_ancestor)
 
921
_register_revspec(RevisionSpec_branch)
 
922
_register_revspec(RevisionSpec_submit)
 
923
 
 
924
# classes in this list should have a "prefix" attribute, against which
 
925
# string specs are matched
 
926
SPEC_TYPES = symbol_versioning.deprecated_list(
 
927
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])