/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 transportgit.py

Merge changes to avoid inventories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from cStringIO import StringIO
20
20
 
 
21
import os
 
22
 
21
23
from dulwich.errors import (
22
24
    NotGitRepository,
23
25
    NoIndexPresent,
33
35
    MemoryPackIndex,
34
36
    PackData,
35
37
    Pack,
36
 
    ThinPackData,
37
38
    iter_sha1,
38
39
    load_pack_index_file,
39
40
    write_pack_data,
42
43
from dulwich.repo import (
43
44
    BaseRepo,
44
45
    RefsContainer,
 
46
    BASE_DIRECTORIES,
45
47
    INDEX_FILENAME,
46
48
    OBJECTDIR,
47
49
    REFSDIR,
52
54
    write_packed_refs,
53
55
    )
54
56
 
 
57
from bzrlib import (
 
58
    transport as _mod_transport,
 
59
    )
55
60
from bzrlib.errors import (
56
61
    FileExists,
57
62
    NoSuchFile,
93
98
 
94
99
    def allkeys(self):
95
100
        keys = set()
96
 
        if self.transport.has("HEAD"):
 
101
        try:
 
102
            self.transport.get_bytes("HEAD")
 
103
        except NoSuchFile:
 
104
            pass
 
105
        else:
97
106
            keys.add("HEAD")
98
107
        try:
99
108
            iter_files = list(self.transport.clone("refs").iter_files_recursive())
276
285
        self._remove_packed_ref(name)
277
286
        return True
278
287
 
 
288
    def get(self, name, default=None):
 
289
        try:
 
290
            return self[name]
 
291
        except KeyError:
 
292
            return default
 
293
 
279
294
 
280
295
class TransportRepo(BaseRepo):
281
296
 
282
 
    def __init__(self, transport):
 
297
    def __init__(self, transport, bare, refs_text=None):
283
298
        self.transport = transport
284
 
        try:
285
 
            if self.transport.has(".git/%s" % OBJECTDIR):
286
 
                self.bare = False
287
 
                self._controltransport = self.transport.clone('.git')
288
 
            elif self.transport.has_any(["info/refs", OBJECTDIR, REFSDIR]):
289
 
                self.bare = True
290
 
                self._controltransport = self.transport
291
 
            else:
292
 
                raise NotGitRepository(self.transport)
293
 
        except NoSuchFile:
294
 
            raise NotGitRepository(self.transport)
 
299
        self.bare = bare
 
300
        if self.bare:
 
301
            self._controltransport = self.transport
 
302
        else:
 
303
            self._controltransport = self.transport.clone('.git')
295
304
        object_store = TransportObjectStore(
296
305
            self._controltransport.clone(OBJECTDIR))
 
306
        if refs_text is not None:
 
307
            from dulwich.repo import InfoRefsContainer # dulwich >= 0.8.2
 
308
            refs_container = InfoRefsContainer(StringIO(refs_text))
 
309
        else:
 
310
            refs_container = TransportRefsContainer(self._controltransport)
297
311
        super(TransportRepo, self).__init__(object_store, 
298
 
                TransportRefsContainer(self._controltransport))
 
312
                refs_container)
299
313
 
300
314
    def get_named_file(self, path):
301
315
        """Get a file from the control dir with a specific name.
312
326
        except NoSuchFile:
313
327
            return None
314
328
 
 
329
    def _put_named_file(self, relpath, contents):
 
330
        self._controltransport.put_bytes(relpath, contents)
 
331
 
315
332
    def index_path(self):
316
333
        """Return the path to the index file."""
317
334
        return self._controltransport.local_abspath(INDEX_FILENAME)
329
346
        # missing index file, which is treated as empty.
330
347
        return not self.bare
331
348
 
 
349
    def get_config(self):
 
350
        from dulwich.config import ConfigFile
 
351
        try:
 
352
            return ConfigFile.from_file(self._controltransport.get('config'))
 
353
        except NoSuchFile:
 
354
            return ConfigFile()
 
355
 
 
356
    def get_config_stack(self):
 
357
        from dulwich.config import StackedConfig
 
358
        backends = []
 
359
        p = self.get_config()
 
360
        if p is not None:
 
361
            backends.append(p)
 
362
            writable = p
 
363
        else:
 
364
            writable = None
 
365
        backends.extend(StackedConfig.default_backends())
 
366
        return StackedConfig(backends, writable=writable)
 
367
 
332
368
    def __repr__(self):
333
369
        return "<%s for %r>" % (self.__class__.__name__, self.transport)
334
370
 
 
371
    @classmethod
 
372
    def init(cls, transport, bare=False):
 
373
        if not bare:
 
374
            transport.mkdir(".git")
 
375
            control_transport = transport.clone(".git")
 
376
        else:
 
377
            control_transport = transport
 
378
        for d in BASE_DIRECTORIES:
 
379
            control_transport.mkdir("/".join(d))
 
380
        control_transport.mkdir(OBJECTDIR)
 
381
        TransportObjectStore.init(control_transport.clone(OBJECTDIR))
 
382
        ret = cls(transport, bare)
 
383
        ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
 
384
        ret._init_files(bare)
 
385
        return ret
 
386
 
335
387
 
336
388
class TransportObjectStore(PackBasedObjectStore):
337
389
    """Git-style object store that exists on disk."""
344
396
        super(TransportObjectStore, self).__init__()
345
397
        self.transport = transport
346
398
        self.pack_transport = self.transport.clone(PACKDIR)
 
399
        self._alternates = None
347
400
 
348
401
    def __repr__(self):
349
402
        return "%s(%r)" % (self.__class__.__name__, self.transport)
351
404
    def _pack_cache_stale(self):
352
405
        return False # FIXME
353
406
 
 
407
    @property
 
408
    def alternates(self):
 
409
        if self._alternates is not None:
 
410
            return self._alternates
 
411
        self._alternates = []
 
412
        for path in self._read_alternate_paths():
 
413
            # FIXME: Check path
 
414
            t = _mod_transport.get_transport_from_path(path)
 
415
            self._alternates.append(self.__class__(t))
 
416
        return self._alternates
 
417
 
 
418
    def _read_alternate_paths(self):
 
419
        try:
 
420
            f = self.transport.get("info/alternates")
 
421
        except NoSuchFile:
 
422
            return []
 
423
        ret = []
 
424
        try:
 
425
            for l in f.readlines():
 
426
                l = l.rstrip("\n")
 
427
                if l[0] == "#":
 
428
                    continue
 
429
                if os.path.isabs(l):
 
430
                    continue
 
431
                ret.append(l)
 
432
            return ret
 
433
        finally:
 
434
            f.close()
 
435
 
354
436
    def _pack_names(self):
355
437
        try:
356
438
            f = self.transport.get('info/packs')
473
555
        :param path: Path to the pack file.
474
556
        """
475
557
        f.seek(0)
476
 
        data = ThinPackData.from_file(self.get_raw, f, len(f.getvalue()))
 
558
        data = PackData.from_file(self.get_raw, f, len(f.getvalue()))
477
559
        idx = MemoryPackIndex(data.sorted_entries(), data.get_stored_checksum())
478
560
        p = Pack.from_objects(data, idx)
479
561
 
480
562
        pack_sha = idx.objects_sha1()
481
563
 
482
 
        datafile = self.pack_transport.open_write_stream("pack-%s.pack" % pack_sha)
 
564
        datafile = self.pack_transport.open_write_stream(
 
565
                "pack-%s.pack" % pack_sha)
483
566
        try:
484
 
            entries, data_sum = write_pack_data(datafile, ((o, None) for o in p.iterobjects()), len(p))
 
567
            entries, data_sum = write_pack_data(datafile, p.pack_tuples())
485
568
        finally:
486
569
            datafile.close()
487
570
        entries.sort()
488
 
        idxfile = self.pack_transport.open_write_stream("pack-%s.idx" % pack_sha)
 
571
        idxfile = self.pack_transport.open_write_stream(
 
572
            "pack-%s.idx" % pack_sha)
489
573
        try:
490
574
            write_pack_index_v2(idxfile, data.sorted_entries(), data_sum)
491
575
        finally:
494
578
        self._add_known_pack(final_pack)
495
579
        return final_pack
496
580
 
497
 
 
498
 
 
499
581
    def add_pack(self):
500
582
        """Add a new pack to this object store. 
501
583