1
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
34
24
class RevisionInfo(object):
35
25
"""The results of applying a revision specification to a branch.
120
def __new__(cls, spec, _internal=False):
122
return object.__new__(cls, spec, _internal=_internal)
124
symbol_versioning.warn('Creating a RevisionSpec directly has'
125
' been deprecated in version 0.11. Use'
126
' RevisionSpec.from_string()'
128
DeprecationWarning, stacklevel=2)
129
return RevisionSpec.from_string(spec)
132
def from_string(spec):
133
"""Parse a revision spec string into a RevisionSpec object.
135
:param spec: A string specified by the user
136
:return: A RevisionSpec object that understands how to parse the
107
def __new__(cls, spec, foo=_marker):
108
"""Parse a revision specifier.
139
if not isinstance(spec, (type(None), basestring)):
140
raise TypeError('error')
143
return RevisionSpec(None, _internal=True)
145
assert isinstance(spec, basestring), \
146
"You should only supply strings not %s" % (type(spec),)
148
for spectype in SPEC_TYPES:
149
if spec.startswith(spectype.prefix):
150
trace.mutter('Returning RevisionSpec %s for %s',
151
spectype.__name__, spec)
152
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):
121
for spectype in SPEC_TYPES:
122
if spec.startswith(spectype.prefix):
123
return object.__new__(spectype, spec)
125
raise BzrError('No namespace registered for string: %r' %
154
# RevisionSpec_revno is special cased, because it is the only
155
# one that directly handles plain integers
156
# TODO: This should not be special cased rather it should be
157
# a method invocation on spectype.canparse()
159
if _revno_regex is None:
160
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
161
if _revno_regex.match(spec) is not None:
162
return RevisionSpec_revno(spec, _internal=True)
164
raise errors.NoSuchRevisionSpec(spec)
166
def __init__(self, spec, _internal=False):
167
"""Create a RevisionSpec referring to the Null revision.
169
:param spec: The original spec supplied by the user
170
:param _internal: Used to ensure that RevisionSpec is not being
171
called directly. Only from RevisionSpec.from_string()
174
# XXX: Update this after 0.10 is released
175
symbol_versioning.warn('Creating a RevisionSpec directly has'
176
' been deprecated in version 0.11. Use'
177
' RevisionSpec.from_string()'
179
DeprecationWarning, stacklevel=2)
180
self.user_spec = spec
128
raise TypeError('Unhandled revision type %s' % spec)
130
def __init__(self, spec):
181
131
if self.prefix and spec.startswith(self.prefix):
182
132
spec = spec[len(self.prefix):]
185
135
def _match_on(self, branch, revs):
186
trace.mutter('Returning RevisionSpec._match_on: None')
187
136
return RevisionInfo(branch, 0, None)
189
138
def _match_on_and_check(self, branch, revs):
194
143
# special case - the empty tree
196
145
elif self.prefix:
197
raise errors.InvalidRevisionSpec(self.user_spec, branch)
146
raise NoSuchRevision(branch, self.prefix + str(self.spec))
199
raise errors.InvalidRevisionSpec(self.spec, branch)
148
raise NoSuchRevision(branch, str(self.spec))
201
150
def in_history(self, branch):
203
revs = branch.revision_history()
205
# this should never trigger.
206
# TODO: make it a deprecated code path. RBC 20060928
151
revs = branch.revision_history()
208
152
return self._match_on_and_check(branch, revs)
210
# FIXME: in_history is somewhat broken,
211
# it will return non-history revisions in many
212
# circumstances. The expected facility is that
213
# in_history only returns revision-history revs,
214
# in_store returns any rev. RBC 20051010
215
# aliases for now, when we fix the core logic, then they
216
# will do what you expect.
217
in_store = in_history
220
154
def __repr__(self):
221
155
# this is mostly for helping with testing
222
return '<%s %s>' % (self.__class__.__name__,
225
def needs_branch(self):
226
"""Whether this revision spec needs a branch.
228
Set this to False the branch argument of _match_on is not used.
232
def get_branch(self):
233
"""When the revision specifier contains a branch location, return it.
235
Otherwise, return None.
156
return '<%s %s%s>' % (self.__class__.__name__,
163
class RevisionSpec_int(RevisionSpec):
164
"""Spec is a number. Special case."""
165
def __init__(self, spec):
166
self.spec = int(spec)
168
def _match_on(self, branch, revs):
170
revno = len(revs) + self.spec + 1
173
rev_id = branch.get_rev_id(revno, revs)
174
return RevisionInfo(branch, revno, rev_id)
242
177
class RevisionSpec_revno(RevisionSpec):
243
178
prefix = 'revno:'
245
180
def _match_on(self, branch, revs):
246
181
"""Lookup a revision by revision number"""
247
loc = self.spec.find(':')
249
revno_spec = self.spec
252
revno_spec = self.spec[:loc]
253
branch_spec = self.spec[loc+1:]
257
raise errors.InvalidRevisionSpec(self.user_spec,
258
branch, 'cannot have an empty revno and no branch')
262
revno = int(revno_spec)
265
# dotted decimal. This arguably should not be here
266
# but the from_string method is a little primitive
267
# right now - RBC 20060928
269
match_revno = tuple((int(number) for number in revno_spec.split('.')))
270
except ValueError, e:
271
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
276
# the user has override the branch to look in.
277
# we need to refresh the revision_history map and
279
from bzrlib.branch import Branch
280
branch = Branch.open(branch_spec)
281
# Need to use a new revision history
282
# because we are using a specific branch
283
revs = branch.revision_history()
288
last_rev = branch.last_revision()
289
merge_sorted_revisions = tsort.merge_sort(
290
branch.repository.get_revision_graph(last_rev),
294
return item[3] == match_revno
295
revisions = filter(match, merge_sorted_revisions)
298
if len(revisions) != 1:
299
return RevisionInfo(branch, None, None)
301
# there is no traditional 'revno' for dotted-decimal revnos.
302
# so for API compatability we return None.
303
return RevisionInfo(branch, None, revisions[0][1])
306
if (-revno) >= len(revs):
309
revno = len(revs) + revno + 1
311
revision_id = branch.get_rev_id(revno, revs)
312
except errors.NoSuchRevision:
313
raise errors.InvalidRevisionSpec(self.user_spec, branch)
314
return RevisionInfo(branch, revno, revision_id)
316
def needs_branch(self):
317
return self.spec.find(':') == -1
319
def get_branch(self):
320
if self.spec.find(':') == -1:
323
return self.spec[self.spec.find(':')+1:]
326
RevisionSpec_int = RevisionSpec_revno
183
return RevisionInfo(branch, int(self.spec))
185
return RevisionInfo(branch, None)
328
187
SPEC_TYPES.append(RevisionSpec_revno)
348
206
def _match_on(self, branch, revs):
351
raise errors.NoCommits(branch)
352
return RevisionInfo(branch, len(revs), revs[-1])
355
208
offset = int(self.spec)
356
except ValueError, e:
357
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
360
raise errors.InvalidRevisionSpec(self.user_spec, branch,
361
'you must supply a positive value')
362
revno = len(revs) - offset + 1
364
revision_id = branch.get_rev_id(revno, revs)
365
except errors.NoSuchRevision:
366
raise errors.InvalidRevisionSpec(self.user_spec, branch)
367
return RevisionInfo(branch, revno, revision_id)
210
return RevisionInfo(branch, None)
213
raise BzrError('You must supply a positive value for --revision last:XXX')
214
return RevisionInfo(branch, len(revs) - offset + 1)
369
216
SPEC_TYPES.append(RevisionSpec_last)
374
221
prefix = 'before:'
376
223
def _match_on(self, branch, revs):
377
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
379
raise errors.InvalidRevisionSpec(self.user_spec, branch,
380
'cannot go before the null: revision')
382
# We need to use the repository history here
383
rev = branch.repository.get_revision(r.rev_id)
384
if not rev.parent_ids:
388
revision_id = rev.parent_ids[0]
390
revno = revs.index(revision_id) + 1
396
revision_id = branch.get_rev_id(revno, revs)
397
except errors.NoSuchRevision:
398
raise errors.InvalidRevisionSpec(self.user_spec,
400
return RevisionInfo(branch, revno, revision_id)
224
r = RevisionSpec(self.spec)._match_on(branch, revs)
225
if (r.revno is None) or (r.revno == 0):
227
return RevisionInfo(branch, r.revno - 1)
402
229
SPEC_TYPES.append(RevisionSpec_before)
408
235
def _match_on(self, branch, revs):
409
raise errors.InvalidRevisionSpec(self.user_spec, branch,
410
'tag: namespace registered,'
411
' but not implemented')
236
raise BzrError('tag: namespace registered, but not implemented.')
413
238
SPEC_TYPES.append(RevisionSpec_tag)
416
class _RevListToTimestamps(object):
417
"""This takes a list of revisions, and allows you to bisect by date"""
419
__slots__ = ['revs', 'branch']
421
def __init__(self, revs, branch):
425
def __getitem__(self, index):
426
"""Get the date of the index'd item"""
427
r = self.branch.repository.get_revision(self.revs[index])
428
# TODO: Handle timezone.
429
return datetime.datetime.fromtimestamp(r.timestamp)
432
return len(self.revs)
435
241
class RevisionSpec_date(RevisionSpec):
437
243
_date_re = re.compile(
462
268
m = self._date_re.match(self.spec)
463
269
if not m or (not m.group('date') and not m.group('time')):
464
raise errors.InvalidRevisionSpec(self.user_spec,
465
branch, 'invalid date')
469
year = int(m.group('year'))
470
month = int(m.group('month'))
471
day = int(m.group('day'))
478
hour = int(m.group('hour'))
479
minute = int(m.group('minute'))
480
if m.group('second'):
481
second = int(m.group('second'))
485
hour, minute, second = 0,0,0
487
raise errors.InvalidRevisionSpec(self.user_spec,
488
branch, 'invalid date')
270
raise BzrError('Invalid revision date %r' % self.spec)
273
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
275
year, month, day = today.year, today.month, today.day
277
hour = int(m.group('hour'))
278
minute = int(m.group('minute'))
279
if m.group('second'):
280
second = int(m.group('second'))
284
hour, minute, second = 0,0,0
490
286
dt = datetime.datetime(year=year, month=month, day=day,
491
287
hour=hour, minute=minute, second=second)
494
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
498
return RevisionInfo(branch, None)
500
return RevisionInfo(branch, rev + 1)
289
for i in range(len(revs)):
290
r = branch.get_revision(revs[i])
291
# TODO: Handle timezone.
292
dt = datetime.datetime.fromtimestamp(r.timestamp)
294
return RevisionInfo(branch, i+1)
295
return RevisionInfo(branch, None)
502
297
SPEC_TYPES.append(RevisionSpec_date)
506
301
prefix = 'ancestor:'
508
303
def _match_on(self, branch, revs):
509
from bzrlib.branch import Branch
511
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
512
other_branch = Branch.open(self.spec)
304
from branch import Branch
305
from revision import common_ancestor, MultipleRevisionSources
306
other_branch = Branch.open_containing(self.spec)
513
307
revision_a = branch.last_revision()
514
308
revision_b = other_branch.last_revision()
515
309
for r, b in ((revision_a, branch), (revision_b, other_branch)):
516
if r in (None, revision.NULL_REVISION):
517
raise errors.NoCommits(b)
518
revision_source = revision.MultipleRevisionSources(
519
branch.repository, other_branch.repository)
520
rev_id = revision.common_ancestor(revision_a, revision_b,
312
revision_source = MultipleRevisionSources(branch, other_branch)
313
rev_id = common_ancestor(revision_a, revision_b, revision_source)
523
315
revno = branch.revision_id_to_revno(rev_id)
524
except errors.NoSuchRevision:
316
except NoSuchRevision:
526
318
return RevisionInfo(branch, revno, rev_id)
528
320
SPEC_TYPES.append(RevisionSpec_ancestor)
531
class RevisionSpec_branch(RevisionSpec):
532
"""A branch: revision specifier.
534
This takes the path to a branch and returns its tip revision id.
538
def _match_on(self, branch, revs):
539
from bzrlib.branch import Branch
540
other_branch = Branch.open(self.spec)
541
revision_b = other_branch.last_revision()
542
if revision_b in (None, revision.NULL_REVISION):
543
raise errors.NoCommits(other_branch)
544
# pull in the remote revisions so we can diff
545
branch.fetch(other_branch, revision_b)
547
revno = branch.revision_id_to_revno(revision_b)
548
except errors.NoSuchRevision:
550
return RevisionInfo(branch, revno, revision_b)
552
SPEC_TYPES.append(RevisionSpec_branch)