/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: John Arbash Meinel
  • Date: 2006-09-20 14:51:03 UTC
  • mfrom: (0.8.23 version_info)
  • mto: This revision was merged to the branch mainline in revision 2028.
  • Revision ID: john@arbash-meinel.com-20060920145103-02725c6d6c886040
[merge] version-info plugin, and cleanup for layout in bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005 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
21
21
 
22
22
from bzrlib import (
23
23
    errors,
24
 
    osutils,
25
24
    revision,
26
25
    symbol_versioning,
27
26
    trace,
28
 
    tsort,
29
27
    )
30
28
 
31
29
 
33
31
 
34
32
 
35
33
class RevisionInfo(object):
36
 
    """The results of applying a revision specification to a branch."""
37
 
 
38
 
    help_txt = """The results of applying a revision specification to a branch.
 
34
    """The results of applying a revision specification to a branch.
39
35
 
40
36
    An instance has two useful attributes: revno, and rev_id.
41
37
 
94
90
        return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
95
91
            self.revno, self.rev_id, self.branch)
96
92
 
97
 
    @staticmethod
98
 
    def from_revision_id(branch, revision_id, revs):
99
 
        """Construct a RevisionInfo given just the id.
100
 
 
101
 
        Use this if you don't know or care what the revno is.
102
 
        """
103
 
        try:
104
 
            revno = revs.index(revision_id) + 1
105
 
        except ValueError:
106
 
            revno = None
107
 
        return RevisionInfo(branch, revno, revision_id)
108
 
 
109
93
 
110
94
# classes in this list should have a "prefix" attribute, against which
111
95
# string specs are matched
114
98
 
115
99
 
116
100
class RevisionSpec(object):
117
 
    """A parsed revision specification."""
118
 
 
119
 
    help_txt = """A parsed revision specification.
 
101
    """A parsed revision specification.
120
102
 
121
103
    A revision specification can be an integer, in which case it is
122
104
    assumed to be a revno (though this will translate negative values
170
152
        else:
171
153
            # RevisionSpec_revno is special cased, because it is the only
172
154
            # 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
155
            global _revno_regex
176
156
            if _revno_regex is None:
177
 
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
157
                _revno_regex = re.compile(r'-?\d+(:.*)?$')
178
158
            if _revno_regex.match(spec) is not None:
179
159
                return RevisionSpec_revno(spec, _internal=True)
180
160
 
219
199
        if branch:
220
200
            revs = branch.revision_history()
221
201
        else:
222
 
            # this should never trigger.
223
 
            # TODO: make it a deprecated code path. RBC 20060928
224
202
            revs = None
225
203
        return self._match_on_and_check(branch, revs)
226
204
 
257
235
# private API
258
236
 
259
237
class RevisionSpec_revno(RevisionSpec):
260
 
    """Selects a revision using a number."""
261
 
 
262
 
    help_txt = """Selects a revision using a number.
263
 
 
264
 
    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.
269
 
    examples:
270
 
      revno:1                   -> return the first revision
271
 
      revno:3:/path/to/branch   -> return the 3rd revision of
272
 
                                   the branch '/path/to/branch'
273
 
      revno:-1                  -> The last revision in a branch.
274
 
      -2:http://other/branch    -> The second to last revision in the
275
 
                                   remote branch.
276
 
      -1000000                  -> Most likely the first revision, unless
277
 
                                   your history is very long.
278
 
    """
279
238
    prefix = 'revno:'
280
239
 
281
240
    def _match_on(self, branch, revs):
296
255
        else:
297
256
            try:
298
257
                revno = int(revno_spec)
299
 
                dotted = False
300
 
            except ValueError:
301
 
                # dotted decimal. This arguably should not be here
302
 
                # but the from_string method is a little primitive 
303
 
                # right now - RBC 20060928
304
 
                try:
305
 
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
306
 
                except ValueError, e:
307
 
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
308
 
 
309
 
                dotted = True
 
258
            except ValueError, e:
 
259
                raise errors.InvalidRevisionSpec(self.user_spec,
 
260
                                                 branch, e)
310
261
 
311
262
        if branch_spec:
312
 
            # the user has override the branch to look in.
313
 
            # we need to refresh the revision_history map and
314
 
            # the branch object.
315
263
            from bzrlib.branch import Branch
316
264
            branch = Branch.open(branch_spec)
317
265
            # Need to use a new revision history
318
266
            # because we are using a specific branch
319
267
            revs = branch.revision_history()
320
268
 
321
 
        if dotted:
322
 
            branch.lock_read()
323
 
            try:
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)
 
269
        if revno < 0:
 
270
            if (-revno) >= len(revs):
 
271
                revno = 1
336
272
            else:
337
 
                # there is no traditional 'revno' for dotted-decimal revnos.
