/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

Handle empty metadata.

Show diffs side-by-side

added added

removed removed

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