/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

Add basic tests for server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""A Git repository implementation that uses a Bazaar transport."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
import os
 
22
 
 
23
from dulwich.errors import (
 
24
    NotGitRepository,
 
25
    NoIndexPresent,
 
26
    )
 
27
from dulwich.objects import (
 
28
    ShaFile,
 
29
    )
 
30
from dulwich.object_store import (
 
31
    PackBasedObjectStore,
 
32
    PACKDIR,
 
33
    )
 
34
from dulwich.pack import (
 
35
    MemoryPackIndex,
 
36
    PackData,
 
37
    Pack,
 
38
    iter_sha1,
 
39
    load_pack_index_file,
 
40
    write_pack_data,
 
41
    write_pack_index_v2,
 
42
    )
 
43
from dulwich.repo import (
 
44
    BaseRepo,
 
45
    RefsContainer,
 
46
    BASE_DIRECTORIES,
 
47
    INDEX_FILENAME,
 
48
    OBJECTDIR,
 
49
    REFSDIR,
 
50
    SYMREF,
 
51
    check_ref_format,
 
52
    read_packed_refs_with_peeled,
 
53
    read_packed_refs,
 
54
    write_packed_refs,
 
55
    )
 
56
 
 
57
from bzrlib import (
 
58
    transport as _mod_transport,
 
59
    )
 
60
from bzrlib.errors import (
 
61
    FileExists,
 
62
    NoSuchFile,
 
63
    TransportNotPossible,
 
64
    )
 
65
 
 
66
 
 
67
class TransportRefsContainer(RefsContainer):
 
68
    """Refs container that reads refs from a transport."""
 
69
 
 
70
    def __init__(self, transport):
 
71
        self.transport = transport
 
72
        self._packed_refs = None
 
73
        self._peeled_refs = None
 
74
 
 
75
    def __repr__(self):
 
76
        return "%s(%r)" % (self.__class__.__name__, self.transport)
 
77
 
 
78
    def _ensure_dir_exists(self, path):
 
79
        for n in range(path.count("/")):
 
80
            dirname = "/".join(path.split("/")[:n+1])
 
81
            try:
 
82
                self.transport.mkdir(dirname)
 
83
            except FileExists:
 
84
                pass
 
85
 
 
86
    def subkeys(self, base):
 
87
        keys = set()
 
88
        try:
 
89
            iter_files = self.transport.clone(base).iter_files_recursive()
 
90
            keys.update(("%s/%s" % (base, refname)).strip("/") for 
 
91
                    refname in iter_files if check_ref_format("%s/%s" % (base, refname)))
 
92
        except (TransportNotPossible, NoSuchFile):
 
93
            pass
 
94
        for key in self.get_packed_refs():
 
95
            if key.startswith(base):
 
96
                keys.add(key[len(base):].strip("/"))
 
97
        return keys
 
98
 
 
99
    def allkeys(self):
 
100
        keys = set()
 
101
        try:
 
102
            self.transport.get_bytes("HEAD")
 
103
        except NoSuchFile:
 
104
            pass
 
105
        else:
 
106
            keys.add("HEAD")
 
107
        try:
 
108
            iter_files = list(self.transport.clone("refs").iter_files_recursive())
 
109
            for filename in iter_files:
 
110
                refname = "refs/%s" % filename
 
111
                if check_ref_format(refname):
 
112
                    keys.add(refname)
 
113
        except (TransportNotPossible, NoSuchFile):
 
114
            pass
 
115
        keys.update(self.get_packed_refs())
 
116
        return keys
 
117
 
 
118
    def get_packed_refs(self):
 
119
        """Get contents of the packed-refs file.
 
120
 
 
121
        :return: Dictionary mapping ref names to SHA1s
 
122
 
 
123
        :note: Will return an empty dictionary when no packed-refs file is
 
124
            present.
 
125
        """
 
126
        # TODO: invalidate the cache on repacking
 
127
        if self._packed_refs is None:
 
128
            # set both to empty because we want _peeled_refs to be
 
129
            # None if and only if _packed_refs is also None.
 
130
            self._packed_refs = {}
 
131
            self._peeled_refs = {}
 
132
            try:
 
133
                f = self.transport.get("packed-refs")
 
134
            except NoSuchFile:
 
135
                return {}
 
