/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-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

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