~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
104
100
 
105
101
        Use this if you don't know or care what the revno is.
106
102
        """
107
 
        if revision_id == revision.NULL_REVISION:
108
 
            return RevisionInfo(branch, 0, revision_id)
109
103
        try:
110
104
            revno = revs.index(revision_id) + 1
111
105
        except ValueError:
113
107
        return RevisionInfo(branch, revno, revision_id)
114
108
 
115
109
 
 
110
# classes in this list should have a "prefix" attribute, against which
 
111
# string specs are matched
 
112
SPEC_TYPES = []
116
113
_revno_regex = None
117
114
 
118
115
 
121
118
 
122
119
    help_txt = """A parsed revision specification.
123
120
 
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.
 
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.
128
125
 
129
126
    Revision specs are an UI element, and they have been moved out
130
127
    of the branch class to leave "back-end" classes unaware of such
136
133
    """
137
134
 
138
135
    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
 
    """
 
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)
148
147
 
149
148
    @staticmethod
150
149
    def from_string(spec):
159
158
 
160
159
        if spec is None:
161
160
            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)
 
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)
168
170
        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)
 
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)
177
182
 
178
183
    def __init__(self, spec, _internal=False):
179
184
        """Create a RevisionSpec referring to the Null revision.
183
188
            called directly. Only from RevisionSpec.from_string()
184
189
        """
185
190
        if not _internal:
 
191
            # XXX: Update this after 0.10 is released
186
192
            symbol_versioning.warn('Creating a RevisionSpec directly has'
187
193
                                   ' been deprecated in version 0.11. Use'
188
194
                                   ' RevisionSpec.from_string()'
195
201
 
196
202
    def _match_on(self, branch, revs):
197
203
        trace.mutter('Returning RevisionSpec._match_on: None')
198
 
        return RevisionInfo(branch, None, None)
 
204
        return RevisionInfo(branch, 0, None)
199
205
 
200
206
    def _match_on_and_check(self, branch, revs):
201
207
        info = self._match_on(branch, revs)
202
208
        if info:
203
209
            return info
204
 
        elif info == (None, None):
205
 
            # special case - nothing supplied
 
210
        elif info == (0, None):
 
211
            # special case - the empty tree
206
212
            return info
207
213
        elif self.prefix:
208
214
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
211
217
 
212
218
    def in_history(self, branch):
213
219
        if branch:
214
 
            if self.wants_revision_history:
215
 
                revs = branch.revision_history()
216
 
            else:
217
 
                revs = None
 
220
            revs = branch.revision_history()
218
221
        else:
219
222
            # this should never trigger.
220
223
            # TODO: make it a deprecated code path. RBC 20060928
230
233
    # will do what you expect.
231
234
    in_store = in_history
232
235
    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
 
 
 
236
        
269
237
    def __repr__(self):
270
238
        # this is mostly for helping with testing
271
239
        return '<%s %s>' % (self.__class__.__name__,
272
240
                              self.user_spec)
273
 
 
 
241
    
274
242
    def needs_branch(self):
275
243
        """Whether this revision spec needs a branch.
276
244
 
280
248
 
281
249
    def get_branch(self):
282
250
        """When the revision specifier contains a branch location, return it.
283
 
 
 
251
        
284
252
        Otherwise, return None.
285
253
        """
286
254
        return None
288
256
 
289
257
# private API
290
258
 
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
259
class RevisionSpec_revno(RevisionSpec):
338
260
    """Selects a revision using a number."""
339
261
 
340
262
    help_txt = """Selects a revision using a number.
341
263
 
342
264
    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
 
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.
 
269
    examples:
 
270
      revno:1                   -> return the first revision
350
271
      revno:3:/path/to/branch   -> return the 3rd revision of
351
272
                                   the branch '/path/to/branch'
352
273
      revno:-1                  -> The last revision in a branch.
356
277
                                   your history is very long.
357
278
    """
358
279
    prefix = 'revno:'
359
 
    wants_revision_history = False
360
280
 
361
281
    def _match_on(self, branch, revs):
362
282
        """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
283
        loc = self.spec.find(':')
368
284
        if loc == -1:
369
285
            revno_spec = self.spec
383
299
                dotted = False
384
300
            except ValueError:
385
301
                # dotted decimal. This arguably should not be here
386
 
                # but the from_string method is a little primitive
 
302
                # but the from_string method is a little primitive 
387
303
                # right now - RBC 20060928
388
304
                try:
389
305
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
398
314
            # the branch object.
399
315
            from bzrlib.branch import Branch
400
316
            branch = Branch.open(branch_spec)
401
 
            revs_or_none = None
 
317
            # Need to use a new revision history
 
318
            # because we are using a specific branch
 
319
            revs = branch.revision_history()
402
320
 
403
321
        if dotted:
 
322
            branch.lock_read()
404
323
            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)
 
