/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

Move refs code to separate module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.bzrdir import (
22
22
    BzrDir,
23
23
    )
 
24
from bzrlib.errors import (
 
25
    NotBranchError,
 
26
    )
24
27
 
25
28
from bzrlib.plugins.git.mapping import (
26
29
    default_mapping,
29
32
    get_object_store
30
33
    )
31
34
from bzrlib.plugins.git.refs import (
32
 
    BazaarRefsContainer,
 
35
    branch_name_to_ref,
 
36
    ref_to_branch_name,
33
37
    )
34
38
 
35
39
from dulwich.server import (
58
62
        self.repo_dir = BzrDir.open_from_transport(self.transport)
59
63
        self.repo = self.repo_dir.find_repository()
60
64
        self.object_store = get_object_store(self.repo)
61
 
        self.refs = BazaarRefsContainer(self.repo_dir, self.object_store)
62
 
        self._refs = self.refs.as_dict() # Much faster for now..
63
 
 
64
 
    def get_refs(self):
65
 
        return self._refs
66
65
 
67
66
    def get_peeled(self, name):
68
67
        return self.get_refs()[name]
69
68
 
 
69
    def get_refs(self):
 
70
        """Return a dict of all tags and branches in repository (and shas) """
 
71
        ret = {}
 
72
        self.repo.lock_read()
 
73
        try:
 
74
            for branch in self.repo_dir.list_branches():
 
75
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
76
                ret[ref] = self.object_store._lookup_revision_sha1(
 
77
                    branch.last_revision())
 
78
                assert type(ref) == str and type(ret[ref]) == str, \
 
79
                        "(%s) %r -> %r" % (branch.name, ref, ret[ref])
 
80
        finally:
 
81
            self.repo.unlock()
 
82
        return ret
 
83
 
 
84
    def set_refs(self, refs):
 
85
        for oldsha, sha, ref in refs:
 
86
            try:
 
87
                branch_name = ref_to_branch_name(ref)
 
88
            except ValueError:
 
89
                # FIXME: Cope with tags!
 
90
                continue
 
91
            try:
 
92
                target_branch = self.repo_dir.open_branch(branch_name)
 
93
            except NotBranchError:
 
94
                target_branch = self.repo.create_branch(branch_name)
 
95
 
 
96
            rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
97
            target_branch.lock_write()
 
98
            try:
 
99
                target_branch.generate_revision_history(rev_id)
 
100
            finally:
 
101
                target_branch.unlock()
 
102
 
70
103
    def fetch_objects(self, determine_wants, graph_walker, progress,
71
104
        get_tagged=None):
72
105
        """ yield git objects to send to client """
73
106
 
 
107
        # If this is a Git repository, just use the existing fetch_objects implementation.
 
108
        if getattr(self.repo, "fetch_objects", None) is not None:
 
109
            return self.repo.fetch_objects(determine_wants, graph_walker, progress,
 
110
                get_tagged)
 
111
 
74
112
        wants = determine_wants(self.get_refs())
75
113
        self.repo.lock_read()
76
114
        try: