/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to shamap.py

Simply refer to bzr's docs in HACKING.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Map from Git sha's to Bazaar objects."""
18
18
 
 
19
from dulwich.objects import (
 
20
    sha_to_hex,
 
21
    hex_to_sha,
 
22
    )
19
23
import os
20
24
import threading
21
25
 
22
26
import bzrlib
23
 
from bzrlib.errors import (
24
 
    NoSuchRevision,
 
27
from bzrlib import (
 
28
    trace,
25
29
    )
26
30
 
27
31
 
 
32
def get_cache_dir():
 
33
    try:
 
34
        from xdg.BaseDirectory import xdg_cache_home
 
35
    except ImportError:
 
36
        from bzrlib.config import config_dir
 
37
        ret = os.path.join(config_dir(), "git")
 
38
    else:
 
39
        ret = os.path.join(xdg_cache_home, "bazaar", "git")
 
40
    if not os.path.isdir(ret):
 
41
        os.makedirs(ret)
 
42
    return ret
 
43
 
 
44
 
28
45
def check_pysqlite_version(sqlite3):
29
46
    """Check that sqlite library is compatible.
30
47
 
32
49
    if (sqlite3.sqlite_version_info[0] < 3 or 
33
50
            (sqlite3.sqlite_version_info[0] == 3 and 
34
51
             sqlite3.sqlite_version_info[1] < 3)):
35
 
        warning('Needs at least sqlite 3.3.x')
 
52
        trace.warning('Needs at least sqlite 3.3.x')
36
53
        raise bzrlib.errors.BzrError("incompatible sqlite library")
37
54
 
38
55
try:
43
60
        from pysqlite2 import dbapi2 as sqlite3
44
61
        check_pysqlite_version(sqlite3)
45
62
except:
46
 
    warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
 
63
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
47
64
            'module')
48
65
    raise bzrlib.errors.BzrError("missing sqlite library")
49
66
 
93
110
        """List the revision ids known."""
94
111
        raise NotImplementedError(self.revids)
95
112
 
96
 
    def sha1s(Self):
 
113
    def sha1s(self):
97
114
        """List the SHA1s."""
98
115
        raise NotImplementedError(self.sha1s)
99
116
 
157
174
 
158
175
    @classmethod
159
176
    def from_repository(cls, repository):
160
 
        return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
 
177
        try:
 
178
            transport = getattr(repository, "_transport", None)
 
179
            if transport is not None:
 
180
                return cls(os.path.join(transport.local_abspath("."), "git.db"))
 
181
        except bzrlib.errors.NotLocalUrl:
 
182
            pass
 
183
        return cls(os.path.join(get_cache_dir(), "remote.db"))
161
184
 
162
185
    def lookup_commit(self, revid):
163
186
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
244
267
                yield row[0].encode("utf-8")
245
268
 
246
269
 
247
 
TDB_MAP_VERSION = 1
 
270
TDB_MAP_VERSION = 2
 
271
TDB_HASH_SIZE = 50000
248
272
 
249
273
 
250
274
class TdbGitShaMap(GitShaMap):
265
289
            self.db = {}
266
290
        else:
267
291
            if not mapdbs().has_key(path):
268
 
                mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT, 
 
292
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
269
293
                                          os.O_RDWR|os.O_CREAT)
270
294
            self.db = mapdbs()[path]    
271
295
        if not "version" in self.db:
272
296
            self.db["version"] = str(TDB_MAP_VERSION)
273
297
        else:
274
 
            assert int(self.db["version"]) == TDB_MAP_VERSION
 
298
            if int(self.db["version"]) != TDB_MAP_VERSION:
 
299
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
 
300
                              self.db["version"], TDB_MAP_VERSION)
 
301
                self.db.clear()
 
302
            self.db["version"] = str(TDB_MAP_VERSION)
275
303
 
276
304
    @classmethod
277
305
    def from_repository(cls, repository):
281
309
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
282
310
        except bzrlib.errors.NotLocalUrl:
283
311
            pass
284
 
        from bzrlib.config import config_dir
285
 
        return cls(os.path.join(config_dir(), "remote-git.tdb"))
 
312
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
286
313
 
287
314
    def lookup_commit(self, revid):
288
 
        return self.db["commit %s" % revid].split(" ")[0]
 
315
        return sha_to_hex(self.db["commit\0" + revid][:20])
289
316
 
290
317
    def commit(self):
291
318
        pass
292
319
 
293
 
    def add_entry(self, sha, type, type_data):
 
320
    def add_entry(self, hexsha, type, type_data):
294
321
        """Add a new entry to the database.
295
322
        """
296
 
        self.db["git %s" % sha] = "%s %s %s" % (type, type_data[0], type_data[1])
 
323
        if hexsha is None:
 
324
            sha = ""
 
325
        else:
 
326
            sha = hex_to_sha(hexsha)
 
327
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
297
328
        if type == "commit":
298
 
            self.db["commit %s" % type_data[0]] = "%s %s" % (sha, type_data[1])
 
329
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
299
330
        else:
300
 
            self.db["%s %s %s" % (type, type_data[0], type_data[1])] = sha
 
331
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
301
332
 
302
333
    def lookup_tree(self, fileid, revid):
303
 
        return self.db["tree %s %s" % (fileid, revid)]
 
334
        sha = self.db["\0".join(("tree", fileid, revid))]
 
335
        if sha == "":
 
336
            return None
 
337
        else:
 
338
            return sha_to_hex(sha)
304
339
 
305
340
    def lookup_blob(self, fileid, revid):
306
 
        return self.db["blob %s %s" % (fileid, revid)]
 
341
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
307
342
 
308
343
    def lookup_git_sha(self, sha):
309
344
        """Lookup a Git sha in the database.
312
347
        :return: (type, type_data) with type_data:
313
348
            revision: revid, tree sha
314
349
        """
315
 
        data = self.db["git %s" % sha].split(" ")
 
350
        if len(sha) == 40:
 
351
            sha = hex_to_sha(sha)
 
352
        data = self.db["git\0" + sha].split("\0")
316
353
        return (data[0], (data[1], data[2]))
317
354
 
318
355
    def revids(self):
319
356
        """List the revision ids known."""
320
357
        for key in self.db.iterkeys():
321
 
            if key.startswith("commit "):
322
 
                yield key.split(" ")[1]
 
358
            if key.startswith("commit\0"):
 
359
                yield key[7:]
323
360
 
324
361
    def sha1s(self):
325
362
        """List the SHA1s."""
326
363
        for key in self.db.iterkeys():
327
 
            if key.startswith("git "):
328
 
                yield key.split(" ")[1]
 
364
            if key.startswith("git\0"):
 
365
                yield sha_to_hex(key[4:])