/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 to_files() for git merge directives.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import bzrlib
27
27
from bzrlib import (
28
 
    registry,
29
28
    trace,
30
29
    )
31
 
from bzrlib.transport import (
32
 
    get_transport,
33
 
    )
34
30
 
35
31
 
36
32
def get_cache_dir():
46
42
    return ret
47
43
 
48
44
 
49
 
def get_remote_cache_transport():
50
 
    return get_transport(get_cache_dir())
51
 
 
52
 
 
53
45
def check_pysqlite_version(sqlite3):
54
46
    """Check that sqlite library is compatible.
55
47
 
86
78
class GitShaMap(object):
87
79
    """Git<->Bzr revision id mapping database."""
88
80
 
 
81
    def add_entry(self, sha, type, type_data):
 
82
        """Add a new entry to the database.
 
83
        """
 
84
        raise NotImplementedError(self.add_entry)
 
85
 
 
86
    def add_entries(self, entries):
 
87
        """Add multiple new entries to the database.
 
88
        """
 
89
        for e in entries:
 
90
            self.add_entry(*e)
 
91
 
 
92
    def lookup_tree(self, fileid, revid):
 
93
        """Lookup the SHA of a git tree."""
 
94
        raise NotImplementedError(self.lookup_tree)
 
95
 
 
96
    def lookup_blob(self, fileid, revid):
 
97
        """Lookup a blob by the fileid it has in a bzr revision."""
 
98
        raise NotImplementedError(self.lookup_blob)
 
99
 
89
100
    def lookup_git_sha(self, sha):
90
101
        """Lookup a Git sha in the database.
 
102
 
91
103
        :param sha: Git object sha
92
104
        :return: (type, type_data) with type_data:
93
105
            revision: revid, tree sha
94
106
        """
95
107
        raise NotImplementedError(self.lookup_git_sha)
96
108
 
97
 
    def lookup_blob_id(self, file_id, revision):
98
 
        """Retrieve a Git blob SHA by file id.
99
 
 
100
 
        :param file_id: File id of the file/symlink
101
 
        :param revision: revision in which the file was last changed.
102
 
        """
103
 
        raise NotImplementedError(self.lookup_blob_id)
104
 
 
105
 
    def lookup_tree_id(self, file_id, revision):
106
 
        """Retrieve a Git tree SHA by file id.
