/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

  • Committer: Jelmer Vernooij
  • Date: 2009-05-16 21:13:17 UTC
  • mto: (0.200.527 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090516211317-a0s14tsrf2qusapf
Fix tests.

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
19
17
import os
20
18
import tempfile
21
19
 
22
 
from dulwich.server import TCPGitServer
23
 
 
24
20
from bzrlib.bzrdir import (
25
21
    BzrDir,
26
 
    BzrDirFormat,
27
22
    )
28
23
from bzrlib.repository import (
29
24
    Repository,
48
43
    PackData,
49
44
    write_pack_index_v2,
50
45
    )
51
 
from dulwich.objects import (
52
 
    ShaFile,
53
 
    sha_to_hex,
54
 
    hex_to_sha,
55
 
    )
 
46
 
56
47
 
57
48
class BzrBackend(Backend):
58
49
 
59
 
    def __init__(self, transport):
60
 
        self.transport = transport
 
50
    def __init__(self, directory):
 
51
        self.directory = directory
61
52
        self.mapping = default_mapping
62
53
 
63
54
    def get_refs(self):
64
55
        """ return a dict of all tags and branches in repository (and shas) """
65
56
        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()
 
57
        repo_dir = BzrDir.open(self.directory)
 
58
        repo = repo_dir.open_repository()
 
59
        store = get_object_store(repo)
 
60
        branch = None
 
61
        for branch in repo.find_branches(using=True):
 
62
            #FIXME: Look for 'master' or 'trunk' in here, and set HEAD accordingly...
 
63
            #FIXME: Need to get branch path relative to its repository and use this instead of nick
 
64
            ret["refs/heads/"+branch.nick] = store._lookup_revision_sha1(branch.last_revision())
 
65
        if 'HEAD' not in ret and branch:
 
66
            ret['HEAD'] = store._lookup_revision_sha1(branch.last_revision())
80
67
        return ret
81
68
 
82
69
    def apply_pack(self, refs, read):
83
 
        """apply pack from client to current repository"""
 
70
        """ apply pack from client to current repository """
84
71
 
85
72
        fd, path = tempfile.mkstemp(suffix=".pack")
86
73
        f = os.fdopen(fd, 'w')
89
76
 
90
77
        p = PackData(path)
91
78
        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
79
        write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
100
80
 
101
 
        repo_dir = BzrDir.open_from_transport(self.transport)
102
 
        target = repo_dir.find_repository()
 
81
        def get_objects():
 
82
            pack = Pack(path[:-5])
 
83
            for obj in pack.iterobjects():
 
84
                yield obj
103
85
 
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
 
86
        target = Repository.open(self.directory)
110
87
 
111
88
        target.lock_write()
112
89
        try:
113
90
            target.start_write_group()
114
91
            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:
 
92
                import_git_objects(target, self.mapping, iter(get_objects()))
 
93
            finally:
122
94
                target.commit_write_group()
123
95
        finally:
124
96
            target.unlock()
126
98
        for oldsha, sha, ref in refs:
127
99
            if ref[:11] == 'refs/heads/':
128
100
                branch_nick = ref[11:]
129
 
                transport = self.transport.clone(branch_nick)
130
101
 
131
102
                try:
132
 
                    target_dir = BzrDir.open_from_transport(transport)
 
103
                    target_dir = BzrDir.open(self.directory + "/" + branch_nick)
133
104
                except:
134
 
                    format = BzrDirFormat.get_default_format()
135
 
                    format.initialize_on_transport(transport)
 
105
                    target_dir = BzrDir.create(self.directory + "/" + branch_nick)
136
106
 
137
107
                try:
138
108
                    target_branch = target_dir.open_branch()
144
114
 
145
115
    def fetch_objects(self, determine_wants, graph_walker, progress):
146
116
        """ yield git objects to send to client """
147
 
        bzrdir = BzrDir.open_from_transport(self.transport)
148
 
        repo = bzrdir.find_repository()
 
117
        repo = Repository.open(self.directory)
149
118
 
150
119
        # If this is a Git repository, just use the existing fetch_objects implementation.
151
120
        if getattr(repo, "fetch_objects", None) is not None:
152
121
            return repo.fetch_objects(determine_wants, graph_walker, None, progress)
153
122
 
154
123
        wants = determine_wants(self.get_refs())
155
 
        graph_walker.reset()
 
124
 
156
125
        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()
 
126
        try:
 
127
            store = BazaarObjectStore(repo)
 
128
            have = store.find_missing_revisions(graph_walker)
 
129
            missing_sha1s = store.find_missing_objects(have, wants, progress)
 
130
            return self.object_store.iter_shas(missing_sha1s)
 
131
        finally:
 
132
            repo.unlock()