324
                last_rev = branch.last_revision()
 
325
                merge_sorted_revisions = tsort.merge_sort(
 
326
                    branch.repository.get_revision_graph(last_rev),
 
327
                    last_rev,
 
328
                    generate_revno=True)
 
329
                def match(item):
 
330
                    return item[3] == match_revno
 
331
                revisions = filter(match, merge_sorted_revisions)
 
332
            finally:
 
333
                branch.unlock()
 
334
            if len(revisions) != 1:
 
335
                return RevisionInfo(branch, None, None)
409
336
            else:
410
337
                # there is no traditional 'revno' for dotted-decimal revnos.
411
338
                # so for  API compatability we return None.
412
 
                return branch, None, revision_id
 
339
                return RevisionInfo(branch, None, revisions[0][1])
413
340
        else:
414
 
            last_revno, last_revision_id = branch.last_revision_info()
415
341
            if revno < 0:
416
342
                # if get_rev_id supported negative revnos, there would not be a
417
343
                # need for this special case.
418
 
                if (-revno) >= last_revno:
 
344
                if (-revno) >= len(revs):
419
345
                    revno = 1
420
346
                else:
421
 
                    revno = last_revno + revno + 1
 
347
                    revno = len(revs) + revno + 1
422
348
            try:
423
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
349
                revision_id = branch.get_rev_id(revno, revs)
424
350
            except errors.NoSuchRevision:
425
351
                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
 
 
 
352
        return RevisionInfo(branch, revno, revision_id)
 
353
        
433
354
    def needs_branch(self):
434
355
        return self.spec.find(':') == -1
435
356
 
439
360
        else:
440
361
            return self.spec[self.spec.find(':')+1:]
441
362
 
442
 
# Old compatibility
 
363
# Old compatibility 
443
364
RevisionSpec_int = RevisionSpec_revno
444
365
 
 
366
SPEC_TYPES.append(RevisionSpec_revno)
445
367
 
446
368
 
447
369
class RevisionSpec_revid(RevisionSpec):
450
372
    help_txt = """Selects a revision using the revision id.
451
373
 
452
374
    Supply a specific revision id, that can be used to specify any
453
 
    revision id in the ancestry of the branch.
 
375
    revision id in the ancestry of the branch. 
454
376
    Including merges, and pending merges.
455
 
    Examples::
456
 
 
 
377
    examples:
457
378
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
458
 
    """
459
 
 
 
379
    """    
460
380
    prefix = 'revid:'
461
381
 
462
382
    def _match_on(self, branch, revs):
466
386
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
467
387
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
468
388
 
469
 
    def _as_revision_id(self, context_branch):
470
 
        return osutils.safe_revision_id(self.spec, warn=False)
471
 
 
 
389
SPEC_TYPES.append(RevisionSpec_revid)
472
390
 
473
391
 
474
392
class RevisionSpec_last(RevisionSpec):
478
396
 
479
397
    Supply a positive number to get the nth revision from the end.
480
398
    This is the same as supplying negative numbers to the 'revno:' spec.
481
 
    Examples::
482
 
 
 
399
    examples:
483
400
      last:1        -> return the last revision
484
401
      last:3        -> return the revision 2 before the end.
485
 
    """
 
