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
25
20
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
62
57
# TODO: otherwise, it should depend on how I was built -
63
58
# if it's in_history(branch), then check revision_history(),
64
59
# if it's in_store(branch), do the check below
65
return self.branch.repository.has_revision(self.rev_id)
60
return self.rev_id in self.branch.revision_store
148
143
# special case - the empty tree
150
145
elif self.prefix:
151
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch)
146
raise NoSuchRevision(branch, self.prefix + str(self.spec))
153
raise errors.InvalidRevisionSpec(self.spec, branch)
148
raise NoSuchRevision(branch, str(self.spec))
155
150
def in_history(self, branch):
157
revs = branch.revision_history()
151
revs = branch.revision_history()
160
152
return self._match_on_and_check(branch, revs)
162
# FIXME: in_history is somewhat broken,
163
# it will return non-history revisions in many
164
# circumstances. The expected facility is that
165
# in_history only returns revision-history revs,
166
# in_store returns any rev. RBC 20051010
167
# aliases for now, when we fix the core logic, then they
168
# will do what you expect.
169
in_store = in_history
172
154
def __repr__(self):
173
155
# this is mostly for helping with testing
174
156
return '<%s %s%s>' % (self.__class__.__name__,
175
157
self.prefix or '',
178
def needs_branch(self):
179
"""Whether this revision spec needs a branch.
181
Set this to False the branch argument of _match_on is not used.
187
163
class RevisionSpec_int(RevisionSpec):
188
164
"""Spec is a number. Special case."""
190
165
def __init__(self, spec):
191
166
self.spec = int(spec)
195
170
revno = len(revs) + self.spec + 1
197
172
revno = self.spec
199
rev_id = branch.get_rev_id(revno, revs)
200
except NoSuchRevision:
201
raise errors.InvalidRevisionSpec(self.spec, branch)
173
rev_id = branch.get_rev_id(revno, revs)
202
174
return RevisionInfo(branch, revno, rev_id)
208
180
def _match_on(self, branch, revs):
209
181
"""Lookup a revision by revision number"""
210
loc = self.spec.find(':')
212
revno_spec = self.spec
215
revno_spec = self.spec[:loc]
216
branch_spec = self.spec[loc+1:]
220
raise errors.InvalidRevisionSpec(self.prefix + self.spec,
221
branch, 'cannot have an empty revno and no branch')
225
revno = int(revno_spec)
226
except ValueError, e:
227
raise errors.InvalidRevisionSpec(self.prefix + self.spec,
231
revno = len(revs) + revno + 1
234
from bzrlib.branch import Branch
235
branch = Branch.open(branch_spec)
238
revid = branch.get_rev_id(revno)
239
except NoSuchRevision:
240
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch)
242
return RevisionInfo(branch, revno)
244
def needs_branch(self):
245
return self.spec.find(':') == -1
183
return RevisionInfo(branch, int(self.spec))
185
return RevisionInfo(branch, None)
247
187
SPEC_TYPES.append(RevisionSpec_revno)
253
193
def _match_on(self, branch, revs):
255
revno = revs.index(self.spec) + 1
195
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
256
196
except ValueError:
258
return RevisionInfo(branch, revno, self.spec)
197
return RevisionInfo(branch, None)
260
199
SPEC_TYPES.append(RevisionSpec_revid)
267
206
def _match_on(self, branch, revs):
270
raise NoCommits(branch)
271
return RevisionInfo(branch, len(revs), revs[-1])
274
208
offset = int(self.spec)
275
except ValueError, e:
276
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch, e)
279
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch,
280
'you must supply a positive value')
281
revno = len(revs) - offset + 1
283
revision_id = branch.get_rev_id(revno, revs)
284
except errors.NoSuchRevision:
285
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch)
286
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)
288
216
SPEC_TYPES.append(RevisionSpec_last)
307
235
def _match_on(self, branch, revs):
308
raise errors.InvalidRevisionSpec(self.prefix + self.spec, branch,
309
'tag: namespace registered,'
310
' but not implemented')
236
raise BzrError('tag: namespace registered, but not implemented.')
312
238
SPEC_TYPES.append(RevisionSpec_tag)
315
class RevisionSpec_revs:
316
def __init__(self, revs, branch):
319
def __getitem__(self, index):
320
r = self.branch.repository.get_revision(self.revs[index])
321
# TODO: Handle timezone.
322
return datetime.datetime.fromtimestamp(r.timestamp)
324
return len(self.revs)
327
241
class RevisionSpec_date(RevisionSpec):
329
243
_date_re = re.compile(
341
255
at a specified time).
343
257
So the proper way of saying 'give me all entries for today' is:
344
-r date:yesterday..date:today
258
-r date:today..date:tomorrow
346
260
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
347
261
if self.spec.lower() == 'yesterday':
372
286
dt = datetime.datetime(year=year, month=month, day=day,
373
287
hour=hour, minute=minute, second=second)
376
rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
380
return RevisionInfo(branch, None)
382
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)
384
297
SPEC_TYPES.append(RevisionSpec_date)
390
303
def _match_on(self, branch, revs):
391
304
from branch import Branch
392
305
from revision import common_ancestor, MultipleRevisionSources
393
other_branch = Branch.open_containing(self.spec)[0]
306
other_branch = Branch.open_containing(self.spec)
394
307
revision_a = branch.last_revision()
395
308
revision_b = other_branch.last_revision()
396
309
for r, b in ((revision_a, branch), (revision_b, other_branch)):
398
311
raise NoCommits(b)
399
revision_source = MultipleRevisionSources(branch.repository,
400
other_branch.repository)
312
revision_source = MultipleRevisionSources(branch, other_branch)
401
313
rev_id = common_ancestor(revision_a, revision_b, revision_source)
403
315
revno = branch.revision_id_to_revno(rev_id)
406
318
return RevisionInfo(branch, revno, rev_id)
408
320
SPEC_TYPES.append(RevisionSpec_ancestor)
410
class RevisionSpec_branch(RevisionSpec):
411
"""A branch: revision specifier.
413
This takes the path to a branch and returns its tip revision id.
417
def _match_on(self, branch, revs):
418
from branch import Branch
419
other_branch = Branch.open_containing(self.spec)[0]
420
revision_b = other_branch.last_revision()
421
if revision_b is None:
422
raise NoCommits(other_branch)
423
# pull in the remote revisions so we can diff
424
branch.fetch(other_branch, revision_b)
426
revno = branch.revision_id_to_revno(revision_b)
427
except NoSuchRevision:
429
return RevisionInfo(branch, revno, revision_b)
431
SPEC_TYPES.append(RevisionSpec_branch)