1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""A Git repository implementation that uses a Bazaar transport."""
from dulwich.errors import (
NotGitRepository,
)
from dulwich.repo import (
BaseRepo,
OBJECTDIR,
REFSDIR,
BASE_DIRECTORIES
)
from bzrlib import (
urlutils,
)
from bzrlib.errors import (
NoSuchFile,
)
class TransportRepo(BaseRepo):
def __init__(self, transport):
super(TransportRepo, self).__init__()
self.transport = transport
if self.transport.has(urlutils.join(".git", OBJECTDIR)):
self.bare = False
self._controltransport = self.transport.clone('.git')
elif (self.transport.has(OBJECTDIR) and
self.transport.has(REFSDIR)):
self.bare = True
self._controltransport = self.transport
else:
raise NotGitRepository(self.transport)
object_store = TransportObjectStore(
self._controltransport.clone(OBJECTDIR))
refs = TransportRefsContainer(self._controltransport)
BaseRepo.__init__(self, object_store, refs)
def get_named_file(self, path):
"""Get a file from the control dir with a specific name.
Although the filename should be interpreted as a filename relative to
the control dir in a disk-baked Repo, the object returned need not be
pointing to a file in that location.
:param path: The path to the file, relative to the control dir.
:return: An open file object, or None if the file does not exist.
"""
try:
return self._controltransport.get(path.lstrip('/'))
except NoSuchFile:
return None
def put_named_file(self, path, contents):
self._controltransport.put_bytes(path.lstrip('/'), contents)
def open_index(self):
"""Open the index for this repository."""
raise NotImplementedError
def __repr__(self):
return "<TransportRepo for %r>" % self.transport
@classmethod
def init(cls, transport, mkdir=True):
transport.mkdir('.git')
controltransport = transport.clone('.git')
cls.init_bare(controltransport)
return cls(controltransport)
@classmethod
def init_bare(cls, transport, mkdir=True):
for d in BASE_DIRECTORIES:
transport.mkdir(urlutils.join(*d))
ret = cls(transport)
ret.refs.set_ref("HEAD", "refs/heads/master")
ret.put_named_file('description', "Unnamed repository")
ret.put_named_file('config', """[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
""")
ret.put_named_file('info/excludes', '')
return ret
create = init_bare
|