~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-18 18:17:19 UTC
  • mfrom: (1711.2.133 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060818181719-90004a4648d8537a
(cfbolz,hpk,robertc,jam) Add SFTP benchmark tests, and tests across a delayed socket

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
2
#
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.
7
 
 
 
7
#
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.
12
 
 
 
12
#
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
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)):
148
149
            raise NoSuchRevision(branch, str(self.spec))
149
150
 
150
151
    def in_history(self, branch):
151
 
        revs = branch.revision_history()
 
152
        if branch:
 
153
            revs = branch.revision_history()
 
154
        else:
 
155
            revs = None
152
156
        return self._match_on_and_check(branch, revs)
153
157
 
 
158
        # FIXME: in_history is somewhat broken,
 
159
        # it will return non-history revisions in many
 
160
        # circumstances. The expected facility is that
 
161
        # in_history only returns revision-history revs,
 
162
        # in_store returns any rev. RBC 20051010
 
163
    # aliases for now, when we fix the core logic, then they
 
164
    # will do what you expect.
 
165
    in_store = in_history
 
166
    in_branch = in_store
 
167
        
154
168
    def __repr__(self):
155
169
        # this is mostly for helping with testing
156
170
        return '<%s %s%s>' % (self.__class__.__name__,
157
171
                              self.prefix or '',
158
172
                              self.spec)
 
173
    
 
174
    def needs_branch(self):
 
175
        """Whether this revision spec needs a branch.
159
176
 
 
177
        Set this to False the branch argument of _match_on is not used.
 
178
        """
 
179
        return True
160
180
 
161
181
# private API
162
182
 
179
199
 
180
200
    def _match_on(self, branch, revs):
181
201
        """Lookup a revision by revision number"""
182
 
        try:
183
 
            return RevisionInfo(branch, int(self.spec))
184
 
        except ValueError:
185
 
            return RevisionInfo(branch, None)
 
202
        if self.spec.find(':') == -1:
 
203
            try:
 
204
                return RevisionInfo(branch, int(self.spec))
 
205
            except ValueError:
 
206
                return RevisionInfo(branch, None)
 
207
        else:
 
208
            from branch import Branch
 
209
            revname = self.spec[self.spec.find(':')+1:]
 
210
            other_branch = Branch.open_containing(revname)[0]
 
211
            try:
 
212
                revno = int(self.spec[:self.spec.find(':')])
 
213
            except ValueError:
 
214
                return RevisionInfo(other_branch, None)
 
215
            revid = other_branch.get_rev_id(revno)
 
216
            return RevisionInfo(other_branch, revno)
 
217
        
 
218
    def needs_branch(self):
 
219
        return self.spec.find(':') == -1
186
220
 
187
221
SPEC_TYPES.append(RevisionSpec_revno)
188
222
 
194
228
        try:
195
229
            return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
196
230
        except ValueError:
197
 
            return RevisionInfo(branch, None)
 
231
            return RevisionInfo(branch, None, self.spec)
198
232
 
199
233
SPEC_TYPES.append(RevisionSpec_revid)
200
234
 
238
272
SPEC_TYPES.append(RevisionSpec_tag)
239
273
 
240
274
 
 
275
class RevisionSpec_revs:
 
276
    def __init__(self, revs, branch):
 
277
        self.revs = revs
 
278
        self.branch = branch
 
279
    def __getitem__(self, index):
 
280
        r = self.branch.repository.get_revision(self.revs[index])
 
281
        # TODO: Handle timezone.
 
282
        return datetime.datetime.fromtimestamp(r.timestamp)
 
283
    def __len__(self):
 
284
        return len(self.revs)
 
285
 
 
286
 
241
287
class RevisionSpec_date(RevisionSpec):
242
288
    prefix = 'date:'
243
289
    _date_re = re.compile(
255
301
          at a specified time).
256
302
 
257
303
          So the proper way of saying 'give me all entries for today' is:
258
 
              -r date:today..date:tomorrow
 
304
              -r date:yesterday..date:today
259
305
        """
260
306
        today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
261
307
        if self.spec.lower() == 'yesterday':
285
331
 
286
332
            dt = datetime.datetime(year=year, month=month, day=day,
287
333
                    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)
 
334
        branch.lock_read()
 
335
        try:
 
336
            rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
 
337
        finally:
 
338
            branch.unlock()
 
339
        if rev == len(revs):
 
340
            return RevisionInfo(branch, None)
 
341
        else:
 
342
            return RevisionInfo(branch, rev + 1)
296
343
 
297
344
SPEC_TYPES.append(RevisionSpec_date)
298
345
 
303
350
    def _match_on(self, branch, revs):
304
351
        from branch import Branch
305
352
        from revision import common_ancestor, MultipleRevisionSources
306
 
        other_branch = Branch.open_containing(self.spec)
 
353
        other_branch = Branch.open_containing(self.spec)[0]
307
354
        revision_a = branch.last_revision()
308
355
        revision_b = other_branch.last_revision()
309
356
        for r, b in ((revision_a, branch), (revision_b, other_branch)):
310
357
            if r is None:
311
358
                raise NoCommits(b)
312
 
        revision_source = MultipleRevisionSources(branch, other_branch)
 
359
        revision_source = MultipleRevisionSources(branch.repository,
 
360
                                                  other_branch.repository)
313
361
        rev_id = common_ancestor(revision_a, revision_b, revision_source)
314
362
        try:
315
363
            revno = branch.revision_id_to_revno(rev_id)
318
366
        return RevisionInfo(branch, revno, rev_id)
319
367
        
320
368
SPEC_TYPES.append(RevisionSpec_ancestor)
 
369
 
 
370
class RevisionSpec_branch(RevisionSpec):
 
371
    """A branch: revision specifier.
 
372
 
 
373
    This takes the path to a branch and returns its tip revision id.
 
374
    """
 
375
    prefix = 'branch:'
 
376
 
 
377
    def _match_on(self, branch, revs):
 
378
        from branch import Branch
 
379
        other_branch = Branch.open_containing(self.spec)[0]
 
380
        revision_b = other_branch.last_revision()
 
381
        if revision_b is None:
 
382
            raise NoCommits(other_branch)
 
383
        # pull in the remote revisions so we can diff
 
384
        branch.fetch(other_branch, revision_b)
 
385
        try:
 
386
            revno = branch.revision_id_to_revno(revision_b)
 
387
        except NoSuchRevision:
 
388
            revno = None
 
389
        return RevisionInfo(branch, revno, revision_b)
 
390
        
 
391
SPEC_TYPES.append(RevisionSpec_branch)