/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

Support reading .git files.

Merged from https://code.launchpad.net/~jelmer/brz-git/read-gitfile/+merge/342256

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""A Git repository implementation that uses a Bazaar transport."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from cStringIO import StringIO
 
22
 
 
23
import os
 
24
import sys
 
25
import urllib
 
26
 
 
27
from dulwich.errors import (
 
28
    NotGitRepository,
 
29
    NoIndexPresent,
 
30
    )
 
31
from dulwich.objects import (
 
32
    ShaFile,
 
33
    )
 
34
from dulwich.object_store import (
 
35
    PackBasedObjectStore,
 
36
    PACKDIR,
 
37
    )
 
38
from dulwich.pack import (
 
39
    MemoryPackIndex,
 
40
    PackData,
 
41
    Pack,
 
42
    iter_sha1,
 
43
    load_pack_index_file,
 
44
    write_pack_data,
 
45
    write_pack_index_v2,
 
46
    )
 
47
from dulwich.repo import (
 
48
    BaseRepo,
 
49
    InfoRefsContainer,
 
50
    RefsContainer,
 
51
    BASE_DIRECTORIES,
 
52
    COMMONDIR,
 
53
    CONTROLDIR,
 
54
    INDEX_FILENAME,
 
55
    OBJECTDIR,
 
56
    REFSDIR,
 
57
    SYMREF,
 
58
    check_ref_format,
 
59
    read_gitfile,
 
60
    read_packed_refs_with_peeled,
 
61
    read_packed_refs,
 
62
    write_packed_refs,
 
63
    )
 
64
 
 
65
from ... import (
 
66
    transport as _mod_transport,
 
67
    )
 
68
from ...errors import (
 
69
    AlreadyControlDirError,
 
70
    FileExists,
 
71
    LockError,
 
72
    NoSuchFile,
 
73
    ReadError,
 
74
    TransportNotPossible,
 
75
    )
 
76
 
 
77
 
 
78
class TransportRefsContainer(RefsContainer):
 
79
    """Refs container that reads refs from a transport."""
 
80
 
 
81
    def __init__(self, transport, worktree_transport=None):
 
82
        self.transport = transport
 
83
        if worktree_transport is None:
 
84
            worktree_transport = transport
 
85
        self.worktree_transport = worktree_transport
 
86
        self._packed_refs = None
 
87
        self._peeled_refs = None
 
88
 
 
89
    def __repr__(self):
 
90
        return "%s(%r)" % (self.__class__.__name__, self.transport)
 
91
 
 
92
    def _ensure_dir_exists(self, path):
 
93
        for n in range(path.count("/")):
 
94
            dirname = "/".join(path.split("/")[:n+1])
 
95
            try:
 
96
                self.transport.mkdir(dirname)
 
97
            except FileExists:
 
98
                pass
 
99
 
 
100
    def subkeys(self, base):
 
101
        """Refs present in this container under a base.
 
102
 
 
103
        :param base: The base to return refs under.
 
104
        :return: A set of valid refs in this container under the base; the base
 
105
            prefix is stripped from the ref names returned.
 
106
        """
 
107
        keys = set()
 
108
        base_len = len(base) + 1
 
109
        for refname in self.allkeys():
 
110
            if refname.startswith(base):
 
111
                keys.add(refname[base_len:])
 
112
        return keys
 
113
 
 
114
    def allkeys(self):
 
115
        keys = set()
 
116
        try:
 
117
            self.worktree_transport.get_bytes("HEAD")
 
118
        except NoSuchFile:
 
119
            pass
 
120
        else:
 
121
            keys.add("HEAD")
 
122
        try:
 
123
            iter_files = list(self.transport.clone("refs").iter_files_recursive())
 
124
            for filename in iter_files:
 
125
                refname = "refs/%s" % urllib.unquote(filename)
 
126
                if check_ref_format(refname):
 
127
                    keys.add(refname)
 
