/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

Implement to_files() for git merge directives.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    BzrDir,
26
26
    BzrDirFormat,
27
27
    )
 
28
from bzrlib.repository import (
 
29
    Repository,
 
30
    )
28
31
 
29
32
from bzrlib.plugins.git.fetch import (
30
33
    import_git_objects,
 
34
    BazaarObjectStore,
31
35
    )
32
36
from bzrlib.plugins.git.mapping import (
33
37
    default_mapping,
38
42
 
39
43
from dulwich.server import (
40
44
    Backend,
41
 
    BackendRepo,
42
45
    )
43
46
from dulwich.pack import (
 
47
    Pack,
44
48
    PackData,
45
49
    write_pack_index_v2,
46
50
    )
47
51
from dulwich.objects import (
48
52
    ShaFile,
 
53
    sha_to_hex,
49
54
    hex_to_sha,
50
55
    )
51
56
 
52
57
class BzrBackend(Backend):
53
 
    """A git serve backend that can use a Bazaar repository."""
54
58
 
55
59
    def __init__(self, transport):
56
60
        self.transport = transport
57
61
        self.mapping = default_mapping
58
62
 
59
 
    def open_repository(self, path):
60
 
        # FIXME: Sanitize path properly
61
 
        return BzrBackendRepo(self.transport.clone(path.lstrip("/")), self.mapping)
62
 
 
63
 
 
64
 
class BzrBackendRepo(BackendRepo):
65
 
 
66
 
    def __init__(self, transport, mapping):
67
 
        self.transport = transport
68
 
        self.mapping = mapping
69
 
        self.repo_dir = BzrDir.open_from_transport(self.transport)
70
 
        self.repo = self.repo_dir.find_repository()
71
 
        self.object_store = get_object_store(self.repo)
72
 
 
73
63
    def get_refs(self):
74
 
        """Return a dict of all tags and branches in repository (and shas) """
 
64
        """ return a dict of all tags and branches in repository (and shas) """
75
65
        ret = {}
76
 
        branch = None
77
 
        for branch in self.repo.find_branches(using=True):
78
 
            #FIXME: Look for 'master' or 'trunk' in here, and set HEAD
79
 
            # accordingly...
80
 
            #FIXME: Need to get branch path relative to its repository and
81
 
            # use this instead of nick
82
 
            ret["refs/heads/"+branch.nick] = self.object_store._lookup_revision_sha1(branch.last_revision())
83
 
        if 'HEAD' not in ret and branch:
84
 
            ret['HEAD'] = self.object_store._lookup_revision_sha1(branch.last_revision())
 
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()
85
80
        return ret
86
81
 
87
82
    def apply_pack(self, refs, read):
88
 
        """Apply pack from client to current repository"""
 
83
        """apply pack from client to current repository"""
89
84
 
90
85
        fd, path = tempfile.mkstemp(suffix=".pack")
91
86
        f = os.fdopen(fd, 'w')
103
98
                heads.append(sha)
104
99
        write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
105
100
 
 
101
        repo_dir = BzrDir.open_from_transport(self.transport)
 
102
        target = repo_dir.find_repository()
 
103
 
106
104
        objects = {}
107
105
        for tup in p.iterobjects():
108
106
            obj_type, obj = p.get_object_at (tup[0])
110
108
                sf = ShaFile.from_raw_string (obj_type, obj)
111
109
                objects[hex_to_sha(sf.id)] = sf
112
110
 
113
 
        self.repo.lock_write()
 
111
        target.lock_write()
114
112
        try:
115
 
            self.repo.start_write_group()
 
113
            target.start_write_group()
116
114
            try:
117
 
                import_git_objects(self.repo, self.mapping, objects,
118
 
                                   self.object_store,
 
115
                import_git_objects(target, self.mapping, objects,
 
116
                                   BazaarObjectStore (target, self.mapping),
119
117
                                   heads)
120
118
            except:
121
 
                self.repo.abort_write_group()
 
119
                target.abort_write_group()
122
120
                raise
123
121
            else:
124
 
                self.repo.commit_write_group()
 
122
                target.commit_write_group()
125
123
        finally:
126
 
            self.repo.unlock()
 
124
            target.unlock()
127
125
 
128
126
        for oldsha, sha, ref in refs:
129
127
            if ref[:11] == 'refs/heads/':
130
128
                branch_nick = ref[11:]
131
 
                transport = self.repo.root_transport.clone(branch_nick)
 
129
                transport = self.transport.clone(branch_nick)
132
130
 
133
131
                try:
134
132
                    target_dir = BzrDir.open_from_transport(transport)
146
144
 
147
145
    def fetch_objects(self, determine_wants, graph_walker, progress):
148
146
        """ yield git objects to send to client """
 
147
        bzrdir = BzrDir.open_from_transport(self.transport)
 
148
        repo = bzrdir.find_repository()
 
149
 
149
150
        # If this is a Git repository, just use the existing fetch_objects implementation.
150
 
        if getattr(self.repo, "fetch_objects", None) is not None:
151
 
            return self.repo.fetch_objects(determine_wants, graph_walker, None, progress)[0]
 
151
        if getattr(repo, "fetch_objects", None) is not None:
 
152
            return repo.fetch_objects(determine_wants, graph_walker, None, progress)[0]
152
153
 
153
154
        wants = determine_wants(self.get_refs())
154
155
        graph_walker.reset()
155
 
        have = self.object_store.find_common_revisions(graph_walker)
156
 
        return self.object_store.generate_pack_contents(have, wants)
 
156
        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)
157
161
 
158
162
 
159
163
def serve_git(transport, host=None, port=None, inet=False):
160
164
    backend = BzrBackend(transport)
161
165
 
162
 
    if host is None:
163
 
        host = 'localhost'
164
 
    if port:
165
 
        server = TCPGitServer(backend, host, port)
166
 
    else:
167
 
        server = TCPGitServer(backend, host)
 
166
    server = TCPGitServer(backend, 'localhost')
168
167
    server.serve_forever()