1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1
# Copyright (C) 2005 Canonical Ltd
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
35
34
class RevisionInfo(object):
36
"""The results of applying a revision specification to a branch."""
38
help_txt = """The results of applying a revision specification to a branch.
35
"""The results of applying a revision specification to a branch.
40
37
An instance has two useful attributes: revno, and rev_id.
94
91
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
95
92
self.revno, self.rev_id, self.branch)
98
def from_revision_id(branch, revision_id, revs):
99
"""Construct a RevisionInfo given just the id.
101
Use this if you don't know or care what the revno is.
104
revno = revs.index(revision_id) + 1
107
return RevisionInfo(branch, revno, revision_id)
110
95
# classes in this list should have a "prefix" attribute, against which
111
96
# string specs are matched
116
101
class RevisionSpec(object):
117
"""A parsed revision specification."""
119
help_txt = """A parsed revision specification.
102
"""A parsed revision specification.
121
104
A revision specification can be an integer, in which case it is
122
105
assumed to be a revno (though this will translate negative values
259
242
class RevisionSpec_revno(RevisionSpec):
260
"""Selects a revision using a number."""
262
help_txt = """Selects a revision using a number.
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.
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
276
-1000000 -> Most likely the first revision, unless
277
your history is very long.
279
243
prefix = 'revno:'
281
245
def _match_on(self, branch, revs):
322
286
branch.lock_read()
324
revision_id_to_revno = branch.get_revision_id_to_revno_map()
325
revisions = [revision_id for revision_id, revno
326
in revision_id_to_revno.iteritems()
327
if revno == match_revno]
288
last_rev = branch.last_revision()
289
merge_sorted_revisions = tsort.merge_sort(
290
branch.repository.get_revision_graph(last_rev),
294
return item[3] == match_revno
295
revisions = filter(match, merge_sorted_revisions)
330
298
if len(revisions) != 1:
333
301
# there is no traditional 'revno' for dotted-decimal revnos.
334
302
# so for API compatability we return None.
335
return RevisionInfo(branch, None, revisions[0])
303
return RevisionInfo(branch, None, revisions[0][1])
338
# if get_rev_id supported negative revnos, there would not be a
339
# need for this special case.
340
306
if (-revno) >= len(revs):
365
331
class RevisionSpec_revid(RevisionSpec):
366
"""Selects a revision using the revision id."""
368
help_txt = """Selects a revision using the revision id.
370
Supply a specific revision id, that can be used to specify any
371
revision id in the ancestry of the branch.
372
Including merges, and pending merges.
374
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
376
332
prefix = 'revid:'
378
334
def _match_on(self, branch, revs):
379
# self.spec comes straight from parsing the command line arguments,
380
# so we expect it to be a Unicode string. Switch it to the internal
382
revision_id = osutils.safe_revision_id(self.spec, warn=False)
383
return RevisionInfo.from_revision_id(branch, revision_id, revs)
336
revno = revs.index(self.spec) + 1
339
return RevisionInfo(branch, revno, self.spec)
385
341
SPEC_TYPES.append(RevisionSpec_revid)
388
344
class RevisionSpec_last(RevisionSpec):
389
"""Selects the nth revision from the end."""
391
help_txt = """Selects the nth revision from the end.
393
Supply a positive number to get the nth revision from the end.
394
This is the same as supplying negative numbers to the 'revno:' spec.
396
last:1 -> return the last revision
397
last:3 -> return the revision 2 before the end.
426
372
class RevisionSpec_before(RevisionSpec):
427
"""Selects the parent of the revision specified."""
429
help_txt = """Selects the parent of the revision specified.
431
Supply any revision spec to return the parent of that revision.
432
It is an error to request the parent of the null revision (before:0).
433
This is mostly useful when inspecting revisions that are not in the
434
revision history of a branch.
437
before:1913 -> Return the parent of revno 1913 (revno 1912)
438
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
440
bzr diff -r before:revid:aaaa..revid:aaaa
441
-> Find the changes between revision 'aaaa' and its parent.
442
(what changes did 'aaaa' introduce)
445
374
prefix = 'before:'
476
405
class RevisionSpec_tag(RevisionSpec):
477
"""Select a revision identified by tag name"""
479
help_txt = """Selects a revision identified by a tag name.
481
Tags are stored in the branch and created by the 'tag' command.
486
408
def _match_on(self, branch, revs):
487
# Can raise tags not supported, NoSuchTag, etc
488
return RevisionInfo.from_revision_id(branch,
489
branch.tags.lookup_tag(self.spec),
409
raise errors.InvalidRevisionSpec(self.user_spec, branch,
410
'tag: namespace registered,'
411
' but not implemented')
492
413
SPEC_TYPES.append(RevisionSpec_tag)
514
435
class RevisionSpec_date(RevisionSpec):
515
"""Selects a revision on the basis of a datestamp."""
517
help_txt = """Selects a revision on the basis of a datestamp.
519
Supply a datestamp to select the first revision that matches the date.
520
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
521
Matches the first entry after a given date (either at midnight or
522
at a specified time).
524
One way to display all the changes since yesterday would be:
525
bzr log -r date:yesterday..-1
528
date:yesterday -> select the first revision since yesterday
529
date:2006-08-14,17:10:14 -> select the first revision after
530
August 14th, 2006 at 5:10pm.
533
437
_date_re = re.compile(
534
438
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
539
443
def _match_on(self, branch, revs):
540
"""Spec for date revisions:
445
Spec for date revisions:
542
447
value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
543
448
matches the first entry after a given date (either at midnight or
544
449
at a specified time).
451
So the proper way of saying 'give me all entries for today' is:
452
-r date:yesterday..date:today
546
# XXX: This doesn't actually work
547
# So the proper way of saying 'give me all entries for today' is:
548
# -r date:yesterday..date:today
549
454
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
550
455
if self.spec.lower() == 'yesterday':
551
456
dt = today - datetime.timedelta(days=1)
600
505
class RevisionSpec_ancestor(RevisionSpec):
601
"""Selects a common ancestor with a second branch."""
603
help_txt = """Selects a common ancestor with a second branch.
605
Supply the path to a branch to select the common ancestor.
607
The common ancestor is the last revision that existed in both
608
branches. Usually this is the branch point, but it could also be
609
a revision that was merged.
611
This is frequently used with 'diff' to return all of the changes
612
that your branch introduces, while excluding the changes that you
613
have not merged from the remote branch.
616
ancestor:/path/to/branch
617
$ bzr diff -r ancestor:../../mainline/branch
619
506
prefix = 'ancestor:'
621
508
def _match_on(self, branch, revs):
509
from bzrlib.branch import Branch
622
511
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
623
return self._find_revision_info(branch, self.spec)
626
def _find_revision_info(branch, other_location):
627
from bzrlib.branch import Branch
629
other_branch = Branch.open(other_location)
512
other_branch = Branch.open(self.spec)
630
513
revision_a = branch.last_revision()
631
514
revision_b = other_branch.last_revision()
632
515
for r, b in ((revision_a, branch), (revision_b, other_branch)):
634
517
raise errors.NoCommits(b)
635
518
revision_source = revision.MultipleRevisionSources(
636
519
branch.repository, other_branch.repository)
637
graph = branch.repository.get_graph(other_branch.repository)
638
revision_a = revision.ensure_null(revision_a)
639
revision_b = revision.ensure_null(revision_b)
640
if revision.NULL_REVISION in (revision_a, revision_b):
641
rev_id = revision.NULL_REVISION
643
rev_id = graph.find_unique_lca(revision_a, revision_b)
644
if rev_id == revision.NULL_REVISION:
645
raise errors.NoCommonAncestor(revision_a, revision_b)
520
rev_id = revision.common_ancestor(revision_a, revision_b,
647
523
revno = branch.revision_id_to_revno(rev_id)
648
524
except errors.NoSuchRevision:
650
526
return RevisionInfo(branch, revno, rev_id)
653
528
SPEC_TYPES.append(RevisionSpec_ancestor)
656
531
class RevisionSpec_branch(RevisionSpec):
657
"""Selects the last revision of a specified branch."""
659
help_txt = """Selects the last revision of a specified branch.
661
Supply the path to a branch to select its last revision.
664
branch:/path/to/branch
532
"""A branch: revision specifier.
534
This takes the path to a branch and returns its tip revision id.
666
536
prefix = 'branch:'
680
550
return RevisionInfo(branch, revno, revision_b)
682
552
SPEC_TYPES.append(RevisionSpec_branch)
685
class RevisionSpec_submit(RevisionSpec_ancestor):
686
"""Selects a common ancestor with a submit branch."""
688
help_txt = """Selects a common ancestor with the submit branch.
690
Diffing against this shows all the changes that were made in this branch,
691
and is a good predictor of what merge will do. The submit branch is
692
used by the bundle and merge directive comands. If no submit branch
693
is specified, the parent branch is used instead.
695
The common ancestor is the last revision that existed in both
696
branches. Usually this is the branch point, but it could also be
697
a revision that was merged.
700
$ bzr diff -r submit:
705
def _match_on(self, branch, revs):
706
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
707
submit_location = branch.get_submit_branch()
708
location_type = 'submit branch'
709
if submit_location is None:
710
submit_location = branch.get_parent()
711
location_type = 'parent branch'
712
if submit_location is None:
713
raise errors.NoSubmitBranch(branch)
714
trace.note('Using %s %s', location_type, submit_location)
715
return self._find_revision_info(branch, submit_location)
718
SPEC_TYPES.append(RevisionSpec_submit)