128
        except (TransportNotPossible, NoSuchFile):
 
129
            pass
 
130
        keys.update(self.get_packed_refs())
 
131
        return keys
 
132
 
 
133
    def get_packed_refs(self):
 
134
        """Get contents of the packed-refs file.
 
135
 
 
136
        :return: Dictionary mapping ref names to SHA1s
 
137
 
 
138
        :note: Will return an empty dictionary when no packed-refs file is
 
139
            present.
 
140
        """
 
141
        # TODO: invalidate the cache on repacking
 
142
        if self._packed_refs is None:
 
143
            # set both to empty because we want _peeled_refs to be
 
144
            # None if and only if _packed_refs is also None.
 
145
            self._packed_refs = {}
 
146
            self._peeled_refs = {}
 
147
            try:
 
148
                f = self.transport.get("packed-refs")
 
149
            except NoSuchFile:
 
150
                return {}
 
151
            try:
 
152
                first_line = iter(f).next().rstrip()
 
153
                if (first_line.startswith("# pack-refs") and " peeled" in
 
154
                        first_line):
 
155
                    for sha, name, peeled in read_packed_refs_with_peeled(f):
 
156
                        self._packed_refs[name] = sha
 
157
                        if peeled:
 
158
                            self._peeled_refs[name] = peeled
 
159
                else:
 
160
                    f.seek(0)
 
161
                    for sha, name in read_packed_refs(f):
 
162
                        self._packed_refs[name] = sha
 
163
            finally:
 
164
                f.close()
 
165
        return self._packed_refs
 
166
 
 
167
    def get_peeled(self, name):
 
168
        """Return the cached peeled value of a ref, if available.
 
169
 
 
170
        :param name: Name of the ref to peel
 
171
        :return: The peeled value of the ref. If the ref is known not point to a
 
172
            tag, this will be the SHA the ref refers to. If the ref may point to
 
173
            a tag, but no cached information is available, None is returned.
 
174
        """
 
175
        self.get_packed_refs()
 
176
        if self._peeled_refs is None or name not in self._packed_refs:
 
177
            # No cache: no peeled refs were read, or this ref is loose
 
178
            return None
 
179
        if name in self._peeled_refs:
 
180
            return self._peeled_refs[name]
 
181
        else:
 
182
            # Known not peelable
 
183
            return self[name]
 
184
 
 
185
    def read_loose_ref(self, name):
 
186
        """Read a reference file and return its contents.
 
187
 
 
188
        If the reference file a symbolic reference, only read the first line of
 
189
        the file. Otherwise, only read the first 40 bytes.
 
190
 
 
191
        :param name: the refname to read, relative to refpath
 
192
        :return: The contents of the ref file, or None if the file does not
 
193
            exist.
 
194
        :raises IOError: if any other error occurs
 
195
        """
 
196
        if name == b'HEAD':
 
197
            transport = self.worktree_transport
 
198
        else:
 
199
            transport = self.transport
 
200
        try:
 
201
            f = transport.get(name)
 
202
        except NoSuchFile:
 
203
            return None
 
204
        f = StringIO(f.read())
 
205
        try:
 
206
            header = f.read(len(SYMREF))
 
207
            if header == SYMREF:
 
208
                # Read only the first line
 
209
                return header + iter(f).next().rstrip("\r\n")
 
210
            else:
 
211
                # Read only the first 40 bytes
 
212
                return header + f.read(40-len(SYMREF))
 
213
        finally:
 
214
            f.close()
 
215
 
 
216
    def _remove_packed_ref(self, name):
 
217
        if self._packed_refs is None:
 
218
            return
 
219
        # reread cached refs from disk, while holding the lock
 
220
 
 
221
        self._packed_refs = None
 
222
        self.get_packed_refs()
 
223
 
 
224
        if name not in self._packed_refs:
 
225
            return
 
226
 
 
227
        del self._packed_refs[name]
 
228
        if name in self._peeled_refs:
 
229
            del self._peeled_refs[name]
 
230
        f = self.transport.open_write_stream("packed-refs")
 
231
        try:
 
232
            write_packed_refs(f, self._packed_refs, self._peeled_refs)
 
233
        finally:
 
234
            f.close()
 
235
 
 
236
    def set_symbolic_ref(self, name, other):
 
237
        """Make a ref point at another ref.
 
238
 
 
239
        :param name: Name of the ref to set
 
240
        :param other: Name of the ref to point at
 
241
        """
 
242
        self._check_refname(name)
 
243
        self._check_refname(other)
 
244
        if name != b'HEAD':
 
245
            transport = self.transport
 
246
            self._ensure_dir_exists(name)
 
247
        else:
 
248
            transport = self.worktree_transport
 
249
        transport.put_bytes(name, SYMREF + other + '\n')
 
250
 
 
251
    def set_if_equals(self, name, old_ref, new_ref):
 
252
        """Set a refname to new_ref only if it currently equals old_ref.
 
253
 
 
254
        This method follows all symbolic references, and can be used to perform
 
255
        an atomic compare-and-swap operation.
 
256
 
 
257
        :param name: The refname to set.
 
258
        :param old_ref: The old sha the refname must refer to, or None to set
 
259
            unconditionally.
 
260
        :param new_ref: The new sha the refname will refer to.
 
261
        :return: True if the set was successful, False otherwise.
 
262
        """
 
263
        try:
 
264
            realnames, _ = self.follow(name)
 
265
            realname = realnames[-1]
 
266
        except (KeyError, IndexError):
 
267
            realname = name
 
268
        if realname == b'HEAD':
 
269
            transport = self.worktree_transport
 
270
        else:
 
271
            transport = self.transport
 
272
            self._ensure_dir_exists(realname)
 
273
        transport.put_bytes(realname, new_ref+"\n")
 
274
        return True
 
275
 
 
276
    def add_if_new(self, name, ref):
 
277
        """Add a new reference only if it does not already exist.
 
278
 
 
279
        This method follows symrefs, and only ensures that the last ref in the
 
280
        chain does not exist.
 
281
 
 
282
        :param name: The refname to set.
 
283
        :param ref: The new sha the refname will refer to.
 
284
        :return: True if the add was successful, False otherwise.
 
285
        """
 
286
        try:
 
287
            realnames, contents = self.follow(name)
 
288
            if contents is not None:
 
289
                return False
 
290
            realname = realnames[-1]
 
291
        except (KeyError, IndexError):
 
292
            realname = name
 
293
        self._check_refname(realname)
 
294
        if realname == b'HEAD':
 
295
            transport = self.worktree_transport
 
296
        else:
 
297
            transport = self.transport
 
298
            self._ensure_dir_exists(realname)
 
299
        transport.put_bytes(realname, ref+"\n")
 
300
        return True
 
301
 
 
302
    def remove_if_equals(self, name, old_ref):
 
303
        """Remove a refname only if it currently equals old_ref.
 
304
 
 
305
        This method does not follow symbolic references. It can be used to
 
306
        perform an atomic compare-and-delete operation.
 
307
 
 
308
        :param name: The refname to delete.
 
309
        :param old_ref: The old sha the refname must refer to, or None to delete
 
310
            unconditionally.
 
311
        :return: True if the delete was successful, False otherwise.
 
312
        """
 
313
        self._check_refname(name)
 
314
        # may only be packed
 
315
        if name == b'HEAD':
 
316
            transport = self.worktree_transport
 
317
        else:
 
318
            transport = self.transport
 
319
        try:
 
320
            transport.delete(name)
 
321
        except NoSuchFile:
 
322
            pass
 
323
        self._remove_packed_ref(name)
 
324
        return True
 
325
 
 
326
    def get(self, name, default=None):
 
327
        try:
 
328
            return self[name]
 
329
        except KeyError:
 
330
            return default
 
331
 
 
332
    def lock_ref(self, name):
 
333
        if name == b"HEAD":
 
334
            transport = self.worktree_transport
 
335
        else:
 
336
            transport = self.transport
 
337
        self._ensure_dir_exists(name)
 
338
        lockname = name + ".lock"
 
339
        try:
 
340
            return transport.lock_write(lockname)
 
341
        except TransportNotPossible:
 
342
            # better than not locking at all, I guess?
 
343
            if transport.has(lockname):
 
344
                raise LockError(lockname + " exists")
 
345
            transport.put_bytes(lockname, "Locked by brz-git")
 
346
            from ...lock import LogicalLockResult
 
347
            return LogicalLockResult(lambda: transport.delete(lockname))
 
348
 
 
349
 
 
350
class TransportRepo(BaseRepo):
 
351
 
 
352
    def __init__(self, transport, bare, refs_text=None):
 
353
        self.transport = transport
 
354
        self.bare = bare
 
355
        try:
 
356
            with transport.get(CONTROLDIR) as f:
 
357
                path = read_gitfile(f)
 
358
        except (ReadError, NoSuchFile):
 
359
            if self.bare:
 
360
                self._controltransport = self.transport
 
361
            else:
 
362
                self._controltransport = self.transport.clone('.git')
 
363
        else:
 
364
            self._controltransport = self.transport.clone(path)
 
365
        commondir = self.get_named_file(COMMONDIR)
 
366
        if commondir is not None:
 
367
            with commondir:
 
368
                commondir = os.path.join(
 
369
                    self.controldir(),
 
370
                    commondir.read().rstrip(b"\r\n").decode(
 
371
                        sys.getfilesystemencoding()))
 
372
                self._commontransport = \
 
373
                    _mod_transport.get_transport_from_path(commondir)
 
374
        else:
 
375
            self._commontransport = self._controltransport
 
376
        object_store = TransportObjectStore(
 
377
            self._commontransport.clone(OBJECTDIR))
 
378
        if refs_text is not None:
 
379
            refs_container = InfoRefsContainer(StringIO(refs_text))
 
380
            try:
 
381
                head = TransportRefsContainer(self._commontransport).read_loose_ref("HEAD")
 
382
            except KeyError:
 
383
                pass
 
384
            else:
 
385
                refs_container._refs["HEAD"] = head
 
386
        else:
 
387
            refs_container = TransportRefsContainer(
 
388
                    self._commontransport, self._controltransport)
 
389
        super(TransportRepo, self).__init__(object_store,
 
390
                refs_container)
 
391
 
 
392
    def controldir(self):
 
393
        return self._controltransport.local_abspath('.')
 
394
 
 
395
    @property
 
396
    def path(self):
 
397
        return self.transport.local_abspath('.')
 
398
 
 
399
    def _determine_file_mode(self):
 
400
        # Be consistent with bzr
 
401
        if sys.platform == 'win32':
 
402
            return False
 
403
        return True
 
404
 
 
405
    def get_named_file(self, path):
 
406
        """Get a file from the control dir with a specific name.
 
407
 
 
408
        Although the filename should be interpreted as a filename relative to
 
409
        the control dir in a disk-baked Repo, the object returned need not be
 
410
        pointing to a file in that location.
 
411
 
 
412
        :param path: The path to the file, relative to the control dir.
 
413
        :return: An open file object, or None if the file does not exist.
 
414
        """
 
415
        try:
 
416
            return self._controltransport.get(path.lstrip('/'))
 
417
        except NoSuchFile:
 
418
            return None
 
419
 
 
420
    def _put_named_file(self, relpath, contents):
 
421
        self._controltransport.put_bytes(relpath, contents)
 
422
 
 
423
    def index_path(self):
 
424
        """Return the path to the index file."""
 
