/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to server.py

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Jelmer Vernooij
 
2
# Copyright (C) 2008 John Carr
1
3
# Copyright (C) 2008 Canonical Ltd
2
4
#
3
5
# This program is free software; you can redistribute it and/or modify
14
16
# along with this program; if not, write to the Free Software
15
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
18
 
17
 
import os
18
 
import stat
19
 
import tempfile
 
19
from dulwich.server import TCPGitServer
20
20
 
21
21
from bzrlib.bzrdir import (
22
22
    BzrDir,
23
23
    )
24
 
from bzrlib.inventory import (
25
 
    InventoryDirectory,
26
 
    InventoryFile,
27
 
    )
28
 
from bzrlib.osutils import (
29
 
    splitpath,
30
 
    )
31
 
from bzrlib.repository import (
32
 
    Repository,
33
 
    )
34
24
 
35
 
from bzrlib.plugins.git.fetch import (
36
 
    import_git_objects,
37
 
    )
38
25
from bzrlib.plugins.git.mapping import (
39
26
    default_mapping,
40
 
    inventory_to_tree_and_blobs,
41
 
    revision_to_commit,
42
27
    )
43
28
from bzrlib.plugins.git.object_store import (
44
29
    get_object_store
45
30
    )
 
31
from bzrlib.plugins.git.refs import (
 
32
    BazaarRefsContainer,
 
33
    )
46
34
 
47
35
from dulwich.server import (
48
36
    Backend,
49
 
    )
50
 
from dulwich.pack import (
51
 
    Pack,
52
 
    PackData,
53
 
    write_pack_index_v2,
54
 
    )
55
 
from dulwich.objects import (
56
 
    Blob,
57
 
    Commit,
58
 
    ShaFile,
59
 
    Tree,
60
 
    )
61
 
 
 
37
    BackendRepo,
 
38
    )
62
39
 
63
40
class BzrBackend(Backend):
 
41
    """A git serve backend that can use a Bazaar repository."""
64
42
 
65
 
    def __init__(self, directory):
66
 
        self.directory = directory
 
43
    def __init__(self, transport):
 
44
        self.transport = transport
67
45
        self.mapping = default_mapping
68
46
 
 
47
    def open_repository(self, path):
 
48
        # FIXME: More secure path sanitization
 
49
        return BzrBackendRepo(self.transport.clone(path.lstrip("/")),
 
50
            self.mapping)
 
51
 
 
52
 
 
53
class BzrBackendRepo(BackendRepo):
 
54
 
 
55
    def __init__(self, transport, mapping):
 
56
        self.transport = transport
 
57
        self.mapping = mapping
 
58
        self.repo_dir = BzrDir.open_from_transport(self.transport)
 
59
        self.repo = self.repo_dir.find_repository()
 
60
        self.object_store = get_object_store(self.repo)
 
61
        self.refs = BazaarRefsContainer(self.repo_dir, self.object_store)
 
62
        self._refs = self.refs.as_dict() # Much faster for now..
 
63
 
69
64
    def get_refs(self):
70
 
        """ return a dict of all tags and branches in repository (and shas) """
71
 
        ret = {}
72
 
        repo_dir = BzrDir.open(self.directory)
73
 
        repo = repo_dir.open_repository()
74
 
        store = get_object_store(repo)
75
 
        branch = None
76
 
        for branch in repo.find_branches(using=True):
77
 
            #FIXME: Look for 'master' or 'trunk' in here, and set HEAD accordingly...
78
 
            #FIXME: Need to get branch path relative to its repository and use this instead of nick
79
 
            ret["refs/heads/"+branch.nick] = store._lookup_revision_sha1(branch.last_revision())
80
 
        if 'HEAD' not in ret and branch:
81
 
            ret['HEAD'] = store._lookup_revision_sha1(branch.last_revision())
82
 
        return ret
83
 
 
84
 
    def apply_pack(self, refs, read):
85
 
        """ apply pack from client to current repository """
86
 
 
87
 
        fd, path = tempfile.mkstemp(suffix=".pack")
88
 
        f = os.fdopen(fd, 'w')
89
 
        f.write(read())
90
 
        f.close()
91
 
 
92
 
        p = PackData(path)
93
 
        entries = p.sorted_entries()
94
 
        write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
95
 
 
96
 
        def get_objects():
97
 
            pack = Pack(path[:-5])
98
 
            for obj in pack.iterobjects():
99
 
                yield obj
100
 
 
101
 
        target = Repository.open(self.directory)
102
 
 
103
 
        target.lock_write()
104
 
        try:
105
 
            target.start_write_group()
106
 
            try:
107
 
                import_git_objects(target, self.mapping, iter(get_objects()))
108
 
            finally:
109
 
                target.commit_write_group()
110
 
        finally:
111
 
            target.unlock()
112
 
 
113
 
        for oldsha, sha, ref in refs:
114
 
            if ref[:11] == 'refs/heads/':
115
 
                branch_nick = ref[11:]
116
 
 
117
 
                try:
118
 
                    target_dir = BzrDir.open(self.directory + "/" + branch_nick)
119
 
                except:
120
 
                    target_dir = BzrDir.create(self.directory + "/" + branch_nick)
121
 
 
122
 
                try:
123
 
                    target_branch = target_dir.open_branch()
124
 
                except:
125
 
                    target_branch = target_dir.create_branch()
126
 
 
127
 
                rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
128
 
                target_branch.generate_revision_history(rev_id)
129
 
 
130
 
    def fetch_objects(self, determine_wants, graph_walker, progress):
 
65
        return self._refs
 
66
 
 
67
    def get_peeled(self, name):
 
68
        return self.get_refs()[name]
 
69
 
 
70
    def fetch_objects(self, determine_wants, graph_walker, progress,
 
71
        get_tagged=None):
131
72
        """ yield git objects to send to client """
132
 
        repo = Repository.open(self.directory)
133
 
 
134
 
        # If this is a Git repository, just use the existing fetch_objects implementation.
135
 
        if getattr(repo, "fetch_objects", None) is not None:
136
 
            return repo.fetch_objects(determine_wants, graph_walker, None, progress)
137
73
 
138
74
        wants = determine_wants(self.get_refs())
139
 
 
140
 
        repo.lock_read()
 
75
        self.repo.lock_read()
141
76
        try:
142
 
            store = BazaarObjectStore(repo)
143
 
            have = store.find_missing_revisions(graph_walker)
144
 
            missing_sha1s = store.find_missing_objects(have, wants, progress)
145
 
            return self.object_store.iter_shas(missing_sha1s)
 
77
            have = self.object_store.find_common_revisions(graph_walker)
 
78
            return self.object_store.generate_pack_contents(have, wants, progress,
 
79
                get_tagged)
146
80
        finally:
147
 
            repo.unlock()
 
81
            self.repo.unlock()
 
82
 
 
83
 
 
84
def serve_git(transport, host=None, port=None, inet=False):
 
85
    backend = BzrBackend(transport)
 
86
 
 
87
    if host is None:
 
88
        host = 'localhost'
 
89
    if port:
 
90
        server = TCPGitServer(backend, host, port)
 
91
    else:
 
92
        server = TCPGitServer(backend, host)
 
93
    server.serve_forever()