/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (C) 2008 Jelmer Vernooij
# Copyright (C) 2008 John Carr
# Copyright (C) 2008 Canonical Ltd
#
# 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

from dulwich.server import TCPGitServer

from bzrlib.bzrdir import (
    BzrDir,
    )
from bzrlib.errors import (
    NotBranchError,
    )

from bzrlib.plugins.git.branch import (
    branch_name_to_ref,
    ref_to_branch_name,
    )
from bzrlib.plugins.git.mapping import (
    default_mapping,
    )
from bzrlib.plugins.git.object_store import (
    get_object_store
    )

from dulwich.server import (
    Backend,
    BackendRepo,
    )

class BzrBackend(Backend):
    """A git serve backend that can use a Bazaar repository."""

    def __init__(self, transport):
        self.transport = transport
        self.mapping = default_mapping

    def open_repository(self, path):
        # FIXME: More secure path sanitization
        return BzrBackendRepo(self.transport.clone(path.lstrip("/")),
            self.mapping)


class BzrBackendRepo(BackendRepo):

    def __init__(self, transport, mapping):
        self.transport = transport
        self.mapping = mapping
        self.repo_dir = BzrDir.open_from_transport(self.transport)
        self.repo = self.repo_dir.find_repository()
        self.object_store = get_object_store(self.repo)

    def get_peeled(self, name):
        return self.get_refs()[name]

    def get_refs(self):
        """Return a dict of all tags and branches in repository (and shas) """
        ret = {}
        self.repo.lock_read()
        try:
            for branch in self.repo_dir.list_branches():
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
                ret[ref] = self.object_store._lookup_revision_sha1(
                    branch.last_revision())
                assert type(ref) == str and type(ret[ref]) == str, \
                        "(%s) %r -> %r" % (branch.name, ref, ret[ref])

        finally:
            self.repo.unlock()
        return ret

    def set_refs(self, refs):
        for oldsha, sha, ref in refs:
            try:
                branch_name = ref_to_branch_name(ref)
            except ValueError:
                # FIXME: Cope with tags!
                continue
            try:
                target_branch = self.repo_dir.open_branch(branch_name)
            except NotBranchError:
                target_branch = self.repo.create_branch(branch_name)

            rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
            target_branch.lock_write()
            try:
                target_branch.generate_revision_history(rev_id)
            finally:
                target_branch.unlock()

    def fetch_objects(self, determine_wants, graph_walker, progress,
        get_tagged=None):
        """ yield git objects to send to client """

        # If this is a Git repository, just use the existing fetch_objects implementation.
        if getattr(self.repo, "fetch_objects", None) is not None:
            return self.repo.fetch_objects(determine_wants, graph_walker, progress,
                get_tagged)

        wants = determine_wants(self.get_refs())
        self.repo.lock_read()
        try:
            have = self.object_store.find_common_revisions(graph_walker)
            return self.object_store.generate_pack_contents(have, wants, progress,
                get_tagged)
        finally:
            self.repo.unlock()


def serve_git(transport, host=None, port=None, inet=False):
    backend = BzrBackend(transport)

    if host is None:
        host = 'localhost'
    if port:
        server = TCPGitServer(backend, host, port)
    else:
        server = TCPGitServer(backend, host)
    server.serve_forever()