/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

Default to using global user id rather than making one up.

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
 
177
157
 
178
158
    @classmethod
179
159
    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"))
 
160
        return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
187
161
 
188
162
    def lookup_commit(self, revid):
189
163
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
270
244
                yield row[0].encode("utf-8")
271
245
 
272
246
 
273
 
TDB_MAP_VERSION = 2
274
 
TDB_HASH_SIZE = 10000
 
247
TDB_MAP_VERSION = 1
275
248
 
276
249
 
277
250
class TdbGitShaMap(GitShaMap):
292
265
            self.db = {}
293
266
        else:
294
267
            if not mapdbs().has_key(path):
295
 
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
 
268
                mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT, 
296
269
                                          os.O_RDWR|os.O_CREAT)
297
270
            self.db = mapdbs()[path]    
298
271
        if not "version" in self.db:
299
272
            self.db["version"] = str(TDB_MAP_VERSION)
300
273
        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)
 
274
            assert int(self.db["version"]) == TDB_MAP_VERSION
306
275
 
307
276
    @classmethod
308
277
    def from_repository(cls, repository):
312
281
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
313
282
        except bzrlib.errors.NotLocalUrl:
314
283
            pass
315
 
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
 
284
        from bzrlib.config import config_dir
 
285
        return cls(os.path.join(config_dir(), "remote-git.tdb"))
316
286
 
317
287
    def lookup_commit(self, revid):
318
 
        return sha_to_hex(self.db["commit\0" + revid][:20])
 
288
        return self.db["commit %s" % revid].split(" ")[0]
319
289
 
320
290
    def commit(self):
321
291
        pass
323
293
    def add_entry(self, sha, type, type_data):
324
294
        """Add a new entry to the database.
325
295
        """
326
 
        self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
 
296
        self.db["git %s" % sha] = "%s %s %s" % (type, type_data[0], type_data[1])
327
297
        if type == "commit":
328
 
            self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
 
298
            self.db["commit %s" % type_data[0]] = "%s %s" % (sha, type_data[1])
329
299
        else:
330
 
            self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
 
300
            self.db["%s %s %s" % (type, type_data[0], type_data[1])] = sha
331
301
 
332
302
    def lookup_tree(self, fileid, revid):
333
 
        return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
 
303
        return self.db["tree %s %s" % (fileid, revid)]
334
304
 
335
305
    def lookup_blob(self, fileid, revid):
336
 
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
 
306
        return self.db["blob %s %s" % (fileid, revid)]
337
307
 
338
308
    def lookup_git_sha(self, sha):
339
309
        """Lookup a Git sha in the database.
342
312
        :return: (type, type_data) with type_data:
343
313
            revision: revid, tree sha
344
314
        """
345
 
        data = self.db["git\0" + hex_to_sha(sha)].split("\0")
 
315
        data = self.db["git %s" % sha].split(" ")
346
316
        return (data[0], (data[1], data[2]))
347
317
 
348
318
    def revids(self):
349
319
        """List the revision ids known."""
350
320
        for key in self.db.iterkeys():
351
 
            if key.startswith("commit\0"):
352
 
                yield key[7:]
 
321
            if key.startswith("commit "):
 
322
                yield key.split(" ")[1]
353
323
 
354
324
    def sha1s(self):
355
325
        """List the SHA1s."""
356
326
        for key in self.db.iterkeys():
357
 
            if key.startswith("git\0"):
358
 
                yield sha_to_hex(key[4:])
 
327
            if key.startswith("git "):
 
328
                yield key.split(" ")[1]