402
    """    
486
403
 
487
404
    prefix = 'last:'
488
405
 
489
406
    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
407
        if self.spec == '':
497
 
            if not last_revno:
498
 
                raise errors.NoCommits(context_branch)
499
 
            return last_revno, last_revision_id
 
408
            if not revs:
 
409
                raise errors.NoCommits(branch)
 
410
            return RevisionInfo(branch, len(revs), revs[-1])
500
411
 
501
412
        try:
502
413
            offset = int(self.spec)
503
414
        except ValueError, e:
504
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
 
415
            raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
505
416
 
506
417
        if offset <= 0:
507
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
418
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
508
419
                                             'you must supply a positive value')
509
 
 
510
 
        revno = last_revno - offset + 1
 
420
        revno = len(revs) - offset + 1
511
421
        try:
512
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
422
            revision_id = branch.get_rev_id(revno, revs)
513
423
        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
 
 
 
424
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
425
        return RevisionInfo(branch, revno, revision_id)
 
426
 
 
427
SPEC_TYPES.append(RevisionSpec_last)
523
428
 
524
429
 
525
430
class RevisionSpec_before(RevisionSpec):
527
432
 
528
433
    help_txt = """Selects the parent of the revision specified.
529
434
 
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
    Supply any revision spec to return the parent of that revision.
534
436
    It is an error to request the parent of the null revision (before:0).
535
 
 
536
 
    Examples::
537
 
 
 
437
    This is mostly useful when inspecting revisions that are not in the
 
438
    revision history of a branch.
 
439
 
 
440
    examples:
538
441
      before:1913    -> Return the parent of revno 1913 (revno 1912)
539
442
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
540
443
                                            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
 
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)
545
447
    """
546
448
 
547
449
    prefix = 'before:'
548
 
 
 
450
    
549
451
    def _match_on(self, branch, revs):
550
452
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
551
453
        if r.revno == 0:
556
458
            rev = branch.repository.get_revision(r.rev_id)
557
459
            if not rev.parent_ids:
558
460
                revno = 0
559
 
                revision_id = revision.NULL_REVISION
 
461
                revision_id = None
560
462
            else:
561
463
                revision_id = rev.parent_ids[0]
562
464
                try:
572
474
                                                 branch)
573
475
        return RevisionInfo(branch, revno, revision_id)
574
476
 
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
 
 
 
477
SPEC_TYPES.append(RevisionSpec_before)
597
478
 
598
479
 
599
480
class RevisionSpec_tag(RevisionSpec):
605
486
    """
606
487
 
607
488
    prefix = 'tag:'
608
 
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
609
489
 
610
490
    def _match_on(self, branch, revs):
611
491
        # Can raise tags not supported, NoSuchTag, etc
613
493
            branch.tags.lookup_tag(self.spec),
614
494
            revs)
615
495
 
616
 
    def _as_revision_id(self, context_branch):
617
 
        return context_branch.tags.lookup_tag(self.spec)
618
 
 
 
496
SPEC_TYPES.append(RevisionSpec_tag)
619
497
 
620
498
 
621
499
class _RevListToTimestamps(object):
647
525
    Matches the first entry after a given date (either at midnight or
648
526
    at a specified time).
649
527
 
650
 
    One way to display all the changes since yesterday would be::
651
 
 
652
 
        bzr log -r date:yesterday..
653
 
 
654
 
    Examples::
655
 
 
 
528
    One way to display all the changes since yesterday would be:
 
529
        bzr log -r date:yesterday..-1
 
530
 
 
531
    examples:
656
532
      date:yesterday            -> select the first revision since yesterday
657
533
      date:2006-08-14,17:10:14  -> select the first revision after
658
534
                                   August 14th, 2006 at 5:10pm.
659
 
    """
 
535
    """    
660
536
    prefix = 'date:'