425
        return self._controltransport.local_abspath(INDEX_FILENAME)
 
426
 
 
427
    def open_index(self):
 
428
        """Open the index for this repository."""
 
429
        from dulwich.index import Index
 
430
        if not self.has_index():
 
431
            raise NoIndexPresent()
 
432
        return Index(self.index_path())
 
433
 
 
434
    def has_index(self):
 
435
        """Check if an index is present."""
 
436
        # Bare repos must never have index files; non-bare repos may have a
 
437
        # missing index file, which is treated as empty.
 
438
        return not self.bare
 
439
 
 
440
    def get_config(self):
 
441
        from dulwich.config import ConfigFile
 
442
        try:
 
443
            return ConfigFile.from_file(self._controltransport.get('config'))
 
444
        except NoSuchFile:
 
445
            return ConfigFile()
 
446
 
 
447
    def get_config_stack(self):
 
448
        from dulwich.config import StackedConfig
 
449
        backends = []
 
450
        p = self.get_config()
 
451
        if p is not None:
 
452
            backends.append(p)
 
453
            writable = p
 
454
        else:
 
455
            writable = None
 
456
        backends.extend(StackedConfig.default_backends())
 
457
        return StackedConfig(backends, writable=writable)
 
458
 
 
459
    def __repr__(self):
 
460
        return "<%s for %r>" % (self.__class__.__name__, self.transport)
 
461
 
 
462
    @classmethod
 
463
    def init(cls, transport, bare=False):
 
464
        if not bare:
 
465
            try:
 
466
                transport.mkdir(".git")
 
467
            except FileExists:
 
468
                raise AlreadyControlDirError(transport.base)
 
469
            control_transport = transport.clone(".git")
 
470
        else:
 
471
            control_transport = transport
 
472
        for d in BASE_DIRECTORIES:
 
473
            try:
 
474
                control_transport.mkdir("/".join(d))
 
475
            except FileExists:
 
476
                pass
 
477
        try:
 
478
            control_transport.mkdir(OBJECTDIR)
 
479
        except FileExists:
 
480
            raise AlreadyControlDirError(transport.base)
 
481
        TransportObjectStore.init(control_transport.clone(OBJECTDIR))
 
482
        ret = cls(transport, bare)
 
483
        ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
 
484
        ret._init_files(bare)
 
485
        return ret
 
486
 
 
487
 
 
488
class TransportObjectStore(PackBasedObjectStore):
 
489
    """Git-style object store that exists on disk."""
 
490
 
 
491
    def __init__(self, transport):
 
492
        """Open an object store.
 
493
 
 
494
        :param transport: Transport to open data from
 
495
        """
 
496
        super(TransportObjectStore, self).__init__()
 
497
        self.transport = transport
 
498
        self.pack_transport = self.transport.clone(PACKDIR)
 
499
        self._alternates = None
 
500
 
 
501
    def __eq__(self, other):
 
502
        if not isinstance(other, TransportObjectStore):
 
503
            return False
 
504
        return self.transport == other.transport
 
505
 
 
506
    def __repr__(self):
 
507
        return "%s(%r)" % (self.__class__.__name__, self.transport)
 
508
 
 
509
    @property
 
510
    def alternates(self):
 
511
        if self._alternates is not None:
 
512
            return self._alternates
 
513
        self._alternates = []
 
514
        for path in self._read_alternate_paths():
 
515
            # FIXME: Check path
 
516
            t = _mod_transport.get_transport_from_path(path)
 
517
            self._alternates.append(self.__class__(t))
 
518
        return self._alternates
 
519
 
 
520
    def _read_alternate_paths(self):
 
521
        try:
 
522
            f = self.transport.get("info/alternates")
 
523
        except NoSuchFile:
 
524
            return []
 
525
        ret = []
 
526
        try:
 
527
            for l in f.read().splitlines():
 
528
                if l[0] == "#":
 
529
                    continue
 
