/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 tests for sha maps, fix cache misses in tdb.

Show diffs side-by-side

added added

removed removed

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