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

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

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