/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Eric Siegerman
  • Date: 2009-03-25 16:53:14 UTC
  • mto: (4204.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 4205.
  • Revision ID: pub08@davor.org-20090325165314-1n3wv0ig2knf44xx
Another broken link.

Show diffs side-by-side

added added

removed removed

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