530
                if os.path.isabs(l):
 
531
                    continue
 
532
                ret.append(l)
 
533
            return ret
 
534
        finally:
 
535
            f.close()
 
536
 
 
537
    @property
 
538
    def packs(self):
 
539
        # FIXME: Never invalidates.
 
540
        if not self._pack_cache:
 
541
            self._update_pack_cache()
 
542
        return self._pack_cache.values()
 
543
 
 
544
    def _update_pack_cache(self):
 
545
        for pack in self._load_packs():
 
546
            self._pack_cache[pack._basename] = pack
 
547
 
 
548
    def _pack_names(self):
 
549
        try:
 
550
            f = self.transport.get('info/packs')
 
551
        except NoSuchFile:
 
552
            return self.pack_transport.list_dir(".")
 
553
        else:
 
554
            ret = []
 
555
            for line in f.read().splitlines():
 
556
                if not line:
 
557
                    continue
 
558
                (kind, name) = line.split(" ", 1)
 
559
                if kind != "P":
 
560
                    continue
 
561
                ret.append(name)
 
562
            return ret
 
563
 
 
564
    def _remove_pack(self, pack):
 
565
        self.pack_transport.delete(os.path.basename(pack.index.path))
 
566
        self.pack_transport.delete(pack.data.filename)
 
567
 
 
568
    def _load_packs(self):
 
569
        ret = []
 
570
        for name in self._pack_names():
 
571
            if name.startswith("pack-") and name.endswith(".pack"):
 
572
                try:
 
573
                    size = self.pack_transport.stat(name).st_size
 
574
                except TransportNotPossible:
 
575
                    # FIXME: This reads the whole pack file at once
 
576
                    f = self.pack_transport.get(name)
 
577
                    contents = f.read()
 
578
                    pd = PackData(name, StringIO(contents), size=len(contents))
 
579
                else:
 
580
                    pd = PackData(name, self.pack_transport.get(name),
 
581
                            size=size)
 
582
                idxname = name.replace(".pack", ".idx")
 
583
                idx = load_pack_index_file(idxname, self.pack_transport.get(idxname))
 
584
                pack = Pack.from_objects(pd, idx)
 
585
                pack._basename = idxname[:-4]
 
586
                ret.append(pack)
 
587
        return ret
 
588
 
 
589
    def _iter_loose_objects(self):
 
590
        for base in self.transport.list_dir('.'):
 
591
            if len(base) != 2:
 
592
                continue
 
593
            for rest in self.transport.list_dir(base):
 
594
                yield base+rest
 
595
 
 
596
    def _split_loose_object(self, sha):
 
597
        return (sha[:2], sha[2:])
 
598
 
 
599
    def _remove_loose_object(self, sha):
 
600
        path = '%s/%s' % self._split_loose_object(sha)
 
601
        self.transport.delete(path)
 
602
 
 
603
    def _get_loose_object(self, sha):
 
604
        path = '%s/%s' % self._split_loose_object(sha)
 
605
        try:
 
606
            return ShaFile.from_file(self.transport.get(path))
 
607
        except NoSuchFile:
 
608
            return None
 
609
 
 
610
    def add_object(self, obj):
 
611
        """Add a single object to this object store.
 
612
 
 
613
        :param obj: Object to add
 
614
        """
 
615
        (dir, file) = self._split_loose_object(obj.id)
 
616
        try:
 
617
            self.transport.mkdir(dir)
 
618
        except FileExists:
 
619
            pass
 
620
        path = "%s/%s" % (dir, file)
 
621
        if self.transport.has(path):
 
622
            return # Already there, no need to write again
 
623
        self.transport.put_bytes(path, obj.as_legacy_object())
 
624
 
 
625
    def move_in_pack(self, f):
 
626
        """Move a specific file containing a pack into the pack directory.
 
627
 
 
628
        :note: The file should be on the same file system as the
 
629
            packs directory.
 
630
 
 
631
        :param path: Path to the pack file.
 
632
        """
 
