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

  • Committer: Jelmer Vernooij
  • Date: 2018-11-24 15:56:05 UTC
  • mto: This revision was merged to the branch mainline in revision 7214.
  • Revision ID: jelmer@jelmer.uk-20181124155605-g8yh28kpruubzgjk
Remove references to bzrtools.

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