141
wants_revision_history = True
144
def from_string(spec):
145
"""Parse a revision spec string into a RevisionSpec object.
147
:param spec: A string specified by the user
148
:return: A RevisionSpec object that understands how to parse the
108
def __new__(cls, spec, foo=_marker):
109
"""Parse a revision specifier.
151
if not isinstance(spec, (type(None), basestring)):
152
raise TypeError('error')
155
return RevisionSpec(None, _internal=True)
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)
112
return object.__new__(RevisionSpec, spec)
119
if isinstance(spec, int):
120
return object.__new__(RevisionSpec_int, spec)
121
elif isinstance(spec, basestring):
163
122
for spectype in SPEC_TYPES:
164
123
if spec.startswith(spectype.prefix):
165
trace.mutter('Returning RevisionSpec %s for %s',
166
spectype.__name__, spec)
167
return spectype(spec, _internal=True)
168
# RevisionSpec_revno is special cased, because it is the only
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()
173
if _revno_regex is None:
174
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
175
if _revno_regex.match(spec) is not None:
176
return RevisionSpec_revno(spec, _internal=True)
178
raise errors.NoSuchRevisionSpec(spec)
180
def __init__(self, spec, _internal=False):
181
"""Create a RevisionSpec referring to the Null revision.
183
:param spec: The original spec supplied by the user
184
:param _internal: Used to ensure that RevisionSpec is not being
185
called directly. Only from RevisionSpec.from_string()
188
symbol_versioning.warn('Creating a RevisionSpec directly has'
189
' been deprecated in version 0.11. Use'
190
' RevisionSpec.from_string()'
192
DeprecationWarning, stacklevel=2)
193
self.user_spec = spec
124
return object.__new__(spectype, spec)
126
raise BzrError('No namespace registered for string: %r' %
129
raise TypeError('Unhandled revision type %s' % spec)
131
def __init__(self, spec):
194
132
if self.prefix and spec.startswith(self.prefix):
195
133
spec = spec[len(self.prefix):]
198
136
def _match_on(self, branch, revs):
199
trace.mutter('Returning RevisionSpec._match_on: None')
200
return RevisionInfo(branch, None, None)
137
return RevisionInfo(branch, 0, None)
202
139
def _match_on_and_check(self, branch, revs):
203
140
info = self._match_on(branch, revs)
206
elif info == (None, None):
207
# special case - nothing supplied
143
elif info == (0, None):
144
# special case - the empty tree
209
146
elif self.prefix:
210
raise errors.InvalidRevisionSpec(self.user_spec, branch)
147
raise NoSuchRevision(branch, self.prefix + str(self.spec))
212
raise errors.InvalidRevisionSpec(self.spec, branch)
149
raise NoSuchRevision(branch, str(self.spec))
214
151
def in_history(self, branch):
216
if self.wants_revision_history:
217
revs = branch.revision_history()
221
# this should never trigger.
222
# TODO: make it a deprecated code path. RBC 20060928
152
revs = branch.revision_history()
224
153
return self._match_on_and_check(branch, revs)
226
155
# FIXME: in_history is somewhat broken,
232
161
# will do what you expect.
233
162
in_store = in_history
234
163
in_branch = in_store
236
def as_revision_id(self, context_branch):
237
"""Return just the revision_id for this revisions spec.
239
Some revision specs require a context_branch to be able to determine
240
their value. Not all specs will make use of it.
242
return self._as_revision_id(context_branch)
244
def _as_revision_id(self, context_branch):
245
"""Implementation of as_revision_id()
247
Classes should override this function to provide appropriate
248
functionality. The default is to just call '.in_history().rev_id'
250
return self.in_history(context_branch).rev_id
252
def as_tree(self, context_branch):
253
"""Return the tree object for this revisions spec.
255
Some revision specs require a context_branch to be able to determine
256
the revision id and access the repository. Not all specs will make
259
return self._as_tree(context_branch)
261
def _as_tree(self, context_branch):
262
"""Implementation of as_tree().
264
Classes should override this function to provide appropriate
265
functionality. The default is to just call '.as_revision_id()'
266
and get the revision tree from context_branch's repository.
268
revision_id = self.as_revision_id(context_branch)
269
return context_branch.repository.revision_tree(revision_id)
271
165
def __repr__(self):
272
166
# this is mostly for helping with testing
273
return '<%s %s>' % (self.__class__.__name__,
276
def needs_branch(self):
277
"""Whether this revision spec needs a branch.
279
Set this to False the branch argument of _match_on is not used.
283
def get_branch(self):
284
"""When the revision specifier contains a branch location, return it.
286
Otherwise, return None.
167
return '<%s %s%s>' % (self.__class__.__name__,
174
class RevisionSpec_int(RevisionSpec):
175
"""Spec is a number. Special case."""
176
def __init__(self, spec):
177
self.spec = int(spec)
179
def _match_on(self, branch, revs):
181
revno = len(revs) + self.spec + 1
184
rev_id = branch.get_rev_id(revno, revs)
185
return RevisionInfo(branch, revno, rev_id)
293
188
class RevisionSpec_revno(RevisionSpec):
294
"""Selects a revision using a number."""
296
help_txt = """Selects a revision using a number.
298
Use an integer to specify a revision in the history of the branch.
299
Optionally a branch can be specified. The 'revno:' prefix is optional.
300
A negative number will count from the end of the branch (-1 is the
301
last revision, -2 the previous one). If the negative number is larger
302
than the branch's history, the first revision is returned.
305
revno:1 -> return the first revision of this branch
306
revno:3:/path/to/branch -> return the 3rd revision of
307
the branch '/path/to/branch'
308
revno:-1 -> The last revision in a branch.
309
-2:http://other/branch -> The second to last revision in the
311
-1000000 -> Most likely the first revision, unless
312
your history is very long.
314
189
prefix = 'revno:'
315
wants_revision_history = False
317
191
def _match_on(self, branch, revs):
318
192
"""Lookup a revision by revision number"""
319
branch, revno, revision_id = self._lookup(branch, revs)
320
return RevisionInfo(branch, revno, revision_id)
322
def _lookup(self, branch, revs_or_none):
323
loc = self.spec.find(':')
325
revno_spec = self.spec
328
revno_spec = self.spec[:loc]
329
branch_spec = self.spec[loc+1:]
333
raise errors.InvalidRevisionSpec(self.user_spec,
334
branch, 'cannot have an empty revno and no branch')
338
revno = int(revno_spec)
341
# dotted decimal. This arguably should not be here
342
# but the from_string method is a little primitive
343
# right now - RBC 20060928
345
match_revno = tuple((int(number) for number in revno_spec.split('.')))
346
except ValueError, e:
347
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
352
# the user has override the branch to look in.
353
# we need to refresh the revision_history map and
355
from bzrlib.branch import Branch
356
branch = Branch.open(branch_spec)
361
revision_id = branch.dotted_revno_to_revision_id(match_revno,
363
except errors.NoSuchRevision:
364
raise errors.InvalidRevisionSpec(self.user_spec, branch)
366
# there is no traditional 'revno' for dotted-decimal revnos.
367
# so for API compatability we return None.
368
return branch, None, revision_id
370
last_revno, last_revision_id = branch.last_revision_info()
372
# if get_rev_id supported negative revnos, there would not be a
373
# need for this special case.
374
if (-revno) >= last_revno:
377
revno = last_revno + revno + 1
379
revision_id = branch.get_rev_id(revno, revs_or_none)
380
except errors.NoSuchRevision:
381
raise errors.InvalidRevisionSpec(self.user_spec, branch)
382
return branch, revno, revision_id
384
def _as_revision_id(self, context_branch):
385
# We would have the revno here, but we don't really care
386
branch, revno, revision_id = self._lookup(context_branch, None)
389
def needs_branch(self):
390
return self.spec.find(':') == -1
392
def get_branch(self):
393
if self.spec.find(':') == -1:
396
return self.spec[self.spec.find(':')+1:]
399
RevisionSpec_int = RevisionSpec_revno
194
return RevisionInfo(branch, int(self.spec))
196
return RevisionInfo(branch, None)
198
SPEC_TYPES.append(RevisionSpec_revno)
403
201
class RevisionSpec_revid(RevisionSpec):
404
"""Selects a revision using the revision id."""
406
help_txt = """Selects a revision using the revision id.
408
Supply a specific revision id, that can be used to specify any
409
revision id in the ancestry of the branch.
410
Including merges, and pending merges.
413
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
416
202
prefix = 'revid:'
418
204
def _match_on(self, branch, revs):
419
# self.spec comes straight from parsing the command line arguments,
420
# so we expect it to be a Unicode string. Switch it to the internal
422
revision_id = osutils.safe_revision_id(self.spec, warn=False)
423
return RevisionInfo.from_revision_id(branch, revision_id, revs)
425
def _as_revision_id(self, context_branch):
426
return osutils.safe_revision_id(self.spec, warn=False)
206
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
208
return RevisionInfo(branch, None, self.spec)
210
SPEC_TYPES.append(RevisionSpec_revid)
430
213
class RevisionSpec_last(RevisionSpec):
431
"""Selects the nth revision from the end."""
433
help_txt = """Selects the nth revision from the end.
435
Supply a positive number to get the nth revision from the end.
436
This is the same as supplying negative numbers to the 'revno:' spec.
439
last:1 -> return the last revision
440
last:3 -> return the revision 2 before the end.
445
217
def _match_on(self, branch, revs):
446
revno, revision_id = self._revno_and_revision_id(branch, revs)
447
return RevisionInfo(branch, revno, revision_id)
449
def _revno_and_revision_id(self, context_branch, revs_or_none):
450
last_revno, last_revision_id = context_branch.last_revision_info()
454
raise errors.NoCommits(context_branch)
455
return last_revno, last_revision_id
458
219
offset = int(self.spec)
459
except ValueError, e:
460
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
463
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
464
'you must supply a positive value')
466
revno = last_revno - offset + 1
468
revision_id = context_branch.get_rev_id(revno, revs_or_none)
469
except errors.NoSuchRevision:
470
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
471
return revno, revision_id
473
def _as_revision_id(self, context_branch):
474
# We compute the revno as part of the process, but we don't really care
476
revno, revision_id = self._revno_and_revision_id(context_branch, None)
221
return RevisionInfo(branch, None)
224
raise BzrError('You must supply a positive value for --revision last:XXX')
225
return RevisionInfo(branch, len(revs) - offset + 1)
227
SPEC_TYPES.append(RevisionSpec_last)
481
230
class RevisionSpec_before(RevisionSpec):
482
"""Selects the parent of the revision specified."""
484
help_txt = """Selects the parent of the revision specified.
486
Supply any revision spec to return the parent of that revision. This is
487
mostly useful when inspecting revisions that are not in the revision history
490
It is an error to request the parent of the null revision (before:0).
494
before:1913 -> Return the parent of revno 1913 (revno 1912)
495
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
497
bzr diff -r before:1913..1913
498
-> Find the changes between revision 1913 and its parent (1912).
499
(What changes did revision 1913 introduce).
500
This is equivalent to: bzr diff -c 1913
503
232
prefix = 'before:'
505
234
def _match_on(self, branch, revs):
506
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
508
raise errors.InvalidRevisionSpec(self.user_spec, branch,
509
'cannot go before the null: revision')
511
# We need to use the repository history here
512
rev = branch.repository.get_revision(r.rev_id)
513
if not rev.parent_ids:
515
revision_id = revision.NULL_REVISION
517
revision_id = rev.parent_ids[0]
519
revno = revs.index(revision_id) + 1
525
revision_id = branch.get_rev_id(revno, revs)
526
except errors.NoSuchRevision:
527
raise errors.InvalidRevisionSpec(self.user_spec,
529
return RevisionInfo(branch, revno, revision_id)
531
def _as_revision_id(self, context_branch):
532
base_revspec = RevisionSpec.from_string(self.spec)
533
base_revision_id = base_revspec.as_revision_id(context_branch)
534
if base_revision_id == revision.NULL_REVISION:
535
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
536
'cannot go before the null: revision')
537
context_repo = context_branch.repository
538
context_repo.lock_read()
540
parent_map = context_repo.get_parent_map([base_revision_id])
542
context_repo.unlock()
543
if base_revision_id not in parent_map:
544
# Ghost, or unknown revision id
545
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
546
'cannot find the matching revision')
547
parents = parent_map[base_revision_id]
549
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
550
'No parents for revision.')
235
r = RevisionSpec(self.spec)._match_on(branch, revs)
236
if (r.revno is None) or (r.revno == 0):
238
return RevisionInfo(branch, r.revno - 1)
240
SPEC_TYPES.append(RevisionSpec_before)
555
243
class RevisionSpec_tag(RevisionSpec):
556
"""Select a revision identified by tag name"""
558
help_txt = """Selects a revision identified by a tag name.
560
Tags are stored in the branch and created by the 'tag' command.
565
246
def _match_on(self, branch, revs):
566
# Can raise tags not supported, NoSuchTag, etc
567
return RevisionInfo.from_revision_id(branch,
568
branch.tags.lookup_tag(self.spec),
571
def _as_revision_id(self, context_branch):
572
return context_branch.tags.lookup_tag(self.spec)
576
class _RevListToTimestamps(object):
577
"""This takes a list of revisions, and allows you to bisect by date"""
579
__slots__ = ['revs', 'branch']
247
raise BzrError('tag: namespace registered, but not implemented.')
249
SPEC_TYPES.append(RevisionSpec_tag)
252
class RevisionSpec_revs:
581
253
def __init__(self, revs, branch):
583
255
self.branch = branch
585
256
def __getitem__(self, index):
586
"""Get the date of the index'd item"""
587
257
r = self.branch.repository.get_revision(self.revs[index])
588
258
# TODO: Handle timezone.
589
259
return datetime.datetime.fromtimestamp(r.timestamp)
591
260
def __len__(self):
592
261
return len(self.revs)
595
264
class RevisionSpec_date(RevisionSpec):
596
"""Selects a revision on the basis of a datestamp."""
598
help_txt = """Selects a revision on the basis of a datestamp.
600
Supply a datestamp to select the first revision that matches the date.
601
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
602
Matches the first entry after a given date (either at midnight or
603
at a specified time).
605
One way to display all the changes since yesterday would be::
607
bzr log -r date:yesterday..
611
date:yesterday -> select the first revision since yesterday
612
date:2006-08-14,17:10:14 -> select the first revision after
613
August 14th, 2006 at 5:10pm.
616
266
_date_re = re.compile(
617
267
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
640
291
m = self._date_re.match(self.spec)
641
292
if not m or (not m.group('date') and not m.group('time')):
642
raise errors.InvalidRevisionSpec(self.user_spec,
643
branch, 'invalid date')
647
year = int(m.group('year'))
648
month = int(m.group('month'))
649
day = int(m.group('day'))
656
hour = int(m.group('hour'))
657
minute = int(m.group('minute'))
658
if m.group('second'):
659
second = int(m.group('second'))
663
hour, minute, second = 0,0,0
665
raise errors.InvalidRevisionSpec(self.user_spec,
666
branch, 'invalid date')
293
raise BzrError('Invalid revision date %r' % self.spec)
296
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
298
year, month, day = today.year, today.month, today.day
300
hour = int(m.group('hour'))
301
minute = int(m.group('minute'))
302
if m.group('second'):
303
second = int(m.group('second'))
307
hour, minute, second = 0,0,0
668
309
dt = datetime.datetime(year=year, month=month, day=day,
669
310
hour=hour, minute=minute, second=second)
670
311
branch.lock_read()
672
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
313
rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
675
316
if rev == len(revs):
676
raise errors.InvalidRevisionSpec(self.user_spec, branch)
317
return RevisionInfo(branch, None)
678
319
return RevisionInfo(branch, rev + 1)
321
SPEC_TYPES.append(RevisionSpec_date)
682
324
class RevisionSpec_ancestor(RevisionSpec):
683
"""Selects a common ancestor with a second branch."""
685
help_txt = """Selects a common ancestor with a second branch.
687
Supply the path to a branch to select the common ancestor.
689
The common ancestor is the last revision that existed in both
690
branches. Usually this is the branch point, but it could also be
691
a revision that was merged.
693
This is frequently used with 'diff' to return all of the changes
694
that your branch introduces, while excluding the changes that you
695
have not merged from the remote branch.
699
ancestor:/path/to/branch
700
$ bzr diff -r ancestor:../../mainline/branch
702
325
prefix = 'ancestor:'
704
327
def _match_on(self, branch, revs):
705
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
706
return self._find_revision_info(branch, self.spec)
708
def _as_revision_id(self, context_branch):
709
return self._find_revision_id(context_branch, self.spec)
712
def _find_revision_info(branch, other_location):
713
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
328
from branch import Branch
329
from revision import common_ancestor, MultipleRevisionSources
330
other_branch = Branch.open_containing(self.spec)[0]
331
revision_a = branch.last_revision()
332
revision_b = other_branch.last_revision()
333
for r, b in ((revision_a, branch), (revision_b, other_branch)):
336
revision_source = MultipleRevisionSources(branch.repository,
337
other_branch.repository)
338
rev_id = common_ancestor(revision_a, revision_b, revision_source)
716
revno = branch.revision_id_to_revno(revision_id)
717
except errors.NoSuchRevision:
340
revno = branch.revision_id_to_revno(rev_id)
341
except NoSuchRevision:
719
return RevisionInfo(branch, revno, revision_id)
722
def _find_revision_id(branch, other_location):
723
from bzrlib.branch import Branch
727
revision_a = revision.ensure_null(branch.last_revision())
728
if revision_a == revision.NULL_REVISION:
729
raise errors.NoCommits(branch)
730
if other_location == '':
731
other_location = branch.get_parent()
732
other_branch = Branch.open(other_location)
733
other_branch.lock_read()
735
revision_b = revision.ensure_null(other_branch.last_revision())
736
if revision_b == revision.NULL_REVISION:
737
raise errors.NoCommits(other_branch)
738
graph = branch.repository.get_graph(other_branch.repository)
739
rev_id = graph.find_unique_lca(revision_a, revision_b)
741
other_branch.unlock()
742
if rev_id == revision.NULL_REVISION:
743
raise errors.NoCommonAncestor(revision_a, revision_b)
343
return RevisionInfo(branch, revno, rev_id)
345
SPEC_TYPES.append(RevisionSpec_ancestor)
751
347
class RevisionSpec_branch(RevisionSpec):
752
"""Selects the last revision of a specified branch."""
754
help_txt = """Selects the last revision of a specified branch.
756
Supply the path to a branch to select its last revision.
760
branch:/path/to/branch
348
"""A branch: revision specifier.
350
This takes the path to a branch and returns its tip revision id.
762
352
prefix = 'branch:'
764
354
def _match_on(self, branch, revs):
765
from bzrlib.branch import Branch
766
other_branch = Branch.open(self.spec)
355
from branch import Branch
356
other_branch = Branch.open_containing(self.spec)[0]
767
357
revision_b = other_branch.last_revision()
768
if revision_b in (None, revision.NULL_REVISION):
769
raise errors.NoCommits(other_branch)
358
if revision_b is None:
359
raise NoCommits(other_branch)
770
360
# pull in the remote revisions so we can diff
771
361
branch.fetch(other_branch, revision_b)
773
363
revno = branch.revision_id_to_revno(revision_b)
774
except errors.NoSuchRevision:
364
except NoSuchRevision:
776
366
return RevisionInfo(branch, revno, revision_b)
778
def _as_revision_id(self, context_branch):
779
from bzrlib.branch import Branch
780
other_branch = Branch.open(self.spec)
781
last_revision = other_branch.last_revision()
782
last_revision = revision.ensure_null(last_revision)
783
context_branch.fetch(other_branch, last_revision)
784
if last_revision == revision.NULL_REVISION:
785
raise errors.NoCommits(other_branch)
788
def _as_tree(self, context_branch):
789
from bzrlib.branch import Branch
790
other_branch = Branch.open(self.spec)
791
last_revision = other_branch.last_revision()
792
last_revision = revision.ensure_null(last_revision)
793
if last_revision == revision.NULL_REVISION:
794
raise errors.NoCommits(other_branch)
795
return other_branch.repository.revision_tree(last_revision)
799
class RevisionSpec_submit(RevisionSpec_ancestor):
800
"""Selects a common ancestor with a submit branch."""
802
help_txt = """Selects a common ancestor with the submit branch.
804
Diffing against this shows all the changes that were made in this branch,
805
and is a good predictor of what merge will do. The submit branch is
806
used by the bundle and merge directive commands. If no submit branch
807
is specified, the parent branch is used instead.
809
The common ancestor is the last revision that existed in both
810
branches. Usually this is the branch point, but it could also be
811
a revision that was merged.
815
$ bzr diff -r submit:
820
def _get_submit_location(self, branch):
821
submit_location = branch.get_submit_branch()
822
location_type = 'submit branch'
823
if submit_location is None:
824
submit_location = branch.get_parent()
825
location_type = 'parent branch'
826
if submit_location is None:
827
raise errors.NoSubmitBranch(branch)
828
trace.note('Using %s %s', location_type, submit_location)
829
return submit_location
831
def _match_on(self, branch, revs):
832
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
833
return self._find_revision_info(branch,
834
self._get_submit_location(branch))
836
def _as_revision_id(self, context_branch):
837
return self._find_revision_id(context_branch,
838
self._get_submit_location(context_branch))
841
revspec_registry = registry.Registry()
842
def _register_revspec(revspec):
843
revspec_registry.register(revspec.prefix, revspec)
845
_register_revspec(RevisionSpec_revno)
846
_register_revspec(RevisionSpec_revid)
847
_register_revspec(RevisionSpec_last)
848
_register_revspec(RevisionSpec_before)
849
_register_revspec(RevisionSpec_tag)
850
_register_revspec(RevisionSpec_date)
851
_register_revspec(RevisionSpec_ancestor)
852
_register_revspec(RevisionSpec_branch)
853
_register_revspec(RevisionSpec_submit)
855
SPEC_TYPES = symbol_versioning.deprecated_list(
856
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
368
SPEC_TYPES.append(RevisionSpec_branch)