136
            try:
 
137
                first_line = iter(f).next().rstrip()
 
138
                if (first_line.startswith("# pack-refs") and " peeled" in
 
139
                        first_line):
 
140
                    for sha, name, peeled in read_packed_refs_with_peeled(f):
 
141
                        self._packed_refs[name] = sha
 
142
                        if peeled:
 
143
                            self._peeled_refs[name] = peeled
 
144
                else:
 
145
                    f.seek(0)
 
146
                    for sha, name in read_packed_refs(f):
 
147
                        self._packed_refs[name] = sha
 
148
            finally:
 
149
                f.close()
 
150
        return self._packed_refs
 
151
 
 
152
    def get_peeled(self, name):
 
153
        """Return the cached peeled value of a ref, if available.
 
154
 
 
155
        :param name: Name of the ref to peel
 
156
        :return: The peeled value of the ref. If the ref is known not point to a
 
157
            tag, this will be the SHA the ref refers to. If the ref may point to
 
158
            a tag, but no cached information is available, None is returned.
 
159
        """
 
160
        self.get_packed_refs()
 
161
        if self._peeled_refs is None or name not in self._packed_refs:
 
162
            # No cache: no peeled refs were read, or this ref is loose
 
163
            return None
 
164
        if name in self._peeled_refs:
 
165
            return self._peeled_refs[name]
 
166
        else:
 
167
            # Known not peelable
 
168
            return self[name]
 
169
 
 
170
    def read_loose_ref(self, name):
 
171
        """Read a reference file and return its contents.
 
172
 
 
173
        If the reference file a symbolic reference, only read the first line of
 
174
        the file. Otherwise, only read the first 40 bytes.
 
175
 
 
176
        :param name: the refname to read, relative to refpath
 
177
        :return: The contents of the ref file, or None if the file does not
 
178
            exist.
 
179
        :raises IOError: if any other error occurs
 
180
        """
 
181
        try:
 
182
            f = self.transport.get(name)
 
183
        except NoSuchFile:
 
184
            return None
 
185
        try:
 
186
            header = f.read(len(SYMREF))
 
187
            if header == SYMREF:
 
188
                # Read only the first line
 
189
                return header + iter(f).next().rstrip("\r\n")
 
190
            else:
 
191
                # Read only the first 40 bytes
 
192
                return header + f.read(40-len(SYMREF))
 
193
        finally:
 
194
            f.close()
 
195
 
 
196
    def _remove_packed_ref(self, name):
 
197
        if self._packed_refs is None:
 
198
            return
 
199
        # reread cached refs from disk, while holding the lock
 
200
 
 
201
        self._packed_refs = None
 
202
        self.get_packed_refs()
 
203
 
 
204
        if name not in self._packed_refs:
 
205
            return
 
206
 
 
207
        del self._packed_refs[name]
 
208
        if name in self._peeled_refs:
 
209
            del self._peeled_refs[name]
 
210
        f = self.transport.open_write_stream("packed-refs")
 
211
        try:
 
212
            write_packed_refs(f, self._packed_refs, self._peeled_refs)
 
213
        finally:
 
214
            f.close()
 
215
 
 
216
    def set_symbolic_ref(self, name, other):
 
217
        """Make a ref point at another ref.
 
218
 
 
219
        :param name: Name of the ref to set
 
220
        :param other: Name of the ref to point at
 
221
        """
 
222
        self._check_refname(name)
 
223
        self._check_refname(other)
 
224
        self._ensure_dir_exists(name)
 
225
        self.transport.put_bytes(name, SYMREF + other + '\n')
 
226
 
 
227
    def set_if_equals(self, name, old_ref, new_ref):
 
228
        """Set a refname to new_ref only if it currently equals old_ref.
 
229
 
 
230
        This method follows all symbolic references, and can be used to perform
 
231
        an atomic compare-and-swap operation.
 
232
 
 
233
        :param name: The refname to set.
 
234
        :param old_ref: The old sha the refname must refer to, or None to set
 
235
            unconditionally.
 
236
        :param new_ref: The new sha the refname will refer to.
 
237
        :return: True if the set was successful, False otherwise.
 
238
        """
 
239
        try:
 
240
            realname, _ = self._follow(name)
 
241
        except KeyError:
 
242
            realname = name
 
