/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

Change root id, for easy use in join --reference.

Merged from https://code.launchpad.net/~jelmer/brz-git/change-root-id/+merge/342153

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