107
 
        """
108
 
        raise NotImplementedError(self.lookup_tree_id)
109
 
 
110
109
    def revids(self):
111
110
        """List the revision ids known."""
112
111
        raise NotImplementedError(self.revids)
132
131
        """Abort any pending changes."""
133
132
 
134
133
 
135
 
class ContentCache(object):
136
 
    """Object that can cache Git objects."""
137
 
 
138
 
    def __getitem__(self, sha):
139
 
        """Retrieve an item, by SHA."""
140
 
        raise NotImplementedError(self.__getitem__)
141
 
 
142
 
 
143
 
class BzrGitCacheFormat(object):
144
 
 
145
 
    def get_format_string(self):
146
 
        raise NotImplementedError(self.get_format_string)
147
 
 
148
 
    def open(self, transport):
149
 
        raise NotImplementedError(self.open)
150
 
 
151
 
    def initialize(self, transport):
152
 
        transport.put_bytes('format', self.get_format_string())
153
 
 
154
 
    @classmethod
155
 
    def from_repository(self, repository):
156
 
        repo_transport = getattr(repository, "_transport", None)
157
 
        if repo_transport is not None:
158
 
            try:
159
 
                repo_transport.mkdir('git')
160
 
            except bzrlib.errors.FileExists:
161
 
                pass
162
 
            transport = repo_transport.clone('git')
163
 
        else:
164
 
            transport = get_remote_cache_transport()
165
 
        try:
166
 
            format_name = transport.get_bytes('format')
167
 
            format = formats.get(format_name)
168
 
        except bzrlib.errors.NoSuchFile:
169
 
            format = formats.get('default')
170
 
            format.initialize(transport)
171
 
        return format.open(transport)
172
 
 
173
 
 
174
 
class CacheUpdater(object):
175
 
 
176
 
    def add_object(self, obj, ie):
177
 
        raise NotImplementedError(self.add_object)
178
 
 
179
 
    def finish(self):
180
 
        raise NotImplementedError(self.finish)
181
 
 
182
 
 
183
 
class BzrGitCache(object):
184
 
    """Caching backend."""
185
 
 
186
 
    def __init__(self, idmap, content_cache, cache_updater_klass):
187
 
        self.idmap = idmap
188
 
        self.content_cache = content_cache
189
 
        self._cache_updater_klass = cache_updater_klass
190
 
 
191
 
    def get_updater(self, rev):
192
 
        return self._cache_updater_klass(self, rev)
193
 
 
194
 
 
195
 
DictBzrGitCache = lambda: BzrGitCache(DictGitShaMap(), None, DictCacheUpdater)
196
 
 
197
 
 
198
 
class DictCacheUpdater(CacheUpdater):
199
 
 
200
 
    def __init__(self, cache, rev):
201
 
        self.cache = cache
202
 
        self.revid = rev.revision_id
203
 
        self.parent_revids = rev.parent_ids
204
 
        self._commit = None
205
 
        self._entries = []
206
 
 
207
 
    def add_object(self, obj, ie):
208
 
        if obj.type_name == "commit":
209
 
            self._commit = obj
210
 
            assert ie is None
211
 
            type_data = (self.revid, self._commit.tree)
212
 
        elif obj.type_name in ("blob", "tree"):
213
 
            if obj.type_name == "blob":
214
 
                revision = ie.revision
215
 
            else:
216
 
                revision = self.revid
217
 
            type_data = (ie.file_id, revision)
218
 
            self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
219
 
        else:
220
 
            raise AssertionError
221
 
        self.cache.idmap._by_sha[obj.id] = (obj.type_name, type_data)
222
 
 
223
 
    def finish(self):
224
 
        if self._commit is None:
225
 
            raise AssertionError("No commit object added")
226
 
        return self._commit
227
 
 
228
 
 
229
134
class DictGitShaMap(GitShaMap):
230
135
 
231
136
    def __init__(self):
232
 
        self._by_sha = {}
233
 
        self._by_fileid = {}
 
137
        self.dict = {}
234
138
 
235
 
    def lookup_blob_id(self, fileid, revision):
236
 
        return self._by_fileid[revision][fileid]
 
139
    def add_entry(self, sha, type, type_data):
 
140
        self.dict[sha] = (type, type_data)
237
141
 
238
142
    def lookup_git_sha(self, sha):
239
 
        return self._by_sha[sha]
240
 
 
241
 
    def lookup_tree_id(self, fileid, revision):
242
 
        return self._base._by_fileid[revision][fileid]
 
143
        return self.dict[sha]
 
144
 
 
145
    def lookup_tree(self, fileid, revid):
 
146
        for k, v in self.dict.iteritems():
 
147
            if v == ("tree", (fileid, revid)):
 
148
                return k
 
149
        raise KeyError((fileid, revid))
 
150
 
 
151
    def lookup_blob(self, fileid, revid):
 
152
        for k, v in self.dict.iteritems():
 
153
            if v == ("blob", (fileid, revid)):
 
154
                return k
 
155
        raise KeyError((fileid, revid))
243
156
 
244
157
    def revids(self):
245
 
        for key, (type, type_data) in self._by_sha.iteritems():
 
158
        for key, (type, type_data) in self.dict.iteritems():
246
159
            if type == "commit":
247
160
                yield type_data[0]
248
161
 
249
162
    def sha1s(self):
250
 
        return self._by_sha.iterkeys()
251
 
 
252
 
 
253
 
class SqliteCacheUpdater(CacheUpdater):
254
 
 
255
 
    def __init__(self, cache, rev):
256
 
        self.cache = cache
257
 
        self.db = self.cache.idmap.db
258
 
        self.revid = rev.revision_id
259
 
        self._commit = None
260
 
        self._trees = []
261
 
        self._blobs = []
262
 
 
263
 
    def add_object(self, obj, ie):
264
 
        if obj.type_name == "commit":
265
 
            self._commit = obj
266
 
            assert ie is None
267
 
        elif obj.type_name == "tree":
268
 
            self._trees.append((obj.id, ie.file_id, self.revid))
269
 
        elif obj.type_name == "blob":
270
 
            self._blobs.append((obj.id, ie.file_id, ie.revision))
271
 
        else:
272
 
            raise AssertionError
273
 
 
274
 
    def finish(self):
275
 
        if self._commit is None:
276
 
            raise AssertionError("No commit object added")
277
 
        self.db.executemany(
278
 
            "replace into trees (sha1, fileid, revid) values (?, ?, ?)",
279
 
            self._trees)
280
 
        self.db.executemany(
281
 
            "replace into blobs (sha1, fileid, revid) values (?, ?, ?)",
282
 
            self._blobs)
283
 
        self.db.execute(
284
 
            "replace into commits (sha1, revid, tree_sha) values (?, ?, ?)",
285
 
            (self._commit.id, self.revid, self._commit.tree))
286
 
        return self._commit
287
 
 
288
 
 
289
 
SqliteBzrGitCache = lambda p: BzrGitCache(SqliteGitShaMap(p), None, SqliteCacheUpdater)
290
 
 
291
 
 
292
 
class SqliteGitCacheFormat(BzrGitCacheFormat):
293
 
 
294
 
    def get_format_string(self):
295
 
        return 'bzr-git sha map version 1 using sqlite\n'
296
 
 
297
 
    def open(self, transport):
298
 
        try:
299
 
            basepath = transport.local_abspath(".")
300
 
        except bzrlib.errors.NotLocalUrl:
301
 
            basepath = get_cache_dir()
302
 
        return SqliteBzrGitCache(os.path.join(basepath, "idmap.db"))
 
163
        return self.dict.iterkeys()
303
164
 
304
165
 
305
166
class SqliteGitShaMap(GitShaMap):
329
190
        create index if not exists blobs_sha1 on blobs(sha1);
330
191
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
331
192
        create table if not exists trees(
332
 
            sha1 text unique not null check(length(sha1) == 40),
 
193
            sha1 text not null check(length(sha1) == 40),
333
194
            fileid text not null,
334
195
            revid text not null
335
196
        );
336
 
        create unique index if not exists trees_sha1 on trees(sha1);
 
197
        create index if not exists trees_sha1 on trees(sha1);
337
198
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
338
199
""")
339
200
 