243
        self._ensure_dir_exists(realname)
 
244
        self.transport.put_bytes(realname, new_ref+"\n")
 
245
        return True
 
246
 
 
247
    def add_if_new(self, name, ref):
 
248
        """Add a new reference only if it does not already exist.
 
249
 
 
250
        This method follows symrefs, and only ensures that the last ref in the
 
251
        chain does not exist.
 
252
 
 
253
        :param name: The refname to set.
 
254
        :param ref: The new sha the refname will refer to.
 
255
        :return: True if the add was successful, False otherwise.
 
256
        """
 
257
        try:
 
258
            realname, contents = self._follow(name)
 
259
            if contents is not None:
 
260
                return False
 
261
        except KeyError:
 
262
            realname = name
 
263
        self._check_refname(realname)
 
264
        self._ensure_dir_exists(realname)
 
265
        self.transport.put_bytes(realname, ref+"\n")
 
266
        return True
 
267
 
 
268
    def remove_if_equals(self, name, old_ref):
 
269
        """Remove a refname only if it currently equals old_ref.
 
270
 
 
271
        This method does not follow symbolic references. It can be used to
 
272
        perform an atomic compare-and-delete operation.
 
273
 
 
274
        :param name: The refname to delete.
 
275
        :param old_ref: The old sha the refname must refer to, or None to delete
 
276
            unconditionally.
 
277
        :return: True if the delete was successful, False otherwise.
 
278
        """
 
279
        self._check_refname(name)
 
280
        # may only be packed
 
281
        try:
 
282
            self.transport.delete(name)
 
283
        except NoSuchFile:
 
284
            pass
 
285
        self._remove_packed_ref(name)
 
286
        return True
 
287
 
 
288
 
 
289
class TransportRepo(BaseRepo):
 
290
 
 
291
    def __init__(self, transport, bare):
 
292
        self.transport = transport
 
293
        self.bare = bare
 
294
        if self.bare:
 
295
            self._controltransport = self.transport
 
296
        else:
 
297
            self._controltransport = self.transport.clone('.git')
 
298
        object_store = TransportObjectStore(
 
299
            self._controltransport.clone(OBJECTDIR))
 
300
        super(TransportRepo, self).__init__(object_store, 
 
301
                TransportRefsContainer(self._controltransport))
 
302
 
 
303
    def get_named_file(self, path):
 
304
        """Get a file from the control dir with a specific name.
 
305
 
 
306
        Although the filename should be interpreted as a filename relative to
 
307
        the control dir in a disk-baked Repo, the object returned need not be
 
308
        pointing to a file in that location.
 
309
 
 
310
        :param path: The path to the file, relative to the control dir.
 
311
        :return: An open file object, or None if the file does not exist.
 
312
        """
 
313
        try:
 
314
            return self._controltransport.get(path.lstrip('/'))
 
315
        except NoSuchFile:
 
316
            return None
 
317
 
 
318
    def _put_named_file(self, relpath, contents):
 
319
        self._controltransport.put_bytes(relpath, contents)
 
320
 
 
321
    def index_path(self):
 
322
        """Return the path to the index file."""
 
323
        return self._controltransport.local_abspath(INDEX_FILENAME)
 
324
 
 
325
    def open_index(self):
 
326
        """Open the index for this repository."""
 
327
        from dulwich.index import Index
 
328
        if not self.has_index():
 
329
            raise NoIndexPresent()
 
330
        return Index(self.index_path())
 
331
 
 
332
    def has_index(self):
 
333
        """Check if an index is present."""
 
334
        # Bare repos must never have index files; non-bare repos may have a
 
335
        # missing index file, which is treated as empty.
 
336
        return not self.bare
 
337
 
 
338
    def __repr__(self):
 
339
        return "<%s for %r>" % (self.__class__.__name__, self.transport)
 
340
 
 
341
    @classmethod
 
342
    def init(cls, transport, bare=False):
 
343
        if not bare:
 
344
            transport.mkdir(".git")
 
345
            control_transport = transport.clone(".git")
 
346
        else:
 
347
            control_transport = transport
 
348
        for d in BASE_DIRECTORIES:
 
349
            control_transport.mkdir("/".join(d))
 
350
        control_transport.mkdir(OBJECTDIR)
 
