/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 remote.py

Fix some bit of fetching.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.trace import info
24
24
from bzrlib.transport import Transport
25
25
 
26
 
from bzrlib.plugins.git import lazy_check_versions
27
 
lazy_check_versions()
28
 
 
 
26
from bzrlib.plugins.git import git
29
27
from bzrlib.plugins.git.branch import GitBranch
30
 
from bzrlib.plugins.git.errors import NoSuchRef
31
28
from bzrlib.plugins.git.dir import GitDir
32
29
from bzrlib.plugins.git.foreign import ForeignBranch
33
30
from bzrlib.plugins.git.repository import GitFormat, GitRepository
34
31
 
35
 
import os
36
 
import tempfile
37
32
import urllib
38
33
import urlparse
39
34
 
40
 
import dulwich as git
41
 
from dulwich.pack import PackData, Pack
42
 
 
43
 
# Don't run any tests on GitSmartTransport as it is not intended to be 
44
 
# a full implementation of Transport
45
 
def get_test_permutations():
46
 
    return []
 
35
from dulwich.pack import PackData
47
36
 
48
37
 
49
38
class GitSmartTransport(Transport):
54
43
        assert scheme == "git"
55
44
        hostport, self._path = urllib.splithost(loc)
56
45
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
57
 
        self._client = _client
58
 
 
59
 
    def _get_client(self):
60
 
        if self._client is not None:
61
 
            ret = self._client
62
 
            self._client = None
63
 
            return ret
64
 
        return git.client.TCPGitClient(self._host, self._port)
 
46
        if _client is not None:
 
47
            self._client = _client
 
48
        else:
 
49
            self._client = git.client.TCPGitClient(self._host, self._port)
65
50
 
66
51
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
67
52
        if progress is None:
68
53
            def progress(text):
69
54
                info("git: %s" % text)
70
 
        self._get_client().fetch_pack(self._path, determine_wants, 
71
 
            graph_walker, pack_data, progress)
 
55
        self._client.fetch_pack(self._path, determine_wants, graph_walker, 
 
56
                pack_data, progress)
 
57
 
 
58
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
59
        fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
 
60
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
 
61
        os.close(fd)
 
62
        try:
 
63
            p = PackData(path)
 
64
            for o in p.iterobjects():
 
65
                yield o
 
66
        finally:
 
67
            os.remove(path)
72
68
 
73
69
    def get(self, path):
74
70
        raise NoSuchFile(path)
75
71
 
76
 
    def abspath(self, relpath):
77
 
        return urlutils.join(self.base, relpath)
78
 
 
79
72
    def clone(self, offset=None):
80
73
        """See Transport.clone()."""
81
74
        if offset is None:
97
90
    def open_repository(self):
98
91
        return RemoteGitRepository(self, self._lockfiles)
99
92
 
100
 
    def open_branch(self, _unsupported=False):
 
93
    def open_branch(self):
101
94
        repo = self.open_repository()
102
95
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
103
96
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
106
99
        raise NotLocalUrl(self.transport.base)
107
100
 
108
101
 
109
 
class TemporaryPackIterator(object):
110
 
 
111
 
    def __init__(self, path):
112
 
        self.path_data = path
113
 
        basename = path[:-len(".pack")]
114
 
        p = PackData(path)
115
 
        self.path_idx = basename+".idx"
116
 
        p.create_index_v2(self.path_idx)
117
 
        self.pack = Pack(basename)
118
 
        self._iter = self.pack.iterobjects()
119
 
 
120
 
    def __del__(self):
121
 
        os.remove(self.path_data)
122
 
        os.remove(self.path_idx)
123
 
 
124
 
    def next(self):
125
 
        return (self._iter.next(), None)
126
 
 
127
 
    def __len__(self):
128
 
        return len(self.pack)
129
 
 
130
 
 
131
102
class RemoteGitRepository(GitRepository):
132
103
 
133
104
    def __init__(self, gitdir, lockfiles):
138
109
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
139
110
            progress)
140
111
 
141
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
142
 
        fd, path = tempfile.mkstemp(suffix=".pack")
143
 
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
144
 
        os.close(fd)
145
 
        ret = TemporaryPackIterator(path)
146
 
        return (len(ret), iter(ret.next, None))
147
 
 
148
112
 
149
113
class RemoteGitBranch(GitBranch):
150
114
 
151
115
    def __init__(self, bzrdir, repository, name, lockfiles):
152
116
        def determine_wants(heads):
153
 
            if not name in heads:
154
 
                raise NoSuchRef(name)
155
117
            self._ref = heads[name]
156
118
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
157
119
                             lambda x: mutter("git: %s" % x))
160
122
    def last_revision(self):
161
123
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
162
124
 
163
 
    def _synchronize_history(self, destination, revision_id):
164
 
        """See Branch._synchronize_history()."""
165
 
        destination.generate_revision_history(self.last_revision())
166