49
44
write_pack_index_v2,
51
from dulwich.objects import (
57
48
class BzrBackend(Backend):
59
def __init__(self, transport):
60
self.transport = transport
50
def __init__(self, directory):
51
self.directory = directory
61
52
self.mapping = default_mapping
63
54
def get_refs(self):
64
55
""" return a dict of all tags and branches in repository (and shas) """
66
repo_dir = BzrDir.open_from_transport(self.transport)
67
repo = repo_dir.find_repository()
70
store = get_object_store(repo)
72
for branch in repo.find_branches(using=True):
73
#FIXME: Look for 'master' or 'trunk' in here, and set HEAD accordingly...
74
#FIXME: Need to get branch path relative to its repository and use this instead of nick
75
ret["refs/heads/"+branch.nick] = store._lookup_revision_sha1(branch.last_revision())
76
if 'HEAD' not in ret and branch:
77
ret['HEAD'] = store._lookup_revision_sha1(branch.last_revision())
57
repo_dir = BzrDir.open(self.directory)
58
repo = repo_dir.open_repository()
59
store = get_object_store(repo)
61
for branch in repo.find_branches(using=True):
62
#FIXME: Look for 'master' or 'trunk' in here, and set HEAD accordingly...
63
#FIXME: Need to get branch path relative to its repository and use this instead of nick
64
ret["refs/heads/"+branch.nick] = store._lookup_revision_sha1(branch.last_revision())
65
if 'HEAD' not in ret and branch:
66
ret['HEAD'] = store._lookup_revision_sha1(branch.last_revision())
82
69
def apply_pack(self, refs, read):
83
"""apply pack from client to current repository"""
70
""" apply pack from client to current repository """
85
72
fd, path = tempfile.mkstemp(suffix=".pack")
86
73
f = os.fdopen(fd, 'w')
91
78
entries = p.sorted_entries()
96
t, o = p.get_object_at (offset)
99
79
write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
101
repo_dir = BzrDir.open_from_transport(self.transport)
102
target = repo_dir.find_repository()
82
pack = Pack(path[:-5])
83
for obj in pack.iterobjects():
105
for tup in p.iterobjects():
106
obj_type, obj = p.get_object_at (tup[0])
107
if obj_type in range(1, 4):
108
sf = ShaFile.from_raw_string (obj_type, obj)
109
objects[hex_to_sha(sf.id)] = sf
86
target = Repository.open(self.directory)
111
88
target.lock_write()
113
90
target.start_write_group()
115
import_git_objects(target, self.mapping, objects,
116
BazaarObjectStore (target, self.mapping),
119
target.abort_write_group()
92
import_git_objects(target, self.mapping, iter(get_objects()))
122
94
target.commit_write_group()
126
98
for oldsha, sha, ref in refs:
127
99
if ref[:11] == 'refs/heads/':
128
100
branch_nick = ref[11:]
129
transport = self.transport.clone(branch_nick)
132
target_dir = BzrDir.open_from_transport(transport)
103
target_dir = BzrDir.open(self.directory + "/" + branch_nick)
134
format = BzrDirFormat.get_default_format()
135
format.initialize_on_transport(transport)
105
target_dir = BzrDir.create(self.directory + "/" + branch_nick)
138
108
target_branch = target_dir.open_branch()
145
115
def fetch_objects(self, determine_wants, graph_walker, progress):
146
116
""" yield git objects to send to client """
147
bzrdir = BzrDir.open_from_transport(self.transport)
148
repo = bzrdir.find_repository()
117
repo = Repository.open(self.directory)
150
119
# If this is a Git repository, just use the existing fetch_objects implementation.
151
120
if getattr(repo, "fetch_objects", None) is not None:
152
121
return repo.fetch_objects(determine_wants, graph_walker, None, progress)
154
123
wants = determine_wants(self.get_refs())
157
store = BazaarObjectStore(repo)
158
have = store.find_common_revisions(graph_walker)
159
missing_sha1s = store.find_missing_objects(have, wants, progress)
160
return store.iter_shas(missing_sha1s)
163
def serve_git(transport, host=None, port=None, inet=False):
164
backend = BzrBackend(transport)
166
server = TCPGitServer(backend, 'localhost')
167
server.serve_forever()
127
store = BazaarObjectStore(repo)
128
have = store.find_missing_revisions(graph_walker)
129
missing_sha1s = store.find_missing_objects(have, wants, progress)
130
return self.object_store.iter_shas(missing_sha1s)