338
 
                # so for  API compatability we return None.
339
 
                return RevisionInfo(branch, None, revisions[0][1])
340
 
        else:
341
 
            if revno < 0:
342
 
                # if get_rev_id supported negative revnos, there would not be a
343
 
                # need for this special case.
344
 
                if (-revno) >= len(revs):
345
 
                    revno = 1
346
 
                else:
347
 
                    revno = len(revs) + revno + 1
348
 
            try:
349
 
                revision_id = branch.get_rev_id(revno, revs)
350
 
            except errors.NoSuchRevision:
351
 
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
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)
352
278
        return RevisionInfo(branch, revno, revision_id)
353
279
        
354
280
    def needs_branch(self):
367
293
 
368
294
 
369
295
class RevisionSpec_revid(RevisionSpec):
370
 
    """Selects a revision using the revision id."""
371
 
 
372
 
    help_txt = """Selects a revision using the revision id.
373
 
 
374
 
    Supply a specific revision id, that can be used to specify any
375
 
    revision id in the ancestry of the branch. 
376
 
    Including merges, and pending merges.
377
 
    examples:
378
 
      revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
379
 
    """    
380
296
    prefix = 'revid:'
381
297
 
382
298
    def _match_on(self, branch, revs):
383
 
        # self.spec comes straight from parsing the command line arguments,
384
 
        # so we expect it to be a Unicode string. Switch it to the internal
385
 
        # representation.
386
 
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
387
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
 
299
        try:
 
300
            revno = revs.index(self.spec) + 1
 
301
        except ValueError:
 
302
            revno = None
 
303
        return RevisionInfo(branch, revno, self.spec)
388
304
 
389
305
SPEC_TYPES.append(RevisionSpec_revid)
390
306
 
391
307
 
392
308
class RevisionSpec_last(RevisionSpec):
393
 
    """Selects the nth revision from the end."""
394
 
 
395
 
    help_txt = """Selects the nth revision from the end.
396
 
 
397
 
    Supply a positive number to get the nth revision from the end.
398
 
    This is the same as supplying negative numbers to the 'revno:' spec.
399
 
    examples:
400
 
      last:1        -> return the last revision
401
 
      last:3        -> return the revision 2 before the end.
402
 
    """    
403
309
 
404
310
    prefix = 'last:'
405
311
 
428
334
 
429
335
 
430
336
class RevisionSpec_before(RevisionSpec):
431
 
    """Selects the parent of the revision specified."""
432
 
 
433
 
    help_txt = """Selects the parent of the revision specified.
434
 
 
435
 
    Supply any revision spec to return the parent of that revision.
436
 
    It is an error to request the parent of the null revision (before:0).
437
 
    This is mostly useful when inspecting revisions that are not in the
438
 
    revision history of a branch.
439
 
 
440
 
    examples:
441
 
      before:1913    -> Return the parent of revno 1913 (revno 1912)
442
 
      before:revid:aaaa@bbbb-1234567890  -> return the parent of revision
443
 
                                            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)
447
 
    """
448
337
 
449
338
    prefix = 'before:'
450
339
    
478
367
 
479
368
 
480
369
class RevisionSpec_tag(RevisionSpec):
481
 
    """Select a revision identified by tag name"""
482
 
 
483
 
    help_txt = """Selects a revision identified by a tag name.
484
 
 
485
 
    Tags are stored in the branch and created by the 'tag' command.
486
 
    """
487
 
 
488
370
    prefix = 'tag:'
489
371
 
490
372
    def _match_on(self, branch, revs):
491
 
        # Can raise tags not supported, NoSuchTag, etc
492
 
        return RevisionInfo.from_revision_id(branch,
493
 
            branch.tags.lookup_tag(self.spec),
494
 
            revs)
 
373
        raise errors.InvalidRevisionSpec(self.user_spec, branch,
 
374
                                         'tag: namespace registered,'
 
375
                                         ' but not implemented')
495
376
 
496
377
SPEC_TYPES.append(RevisionSpec_tag)
497
378
 
516
397
 
517
398
 
518
399
class RevisionSpec_date(RevisionSpec):
519
 
    """Selects a revision on the basis of a datestamp."""
520
 
 
521
 
    help_txt = """Selects a revision on the basis of a datestamp.
522
 
 
523
 
    Supply a datestamp to select the first revision that matches the date.
524
 
    Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
525
 
    Matches the first entry after a given date (either at midnight or
526
 
    at a specified time).
527
 
 
528
 
    One way to display all the changes since yesterday would be:
529
 
        bzr log -r date:yesterday..-1
530
 
 
531
 
    examples:
532
 
      date:yesterday            -> select the first revision since yesterday
533
 
      date:2006-08-14,17:10:14  -> select the first revision after
534
 
                                   August 14th, 2006 at 5:10pm.
535
 
    """    
