/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

Implement GitRepository.revision_graph_can_have_wrong_parents().

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