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
107
def __new__(cls, spec, foo=_marker):
108
"""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)
111
return object.__new__(RevisionSpec, spec)
118
if isinstance(spec, int):
119
return object.__new__(RevisionSpec_int, spec)
120
elif isinstance(spec, basestring):
163
121
for spectype in SPEC_TYPES:
164
trace.mutter('Returning RevisionSpec %s for %s',
165
spectype.__name__, spec)
166
122
if spec.startswith(spectype.prefix):
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
# XXX: Update this after 0.10 is released
189
symbol_versioning.warn('Creating a RevisionSpec directly has'
190
' been deprecated in version 0.11. Use'
191
' RevisionSpec.from_string()'
193
DeprecationWarning, stacklevel=2)
194
self.user_spec = spec
123
return object.__new__(spectype, spec)
125
raise BzrError('No namespace registered for string: %r' %
128
raise TypeError('Unhandled revision type %s' % spec)
130
def __init__(self, spec):
195
131
if self.prefix and spec.startswith(self.prefix):
196
132
spec = spec[len(self.prefix):]
199
135
def _match_on(self, branch, revs):
200
trace.mutter('Returning RevisionSpec._match_on: None')
201
return RevisionInfo(branch, None, None)
136
return RevisionInfo(branch, 0, None)
203
138
def _match_on_and_check(self, branch, revs):
204
139
info = self._match_on(branch, revs)
207
elif info == (None, None):
208
# special case - nothing supplied
142
elif info == (0, None):
143
# special case - the empty tree
210
145
elif self.prefix:
211
raise errors.InvalidRevisionSpec(self.user_spec, branch)
146
raise NoSuchRevision(branch, self.prefix + str(self.spec))
213
raise errors.InvalidRevisionSpec(self.spec, branch)
148
raise NoSuchRevision(branch, str(self.spec))
215
150
def in_history(self, branch):
217
if self.wants_revision_history:
218
revs = branch.revision_history()
222
# this should never trigger.
223
# TODO: make it a deprecated code path. RBC 20060928
151
revs = branch.revision_history()
225
152
return self._match_on_and_check(branch, revs)
227
154
# FIXME: in_history is somewhat broken,
233
160
# will do what you expect.
234
161
in_store = in_history
235
162
in_branch = in_store
237
def as_revision_id(self, context_branch):
238
"""Return just the revision_id for this revisions spec.
240
Some revision specs require a context_branch to be able to determine
241
their value. Not all specs will make use of it.
243
return self._as_revision_id(context_branch)
245
def _as_revision_id(self, context_branch):
246
"""Implementation of as_revision_id()
248
Classes should override this function to provide appropriate
249
functionality. The default is to just call '.in_history().rev_id'
251
return self.in_history(context_branch).rev_id
253
def as_tree(self, context_branch):
254
"""Return the tree object for this revisions spec.
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
260
return self._as_tree(context_branch)
262
def _as_tree(self, context_branch):
263
"""Implementation of as_tree().
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.
269
revision_id = self.as_revision_id(context_branch)
270
return context_branch.repository.revision_tree(revision_id)
272
164
def __repr__(self):
273
165
# this is mostly for helping with testing
274
return '<%s %s>' % (self.__class__.__name__,
277
def needs_branch(self):
278
"""Whether this revision spec needs a branch.
280
Set this to False the branch argument of _match_on is not used.
284
def get_branch(self):
285
"""When the revision specifier contains a branch location, return it.
287
Otherwise, return None.
166
return '<%s %s%s>' % (self.__class__.__name__,
173
class RevisionSpec_int(RevisionSpec):
174
"""Spec is a number. Special case."""
175
def __init__(self, spec):
176
self.spec = int(spec)
178
def _match_on(self, branch, revs):
180
revno = len(revs) + self.spec + 1
183
rev_id = branch.get_rev_id(revno, revs)
184
return RevisionInfo(branch, revno, rev_id)
294
187
class RevisionSpec_revno(RevisionSpec):
295
"""Selects a revision using a number."""
297
help_txt = """Selects a revision using a number.
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.
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
312
-1000000 -> Most likely the first revision, unless
313
your history is very long.
315
188
prefix = 'revno:'
316
wants_revision_history = False
318
190
def _match_on(self, branch, revs):
319
191
"""Lookup a revision by revision number"""
320
branch, revno, revision_id = self._lookup(branch, revs)
321
return RevisionInfo(branch, revno, revision_id)
323
def _lookup(self, branch, revs_or_none):
324
loc = self.spec.find(':')
326
revno_spec = self.spec
329
revno_spec = self.spec[:loc]
330
branch_spec = self.spec[loc+1:]
334
raise errors.InvalidRevisionSpec(self.user_spec,
335
branch, 'cannot have an empty revno and no branch')
339
revno = int(revno_spec)
342
# dotted decimal. This arguably should not be here
343
# but the from_string method is a little primitive
344
# right now - RBC 20060928
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)
353
# the user has override the branch to look in.
354
# we need to refresh the revision_history map and
356
from bzrlib.branch import Branch
357
branch = Branch.open(branch_spec)
362
revision_id = branch.dotted_revno_to_revision_id(match_revno,
364
except errors.NoSuchRevision:
365
raise errors.InvalidRevisionSpec(self.user_spec, branch)
367
# there is no traditional 'revno' for dotted-decimal revnos.
368
# so for API compatability we return None.
369
return branch, None, revision_id
371
last_revno, last_revision_id = branch.last_revision_info()
373
# if get_rev_id supported negative revnos, there would not be a
374
# need for this special case.
375
if (-revno) >= last_revno:
378
revno = last_revno + revno + 1
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
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)
390
def needs_branch(self):
391
return self.spec.find(':') == -1
393
def get_branch(self):
394
if self.spec.find(':') == -1:
397
return self.spec[self.spec.find(':')+1:]
400
RevisionSpec_int = RevisionSpec_revno
193
return RevisionInfo(branch, int(self.spec))
195
return RevisionInfo(branch, None)
197
SPEC_TYPES.append(RevisionSpec_revno)
404
200
class RevisionSpec_revid(RevisionSpec):
405
"""Selects a revision using the revision id."""
407
help_txt = """Selects a revision using the revision id.
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.
414
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
417
201
prefix = 'revid:'
419
203
def _match_on(self, branch, revs):
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
423
revision_id = osutils.safe_revision_id(self.spec, warn=False)
424
return RevisionInfo.from_revision_id(branch, revision_id, revs)
426
def _as_revision_id(self, context_branch):
427
return osutils.safe_revision_id(self.spec, warn=False)
205
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
207
return RevisionInfo(branch, None, self.spec)
209
SPEC_TYPES.append(RevisionSpec_revid)
431
212
class RevisionSpec_last(RevisionSpec):
432
"""Selects the nth revision from the end."""
434
help_txt = """Selects the nth revision from the end.
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.
440
last:1 -> return the last revision
441
last:3 -> return the revision 2 before the end.
446
216
def _match_on(self, branch, revs):
447
revno, revision_id = self._revno_and_revision_id(branch, revs)
448
return RevisionInfo(branch, revno, revision_id)
450
def _revno_and_revision_id(self, context_branch, revs_or_none):
451
last_revno, last_revision_id = context_branch.last_revision_info()
455
raise errors.NoCommits(context_branch)
456
return last_revno, last_revision_id
459
218
offset = int(self.spec)
460
except ValueError, e:
461
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
464
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
465
'you must supply a positive value')
467
revno = last_revno - offset + 1
469
revision_id = context_branch.get_rev_id(revno, revs_or_none)
470
except errors.NoSuchRevision:
471
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
472
return revno, revision_id
474
def _as_revision_id(self, context_branch):
475
# We compute the revno as part of the process, but we don't really care
477
revno, revision_id = self._revno_and_revision_id(context_branch, None)
220
return RevisionInfo(branch, None)
223
raise BzrError('You must supply a positive value for --revision last:XXX')
224
return RevisionInfo(branch, len(revs) - offset + 1)
226
SPEC_TYPES.append(RevisionSpec_last)
482
229
class RevisionSpec_before(RevisionSpec):
483
"""Selects the parent of the revision specified."""
485
help_txt = """Selects the parent of the revision specified.
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
491
It is an error to request the parent of the null revision (before:0).
495
before:1913 -> Return the parent of revno 1913 (revno 1912)
496
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
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
504
231
prefix = 'before:'
506
233
def _match_on(self, branch, revs):
507
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
509
raise errors.InvalidRevisionSpec(self.user_spec, branch,
510
'cannot go before the null: revision')
512
# We need to use the repository history here
513
rev = branch.repository.get_revision(r.rev_id)
514
if not rev.parent_ids:
516
revision_id = revision.NULL_REVISION
518
revision_id = rev.parent_ids[0]
520
revno = revs.index(revision_id) + 1
526
revision_id = branch.get_rev_id(revno, revs)
527
except errors.NoSuchRevision:
528
raise errors.InvalidRevisionSpec(self.user_spec,
530
return RevisionInfo(branch, revno, revision_id)
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()
541
parent_map = context_repo.get_parent_map([base_revision_id])
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]
550
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
551
'No parents for revision.')
234
r = RevisionSpec(self.spec)._match_on(branch, revs)
235
if (r.revno is None) or (r.revno == 0):
237
return RevisionInfo(branch, r.revno - 1)
239
SPEC_TYPES.append(RevisionSpec_before)
556
242
class RevisionSpec_tag(RevisionSpec):
557
"""Select a revision identified by tag name"""
559
help_txt = """Selects a revision identified by a tag name.
561
Tags are stored in the branch and created by the 'tag' command.
566
245
def _match_on(self, branch, revs):
567
# Can raise tags not supported, NoSuchTag, etc
568
return RevisionInfo.from_revision_id(branch,
569
branch.tags.lookup_tag(self.spec),
572
def _as_revision_id(self, context_branch):
573
return context_branch.tags.lookup_tag(self.spec)
577
class _RevListToTimestamps(object):
578
"""This takes a list of revisions, and allows you to bisect by date"""
580
__slots__ = ['revs', 'branch']
582
def __init__(self, revs, branch):
586
def __getitem__(self, index):
587
"""Get the date of the index'd item"""
588
r = self.branch.repository.get_revision(self.revs[index])
589
# TODO: Handle timezone.
590
return datetime.datetime.fromtimestamp(r.timestamp)
593
return len(self.revs)
246
raise BzrError('tag: namespace registered, but not implemented.')
248
SPEC_TYPES.append(RevisionSpec_tag)
596
251
class RevisionSpec_date(RevisionSpec):
597
"""Selects a revision on the basis of a datestamp."""
599
help_txt = """Selects a revision on the basis of a datestamp.
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).
606
One way to display all the changes since yesterday would be::
608
bzr log -r date:yesterday..
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.
617
253
_date_re = re.compile(
618
254
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
641
278
m = self._date_re.match(self.spec)
642
279
if not m or (not m.group('date') and not m.group('time')):
643
raise errors.InvalidRevisionSpec(self.user_spec,
644
branch, 'invalid date')
648
year = int(m.group('year'))
649
month = int(m.group('month'))
650
day = int(m.group('day'))
657
hour = int(m.group('hour'))
658
minute = int(m.group('minute'))
659
if m.group('second'):
660
second = int(m.group('second'))
664
hour, minute, second = 0,0,0
666
raise errors.InvalidRevisionSpec(self.user_spec,
667
branch, 'invalid date')
280
raise BzrError('Invalid revision date %r' % self.spec)
283
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
285
year, month, day = today.year, today.month, today.day
287
hour = int(m.group('hour'))
288
minute = int(m.group('minute'))
289
if m.group('second'):
290
second = int(m.group('second'))
294
hour, minute, second = 0,0,0
669
296
dt = datetime.datetime(year=year, month=month, day=day,
670
297
hour=hour, minute=minute, second=second)
673
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
677
raise errors.InvalidRevisionSpec(self.user_spec, branch)
679
return RevisionInfo(branch, rev + 1)
299
for i in range(len(revs)):
300
r = branch.repository.get_revision(revs[i])
301
# TODO: Handle timezone.
302
dt = datetime.datetime.fromtimestamp(r.timestamp)
304
return RevisionInfo(branch, i+1)
305
return RevisionInfo(branch, None)
307
SPEC_TYPES.append(RevisionSpec_date)
683
310
class RevisionSpec_ancestor(RevisionSpec):
684
"""Selects a common ancestor with a second branch."""
686
help_txt = """Selects a common ancestor with a second branch.
688
Supply the path to a branch to select the common ancestor.
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.
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.
700
ancestor:/path/to/branch
701
$ bzr diff -r ancestor:../../mainline/branch
703
311
prefix = 'ancestor:'
705
313
def _match_on(self, branch, revs):
706
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
707
return self._find_revision_info(branch, self.spec)
709
def _as_revision_id(self, context_branch):
710
return self._find_revision_id(context_branch, self.spec)
713
def _find_revision_info(branch, other_location):
714
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
314
from branch import Branch
315
from revision import common_ancestor, MultipleRevisionSources
316
other_branch = Branch.open_containing(self.spec)[0]
317
revision_a = branch.last_revision()
318
revision_b = other_branch.last_revision()
319
for r, b in ((revision_a, branch), (revision_b, other_branch)):
322
revision_source = MultipleRevisionSources(branch.repository,
323
other_branch.repository)
324
rev_id = common_ancestor(revision_a, revision_b, revision_source)
717
revno = branch.revision_id_to_revno(revision_id)
718
except errors.NoSuchRevision:
326
revno = branch.revision_id_to_revno(rev_id)
327
except NoSuchRevision:
720
return RevisionInfo(branch, revno, revision_id)
723
def _find_revision_id(branch, other_location):
724
from bzrlib.branch import Branch
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()
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)
742
other_branch.unlock()
743
if rev_id == revision.NULL_REVISION:
744
raise errors.NoCommonAncestor(revision_a, revision_b)
329
return RevisionInfo(branch, revno, rev_id)
331
SPEC_TYPES.append(RevisionSpec_ancestor)
752
333
class RevisionSpec_branch(RevisionSpec):
753
"""Selects the last revision of a specified branch."""
755
help_txt = """Selects the last revision of a specified branch.
757
Supply the path to a branch to select its last revision.
761
branch:/path/to/branch
334
"""A branch: revision specifier.
336
This takes the path to a branch and returns its tip revision id.
763
338
prefix = 'branch:'
765
340
def _match_on(self, branch, revs):
766
from bzrlib.branch import Branch
767
other_branch = Branch.open(self.spec)
341
from branch import Branch
342
from fetch import greedy_fetch
343
other_branch = Branch.open_containing(self.spec)[0]
768
344
revision_b = other_branch.last_revision()
769
if revision_b in (None, revision.NULL_REVISION):
770
raise errors.NoCommits(other_branch)
345
if revision_b is None:
346
raise NoCommits(other_branch)
771
347
# pull in the remote revisions so we can diff
772
branch.fetch(other_branch, revision_b)
348
greedy_fetch(branch, other_branch, revision=revision_b)
774
350
revno = branch.revision_id_to_revno(revision_b)
775
except errors.NoSuchRevision:
351
except NoSuchRevision:
777
353
return RevisionInfo(branch, revno, revision_b)
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)
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)
800
class RevisionSpec_submit(RevisionSpec_ancestor):
801
"""Selects a common ancestor with a submit branch."""
803
help_txt = """Selects a common ancestor with the submit branch.
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.
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.
816
$ bzr diff -r submit:
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
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))
837
def _as_revision_id(self, context_branch):
838
return self._find_revision_id(context_branch,
839
self._get_submit_location(context_branch))
842
revspec_registry = registry.Registry()
843
def _register_revspec(revspec):
844
revspec_registry.register(revspec.prefix, revspec)
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)
856
SPEC_TYPES = symbol_versioning.deprecated_list(
857
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
355
SPEC_TYPES.append(RevisionSpec_branch)