351
        TransportObjectStore.init(control_transport.clone(OBJECTDIR))
 
352
        ret = cls(transport, bare)
 
353
        ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
 
354
        ret._init_files(bare)
 
355
        return ret
 
356
 
 
357
 
 
358
class TransportObjectStore(PackBasedObjectStore):
 
359
    """Git-style object store that exists on disk."""
 
360
 
 
361
    def __init__(self, transport):
 
362
        """Open an object store.
 
363
 
 
364
        :param transport: Transport to open data from
 
365
        """
 
366
        super(TransportObjectStore, self).__init__()
 
367
        self.transport = transport
 
368
        self.pack_transport = self.transport.clone(PACKDIR)
 
369
        self._alternates = None
 
370
 
 
371
    def __repr__(self):
 
372
        return "%s(%r)" % (self.__class__.__name__, self.transport)
 
373
 
 
374
    def _pack_cache_stale(self):
 
375
        return False # FIXME
 
376
 
 
377
    @property
 
378
    def alternates(self):
 
379
        if self._alternates is not None:
 
380
            return self._alternates
 
381
        self._alternates = []
 
382
        for path in self._read_alternate_paths():
 
383
            # FIXME: Check path
 
384
            t = _mod_transport.get_transport_from_path(path)
 
385
            self._alternates.append(self.__class__(t))
 
386
        return self._alternates
 
387
 
 
388
    def _read_alternate_paths(self):
 
389
        try:
 
390
            f = self.transport.get("info/alternates")
 
391
        except NoSuchFile:
 
392
            return []
 
393
        ret = []
 
394
        try:
 
395
            for l in f.readlines():
 
396
                l = l.rstrip("\n")
 
397
                if l[0] == "#":
 
398
                    continue
 
399
                if os.path.isabs(l):
 
400
                    continue
 
401
                ret.append(l)
 
402
            return ret
 
403
        finally:
 
404
            f.close()
 
405
 
 
406
    def _pack_names(self):
 
407
        try:
 
408
            f = self.transport.get('info/packs')
 
409
        except NoSuchFile:
 
410
            return self.pack_transport.list_dir(".")
 
411
        else:
 
412
            ret = []
 
413
            for line in f.readlines():
 
414
                line = line.rstrip("\n")
 
415
                if not line:
 
416
                    continue
 
417
                (kind, name) = line.split(" ", 1)
 
418
                if kind != "P":
 
419
                    continue
 
420
                ret.append(name)
 
421
            return ret
 
422
 
 
423
    def _load_packs(self):
 
424
        ret = []
 
425
        for name in self._pack_names():
 
426
            if name.startswith("pack-") and name.endswith(".pack"):
 
427
                try:
 
428
                    size = self.pack_transport.stat(name).st_size
 
429
                except TransportNotPossible:
 
430
                    # FIXME: This reads the whole pack file at once
 
431
                    f = self.pack_transport.get(name)
 
432
                    contents = f.read()
 
433
                    pd = PackData(name, StringIO(contents), size=len(contents))
 
434
                else:
 
435
                    pd = PackData(name, self.pack_transport.get(name),
 
436
                            size=size)
 
437
                idxname = name.replace(".pack", ".idx")
 
438
                idx = load_pack_index_file(idxname, self.pack_transport.get(idxname))
 
439
                pack = Pack.from_objects(pd, idx)
 
440
                ret.append(pack)
 
441
        return ret
 
442
 
 
443
    def _iter_loose_objects(self):
 
444
        for base in self.transport.list_dir('.'):
 
445
            if len(base) != 2:
 
446
                continue
 
447
            for rest in self.transport.list_dir(base):
 
448
                yield base+rest
 
449
 
 
450
    def _split_loose_object(self, sha):
 
451
        return (sha[:2], sha[2:])
 
452
 
 
453
    def _remove_loose_object(self, sha):
 
454
        path = '%s/%s' % self._split_loose_object(sha)
 
455
        self.transport.delete(path)
 
456
 
 
457
    def _get_loose_object(self, sha):
 
458
        path = '%s/%s' % self._split_loose_object(sha)
 
459
        try:
 
460
            return ShaFile.from_file(self.transport.get(path))
 
461
        except NoSuchFile:
 
462
            return None
 
463
 
 
464
    def add_object(self, obj):
 
