108
def __new__(cls, spec, foo=_marker):
109
"""Parse a revision specifier.
138
wants_revision_history = True
140
def __new__(cls, spec, _internal=False):
142
return object.__new__(cls, spec, _internal=_internal)
144
symbol_versioning.warn('Creating a RevisionSpec directly has'
145
' been deprecated in version 0.11. Use'
146
' RevisionSpec.from_string()'
148
DeprecationWarning, stacklevel=2)
149
return RevisionSpec.from_string(spec)
152
def from_string(spec):
153
"""Parse a revision spec string into a RevisionSpec object.
155
:param spec: A string specified by the user
156
:return: A RevisionSpec object that understands how to parse the
159
if not isinstance(spec, (type(None), basestring)):
160
raise TypeError('error')
112
return object.__new__(RevisionSpec, spec)
119
if isinstance(spec, int):
120
return object.__new__(RevisionSpec_int, spec)
121
elif isinstance(spec, basestring):
122
for spectype in SPEC_TYPES:
123
if spec.startswith(spectype.prefix):
124
return object.__new__(spectype, spec)
126
raise BzrError('No namespace registered for string: %r' %
163
return RevisionSpec(None, _internal=True)
164
for spectype in SPEC_TYPES:
165
if spec.startswith(spectype.prefix):
166
trace.mutter('Returning RevisionSpec %s for %s',
167
spectype.__name__, spec)
168
return spectype(spec, _internal=True)
129
raise TypeError('Unhandled revision type %s' % spec)
131
def __init__(self, spec):
170
# RevisionSpec_revno is special cased, because it is the only
171
# one that directly handles plain integers
172
# TODO: This should not be special cased rather it should be
173
# a method invocation on spectype.canparse()
175
if _revno_regex is None:
176
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
177
if _revno_regex.match(spec) is not None:
178
return RevisionSpec_revno(spec, _internal=True)
180
raise errors.NoSuchRevisionSpec(spec)
182
def __init__(self, spec, _internal=False):
183
"""Create a RevisionSpec referring to the Null revision.
185
:param spec: The original spec supplied by the user
186
:param _internal: Used to ensure that RevisionSpec is not being
187
called directly. Only from RevisionSpec.from_string()
190
# XXX: Update this after 0.10 is released
191
symbol_versioning.warn('Creating a RevisionSpec directly has'
192
' been deprecated in version 0.11. Use'
193
' RevisionSpec.from_string()'
195
DeprecationWarning, stacklevel=2)
196
self.user_spec = spec
132
197
if self.prefix and spec.startswith(self.prefix):
133
198
spec = spec[len(self.prefix):]
136
201
def _match_on(self, branch, revs):
137
return RevisionInfo(branch, 0, None)
202
trace.mutter('Returning RevisionSpec._match_on: None')
203
return RevisionInfo(branch, None, None)
139
205
def _match_on_and_check(self, branch, revs):
140
206
info = self._match_on(branch, revs)
143
elif info == (0, None):
144
# special case - the empty tree
209
elif info == (None, None):
210
# special case - nothing supplied
146
212
elif self.prefix:
147
raise NoSuchRevision(branch, self.prefix + str(self.spec))
213
raise errors.InvalidRevisionSpec(self.user_spec, branch)
149
raise NoSuchRevision(branch, str(self.spec))
215
raise errors.InvalidRevisionSpec(self.spec, branch)
151
217
def in_history(self, branch):
152
revs = branch.revision_history()
219
if self.wants_revision_history:
220
revs = branch.revision_history()
224
# this should never trigger.
225
# TODO: make it a deprecated code path. RBC 20060928
153
227
return self._match_on_and_check(branch, revs)
155
229
# FIXME: in_history is somewhat broken,
161
235
# will do what you expect.
162
236
in_store = in_history
163
237
in_branch = in_store
239
def as_revision_id(self, context_branch):
240
"""Return just the revision_id for this revisions spec.
242
Some revision specs require a context_branch to be able to determine
243
their value. Not all specs will make use of it.
245
return self._as_revision_id(context_branch)
247
def _as_revision_id(self, context_branch):
248
"""Implementation of as_revision_id()
250
Classes should override this function to provide appropriate
251
functionality. The default is to just call '.in_history().rev_id'
253
return self.in_history(context_branch).rev_id
165
255
def __repr__(self):
166
256
# this is mostly for helping with testing
167
return '<%s %s%s>' % (self.__class__.__name__,
257
return '<%s %s>' % (self.__class__.__name__,
260
def needs_branch(self):
261
"""Whether this revision spec needs a branch.
263
Set this to False the branch argument of _match_on is not used.
267
def get_branch(self):
268
"""When the revision specifier contains a branch location, return it.
270
Otherwise, return None.
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)
188
277
class RevisionSpec_revno(RevisionSpec):
278
"""Selects a revision using a number."""
280
help_txt = """Selects a revision using a number.
282
Use an integer to specify a revision in the history of the branch.
283
Optionally a branch can be specified. The 'revno:' prefix is optional.
284
A negative number will count from the end of the branch (-1 is the
285
last revision, -2 the previous one). If the negative number is larger
286
than the branch's history, the first revision is returned.
289
revno:1 -> return the first revision
290
revno:3:/path/to/branch -> return the 3rd revision of
291
the branch '/path/to/branch'
292
revno:-1 -> The last revision in a branch.
293
-2:http://other/branch -> The second to last revision in the
295
-1000000 -> Most likely the first revision, unless
296
your history is very long.
189
298
prefix = 'revno:'
299
wants_revision_history = False
191
301
def _match_on(self, branch, revs):
192
302
"""Lookup a revision by revision number"""
194
return RevisionInfo(branch, int(self.spec))
196
return RevisionInfo(branch, None)
303
branch, revno, revision_id = self._lookup(branch, revs)
304
return RevisionInfo(branch, revno, revision_id)
306
def _lookup(self, branch, revs_or_none):
307
loc = self.spec.find(':')
309
revno_spec = self.spec
312
revno_spec = self.spec[:loc]
313
branch_spec = self.spec[loc+1:]
317
raise errors.InvalidRevisionSpec(self.user_spec,
318
branch, 'cannot have an empty revno and no branch')
322
revno = int(revno_spec)
325
# dotted decimal. This arguably should not be here
326
# but the from_string method is a little primitive
327
# right now - RBC 20060928
329
match_revno = tuple((int(number) for number in revno_spec.split('.')))
330
except ValueError, e:
331
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
336
# the user has override the branch to look in.
337
# we need to refresh the revision_history map and
339
from bzrlib.branch import Branch
340
branch = Branch.open(branch_spec)
346
revision_id_to_revno = branch.get_revision_id_to_revno_map()
347
revisions = [revision_id for revision_id, revno
348
in revision_id_to_revno.iteritems()
349
if revno == match_revno]
352
if len(revisions) != 1:
353
return branch, None, None
355
# there is no traditional 'revno' for dotted-decimal revnos.
356
# so for API compatability we return None.
357
return branch, None, revisions[0]
359
last_revno, last_revision_id = branch.last_revision_info()
361
# if get_rev_id supported negative revnos, there would not be a
362
# need for this special case.
363
if (-revno) >= last_revno:
366
revno = last_revno + revno + 1
368
revision_id = branch.get_rev_id(revno, revs_or_none)
369
except errors.NoSuchRevision:
370
raise errors.InvalidRevisionSpec(self.user_spec, branch)
371
return branch, revno, revision_id
373
def _as_revision_id(self, context_branch):
374
# We would have the revno here, but we don't really care
375
branch, revno, revision_id = self._lookup(context_branch, None)
378
def needs_branch(self):
379
return self.spec.find(':') == -1
381
def get_branch(self):
382
if self.spec.find(':') == -1:
385
return self.spec[self.spec.find(':')+1:]
388
RevisionSpec_int = RevisionSpec_revno
198
390
SPEC_TYPES.append(RevisionSpec_revno)
201
393
class RevisionSpec_revid(RevisionSpec):
394
"""Selects a revision using the revision id."""
396
help_txt = """Selects a revision using the revision id.
398
Supply a specific revision id, that can be used to specify any
399
revision id in the ancestry of the branch.
400
Including merges, and pending merges.
403
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
202
406
prefix = 'revid:'
204
408
def _match_on(self, branch, revs):
206
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
208
return RevisionInfo(branch, None, self.spec)
409
# self.spec comes straight from parsing the command line arguments,
410
# so we expect it to be a Unicode string. Switch it to the internal
412
revision_id = osutils.safe_revision_id(self.spec, warn=False)
413
return RevisionInfo.from_revision_id(branch, revision_id, revs)
415
def _as_revision_id(self, context_branch):
416
return osutils.safe_revision_id(self.spec, warn=False)
210
418
SPEC_TYPES.append(RevisionSpec_revid)
213
421
class RevisionSpec_last(RevisionSpec):
422
"""Selects the nth revision from the end."""
424
help_txt = """Selects the nth revision from the end.
426
Supply a positive number to get the nth revision from the end.
427
This is the same as supplying negative numbers to the 'revno:' spec.
430
last:1 -> return the last revision
431
last:3 -> return the revision 2 before the end.
217
436
def _match_on(self, branch, revs):
437
revno, revision_id = self._revno_and_revision_id(branch, revs)
438
return RevisionInfo(branch, revno, revision_id)
440
def _revno_and_revision_id(self, context_branch, revs_or_none):
441
last_revno, last_revision_id = context_branch.last_revision_info()
445
raise errors.NoCommits(context_branch)
446
return last_revno, last_revision_id
219
449
offset = int(self.spec)
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)
450
except ValueError, e:
451
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
454
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
455
'you must supply a positive value')
457
revno = last_revno - offset + 1
459
revision_id = context_branch.get_rev_id(revno, revs_or_none)
460
except errors.NoSuchRevision:
461
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
462
return revno, revision_id
464
def _as_revision_id(self, context_branch):
465
# We compute the revno as part of the process, but we don't really care
467
revno, revision_id = self._revno_and_revision_id(context_branch, None)
227
470
SPEC_TYPES.append(RevisionSpec_last)
230
473
class RevisionSpec_before(RevisionSpec):
474
"""Selects the parent of the revision specified."""
476
help_txt = """Selects the parent of the revision specified.
478
Supply any revision spec to return the parent of that revision.
479
It is an error to request the parent of the null revision (before:0).
480
This is mostly useful when inspecting revisions that are not in the
481
revision history of a branch.
485
before:1913 -> Return the parent of revno 1913 (revno 1912)
486
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
488
bzr diff -r before:revid:aaaa..revid:aaaa
489
-> Find the changes between revision 'aaaa' and its parent.
490
(what changes did 'aaaa' introduce)
232
493
prefix = 'before:'
234
495
def _match_on(self, branch, revs):
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)
496
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
498
raise errors.InvalidRevisionSpec(self.user_spec, branch,
499
'cannot go before the null: revision')
501
# We need to use the repository history here
502
rev = branch.repository.get_revision(r.rev_id)
503
if not rev.parent_ids:
505
revision_id = revision.NULL_REVISION
507
revision_id = rev.parent_ids[0]
509
revno = revs.index(revision_id) + 1
515
revision_id = branch.get_rev_id(revno, revs)
516
except errors.NoSuchRevision:
517
raise errors.InvalidRevisionSpec(self.user_spec,
519
return RevisionInfo(branch, revno, revision_id)
521
def _as_revision_id(self, context_branch):
522
base_revspec = RevisionSpec.from_string(self.spec)
523
base_revision_id = base_revspec.as_revision_id(context_branch)
524
if base_revision_id == revision.NULL_REVISION:
525
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
526
'cannot go before the null: revision')
527
context_repo = context_branch.repository
528
context_repo.lock_read()
530
parent_map = context_repo.get_parent_map([base_revision_id])
532
context_repo.unlock()
533
if base_revision_id not in parent_map:
534
# Ghost, or unknown revision id
535
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
536
'cannot find the matching revision')
537
parents = parent_map[base_revision_id]
539
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
540
'No parents for revision.')
240
543
SPEC_TYPES.append(RevisionSpec_before)
243
546
class RevisionSpec_tag(RevisionSpec):
547
"""Select a revision identified by tag name"""
549
help_txt = """Selects a revision identified by a tag name.
551
Tags are stored in the branch and created by the 'tag' command.
246
556
def _match_on(self, branch, revs):
247
raise BzrError('tag: namespace registered, but not implemented.')
557
# Can raise tags not supported, NoSuchTag, etc
558
return RevisionInfo.from_revision_id(branch,
559
branch.tags.lookup_tag(self.spec),
562
def _as_revision_id(self, context_branch):
563
return context_branch.tags.lookup_tag(self.spec)
249
565
SPEC_TYPES.append(RevisionSpec_tag)
252
class RevisionSpec_revs:
568
class _RevListToTimestamps(object):
569
"""This takes a list of revisions, and allows you to bisect by date"""
571
__slots__ = ['revs', 'branch']
253
573
def __init__(self, revs, branch):
255
575
self.branch = branch
256
577
def __getitem__(self, index):
578
"""Get the date of the index'd item"""
257
579
r = self.branch.repository.get_revision(self.revs[index])
258
580
# TODO: Handle timezone.
259
581
return datetime.datetime.fromtimestamp(r.timestamp)
260
583
def __len__(self):
261
584
return len(self.revs)
264
587
class RevisionSpec_date(RevisionSpec):
588
"""Selects a revision on the basis of a datestamp."""
590
help_txt = """Selects a revision on the basis of a datestamp.
592
Supply a datestamp to select the first revision that matches the date.
593
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
594
Matches the first entry after a given date (either at midnight or
595
at a specified time).
597
One way to display all the changes since yesterday would be::
599
bzr log -r date:yesterday..
603
date:yesterday -> select the first revision since yesterday
604
date:2006-08-14,17:10:14 -> select the first revision after
605
August 14th, 2006 at 5:10pm.
266
608
_date_re = re.compile(
267
609
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
324
675
class RevisionSpec_ancestor(RevisionSpec):
676
"""Selects a common ancestor with a second branch."""
678
help_txt = """Selects a common ancestor with a second branch.
680
Supply the path to a branch to select the common ancestor.
682
The common ancestor is the last revision that existed in both
683
branches. Usually this is the branch point, but it could also be
684
a revision that was merged.
686
This is frequently used with 'diff' to return all of the changes
687
that your branch introduces, while excluding the changes that you
688
have not merged from the remote branch.
692
ancestor:/path/to/branch
693
$ bzr diff -r ancestor:../../mainline/branch
325
695
prefix = 'ancestor:'
327
697
def _match_on(self, branch, revs):
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)
698
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
699
return self._find_revision_info(branch, self.spec)
701
def _as_revision_id(self, context_branch):
702
return self._find_revision_id(context_branch, self.spec)
705
def _find_revision_info(branch, other_location):
706
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
340
revno = branch.revision_id_to_revno(rev_id)
341
except NoSuchRevision:
709
revno = branch.revision_id_to_revno(revision_id)
710
except errors.NoSuchRevision:
343
return RevisionInfo(branch, revno, rev_id)
712
return RevisionInfo(branch, revno, revision_id)
715
def _find_revision_id(branch, other_location):
716
from bzrlib.branch import Branch
720
revision_a = revision.ensure_null(branch.last_revision())
721
if revision_a == revision.NULL_REVISION:
722
raise errors.NoCommits(branch)
723
other_branch = Branch.open(other_location)
724
other_branch.lock_read()
726
revision_b = revision.ensure_null(other_branch.last_revision())
727
if revision_b == revision.NULL_REVISION:
728
raise errors.NoCommits(other_branch)
729
graph = branch.repository.get_graph(other_branch.repository)
730
rev_id = graph.find_unique_lca(revision_a, revision_b)
732
other_branch.unlock()
733
if rev_id == revision.NULL_REVISION:
734
raise errors.NoCommonAncestor(revision_a, revision_b)
345
740
SPEC_TYPES.append(RevisionSpec_ancestor)
347
743
class RevisionSpec_branch(RevisionSpec):
348
"""A branch: revision specifier.
350
This takes the path to a branch and returns its tip revision id.
744
"""Selects the last revision of a specified branch."""
746
help_txt = """Selects the last revision of a specified branch.
748
Supply the path to a branch to select its last revision.
752
branch:/path/to/branch
352
754
prefix = 'branch:'
354
756
def _match_on(self, branch, revs):
355
from branch import Branch
356
other_branch = Branch.open_containing(self.spec)[0]
757
from bzrlib.branch import Branch
758
other_branch = Branch.open(self.spec)
357
759
revision_b = other_branch.last_revision()
358
if revision_b is None:
359
raise NoCommits(other_branch)
760
if revision_b in (None, revision.NULL_REVISION):
761
raise errors.NoCommits(other_branch)
360
762
# pull in the remote revisions so we can diff
361
763
branch.fetch(other_branch, revision_b)
363
765
revno = branch.revision_id_to_revno(revision_b)
364
except NoSuchRevision:
766
except errors.NoSuchRevision:
366
768
return RevisionInfo(branch, revno, revision_b)
770
def _as_revision_id(self, context_branch):
771
from bzrlib.branch import Branch
772
other_branch = Branch.open(self.spec)
773
last_revision = other_branch.last_revision()
774
last_revision = revision.ensure_null(last_revision)
775
context_branch.fetch(other_branch, last_revision)
776
if last_revision == revision.NULL_REVISION:
777
raise errors.NoCommits(other_branch)
368
780
SPEC_TYPES.append(RevisionSpec_branch)
783
class RevisionSpec_submit(RevisionSpec_ancestor):
784
"""Selects a common ancestor with a submit branch."""
786
help_txt = """Selects a common ancestor with the submit branch.
788
Diffing against this shows all the changes that were made in this branch,
789
and is a good predictor of what merge will do. The submit branch is
790
used by the bundle and merge directive commands. If no submit branch
791
is specified, the parent branch is used instead.
793
The common ancestor is the last revision that existed in both
794
branches. Usually this is the branch point, but it could also be
795
a revision that was merged.
799
$ bzr diff -r submit:
804
def _get_submit_location(self, branch):
805
submit_location = branch.get_submit_branch()
806
location_type = 'submit branch'
807
if submit_location is None:
808
submit_location = branch.get_parent()
809
location_type = 'parent branch'
810
if submit_location is None:
811
raise errors.NoSubmitBranch(branch)
812
trace.note('Using %s %s', location_type, submit_location)
813
return submit_location
815
def _match_on(self, branch, revs):
816
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
817
return self._find_revision_info(branch,
818
self._get_submit_location(branch))
820
def _as_revision_id(self, context_branch):
821
return self._find_revision_id(context_branch,
822
self._get_submit_location(context_branch))
825
SPEC_TYPES.append(RevisionSpec_submit)