24
24
# This should match a prefix with a function which accepts it
25
25
REVISION_NAMESPACES = {}
27
def get_revision_info(branch, spec):
28
"""Return (revno, revision id) for revision specifier.
30
spec can be an integer, in which case it is assumed to be revno
31
(though this will translate negative values into positive ones)
32
spec can also be a string, in which case it is parsed for something
33
like 'date:' or 'revid:' etc.
35
A revid is always returned. If it is None, the specifier referred to
36
the null revision. If the revid does not occur in the revision
37
history, revno will be None.
27
class RevisionSpec(object):
28
"""Equivalent to the old get_revision_info().
29
An instance has two useful attributes: revno, and rev_id.
31
They can also be accessed as spec[0] and spec[1] respectively,
32
so that you can write code like:
33
revno, rev_id = RevisionSpec(branch, spec)
34
although this is probably going to be deprecated later.
36
Revision specs are an UI element, and they have been moved out
37
of the branch class to leave "back-end" classes unaware of such
38
details. Code that gets a revno or rev_id from other code should
39
not be using revision specs - revnos and revision ids are the
40
accepted ways to refer to revisions internally.
43
try:# Convert to int if possible
47
revs = branch.revision_history()
48
if isinstance(spec, int):
50
revno = len(revs) + spec + 1
53
rev_id = branch.get_rev_id(revno, revs)
54
elif isinstance(spec, basestring):
55
for prefix, func in REVISION_NAMESPACES.iteritems():
56
if spec.startswith(prefix):
57
result = func(branch, revs, spec)
59
revno, rev_id = result
62
rev_id = branch.get_rev_id(revno, revs)
65
raise BzrError('No namespace registered for string: %r' %
68
raise TypeError('Unhandled revision type %s' % spec)
70
if revno is None or rev_id is None:
71
raise NoSuchRevision(branch, spec)
42
def __init__(self, branch, spec):
43
"""Parse a revision specifier.
45
spec can be an integer, in which case it is assumed to be revno
46
(though this will translate negative values into positive ones)
47
spec can also be a string, in which case it is parsed for something
48
like 'date:' or 'revid:' etc.
57
try:# Convert to int if possible
61
revs = branch.revision_history()
62
if isinstance(spec, int):
64
self.revno = len(revs) + spec + 1
67
self.rev_id = branch.get_rev_id(self.revno, revs)
68
elif isinstance(spec, basestring):
69
for prefix, func in REVISION_NAMESPACES.iteritems():
70
if spec.startswith(prefix):
71
result = func(branch, revs, spec)
73
self.revno, self.rev_id = result
75
self.revno = result[0]
76
self.rev_id = branch.get_rev_id(self.revno, revs)
79
raise BzrError('No namespace registered for string: %r' %
82
raise TypeError('Unhandled revision type %s' % spec)
84
if self.revno is None or self.rev_id is None:
85
raise NoSuchRevision(branch, spec)
90
def __getitem__(self, index):
91
if index == 0: return self.revno
92
if index == 1: return self.rev_id
93
raise IndexError(index)
96
return self.branch.get_revision(self.rev_id)
98
def __eq__(self, other):
99
if type(other) not in (tuple, list, type(self)):
101
if type(other) is type(self) and self.branch is not other.branch:
103
print 'comparing', tuple(self), tuple(other)
104
return tuple(self) == tuple(other)
107
return '<bzrlib.revisionspec.RevisionSpec object %s, %s for %r>' % (
108
self.revno, self.rev_id, self.branch)