340
 
    def __repr__(self):
341
 
        return "%s(%r)" % (self.__class__.__name__, self.path)
342
 
    
 
201
    @classmethod
 
202
    def from_repository(cls, repository):
 
203
        try:
 
204
            transport = getattr(repository, "_transport", None)
 
205
            if transport is not None:
 
206
                return cls(os.path.join(transport.local_abspath("."), "git.db"))
 
207
        except bzrlib.errors.NotLocalUrl:
 
208
            pass
 
209
        return cls(os.path.join(get_cache_dir(), "remote.db"))
 
210
 
343
211
    def lookup_commit(self, revid):
344
212
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
345
213
        if row is not None:
349
217
    def commit_write_group(self):
350
218
        self.db.commit()
351
219
 
352
 
    def lookup_blob_id(self, fileid, revision):
353
 
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
354
 
        if row is not None:
355
 
            return row[0]
356
 
        raise KeyError(fileid)
357
 
 
358
 
    def lookup_tree_id(self, fileid, revision):
359
 
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
360
 
        if row is not None:
361
 
            return row[0]
362
 
        raise KeyError(fileid)
 
220
    def add_entries(self, entries):
 
221
        trees = []
 
222
        blobs = []
 
223
        for sha, type, type_data in entries:
 
224
            assert isinstance(type_data[0], str)
 
225
            assert isinstance(type_data[1], str)
 
226
            entry = (sha, type_data[0], type_data[1])
 
227
            if type == "tree":
 
228
                trees.append(entry)
 
229
            elif type == "blob":
 
230
                blobs.append(entry)
 
231
            else:
 
232
                raise AssertionError
 
233
        if trees:
 
234
            self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
 
235
        if blobs:
 
236
            self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
 
237
 
 
238
 
 
239
    def add_entry(self, sha, type, type_data):
 
240
        """Add a new entry to the database.
 
241
        """
 
242
        assert isinstance(type_data, tuple)
 
243
        if sha is None:
 
244
            return
 
245
        assert isinstance(sha, str), "type was %r" % sha
 
246
        if type == "commit":
 
247
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
248
        elif type in ("blob", "tree"):
 
249
            self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
 
250
        else:
 
251
            raise AssertionError("Unknown type %s" % type)
 
252
 
 
253
    def lookup_tree(self, fileid, revid):
 
254
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
 
255
        if row is None:
 
256
            raise KeyError((fileid, revid))
 
257
        return row[0]
 
258
 
 
259
    def lookup_blob(self, fileid, revid):
 
