/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: Robert Collins
  • Date: 2010-05-06 07:48:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506074822-0bsgf2j4h8jx0xkk
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
our first in-tree matcher. See the module docstring for details.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
 
 
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
28
 
 
29
 
class BzrBackend(Backend):
30
 
 
31
 
    def __init__(self, directory):
32
 
        self.directory = directory
33
 
        self.mapping = default_mapping
34
 
 
35
 
    def get_refs(self):
36
 
        """ return a dict of all tags and branches in repository (and shas) """
37
 
        return {}
38
 
 
39
 
    def apply_pack(self, refs, read):
40
 
        """ apply pack from client to current repository """
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) 
84
 
 
85
 
    def fetch_objects(self, determine_wants, graph_walker, progress):
86
 
        """ yield git objects to send to client """
87