13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from bzrlib.lazy_import import lazy_import
21
lazy_import(globals(), """
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
22
from bzrlib import (
122
121
help_txt = """A parsed revision specification.
124
A revision specification is a string, which may be unambiguous about
125
what it represents by giving a prefix like 'date:' or 'revid:' etc,
126
or it may have no prefix, in which case it's tried against several
127
specifier types in sequence to determine what the user meant.
123
A revision specification can be an integer, in which case it is
124
assumed to be a revno (though this will translate negative values
125
into positive ones); or it can be a string, in which case it is
126
parsed for something like 'date:' or 'revid:' etc.
129
128
Revision specs are an UI element, and they have been moved out
130
129
of the branch class to leave "back-end" classes unaware of such
139
wants_revision_history = True
140
dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
141
"""Exceptions that RevisionSpec_dwim._match_on will catch.
143
If the revspec is part of ``dwim_revspecs``, it may be tried with an
144
invalid revspec and raises some exception. The exceptions mentioned here
145
will not be reported to the user but simply ignored without stopping the
139
def __new__(cls, spec, _internal=False):
141
return object.__new__(cls, spec, _internal=_internal)
143
symbol_versioning.warn('Creating a RevisionSpec directly has'
144
' been deprecated in version 0.11. Use'
145
' RevisionSpec.from_string()'
147
DeprecationWarning, stacklevel=2)
148
return RevisionSpec.from_string(spec)
150
151
def from_string(spec):
161
162
return RevisionSpec(None, _internal=True)
162
match = revspec_registry.get_prefix(spec)
163
if match is not None:
164
spectype, specsuffix = match
165
trace.mutter('Returning RevisionSpec %s for %s',
166
spectype.__name__, spec)
167
return spectype(spec, _internal=True)
164
assert isinstance(spec, basestring), \
165
"You should only supply strings not %s" % (type(spec),)
167
for spectype in SPEC_TYPES:
168
if spec.startswith(spectype.prefix):
169
trace.mutter('Returning RevisionSpec %s for %s',
170
spectype.__name__, spec)
171
return spectype(spec, _internal=True)
169
for spectype in SPEC_TYPES:
170
if spec.startswith(spectype.prefix):
171
trace.mutter('Returning RevisionSpec %s for %s',
172
spectype.__name__, spec)
173
return spectype(spec, _internal=True)
174
# Otherwise treat it as a DWIM, build the RevisionSpec object and
175
# wait for _match_on to be called.
176
return RevisionSpec_dwim(spec, _internal=True)
173
# RevisionSpec_revno is special cased, because it is the only
174
# one that directly handles plain integers
175
# TODO: This should not be special cased rather it should be
176
# a method invocation on spectype.canparse()
178
if _revno_regex is None:
179
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
180
if _revno_regex.match(spec) is not None:
181
return RevisionSpec_revno(spec, _internal=True)
183
raise errors.NoSuchRevisionSpec(spec)
178
185
def __init__(self, spec, _internal=False):
179
186
"""Create a RevisionSpec referring to the Null revision.
248
253
return self.in_history(context_branch).rev_id
250
def as_tree(self, context_branch):
251
"""Return the tree object for this revisions spec.
253
Some revision specs require a context_branch to be able to determine
254
the revision id and access the repository. Not all specs will make
257
return self._as_tree(context_branch)
259
def _as_tree(self, context_branch):
260
"""Implementation of as_tree().
262
Classes should override this function to provide appropriate
263
functionality. The default is to just call '.as_revision_id()'
264
and get the revision tree from context_branch's repository.
266
revision_id = self.as_revision_id(context_branch)
267
return context_branch.repository.revision_tree(revision_id)
269
255
def __repr__(self):
270
256
# this is mostly for helping with testing
271
257
return '<%s %s>' % (self.__class__.__name__,
274
260
def needs_branch(self):
275
261
"""Whether this revision spec needs a branch.
291
class RevisionSpec_dwim(RevisionSpec):
292
"""Provides a DWIMish revision specifier lookup.
294
Note that this does not go in the revspec_registry because by definition
295
there is no prefix to identify it. It's solely called from
296
RevisionSpec.from_string() because the DWIMification happen when _match_on
297
is called so the string describing the revision is kept here until needed.
301
# We don't need to build the revision history ourself, that's delegated to
302
# each revspec we try.
303
wants_revision_history = False
305
def _try_spectype(self, rstype, branch):
306
rs = rstype(self.spec, _internal=True)
307
# Hit in_history to find out if it exists, or we need to try the
309
return rs.in_history(branch)
311
def _match_on(self, branch, revs):
312
"""Run the lookup and see what we can get."""
314
# First, see if it's a revno
316
if _revno_regex is None:
317
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
318
if _revno_regex.match(self.spec) is not None:
320
return self._try_spectype(RevisionSpec_revno, branch)
321
except RevisionSpec_revno.dwim_catchable_exceptions:
324
# Next see what has been registered
325
for rs_class in dwim_revspecs:
327
return self._try_spectype(rs_class, branch)
328
except rs_class.dwim_catchable_exceptions:
331
# Well, I dunno what it is. Note that we don't try to keep track of the
332
# first of last exception raised during the DWIM tries as none seems
334
raise errors.InvalidRevisionSpec(self.spec, branch)
337
277
class RevisionSpec_revno(RevisionSpec):
338
278
"""Selects a revision using a number."""
340
280
help_txt = """Selects a revision using a number.
342
282
Use an integer to specify a revision in the history of the branch.
343
Optionally a branch can be specified. A negative number will count
344
from the end of the branch (-1 is the last revision, -2 the previous
345
one). If the negative number is larger than the branch's history, the
346
first revision is returned.
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.
349
revno:1 -> return the first revision of this branch
289
revno:1 -> return the first revision
350
290
revno:3:/path/to/branch -> return the 3rd revision of
351
291
the branch '/path/to/branch'
352
292
revno:-1 -> The last revision in a branch.
401
340
revs_or_none = None
405
revision_id = branch.dotted_revno_to_revision_id(match_revno,
407
except errors.NoSuchRevision:
408
raise errors.InvalidRevisionSpec(self.user_spec, branch)
345
revision_id_to_revno = branch.get_revision_id_to_revno_map()
346
revisions = [revision_id for revision_id, revno
347
in revision_id_to_revno.iteritems()
348
if revno == match_revno]
351
if len(revisions) != 1:
352
return branch, None, None
410
354
# there is no traditional 'revno' for dotted-decimal revnos.
411
355
# so for API compatability we return None.
412
return branch, None, revision_id
356
return branch, None, revisions[0]
414
358
last_revno, last_revision_id = branch.last_revision_info()
528
475
help_txt = """Selects the parent of the revision specified.
530
Supply any revision spec to return the parent of that revision. This is
531
mostly useful when inspecting revisions that are not in the revision history
477
Supply any revision spec to return the parent of that revision.
534
478
It is an error to request the parent of the null revision (before:0).
479
This is mostly useful when inspecting revisions that are not in the
480
revision history of a branch.
538
484
before:1913 -> Return the parent of revno 1913 (revno 1912)
539
485
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
540
486
aaaa@bbbb-1234567890
541
bzr diff -r before:1913..1913
542
-> Find the changes between revision 1913 and its parent (1912).
543
(What changes did revision 1913 introduce).
544
This is equivalent to: bzr diff -c 1913
487
bzr diff -r before:revid:aaaa..revid:aaaa
488
-> Find the changes between revision 'aaaa' and its parent.
489
(what changes did 'aaaa' introduce)
547
492
prefix = 'before:'
549
494
def _match_on(self, branch, revs):
550
495
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
576
521
base_revspec = RevisionSpec.from_string(self.spec)
577
522
base_revision_id = base_revspec.as_revision_id(context_branch)
578
523
if base_revision_id == revision.NULL_REVISION:
579
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
524
raise errors.InvalidRevisionSpec(self.user_spec, branch,
580
525
'cannot go before the null: revision')
581
526
context_repo = context_branch.repository
582
527
context_repo.lock_read()
650
596
One way to display all the changes since yesterday would be::
652
bzr log -r date:yesterday..
598
bzr log -r date:yesterday..-1
656
602
date:yesterday -> select the first revision since yesterday
657
603
date:2006-08-14,17:10:14 -> select the first revision after
658
604
August 14th, 2006 at 5:10pm.
661
607
_date_re = re.compile(
662
608
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
813
758
revision_b = other_branch.last_revision()
814
759
if revision_b in (None, revision.NULL_REVISION):
815
760
raise errors.NoCommits(other_branch)
817
branch = other_branch
820
# pull in the remote revisions so we can diff
821
branch.fetch(other_branch, revision_b)
822
except errors.ReadOnlyError:
823
branch = other_branch
761
# pull in the remote revisions so we can diff
762
branch.fetch(other_branch, revision_b)
825
764
revno = branch.revision_id_to_revno(revision_b)
826
765
except errors.NoSuchRevision:
832
771
other_branch = Branch.open(self.spec)
833
772
last_revision = other_branch.last_revision()
834
773
last_revision = revision.ensure_null(last_revision)
835
context_branch.fetch(other_branch, last_revision)
836
774
if last_revision == revision.NULL_REVISION:
837
775
raise errors.NoCommits(other_branch)
838
776
return last_revision
840
def _as_tree(self, context_branch):
841
from bzrlib.branch import Branch
842
other_branch = Branch.open(self.spec)
843
last_revision = other_branch.last_revision()
844
last_revision = revision.ensure_null(last_revision)
845
if last_revision == revision.NULL_REVISION:
846
raise errors.NoCommits(other_branch)
847
return other_branch.repository.revision_tree(last_revision)
849
def needs_branch(self):
852
def get_branch(self):
778
SPEC_TYPES.append(RevisionSpec_branch)
857
781
class RevisionSpec_submit(RevisionSpec_ancestor):
862
786
Diffing against this shows all the changes that were made in this branch,
863
787
and is a good predictor of what merge will do. The submit branch is
864
used by the bundle and merge directive commands. If no submit branch
788
used by the bundle and merge directive comands. If no submit branch
865
789
is specified, the parent branch is used instead.
867
791
The common ancestor is the last revision that existed in both
896
820
self._get_submit_location(context_branch))
899
# The order in which we want to DWIM a revision spec without any prefix.
900
# revno is always tried first and isn't listed here, this is used by
901
# RevisionSpec_dwim._match_on
903
RevisionSpec_tag, # Let's try for a tag
904
RevisionSpec_revid, # Maybe it's a revid?
905
RevisionSpec_date, # Perhaps a date?
906
RevisionSpec_branch, # OK, last try, maybe it's a branch
910
revspec_registry = registry.Registry()
911
def _register_revspec(revspec):
912
revspec_registry.register(revspec.prefix, revspec)
914
_register_revspec(RevisionSpec_revno)
915
_register_revspec(RevisionSpec_revid)
916
_register_revspec(RevisionSpec_last)
917
_register_revspec(RevisionSpec_before)
918
_register_revspec(RevisionSpec_tag)
919
_register_revspec(RevisionSpec_date)
920
_register_revspec(RevisionSpec_ancestor)
921
_register_revspec(RevisionSpec_branch)
922
_register_revspec(RevisionSpec_submit)
924
# classes in this list should have a "prefix" attribute, against which
925
# string specs are matched
926
SPEC_TYPES = symbol_versioning.deprecated_list(
927
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
823
SPEC_TYPES.append(RevisionSpec_submit)