465
        """Add a single object to this object store.
 
466
 
 
467
        :param obj: Object to add
 
468
        """
 
469
        (dir, file) = self._split_loose_object(obj.id)
 
470
        try:
 
471
            self.transport.mkdir(dir)
 
472
        except FileExists:
 
473
            pass
 
474
        path = "%s/%s" % (dir, file)
 
475
        if self.transport.has(path):
 
476
            return # Already there, no need to write again
 
477
        self.transport.put_bytes(path, obj.as_legacy_object())
 
478
 
 
479
    def move_in_pack(self, f):
 
480
        """Move a specific file containing a pack into the pack directory.
 
481
 
 
482
        :note: The file should be on the same file system as the
 
483
            packs directory.
 
484
 
 
485
        :param path: Path to the pack file.
 
486
        """
 
487
        f.seek(0)
 
488
        p = PackData(None, f, len(f.getvalue()))
 
489
        entries = p.sorted_entries()
 
490
        basename = "pack-%s" % iter_sha1(entry[0] for entry in entries)
 
491
        f.seek(0)
 
492
        self.pack_transport.put_file(basename + ".pack", f)
 
493
        idxfile = self.pack_transport.open_write_stream(basename + ".idx")
 
494
        try:
 
495
            write_pack_index_v2(idxfile, entries, p.get_stored_checksum())
 
496
        finally:
 
497
            idxfile.close()
 
498
        idxfile = self.pack_transport.get(basename + ".idx")
 
499
        idx = load_pack_index_file(basename+".idx", idxfile)
 
500
        final_pack = Pack.from_objects(p, idx)
 
501
        self._add_known_pack(final_pack)
 
502
        return final_pack
 
503
 
 
504
    def add_thin_pack(self):
 
505
        """Add a new thin pack to this object store.
 
506
 
 
507
        Thin packs are packs that contain deltas with parents that exist
 
508
        in a different pack.
 
509
        """
 
510
        from cStringIO import StringIO
 
511
        f = StringIO()
 
512
        def commit():
 
513
            if len(f.getvalue()) > 0:
 
514
                return self.move_in_thin_pack(f)
 
515
            else:
 
516
                return None
 
517
        return f, commit
 
518
 
 
519
    def move_in_thin_pack(self, f):
 
520
        """Move a specific file containing a pack into the pack directory.
 
521
 
 
522
        :note: The file should be on the same file system as the
 
523
            packs directory.
 
524
 
 
525
        :param path: Path to the pack file.
 
526
        """
 
527
        f.seek(0)
 
528
        data = PackData.from_file(self.get_raw, f, len(f.getvalue()))
 
529
        idx = MemoryPackIndex(data.sorted_entries(), data.get_stored_checksum())
 
530
        p = Pack.from_objects(data, idx)
 
531
 
 
532
        pack_sha = idx.objects_sha1()
 
533
 
 
534
        datafile = self.pack_transport.open_write_stream(
 
535
                "pack-%s.pack" % pack_sha)
 
536
        try:
 
537
            entries, data_sum = write_pack_data(datafile, p.pack_tuples())
 
538
        finally:
 
539
            datafile.close()
 
540
        entries.sort()
 
541
        idxfile = self.pack_transport.open_write_stream(
 
542
            "pack-%s.idx" % pack_sha)
 
543
        try:
 
544
            write_pack_index_v2(idxfile, data.sorted_entries(), data_sum)
 
545
        finally:
 
546
            idxfile.close()
 
547
        final_pack = Pack("pack-%s" % pack_sha)
 
548
        self._add_known_pack(final_pack)
 
549
        return final_pack
 
550
 
 
551
    def add_pack(self):
 
552
        """Add a new pack to this object store. 
 
553
 
 
554
        :return: Fileobject to write to and a commit function to 
 
555
            call when the pack is finished.
 
556
        """
 
557
        from cStringIO import StringIO
 
558
        f = StringIO()
 
559
        def commit():
 
560
            if len(f.getvalue()) > 0:
 
561
                return self.move_in_pack(f)
 
562
            else:
 
563
                return None
 
564
        return f, commit
 
565
 
 
566
    @classmethod
 
567
    def init(cls, transport):
 
568
        transport.mkdir('info')
 
569
        transport.mkdir(PACKDIR)
 
570
        return cls(transport)