124
def __new__(cls, spec, _internal=False):
126
return object.__new__(cls, spec, _internal=_internal)
128
symbol_versioning.warn('Creating a RevisionSpec directly has'
129
' been deprecated in version 0.11. Use'
130
' RevisionSpec.from_string()'
132
DeprecationWarning, stacklevel=2)
133
return RevisionSpec.from_string(spec)
136
def from_string(spec):
137
"""Parse a revision spec string into a RevisionSpec object.
139
:param spec: A string specified by the user
140
:return: A RevisionSpec object that understands how to parse the
107
def __new__(cls, spec, foo=_marker):
108
"""Parse a revision specifier.
143
if not isinstance(spec, (type(None), basestring)):
144
raise TypeError('error')
147
return RevisionSpec(None, _internal=True)
149
assert isinstance(spec, basestring), \
150
"You should only supply strings not %s" % (type(spec),)
152
for spectype in SPEC_TYPES:
153
if spec.startswith(spectype.prefix):
154
trace.mutter('Returning RevisionSpec %s for %s',
155
spectype.__name__, spec)
156
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' %
158
# RevisionSpec_revno is special cased, because it is the only
159
# one that directly handles plain integers
160
# TODO: This should not be special cased rather it should be
161
# a method invocation on spectype.canparse()
163
if _revno_regex is None:
164
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
165
if _revno_regex.match(spec) is not None:
166
return RevisionSpec_revno(spec, _internal=True)
168
raise errors.NoSuchRevisionSpec(spec)
170
def __init__(self, spec, _internal=False):
171
"""Create a RevisionSpec referring to the Null revision.
173
:param spec: The original spec supplied by the user
174
:param _internal: Used to ensure that RevisionSpec is not being
175
called directly. Only from RevisionSpec.from_string()
178
# XXX: Update this after 0.10 is released
179
symbol_versioning.warn('Creating a RevisionSpec directly has'
180
' been deprecated in version 0.11. Use'
181
' RevisionSpec.from_string()'
183
DeprecationWarning, stacklevel=2)
184
self.user_spec = spec
128
raise TypeError('Unhandled revision type %s' % spec)
130
def __init__(self, spec):
185
131
if self.prefix and spec.startswith(self.prefix):
186
132
spec = spec[len(self.prefix):]
189
135
def _match_on(self, branch, revs):
190
trace.mutter('Returning RevisionSpec._match_on: None')
191
136
return RevisionInfo(branch, 0, None)
193
138
def _match_on_and_check(self, branch, revs):
224
164
def __repr__(self):
225
165
# this is mostly for helping with testing
226
return '<%s %s>' % (self.__class__.__name__,
229
def needs_branch(self):
230
"""Whether this revision spec needs a branch.
232
Set this to False the branch argument of _match_on is not used.
236
def get_branch(self):
237
"""When the revision specifier contains a branch location, return it.
239
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)
246
187
class RevisionSpec_revno(RevisionSpec):
247
"""Selects a revision using a number."""
249
help_txt = """Selects a revision using a number.
251
Use an integer to specify a revision in the history of the branch.
252
Optionally a branch can be specified. The 'revno:' prefix is optional.
253
A negative number will count from the end of the branch (-1 is the
254
last revision, -2 the previous one). If the negative number is larger
255
than the branch's history, the first revision is returned.
257
revno:1 -> return the first revision
258
revno:3:/path/to/branch -> return the 3rd revision of
259
the branch '/path/to/branch'
260
revno:-1 -> The last revision in a branch.
261
-2:http://other/branch -> The second to last revision in the
263
-1000000 -> Most likely the first revision, unless
264
your history is very long.
266
188
prefix = 'revno:'
268
190
def _match_on(self, branch, revs):
269
191
"""Lookup a revision by revision number"""
270
loc = self.spec.find(':')
272
revno_spec = self.spec
275
revno_spec = self.spec[:loc]
276
branch_spec = self.spec[loc+1:]
280
raise errors.InvalidRevisionSpec(self.user_spec,
281
branch, 'cannot have an empty revno and no branch')
285
revno = int(revno_spec)
288
# dotted decimal. This arguably should not be here
289
# but the from_string method is a little primitive
290
# right now - RBC 20060928
292
match_revno = tuple((int(number) for number in revno_spec.split('.')))
293
except ValueError, e:
294
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
299
# the user has override the branch to look in.
300
# we need to refresh the revision_history map and
302
from bzrlib.branch import Branch
303
branch = Branch.open(branch_spec)
304
# Need to use a new revision history
305
# because we are using a specific branch
306
revs = branch.revision_history()
311
last_rev = branch.last_revision()
312
merge_sorted_revisions = tsort.merge_sort(
313
branch.repository.get_revision_graph(last_rev),
317
return item[3] == match_revno
318
revisions = filter(match, merge_sorted_revisions)
321
if len(revisions) != 1:
322
return RevisionInfo(branch, None, None)
324
# there is no traditional 'revno' for dotted-decimal revnos.
325
# so for API compatability we return None.
326
return RevisionInfo(branch, None, revisions[0][1])
329
if (-revno) >= len(revs):
332
revno = len(revs) + revno + 1
334
revision_id = branch.get_rev_id(revno, revs)
335
except errors.NoSuchRevision:
336
raise errors.InvalidRevisionSpec(self.user_spec, branch)
337
return RevisionInfo(branch, revno, revision_id)
339
def needs_branch(self):
340
return self.spec.find(':') == -1
342
def get_branch(self):
343
if self.spec.find(':') == -1:
346
return self.spec[self.spec.find(':')+1:]
349
RevisionSpec_int = RevisionSpec_revno
193
return RevisionInfo(branch, int(self.spec))
195
return RevisionInfo(branch, None)
351
197
SPEC_TYPES.append(RevisionSpec_revno)
354
200
class RevisionSpec_revid(RevisionSpec):
355
"""Selects a revision using the revision id."""
357
help_txt = """Selects a revision using the revision id.
359
Supply a specific revision id, that can be used to specify any
360
revision id in the ancestry of the branch.
361
Including merges, and pending merges.
363
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
365
201
prefix = 'revid:'
367
203
def _match_on(self, branch, revs):
369
revno = revs.index(self.spec) + 1
205
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
370
206
except ValueError:
372
return RevisionInfo(branch, revno, self.spec)
207
return RevisionInfo(branch, None)
374
209
SPEC_TYPES.append(RevisionSpec_revid)
377
212
class RevisionSpec_last(RevisionSpec):
378
"""Selects the nth revision from the end."""
380
help_txt = """Selects the nth revision from the end.
382
Supply a positive number to get the nth revision from the end.
383
This is the same as supplying negative numbers to the 'revno:' spec.
385
last:1 -> return the last revision
386
last:3 -> return the revision 2 before the end.
391
216
def _match_on(self, branch, revs):
394
raise errors.NoCommits(branch)
395
return RevisionInfo(branch, len(revs), revs[-1])
398
218
offset = int(self.spec)
399
except ValueError, e:
400
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
403
raise errors.InvalidRevisionSpec(self.user_spec, branch,
404
'you must supply a positive value')
405
revno = len(revs) - offset + 1
407
revision_id = branch.get_rev_id(revno, revs)
408
except errors.NoSuchRevision:
409
raise errors.InvalidRevisionSpec(self.user_spec, branch)
410
return RevisionInfo(branch, revno, revision_id)
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)
412
226
SPEC_TYPES.append(RevisionSpec_last)
415
229
class RevisionSpec_before(RevisionSpec):
416
"""Selects the parent of the revision specified."""
418
help_txt = """Selects the parent of the revision specified.
420
Supply any revision spec to return the parent of that revision.
421
It is an error to request the parent of the null revision (before:0).
422
This is mostly useful when inspecting revisions that are not in the
423
revision history of a branch.
426
before:1913 -> Return the parent of revno 1913 (revno 1912)
427
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
429
bzr diff -r before:revid:aaaa..revid:aaaa
430
-> Find the changes between revision 'aaaa' and its parent.
431
(what changes did 'aaaa' introduce)
434
231
prefix = 'before:'
436
233
def _match_on(self, branch, revs):
437
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
439
raise errors.InvalidRevisionSpec(self.user_spec, branch,
440
'cannot go before the null: revision')
442
# We need to use the repository history here
443
rev = branch.repository.get_revision(r.rev_id)
444
if not rev.parent_ids:
448
revision_id = rev.parent_ids[0]
450
revno = revs.index(revision_id) + 1
456
revision_id = branch.get_rev_id(revno, revs)
457
except errors.NoSuchRevision:
458
raise errors.InvalidRevisionSpec(self.user_spec,
460
return RevisionInfo(branch, revno, revision_id)
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)
462
239
SPEC_TYPES.append(RevisionSpec_before)
465
242
class RevisionSpec_tag(RevisionSpec):
466
"""To be implemented."""
468
help_txt = """To be implemented."""
472
245
def _match_on(self, branch, revs):
473
raise errors.InvalidRevisionSpec(self.user_spec, branch,
474
'tag: namespace registered,'
475
' but not implemented')
246
raise BzrError('tag: namespace registered, but not implemented.')
477
248
SPEC_TYPES.append(RevisionSpec_tag)
480
class _RevListToTimestamps(object):
481
"""This takes a list of revisions, and allows you to bisect by date"""
483
__slots__ = ['revs', 'branch']
485
def __init__(self, revs, branch):
489
def __getitem__(self, index):
490
"""Get the date of the index'd item"""
491
r = self.branch.repository.get_revision(self.revs[index])
492
# TODO: Handle timezone.
493
return datetime.datetime.fromtimestamp(r.timestamp)
496
return len(self.revs)
499
251
class RevisionSpec_date(RevisionSpec):
500
"""Selects a revision on the basis of a datestamp."""
502
help_txt = """Selects a revision on the basis of a datestamp.
504
Supply a datestamp to select the first revision that matches the date.
505
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
506
Matches the first entry after a given date (either at midnight or
507
at a specified time).
509
One way to display all the changes since yesterday would be:
510
bzr log -r date:yesterday..-1
513
date:yesterday -> select the first revision since yesterday
514
date:2006-08-14,17:10:14 -> select the first revision after
515
August 14th, 2006 at 5:10pm.
518
253
_date_re = re.compile(
519
254
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
542
278
m = self._date_re.match(self.spec)
543
279
if not m or (not m.group('date') and not m.group('time')):
544
raise errors.InvalidRevisionSpec(self.user_spec,
545
branch, 'invalid date')
549
year = int(m.group('year'))
550
month = int(m.group('month'))
551
day = int(m.group('day'))
558
hour = int(m.group('hour'))
559
minute = int(m.group('minute'))
560
if m.group('second'):
561
second = int(m.group('second'))
565
hour, minute, second = 0,0,0
567
raise errors.InvalidRevisionSpec(self.user_spec,
568
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
570
296
dt = datetime.datetime(year=year, month=month, day=day,
571
297
hour=hour, minute=minute, second=second)
574
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
578
return RevisionInfo(branch, None)
580
return RevisionInfo(branch, rev + 1)
299
for i in range(len(revs)):
300
r = branch.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)
582
307
SPEC_TYPES.append(RevisionSpec_date)
585
310
class RevisionSpec_ancestor(RevisionSpec):
586
"""Selects a common ancestor with a second branch."""
588
help_txt = """Selects a common ancestor with a second branch.
590
Supply the path to a branch to select the common ancestor.
592
The common ancestor is the last revision that existed in both
593
branches. Usually this is the branch point, but it could also be
594
a revision that was merged.
596
This is frequently used with 'diff' to return all of the changes
597
that your branch introduces, while excluding the changes that you
598
have not merged from the remote branch.
601
ancestor:/path/to/branch
602
$ bzr diff -r ancestor:../../mainline/branch
604
311
prefix = 'ancestor:'
606
313
def _match_on(self, branch, revs):
607
from bzrlib.branch import Branch
609
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
610
other_branch = Branch.open(self.spec)
314
from branch import Branch
315
from revision import common_ancestor, MultipleRevisionSources
316
other_branch = Branch.open_containing(self.spec)[0]
611
317
revision_a = branch.last_revision()
612
318
revision_b = other_branch.last_revision()
613
319
for r, b in ((revision_a, branch), (revision_b, other_branch)):
614
if r in (None, revision.NULL_REVISION):
615
raise errors.NoCommits(b)
616
revision_source = revision.MultipleRevisionSources(
617
branch.repository, other_branch.repository)
618
rev_id = revision.common_ancestor(revision_a, revision_b,
322
revision_source = MultipleRevisionSources(branch, other_branch)
323
rev_id = common_ancestor(revision_a, revision_b, revision_source)
621
325
revno = branch.revision_id_to_revno(rev_id)
622
except errors.NoSuchRevision:
326
except NoSuchRevision:
624
328
return RevisionInfo(branch, revno, rev_id)
626
330
SPEC_TYPES.append(RevisionSpec_ancestor)
629
332
class RevisionSpec_branch(RevisionSpec):
630
"""Selects the last revision of a specified branch."""
632
help_txt = """Selects the last revision of a specified branch.
634
Supply the path to a branch to select its last revision.
637
branch:/path/to/branch
333
"""A branch: revision specifier.
335
This takes the path to a branch and returns its tip revision id.
639
337
prefix = 'branch:'
641
339
def _match_on(self, branch, revs):
642
from bzrlib.branch import Branch
643
other_branch = Branch.open(self.spec)
340
from branch import Branch
341
from fetch import greedy_fetch
342
other_branch = Branch.open_containing(self.spec)[0]
644
343
revision_b = other_branch.last_revision()
645
if revision_b in (None, revision.NULL_REVISION):
646
raise errors.NoCommits(other_branch)
344
if revision_b is None:
345
raise NoCommits(other_branch)
647
346
# pull in the remote revisions so we can diff
648
branch.fetch(other_branch, revision_b)
347
greedy_fetch(branch, other_branch, revision=revision_b)
650
349
revno = branch.revision_id_to_revno(revision_b)
651
except errors.NoSuchRevision:
350
except NoSuchRevision:
653
352
return RevisionInfo(branch, revno, revision_b)