260
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
 
261
        if row is None:
 
262
            raise KeyError((fileid, revid))
 
263
        return row[0]
363
264
 
364
265
    def lookup_git_sha(self, sha):
365
266
        """Lookup a Git sha in the database.
368
269
        :return: (type, type_data) with type_data:
369
270
            revision: revid, tree sha
370
271
        """
 
272
        def format(type, row):
 
273
            return (type, (row[0], row[1]))
371
274
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
372
275
        if row is not None:
373
 
            return ("commit", row)
 
276
            return format("commit", row)
374
277
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
375
278
        if row is not None:
376
 
            return ("blob", row)
 
279
            return format("blob", row)
377
280
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
378
281
        if row is not None:
379
 
            return ("tree", row)
 
282
            return format("tree", row)
380
283
        raise KeyError(sha)
381
284
 
382
285
    def revids(self):
383
286
        """List the revision ids known."""
384
 
        return (row for (row,) in self.db.execute("select revid from commits"))
 
287
        for row in self.db.execute("select revid from commits").fetchall():
 
288
            yield row[0]
385
289
 
386
290
    def sha1s(self):
387
291
        """List the SHA1s."""
388
292
        for table in ("blobs", "commits", "trees"):
389
 
            for (sha,) in self.db.execute("select sha1 from %s" % table):
390
 
                yield sha
391
 
 
392
 
 
393
 
class TdbCacheUpdater(CacheUpdater):
394
 
 
395
 
    def __init__(self, cache, rev):
396
 
        self.cache = cache
397
 
        self.db = cache.idmap.db
398
 
        self.revid = rev.revision_id
399
 
        self.parent_revids = rev.parent_ids
400
 
        self._commit = None
401
 
        self._entries = []
402
 
 
403
 
    def add_object(self, obj, ie):
404
 
        sha = obj.sha().digest()
405
 
        if obj.type_name == "commit":
406
 
            self.db["commit\0" + self.revid] = "\0".join((obj.id, obj.tree))
407
 
            type_data = (self.revid, obj.tree)
408
 
            self._commit = obj
409
 
            assert ie is None
410
 
        elif obj.type_name == "blob":
411
 
            self.db["\0".join(("blob", ie.file_id, ie.revision))] = sha
412
 
            type_data = (ie.file_id, ie.revision)
413
 
        elif obj.type_name == "tree":
414
 
            type_data = (ie.file_id, self.revid)
415
 
        else:
416
 
            raise AssertionError
417
 
        self.db["git\0" + sha] = "\0".join((obj.type_name,
418
 
            type_data[0], type_data[1]))
419
 
 
420
 
    def finish(self):
421
 
        if self._commit is None:
422
 
            raise AssertionError("No commit object added")
423
 
        return self._commit
424
 
 
425
 
 
426
 
TdbBzrGitCache = lambda p: BzrGitCache(TdbGitShaMap(p), None, TdbCacheUpdater)
427
 
 
428
 
class TdbGitCacheFormat(BzrGitCacheFormat):
429
 
 
430
 
    def get_format_string(self):
431
 
        return 'bzr-git sha map version 3 using tdb\n'
432
 
 
433
 
    def open(self, transport):
434
 
        try:
435
 
            basepath = transport.local_abspath(".")
436
 
        except bzrlib.errors.NotLocalUrl:
437
 
            basepath = get_cache_dir()
438
 
        try:
439
 
            return TdbBzrGitCache(os.path.join(basepath, "idmap.tdb"))
440
 
        except ImportError:
441
 
            raise ImportError(
442
 
                "Unable to open existing bzr-git cache because 'tdb' is not "
443
 
                "installed.")
 
293
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
 
294
                yield row[0]
 
295
 
 
296
 
 
297
TDB_MAP_VERSION = 2
 
298
TDB_HASH_SIZE = 50000
444
299
 
445
300
 
446
301
class TdbGitShaMap(GitShaMap):
454
309
    "blob fileid revid" -> "<sha1>"
