~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Robert Collins
  • Date: 2006-06-26 16:23:10 UTC
  • mfrom: (1780.2.1 misc-fixen)
  • mto: This revision was merged to the branch mainline in revision 1815.
  • Revision ID: robertc@robertcollins.net-20060626162310-98f5b55b8cc19d46
(robertc) Misc minor typos and the like.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import datetime
19
19
import re
 
20
import bisect
20
21
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
21
22
 
22
23
_marker = []
57
58
        # TODO: otherwise, it should depend on how I was built -
58
59
        # if it's in_history(branch), then check revision_history(),
59
60
        # if it's in_store(branch), do the check below
60
 
        return self.rev_id in self.branch.revision_store
 
61
        return self.branch.repository.has_revision(self.rev_id)
61
62
 
62
63
    def __len__(self):
63
64
        return 2
68
69
        raise IndexError(index)
69
70
 
70
71
    def get(self):
71
 
        return self.branch.get_revision(self.rev_id)
 
72
        return self.branch.repository.get_revision(self.rev_id)
72
73
 
73
74
    def __eq__(self, other):
74
75
        if type(other) not in (tuple, list, type(self)):
151
152
        revs = branch.revision_history()
152
153
        return self._match_on_and_check(branch, revs)
153
154
 
 
155
        # FIXME: in_history is somewhat broken,
 
156
        # it will return non-history revisions in many
 
157
        # circumstances. The expected facility is that
 
158
        # in_history only returns revision-history revs,
 
159
        # in_store returns any rev. RBC 20051010
 
160
    # aliases for now, when we fix the core logic, then they
 
161
    # will do what you expect.
 
162
    in_store = in_history
 
163
    in_branch = in_store
 
164
        
154
165
    def __repr__(self):
155
166
        # this is mostly for helping with testing
156
167
        return '<%s %s%s>' % (self.__class__.__name__,
194
205
        try:
195
206
            return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
196
207
        except ValueError:
197
 
            return RevisionInfo(branch, None)
 
208
            return RevisionInfo(branch, None, self.spec)
198
209
 
199
210
SPEC_TYPES.append(RevisionSpec_revid)
200
211
 
238
249
SPEC_TYPES.append(RevisionSpec_tag)
239
250
 
240
251
 
 
252
class RevisionSpec_revs:
 
253
    def __init__(self, revs, branch):
 
254
        self.revs = revs
 
255
        self.branch = branch
 
256
    def __getitem__(self, index):
 
257
        r = self.branch.repository.get_revision(self.revs[index])
 
258
        # TODO: Handle timezone.
 
259
        return datetime.datetime.fromtimestamp(r.timestamp)
 
260
    def __len__(self):
 
261
        return len(self.revs)
 
262
 
 
263
 
241
264
class RevisionSpec_date(RevisionSpec):
242
265
    prefix = 'date:'
243
266
    _date_re = re.compile(
285
308
 
286
309
            dt = datetime.datetime(year=year, month=month, day=day,
287
310
                    hour=hour, minute=minute, second=second)
288
 
        first = dt
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)
293
 
            if first <= dt:
294
 
                return RevisionInfo(branch, i+1)
295
 
        return RevisionInfo(branch, None)
 
311
        branch.lock_read()
 
312
        try:
 
313
            rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
 
314
        finally:
 
315
            branch.unlock()
 
316
        if rev == len(revs):
 
317
            return RevisionInfo(branch, None)
 
318
        else:
 
319
            return RevisionInfo(branch, rev + 1)
296
320
 
297
321
SPEC_TYPES.append(RevisionSpec_date)
298
322
 
303
327
    def _match_on(self, branch, revs):
304
328
        from branch import Branch
305
329
        from revision import common_ancestor, MultipleRevisionSources
306
 
        other_branch = Branch.open_containing(self.spec)
 
330
        other_branch = Branch.open_containing(self.spec)[0]
307
331
        revision_a = branch.last_revision()
308
332
        revision_b = other_branch.last_revision()
309
333
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
310
334
            if r is None:
311
335
                raise NoCommits(b)
312
 
        revision_source = MultipleRevisionSources(branch, other_branch)
 
336
        revision_source = MultipleRevisionSources(branch.repository,
 
337
                                                  other_branch.repository)
313
338
        rev_id = common_ancestor(revision_a, revision_b, revision_source)
314
339
        try:
315
340
            revno = branch.revision_id_to_revno(rev_id)
318
343
        return RevisionInfo(branch, revno, rev_id)
319
344
        
320
345
SPEC_TYPES.append(RevisionSpec_ancestor)
 
346
 
 
347
class RevisionSpec_branch(RevisionSpec):
 
348
    """A branch: revision specifier.
 
349
 
 
350
    This takes the path to a branch and returns its tip revision id.
 
351
    """
 
352
    prefix = 'branch:'
 
353
 
 
354
    def _match_on(self, branch, revs):
 
355
        from branch import Branch
 
356
        other_branch = Branch.open_containing(self.spec)[0]
 
357
        revision_b = other_branch.last_revision()
 
358
        if revision_b is None:
 
359
            raise NoCommits(other_branch)
 
360
        # pull in the remote revisions so we can diff
 
361
        branch.fetch(other_branch, revision_b)
 
362
        try:
 
363
            revno = branch.revision_id_to_revno(revision_b)
 
364
        except NoSuchRevision:
 
365
            revno = None
 
366
        return RevisionInfo(branch, revno, revision_b)
 
367
        
 
368
SPEC_TYPES.append(RevisionSpec_branch)