/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

Update docs.

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
 
    )
23
19
import os
24
20
import threading
25
21
 
26
22
import bzrlib
27
 
from bzrlib import (
28
 
    trace,
29
 
    )
30
23
from bzrlib.errors import (
31
24
    NoSuchRevision,
32
25
    )
33
26
 
34
27
 
35
 
def get_cache_dir():
36
 
    try:
37
 
        from xdg.BaseDirectory import xdg_cache_home
38
 
    except ImportError:
39
 
        from bzrlib.config import config_dir
40
 
        ret = os.path.join(config_dir(), "git")
41
 
    else:
42
 
        ret = os.path.join(xdg_cache_home, "bazaar", "git")
43
 
    if not os.path.isdir(ret):
44
 
        os.makedirs(ret)
45
 
    return ret
46
 
 
47
 
 
48
28
def check_pysqlite_version(sqlite3):
49
29
    """Check that sqlite library is compatible.
50
30
 
97
77
        raise NotImplementedError(self.lookup_tree)
98
78
 
99
79
    def lookup_blob(self, fileid, revid):
100
 
        """Lookup a blob by the fileid it has in a bzr revision."""
101
80
        raise NotImplementedError(self.lookup_blob)
102
81
 
103
82
    def lookup_git_sha(self, sha):
177
156
 
178
157
    @classmethod
179
158
    def from_repository(cls, repository):
180
 
        try:
181
 
            transport = getattr(repository, "_transport", None)
182
 
            if transport is not None:
183
 
                return cls(os.path.join(transport.local_abspath("."), "git.db"))
184
 
        except bzrlib.errors.NotLocalUrl:
185
 
            pass
186
 
        return cls(os.path.join(get_cache_dir(), "remote.db"))
 
159
        return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
187
160
 
188
 
    def lookup_commit(self, revid):
 
161
    def _parent_lookup(self, revid):
189
162
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
190
163
        if row is not None:
191
164
            return row[0].encode("utf-8")
268
241
        for table in ("blobs", "commits", "trees"):
269
242
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
270
243
                yield row[0].encode("utf-8")
271
 
 
272
 
 
273
 
TDB_MAP_VERSION = 2
274
 
TDB_HASH_SIZE = 10000
275
 
 
276
 
 
277
 
class TdbGitShaMap(GitShaMap):
278
 
    """SHA Map that uses a TDB database.
279
 
 
280
 
    Entries:
281
 
 
282
 
    "git <sha1>" -> "<type> <type-data1> <type-data2>"
283
 
    "commit revid" -> "<sha1> <tree-id>"
284
 
    "tree fileid revid" -> "<sha1>"
285
 
    "blob fileid revid" -> "<sha1>"
286
 
    """
287
 
 
288
 
    def __init__(self, path=None):
289
 
        import tdb
290
 
        self.path = path
291
 
        if path is None:
292
 
            self.db = {}
293
 
        else:
294
 
            if not mapdbs().has_key(path):
295
 
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
296
 
                                          os.O_RDWR|os.O_CREAT)
297
 
            self.db = mapdbs()[path]    
298
 
        if not "version" in self.db:
299
 
            self.db["version"] = str(TDB_MAP_VERSION)
300
 
        else:
301
 
            if int(self.db["version"]) != TDB_MAP_VERSION:
302
 
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
303
 
                              self.db["version"], TDB_MAP_VERSION)
304
 
                self.db.clear()
305
 
            self.db["version"] = str(TDB_MAP_VERSION)
306
 
 
307
 
    @classmethod
308
 
    def from_repository(cls, repository):
309
 
        try:
310
 
            transport = getattr(repository, "_transport", None)
311
 
            if transport is not None:
312
 
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
313
 
        except bzrlib.errors.NotLocalUrl:
314
 
            pass
315
 
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
316
 
 
317
 
    def lookup_commit(self, revid):
318
 
        return sha_to_hex(self.db["commit\0" + revid][:20])
319
 
 
320
 
    def commit(self):
321
 
        pass
322
 
 
323
 
    def add_entry(self, sha, type, type_data):
324
 
        """Add a new entry to the database.
325
 
        """
326
 
        self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
327
 
        if type == "commit":
328
 
            self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
329
 
        else:
330
 
            self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
331
 
 
332
 
    def lookup_tree(self, fileid, revid):
333
 
        return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
334
 
 
335
 
    def lookup_blob(self, fileid, revid):
336
 
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
337
 
 
338
 
    def lookup_git_sha(self, sha):
339
 
        """Lookup a Git sha in the database.
340
 
 
341
 
        :param sha: Git object sha
342
 
        :return: (type, type_data) with type_data:
343
 
            revision: revid, tree sha
344
 
        """
345
 
        data = self.db["git\0" + hex_to_sha(sha)].split("\0")
346
 
        return (data[0], (data[1], data[2]))
347
 
 
348
 
    def revids(self):
349
 
        """List the revision ids known."""
350
 
        for key in self.db.iterkeys():
351
 
            if key.startswith("commit\0"):
352
 
                yield key[7:]
353
 
 
354
 
    def sha1s(self):
355
 
        """List the SHA1s."""
356
 
        for key in self.db.iterkeys():
357
 
            if key.startswith("git\0"):
358
 
                yield sha_to_hex(key[4:])