536
400
    prefix = 'date:'
537
401
    _date_re = re.compile(
538
402
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
541
405
        )
542
406
 
543
407
    def _match_on(self, branch, revs):
544
 
        """Spec for date revisions:
 
408
        """
 
409
        Spec for date revisions:
545
410
          date:value
546
411
          value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
547
412
          matches the first entry after a given date (either at midnight or
548
413
          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
549
417
        """
550
 
        #  XXX: This doesn't actually work
551
 
        #  So the proper way of saying 'give me all entries for today' is:
552
 
        #      -r date:yesterday..date:today
553
418
        today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
554
419
        if self.spec.lower() == 'yesterday':
555
420
            dt = today - datetime.timedelta(days=1)
602
467
 
603
468
 
604
469
class RevisionSpec_ancestor(RevisionSpec):
605
 
    """Selects a common ancestor with a second branch."""
606
 
 
607
 
    help_txt = """Selects a common ancestor with a second branch.
608
 
 
609
 
    Supply the path to a branch to select the common ancestor.
610
 
 
611
 
    The common ancestor is the last revision that existed in both
612
 
    branches. Usually this is the branch point, but it could also be
613
 
    a revision that was merged.
614
 
 
615
 
    This is frequently used with 'diff' to return all of the changes
616
 
    that your branch introduces, while excluding the changes that you
617
 
    have not merged from the remote branch.
618
 
 
619
 
    examples:
620
 
      ancestor:/path/to/branch
621
 
      $ bzr diff -r ancestor:../../mainline/branch
622
 
    """
623
470
    prefix = 'ancestor:'
624
471
 
625
472
    def _match_on(self, branch, revs):
 
473
        from bzrlib.branch import Branch
 
474
 
626
475
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
627
 
        return self._find_revision_info(branch, self.spec)
628
 
 
629
 
    @staticmethod
630
 
    def _find_revision_info(branch, other_location):
631
 
        from bzrlib.branch import Branch
632
 
 
633
 
        other_branch = Branch.open(other_location)
 
476
        other_branch = Branch.open(self.spec)
634
477
        revision_a = branch.last_revision()
635
478
        revision_b = other_branch.last_revision()
636
479
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
645
488
        except errors.NoSuchRevision:
646
489
            revno = None
647
490
        return RevisionInfo(branch, revno, rev_id)
648
 
 
649
 
 
 
491
        
650
492
SPEC_TYPES.append(RevisionSpec_ancestor)
651
493
 
652
494
 
653
495
class RevisionSpec_branch(RevisionSpec):
654
 
    """Selects the last revision of a specified branch."""
655
 
 
656
 
    help_txt = """Selects the last revision of a specified branch.
657
 
 
658
 
    Supply the path to a branch to select its last revision.
659
 
 
660
 
    examples:
661
 
      branch:/path/to/branch
 
496
    """A branch: revision specifier.
 
497
 
 
498
    This takes the path to a branch and returns its tip revision id.
662
499
    """
663
500
    prefix = 'branch:'
664
501
 
677
514
        return RevisionInfo(branch, revno, revision_b)
678
515
        
679
516
SPEC_TYPES.append(RevisionSpec_branch)
680
 
 
681
 
 
682
 
class RevisionSpec_submit(RevisionSpec_ancestor):
683
 
    """Selects a common ancestor with a submit branch."""
684
 
 
685
 
    help_txt = """Selects a common ancestor with the submit branch.
686
 
 
687
 
    Diffing against this shows all the changes that were made in this branch,
688
 
    and is a good predictor of what merge will do.  The submit branch is
689
 
    used by the bundle and merge directive comands.  If no submit branch
690
 
    is specified, the parent branch is used instead.
691
 
 
692
 
    The common ancestor is the last revision that existed in both
693
 
    branches. Usually this is the branch point, but it could also be
694
 
    a revision that was merged.
695
 
 
696
 
    examples:
697
 
      $ bzr diff -r submit:
698
 
    """
699
 
 
700
 
    prefix = 'submit:'
701
 
 
702
 
    def _match_on(self, branch, revs):
703
 
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
704
 
        submit_location = branch.get_submit_branch()
705
 
        location_type = 'submit branch'
706
 
        if submit_location is None:
707
 
            submit_location = branch.get_parent()
708
 
            location_type = 'parent branch'
709
 
        if submit_location is None:
710
 
            raise errors.NoSubmitBranch(branch)
711
 
        trace.note('Using %s %s', location_type, submit_location)
712
 
        return self._find_revision_info(branch, submit_location)
713
 
 
714
 
 
715
 
SPEC_TYPES.append(RevisionSpec_submit)