1
# Copyright (C) 2008 Canonical Ltd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from bzrlib.bzrdir import BzrDir
18
from bzrlib.repository import Repository
20
from bzrlib.plugins.git.fetch import import_git_objects
21
from bzrlib.plugins.git.mapping import default_mapping
23
from dulwich.server import Backend
24
from dulwich.pack import Pack, PackData, write_pack_index_v2
25
from dulwich.objects import ShaFile
29
class BzrBackend(Backend):
31
def __init__(self, directory):
32
self.directory = directory
33
self.mapping = default_mapping
36
""" return a dict of all tags and branches in repository (and shas) """
39
def apply_pack(self, refs, read):
40
""" apply pack from client to current repository """
42
fd, path = tempfile.mkstemp(suffix=".pack")
43
f = os.fdopen(fd, 'w')
48
entries = p.sorted_entries()
49
write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
52
pack = Pack(path[:-5])
53
for obj in pack.iterobjects():
56
target = Repository.open(self.directory)
60
target.start_write_group()
62
import_git_objects(target, self.mapping, iter(get_objects()))
64
target.commit_write_group()
68
for oldsha, sha, ref in refs:
69
if ref[:11] == 'refs/heads/':
70
branch_nick = ref[11:]
73
target_dir = BzrDir.open(self.directory + "/" + branch_nick)
75
target_dir = BzrDir.create(self.directory + "/" + branch_nick)
78
target_branch = target_dir.open_branch()
80
target_branch = target_dir.create_branch()
82
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
83
target_branch.generate_revision_history(rev_id)
85
def fetch_objects(self, determine_wants, graph_walker, progress):
86
""" yield git objects to send to client """