455
310
    """
456
311
 
457
 
    TDB_MAP_VERSION = 3
458
 
    TDB_HASH_SIZE = 50000
459
 
 
460
312
    def __init__(self, path=None):
461
313
        import tdb
462
314
        self.path = path
464
316
            self.db = {}
465
317
        else:
466
318
            if not mapdbs().has_key(path):
467
 
                mapdbs()[path] = tdb.Tdb(path, self.TDB_HASH_SIZE, tdb.DEFAULT,
 
319
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
468
320
                                          os.O_RDWR|os.O_CREAT)
469
321
            self.db = mapdbs()[path]
470
322
        try:
471
 
            if int(self.db["version"]) not in (2, 3):
 
323
            if int(self.db["version"]) != TDB_MAP_VERSION:
472
324
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
473
 
                              self.db["version"], self.TDB_MAP_VERSION)
 
325
                              self.db["version"], TDB_MAP_VERSION)
474
326
                self.db.clear()
 
327
                self.db["version"] = str(TDB_MAP_VERSION)
475
328
        except KeyError:
 
329
            self.db["version"] = str(TDB_MAP_VERSION)
 
330
 
 
331
    @classmethod
 
332
    def from_repository(cls, repository):
 
333
        try:
 
334
            transport = getattr(repository, "_transport", None)
 
335
            if transport is not None:
 
336
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
 
337
        except bzrlib.errors.NotLocalUrl:
476
338
            pass
477
 
        self.db["version"] = str(self.TDB_MAP_VERSION)
478
 
 
479
 
    def start_write_group(self):
480
 
        """Start writing changes."""
481
 
        self.db.transaction_start()
482
 
 
483
 
    def commit_write_group(self):
484
 
        """Commit any pending changes."""
485
 
        self.db.transaction_commit()
486
 
 
487
 
    def abort_write_group(self):
488
 
        """Abort any pending changes."""
489
 
        self.db.transaction_cancel()
490
 
 
491
 
    def __repr__(self):
492
 
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
339
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
493
340
 
494
341
    def lookup_commit(self, revid):
495
342
        return sha_to_hex(self.db["commit\0" + revid][:20])
496
343
 
497
 
    def lookup_blob_id(self, fileid, revision):
498
 
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
499
 
                
 
344
    def add_entry(self, hexsha, type, type_data):
 
345
        """Add a new entry to the database.
 
346
        """
 
347
        if hexsha is None:
 
348
            sha = ""
 
349
        else:
 
350
            sha = hex_to_sha(hexsha)
 
351
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
 
352
        if type == "commit":
 
353
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
 
354
        else:
 
355
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
 
356
 
 
357
    def lookup_tree(self, fileid, revid):
 
358
        sha = self.db["\0".join(("tree", fileid, revid))]
 
359
        if sha == "":
 
360
            return None
 
361
        else:
 
362
            return sha_to_hex(sha)
 
363
 
 
364
    def lookup_blob(self, fileid, revid):
 
365
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
 
366
 
500
367
    def lookup_git_sha(self, sha):
501
368
        """Lookup a Git sha in the database.
502
369
 
527
394
        for key in self.db.iterkeys():
528
395
            if key.startswith("git\0"):
529
396
                yield sha_to_hex(key[4:])
530
 
 
531
 
 
532
 
formats = registry.Registry()
533
 
formats.register(TdbGitCacheFormat().get_format_string(),
534
 
    TdbGitCacheFormat())
535
 
formats.register(SqliteGitCacheFormat().get_format_string(),
536
 
    SqliteGitCacheFormat())
537
 
try:
538
 
    import tdb
539
 
except ImportError:
540
 
    formats.register('default', SqliteGitCacheFormat())
541
 
else:
542
 
    formats.register('default', TdbGitCacheFormat())
543
 
 
544
 
 
545
 
def migrate_ancient_formats(repo_transport):
546
 
    if repo_transport.has("git.tdb"):
547
 
        TdbGitCacheFormat().initialize(repo_transport.clone("git"))
548
 
        repo_transport.rename("git.tdb", "git/idmap.tdb")
549
 
    elif repo_transport.has("git.db"):
550
 
        SqliteGitCacheFormat().initialize(repo_transport.clone("git"))
551
 
        repo_transport.rename("git.db", "git/idmap.db")
552
 
 
553
 
 
554
 
def from_repository(repository):
555
 
    repo_transport = getattr(repository, "_transport", None)
556
 
    if repo_transport is not None:
557
 
        # Migrate older cache formats
558
 
        try:
559
 
            repo_transport.mkdir("git")
560
 
        except bzrlib.errors.FileExists:
561
 
            pass
562
 
        else:
563
 
            migrate_ancient_formats(repo_transport)
564
 
    return BzrGitCacheFormat.from_repository(repository)