/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

Merge new bzr-foreign.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from bzrlib.bzrdir import BzrDir
 
18
from bzrlib.repository import Repository
 
19
 
 
20
from bzrlib.plugins.git.fetch import import_git_objects
 
21
from bzrlib.plugins.git.mapping import default_mapping
 
22
 
17
23
from dulwich.server import Backend
 
24
from dulwich.pack import Pack, PackData, write_pack_index_v2
 
25
from dulwich.objects import ShaFile
 
26
 
 
27
import os, tempfile
18
28
 
19
29
class BzrBackend(Backend):
20
30
 
21
31
    def __init__(self, directory):
22
32
        self.directory = directory
 
33
        self.mapping = default_mapping
23
34
 
24
35
    def get_refs(self):
25
36
        """ return a dict of all tags and branches in repository (and shas) """
27
38
 
28
39
    def apply_pack(self, refs, read):
29
40
        """ apply pack from client to current repository """
30
 
        self.read()
 
41
 
 
42
        fd, path = tempfile.mkstemp(suffix=".pack")
 
43
        f = os.fdopen(fd, 'w')
 
44
        f.write(read())
 
45
        f.close()
 
46
 
 
47
        p = PackData(path)
 
48
        entries = p.sorted_entries()
 
49
        write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())
 
50
 
 
51
        def get_objects():
 
52
            pack = Pack(path[:-5])
 
53
            for obj in pack.iterobjects():
 
54
                yield obj
 
55
 
 
56
        target = Repository.open(self.directory)
 
57
 
 
58
        target.lock_write()
 
59
        try:
 
60
            target.start_write_group()
 
61
            try:
 
62
                import_git_objects(target, self.mapping, iter(get_objects()))
 
63
            finally:
 
64
                target.commit_write_group()
 
65
        finally:
 
66
            target.unlock()
 
67
 
 
68
        for oldsha, sha, ref in refs:
 
69
            if ref[:11] == 'refs/heads/':
 
70
                branch_nick = ref[11:]
 
71
 
 
72
                try:
 
73
                    target_dir = BzrDir.open(self.directory + "/" + branch_nick)
 
74
                except:
 
75
                    target_dir = BzrDir.create(self.directory + "/" + branch_nick)
 
76
 
 
77
                try:
 
78
                    target_branch = target_dir.open_branch()
 
79
                except:
 
80
                    target_branch = target_dir.create_branch()
 
81
               
 
82
                rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
83
                target_branch.generate_revision_history(rev_id) 
31
84
 
32
85
    def fetch_objects(self, determine_wants, graph_walker, progress):
33
86
        """ yield git objects to send to client """