661
537
    _date_re = re.compile(
662
538
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
718
594
        finally:
719
595
            branch.unlock()
720
596
        if rev == len(revs):
721
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
597
            return RevisionInfo(branch, None)
722
598
        else:
723
599
            return RevisionInfo(branch, rev + 1)
724
600
 
 
601
SPEC_TYPES.append(RevisionSpec_date)
725
602
 
726
603
 
727
604
class RevisionSpec_ancestor(RevisionSpec):
739
616
    that your branch introduces, while excluding the changes that you
740
617
    have not merged from the remote branch.
741
618
 
742
 
    Examples::
743
 
 
 
619
    examples:
744
620
      ancestor:/path/to/branch
745
621
      $ bzr diff -r ancestor:../../mainline/branch
746
622
    """
750
626
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
751
627
        return self._find_revision_info(branch, self.spec)
752
628
 
753
 
    def _as_revision_id(self, context_branch):
754
 
        return self._find_revision_id(context_branch, self.spec)
755
 
 
756
629
    @staticmethod
757
630
    def _find_revision_info(branch, other_location):
758
 
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
759
 
                                                              other_location)
 
631
        from bzrlib.branch import Branch
 
632
 
 
633
        other_branch = Branch.open(other_location)
 
634
        revision_a = branch.last_revision()
 
635
        revision_b = other_branch.last_revision()
 
636
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
 
637
            if r in (None, revision.NULL_REVISION):
 
638
                raise errors.NoCommits(b)
 
639
        revision_source = revision.MultipleRevisionSources(
 
640
                branch.repository, other_branch.repository)
 
641
        rev_id = revision.common_ancestor(revision_a, revision_b,
 
642
                                          revision_source)
760
643
        try:
761
 
            revno = branch.revision_id_to_revno(revision_id)
 
644
            revno = branch.revision_id_to_revno(rev_id)
762
645
        except errors.NoSuchRevision:
763
646
            revno = None
764
 
        return RevisionInfo(branch, revno, revision_id)
765
 
 
766
 
    @staticmethod
767
 
    def _find_revision_id(branch, other_location):
768
 
        from bzrlib.branch import Branch
769
 
 
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()
787
 
            if rev_id == revision.NULL_REVISION:
788
 
                raise errors.NoCommonAncestor(revision_a, revision_b)
789
 
            return rev_id
790
 
        finally:
791
 
            branch.unlock()
792
 
 
793
 
 
 
647
        return RevisionInfo(branch, revno, rev_id)
 
648
 
 
649
 
 
650
SPEC_TYPES.append(RevisionSpec_ancestor)
794
651
 
795
652
 
796
653
class RevisionSpec_branch(RevisionSpec):
800
657
 
801
658
    Supply the path to a branch to select its last revision.
802
659
 
803
 
    Examples::
804
 
 
 
660
    examples:
805
661
      branch:/path/to/branch
806
662
    """
807
663
    prefix = 'branch:'
808
 
    dwim_catchable_exceptions = (errors.NotBranchError,)
809
664
 
810
665
    def _match_on(self, branch, revs):
811
666
        from bzrlib.branch import Branch
820
675
        except errors.NoSuchRevision:
821
676
            revno = None
822
677
        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
 
 
 
678
        
 
679
SPEC_TYPES.append(RevisionSpec_branch)
843
680
 
844
681
 
845
682
class RevisionSpec_submit(RevisionSpec_ancestor):
849
686
 
850
687
    Diffing against this shows all the changes that were made in this branch,
851
688
    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
 
689
    used by the bundle and merge directive comands.  If no submit branch
853
690
    is specified, the parent branch is used instead.
854
691
 
855
692
    The common ancestor is the last revision that existed in both
856
693
    branches. Usually this is the branch point, but it could also be
857
694
    a revision that was merged.
858
695
 
859
 
    Examples::
860
 
 
 
696
    examples:
861
697
      $ bzr diff -r submit:
862
698
    """
863
699
 
864
700
    prefix = 'submit:'
865
701
 
866
 
    def _get_submit_location(self, branch):
 
702
    def _match_on(self, branch, revs):
 
703
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
867
704
        submit_location = branch.get_submit_branch()
868
705
        location_type = 'submit branch'
869
706
        if submit_location is None:
872
709
        if submit_location is None:
873
710
            raise errors.NoSubmitBranch(branch)
874
711
        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", [])
 
712
        return self._find_revision_info(branch, submit_location)
 
713
 
 
714
 
 
715
SPEC_TYPES.append(RevisionSpec_submit)