633
        f.seek(0)
 
634
        p = PackData("", f, len(f.getvalue()))
 
635
        entries = p.sorted_entries()
 
636
        basename = "pack-%s" % iter_sha1(entry[0] for entry in entries)
 
637
        p._filename = basename + ".pack"
 
638
        f.seek(0)
 
639
        self.pack_transport.put_file(basename + ".pack", f)
 
640
        idxfile = self.pack_transport.open_write_stream(basename + ".idx")
 
641
        try:
 
642
            write_pack_index_v2(idxfile, entries, p.get_stored_checksum())
 
643
        finally:
 
644
            idxfile.close()
 
645
        idxfile = self.pack_transport.get(basename + ".idx")
 
646
        idx = load_pack_index_file(basename+".idx", idxfile)
 
647
        final_pack = Pack.from_objects(p, idx)
 
648
        final_pack._basename = basename
 
649
        self._add_known_pack(basename, final_pack)
 
650
        return final_pack
 
651
 
 
652
    def add_thin_pack(self):
 
653
        """Add a new thin pack to this object store.
 
654
 
 
655
        Thin packs are packs that contain deltas with parents that exist
 
656
        in a different pack.
 
657
        """
 
658
        from cStringIO import StringIO
 
659
        f = StringIO()
 
660
        def commit():
 
661
            if len(f.getvalue()) > 0:
 
662
                return self.move_in_thin_pack(f)
 
663
            else:
 
664
                return None
 
665
        return f, commit
 
666
 
 
667
    def move_in_thin_pack(self, f):
 
668
        """Move a specific file containing a pack into the pack directory.
 
669
 
 
670
        :note: The file should be on the same file system as the
 
671
            packs directory.
 
672
 
 
673
        :param path: Path to the pack file.
 
674
        """
 
675
        f.seek(0)
 
676
        data = PackData.from_file(self.get_raw, f, len(f.getvalue()))
 
677
        idx = MemoryPackIndex(data.sorted_entries(), data.get_stored_checksum())
 
678
        p = Pack.from_objects(data, idx)
 
679
 
 
680
        pack_sha = idx.objects_sha1()
 
681
 
 
682
        datafile = self.pack_transport.open_write_stream(
 
683
                "pack-%s.pack" % pack_sha)
 
684
        try:
 
685
            entries, data_sum = write_pack_data(datafile, p.pack_tuples())
 
686
        finally:
 
687
            datafile.close()
 
688
        entries.sort()
 
689
        idxfile = self.pack_transport.open_write_stream(
 
690
            "pack-%s.idx" % pack_sha)
 
691
        try:
 
692
            write_pack_index_v2(idxfile, data.sorted_entries(), data_sum)
 
693
        finally:
 
694
            idxfile.close()
 
695
        basename = "pack-%s" % pack_sha
 
696
        final_pack = Pack(basename)
 
697
        self._add_known_pack(basename, final_pack)
 
698
        return final_pack
 
699
 
 
700
    def add_pack(self):
 
701
        """Add a new pack to this object store.
 
702
 
 
703
        :return: Fileobject to write to and a commit function to
 
704
            call when the pack is finished.
 
705
        """
 
706
        from cStringIO import StringIO
 
707
        f = StringIO()
 
708
        def commit():
 
709
            if len(f.getvalue()) > 0:
 
710
                return self.move_in_pack(f)
 
711
            else:
 
712
                return None
 
713
        def abort():
 
714
            return None
 
715
        return f, commit, abort
 
716
 
 
717
    @classmethod
 
718
    def init(cls, transport):
 
719
        try:
 
720
            transport.mkdir('info')
 
721
        except FileExists:
 
722
            pass
 
723
        try:
 
724
            transport.mkdir(PACKDIR)
 
725
        except FileExists:
 
726
            pass
 
727
        return cls(transport)