1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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
17
"""A Git repository implementation that uses a Bazaar transport."""
19
from __future__ import absolute_import
21
from cStringIO import StringIO
27
from dulwich.errors import (
31
from dulwich.objects import (
34
from dulwich.object_store import (
38
from dulwich.pack import (
47
from dulwich.repo import (
58
read_packed_refs_with_peeled,
64
transport as _mod_transport,
66
from ...errors import (
67
AlreadyControlDirError,
75
class TransportRefsContainer(RefsContainer):
76
"""Refs container that reads refs from a transport."""
78
def __init__(self, transport, worktree_transport=None):
79
self.transport = transport
80
if worktree_transport is None:
81
worktree_transport = transport
82
self.worktree_transport = worktree_transport
83
self._packed_refs = None
84
self._peeled_refs = None
87
return "%s(%r)" % (self.__class__.__name__, self.transport)
89
def _ensure_dir_exists(self, path):
90
for n in range(path.count("/")):
91
dirname = "/".join(path.split("/")[:n+1])
93
self.transport.mkdir(dirname)
97
def subkeys(self, base):
98
"""Refs present in this container under a base.
100
:param base: The base to return refs under.
101
:return: A set of valid refs in this container under the base; the base
102
prefix is stripped from the ref names returned.
105
base_len = len(base) + 1
106
for refname in self.allkeys():
107
if refname.startswith(base):
108
keys.add(refname[base_len:])
114
self.worktree_transport.get_bytes("HEAD")
120
iter_files = list(self.transport.clone("refs").iter_files_recursive())
121
for filename in iter_files:
122
refname = "refs/%s" % urllib.unquote(filename)
123
if check_ref_format(refname):
125
except (TransportNotPossible, NoSuchFile):
127
keys.update(self.get_packed_refs())
130
def get_packed_refs(self):
131
"""Get contents of the packed-refs file.
133
:return: Dictionary mapping ref names to SHA1s
135
:note: Will return an empty dictionary when no packed-refs file is
138
# TODO: invalidate the cache on repacking
139
if self._packed_refs is None:
140
# set both to empty because we want _peeled_refs to be
141
# None if and only if _packed_refs is also None.
142
self._packed_refs = {}
143
self._peeled_refs = {}
145
f = self.transport.get("packed-refs")
149
first_line = iter(f).next().rstrip()
150
if (first_line.startswith("# pack-refs") and " peeled" in
152
for sha, name, peeled in read_packed_refs_with_peeled(f):
153
self._packed_refs[name] = sha
155
self._peeled_refs[name] = peeled
158
for sha, name in read_packed_refs(f):
159
self._packed_refs[name] = sha
162
return self._packed_refs
164
def get_peeled(self, name):
165
"""Return the cached peeled value of a ref, if available.
167
:param name: Name of the ref to peel
168
:return: The peeled value of the ref. If the ref is known not point to a
169
tag, this will be the SHA the ref refers to. If the ref may point to
170
a tag, but no cached information is available, None is returned.
172
self.get_packed_refs()
173
if self._peeled_refs is None or name not in self._packed_refs:
174
# No cache: no peeled refs were read, or this ref is loose
176
if name in self._peeled_refs:
177
return self._peeled_refs[name]
182
def read_loose_ref(self, name):
183
"""Read a reference file and return its contents.
185
If the reference file a symbolic reference, only read the first line of
186
the file. Otherwise, only read the first 40 bytes.
188
:param name: the refname to read, relative to refpath
189
:return: The contents of the ref file, or None if the file does not
191
:raises IOError: if any other error occurs
194
transport = self.worktree_transport
196
transport = self.transport
198
f = transport.get(name)
201
f = StringIO(f.read())
203
header = f.read(len(SYMREF))
205
# Read only the first line
206
return header + iter(f).next().rstrip("\r\n")
208
# Read only the first 40 bytes
209
return header + f.read(40-len(SYMREF))
213
def _remove_packed_ref(self, name):
214
if self._packed_refs is None:
216
# reread cached refs from disk, while holding the lock
218
self._packed_refs = None
219
self.get_packed_refs()
221
if name not in self._packed_refs:
224
del self._packed_refs[name]
225
if name in self._peeled_refs:
226
del self._peeled_refs[name]
227
f = self.transport.open_write_stream("packed-refs")
229
write_packed_refs(f, self._packed_refs, self._peeled_refs)
233
def set_symbolic_ref(self, name, other):
234
"""Make a ref point at another ref.
236
:param name: Name of the ref to set
237
:param other: Name of the ref to point at
239
self._check_refname(name)
240
self._check_refname(other)
242
transport = self.transport
243
self._ensure_dir_exists(name)
245
transport = self.worktree_transport
246
transport.put_bytes(name, SYMREF + other + '\n')
248
def set_if_equals(self, name, old_ref, new_ref):
249
"""Set a refname to new_ref only if it currently equals old_ref.
251
This method follows all symbolic references, and can be used to perform
252
an atomic compare-and-swap operation.
254
:param name: The refname to set.
255
:param old_ref: The old sha the refname must refer to, or None to set
257
:param new_ref: The new sha the refname will refer to.
258
:return: True if the set was successful, False otherwise.
261
realnames, _ = self.follow(name)
262
realname = realnames[-1]
263
except (KeyError, IndexError):
265
if realname == b'HEAD':
266
transport = self.worktree_transport
268
transport = self.transport
269
self._ensure_dir_exists(realname)
270
transport.put_bytes(realname, new_ref+"\n")
273
def add_if_new(self, name, ref):
274
"""Add a new reference only if it does not already exist.
276
This method follows symrefs, and only ensures that the last ref in the
277
chain does not exist.
279
:param name: The refname to set.
280
:param ref: The new sha the refname will refer to.
281
:return: True if the add was successful, False otherwise.
284
realnames, contents = self.follow(name)
285
if contents is not None:
287
realname = realnames[-1]
288
except (KeyError, IndexError):
290
self._check_refname(realname)
291
if realname == b'HEAD':
292
transport = self.worktree_transport
294
transport = self.transport
295
self._ensure_dir_exists(realname)
296
transport.put_bytes(realname, ref+"\n")
299
def remove_if_equals(self, name, old_ref):
300
"""Remove a refname only if it currently equals old_ref.
302
This method does not follow symbolic references. It can be used to
303
perform an atomic compare-and-delete operation.
305
:param name: The refname to delete.
306
:param old_ref: The old sha the refname must refer to, or None to delete
308
:return: True if the delete was successful, False otherwise.
310
self._check_refname(name)
313
transport = self.worktree_transport
315
transport = self.transport
317
transport.delete(name)
320
self._remove_packed_ref(name)
323
def get(self, name, default=None):
329
def lock_ref(self, name):
331
transport = self.worktree_transport
333
transport = self.transport
334
self._ensure_dir_exists(name)
335
lockname = name + ".lock"
337
return transport.lock_write(lockname)
338
except TransportNotPossible:
339
# better than not locking at all, I guess?
340
if transport.has(lockname):
341
raise LockError(lockname + " exists")
342
transport.put_bytes(lockname, "Locked by brz-git")
343
from ...lock import LogicalLockResult
344
return LogicalLockResult(lambda: transport.delete(lockname))
347
class TransportRepo(BaseRepo):
349
def __init__(self, transport, bare, refs_text=None):
350
self.transport = transport
353
self._controltransport = self.transport
355
self._controltransport = self.transport.clone('.git')
356
commondir = self.get_named_file(COMMONDIR)
357
if commondir is not None:
359
commondir = os.path.join(
361
commondir.read().rstrip(b"\r\n").decode(
362
sys.getfilesystemencoding()))
363
self._commontransport = \
364
_mod_transport.get_transport_from_path(commondir)
366
self._commontransport = self._controltransport
367
object_store = TransportObjectStore(
368
self._commontransport.clone(OBJECTDIR))
369
if refs_text is not None:
370
refs_container = InfoRefsContainer(StringIO(refs_text))
372
head = TransportRefsContainer(self._commontransport).read_loose_ref("HEAD")
376
refs_container._refs["HEAD"] = head
378
refs_container = TransportRefsContainer(
379
self._commontransport, self._controltransport)
380
super(TransportRepo, self).__init__(object_store,
383
def controldir(self):
384
return self._controltransport.local_abspath('.')
388
return self.transport.local_abspath('.')
390
def _determine_file_mode(self):
391
# Be consistent with bzr
392
if sys.platform == 'win32':
396
def get_named_file(self, path):
397
"""Get a file from the control dir with a specific name.
399
Although the filename should be interpreted as a filename relative to
400
the control dir in a disk-baked Repo, the object returned need not be
401
pointing to a file in that location.
403
:param path: The path to the file, relative to the control dir.
404
:return: An open file object, or None if the file does not exist.
407
return self._controltransport.get(path.lstrip('/'))
411
def _put_named_file(self, relpath, contents):
412
self._controltransport.put_bytes(relpath, contents)
414
def index_path(self):
415
"""Return the path to the index file."""
416
return self._controltransport.local_abspath(INDEX_FILENAME)
418
def open_index(self):
419
"""Open the index for this repository."""
420
from dulwich.index import Index
421
if not self.has_index():
422
raise NoIndexPresent()
423
return Index(self.index_path())
426
"""Check if an index is present."""
427
# Bare repos must never have index files; non-bare repos may have a
428
# missing index file, which is treated as empty.
431
def get_config(self):
432
from dulwich.config import ConfigFile
434
return ConfigFile.from_file(self._controltransport.get('config'))
438
def get_config_stack(self):
439
from dulwich.config import StackedConfig
441
p = self.get_config()
447
backends.extend(StackedConfig.default_backends())
448
return StackedConfig(backends, writable=writable)
451
return "<%s for %r>" % (self.__class__.__name__, self.transport)
454
def init(cls, transport, bare=False):
457
transport.mkdir(".git")
459
raise AlreadyControlDirError(transport.base)
460
control_transport = transport.clone(".git")
462
control_transport = transport
463
for d in BASE_DIRECTORIES:
465
control_transport.mkdir("/".join(d))
469
control_transport.mkdir(OBJECTDIR)
471
raise AlreadyControlDirError(transport.base)
472
TransportObjectStore.init(control_transport.clone(OBJECTDIR))
473
ret = cls(transport, bare)
474
ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
475
ret._init_files(bare)
479
class TransportObjectStore(PackBasedObjectStore):
480
"""Git-style object store that exists on disk."""
482
def __init__(self, transport):
483
"""Open an object store.
485
:param transport: Transport to open data from
487
super(TransportObjectStore, self).__init__()
488
self.transport = transport
489
self.pack_transport = self.transport.clone(PACKDIR)
490
self._alternates = None
492
def __eq__(self, other):
493
if not isinstance(other, TransportObjectStore):
495
return self.transport == other.transport
498
return "%s(%r)" % (self.__class__.__name__, self.transport)
501
def alternates(self):
502
if self._alternates is not None:
503
return self._alternates
504
self._alternates = []
505
for path in self._read_alternate_paths():
507
t = _mod_transport.get_transport_from_path(path)
508
self._alternates.append(self.__class__(t))
509
return self._alternates
511
def _read_alternate_paths(self):
513
f = self.transport.get("info/alternates")
518
for l in f.read().splitlines():
530
# FIXME: Never invalidates.
531
if not self._pack_cache:
532
self._update_pack_cache()
533
return self._pack_cache.values()
535
def _update_pack_cache(self):
536
for pack in self._load_packs():
537
self._pack_cache[pack._basename] = pack
539
def _pack_names(self):
541
f = self.transport.get('info/packs')
543
return self.pack_transport.list_dir(".")
546
for line in f.read().splitlines():
549
(kind, name) = line.split(" ", 1)
555
def _remove_pack(self, pack):
556
self.pack_transport.delete(os.path.basename(pack.index.path))
557
self.pack_transport.delete(pack.data.filename)
559
def _load_packs(self):
561
for name in self._pack_names():
562
if name.startswith("pack-") and name.endswith(".pack"):
564
size = self.pack_transport.stat(name).st_size
565
except TransportNotPossible:
566
# FIXME: This reads the whole pack file at once
567
f = self.pack_transport.get(name)
569
pd = PackData(name, StringIO(contents), size=len(contents))
571
pd = PackData(name, self.pack_transport.get(name),
573
idxname = name.replace(".pack", ".idx")
574
idx = load_pack_index_file(idxname, self.pack_transport.get(idxname))
575
pack = Pack.from_objects(pd, idx)
576
pack._basename = idxname[:-4]
580
def _iter_loose_objects(self):
581
for base in self.transport.list_dir('.'):
584
for rest in self.transport.list_dir(base):
587
def _split_loose_object(self, sha):
588
return (sha[:2], sha[2:])
590
def _remove_loose_object(self, sha):
591
path = '%s/%s' % self._split_loose_object(sha)
592
self.transport.delete(path)
594
def _get_loose_object(self, sha):
595
path = '%s/%s' % self._split_loose_object(sha)
597
return ShaFile.from_file(self.transport.get(path))
601
def add_object(self, obj):
602
"""Add a single object to this object store.
604
:param obj: Object to add
606
(dir, file) = self._split_loose_object(obj.id)
608
self.transport.mkdir(dir)
611
path = "%s/%s" % (dir, file)
612
if self.transport.has(path):
613
return # Already there, no need to write again
614
self.transport.put_bytes(path, obj.as_legacy_object())
616
def move_in_pack(self, f):
617
"""Move a specific file containing a pack into the pack directory.
619
:note: The file should be on the same file system as the
622
:param path: Path to the pack file.
625
p = PackData("", f, len(f.getvalue()))
626
entries = p.sorted_entries()
627
basename = "pack-%s" % iter_sha1(entry[0] for entry in entries)
628
p._filename = basename + ".pack"
630
self.pack_transport.put_file(basename + ".pack", f)
631
idxfile = self.pack_transport.open_write_stream(basename + ".idx")
633
write_pack_index_v2(idxfile, entries, p.get_stored_checksum())
636
idxfile = self.pack_transport.get(basename + ".idx")
637
idx = load_pack_index_file(basename+".idx", idxfile)
638
final_pack = Pack.from_objects(p, idx)
639
final_pack._basename = basename
640
self._add_known_pack(basename, final_pack)
643
def add_thin_pack(self):
644
"""Add a new thin pack to this object store.
646
Thin packs are packs that contain deltas with parents that exist
649
from cStringIO import StringIO
652
if len(f.getvalue()) > 0:
653
return self.move_in_thin_pack(f)
658
def move_in_thin_pack(self, f):
659
"""Move a specific file containing a pack into the pack directory.
661
:note: The file should be on the same file system as the
664
:param path: Path to the pack file.
667
data = PackData.from_file(self.get_raw, f, len(f.getvalue()))
668
idx = MemoryPackIndex(data.sorted_entries(), data.get_stored_checksum())
669
p = Pack.from_objects(data, idx)
671
pack_sha = idx.objects_sha1()
673
datafile = self.pack_transport.open_write_stream(
674
"pack-%s.pack" % pack_sha)
676
entries, data_sum = write_pack_data(datafile, p.pack_tuples())
680
idxfile = self.pack_transport.open_write_stream(
681
"pack-%s.idx" % pack_sha)
683
write_pack_index_v2(idxfile, data.sorted_entries(), data_sum)
686
basename = "pack-%s" % pack_sha
687
final_pack = Pack(basename)
688
self._add_known_pack(basename, final_pack)
692
"""Add a new pack to this object store.
694
:return: Fileobject to write to and a commit function to
695
call when the pack is finished.
697
from cStringIO import StringIO
700
if len(f.getvalue()) > 0:
701
return self.move_in_pack(f)
706
return f, commit, abort
709
def init(cls, transport):
711
transport.mkdir('info')
715
transport.mkdir(PACKDIR)
718
return cls(transport)