/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

Add tests for revspec.

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
 
import bzrlib
18
17
from bzrlib import (
19
 
    branch,
 
18
    config,
 
19
    debug,
20
20
    tag,
 
21
    trace,
21
22
    ui,
22
23
    urlutils,
23
24
    )
28
29
    NoSuchRevision,
29
30
    NotLocalUrl,
30
31
    )
31
 
from bzrlib.trace import (
32
 
    info,
33
 
    )
34
32
from bzrlib.transport import (
35
33
    Transport,
36
34
    )
55
53
    mapping_registry,
56
54
    )
57
55
from bzrlib.plugins.git.repository import (
58
 
    GitRepositoryFormat,
59
56
    GitRepository,
60
57
    )
61
58
 
71
68
import tempfile
72
69
import urllib
73
70
import urlparse
74
 
 
75
 
try:
76
 
    from dulwich.pack import load_pack_index
77
 
except ImportError:
78
 
    from dulwich.pack import PackIndex as load_pack_index
79
 
 
80
 
 
81
 
# Don't run any tests on GitSmartTransport as it is not intended to be 
 
71
urlparse.uses_netloc.extend(['git', 'git+ssh'])
 
72
 
 
73
from dulwich.pack import load_pack_index
 
74
 
 
75
 
 
76
# Don't run any tests on GitSmartTransport as it is not intended to be
82
77
# a full implementation of Transport
83
78
def get_test_permutations():
84
79
    return []
85
80
 
86
81
 
 
82
def split_git_url(url):
 
83
    """Split a Git URL.
 
84
 
 
85
    :param url: Git URL
 
86
    :return: Tuple with host, port, username, path.
 
87
    """
 
88
    (scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
 
89
    path = urllib.unquote(loc)
 
90
    if path.startswith("/~"):
 
91
        path = path[1:]
 
92
    (username, hostport) = urllib.splituser(netloc)
 
93
    (host, port) = urllib.splitnport(hostport, None)
 
94
    return (host, port, username, path)
 
95
 
 
96
 
87
97
class GitSmartTransport(Transport):
88
98
 
89
99
    def __init__(self, url, _client=None):
90
100
        Transport.__init__(self, url)
91
 
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
92
 
        hostport, self._path = urllib.splithost(loc)
93
 
        (self._username, hostport) = urllib.splituser(hostport)
94
 
        (self._host, self._port) = urllib.splitnport(hostport, None)
 
101
        (self._host, self._port, self._username, self._path) = \
 
102
            split_git_url(url)
 
103
        if 'transport' in debug.debug_flags:
 
104
            trace.mutter('host: %r, user: %r, port: %r, path: %r',
 
105
                         self._host, self._username, self._port, self._path)
95
106
        self._client = _client
96
107
 
97
108
    def external_url(self):
100
111
    def has(self, relpath):
101
112
        return False
102
113
 
103
 
    def _get_client(self):
 
114
    def _get_client(self, thin_packs):
104
115
        raise NotImplementedError(self._get_client)
105
116
 
106
117
    def _get_path(self):
109
120
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
110
121
        if progress is None:
111
122
            def progress(text):
112
 
                info("git: %s" % text)
113
 
        client = self._get_client()
 
123
                trace.info("git: %s" % text)
 
124
        client = self._get_client(thin_packs=False)
114
125
        try:
115
 
            return client.fetch_pack(self._get_path(), determine_wants, 
 
126
            return client.fetch_pack(self._get_path(), determine_wants,
116
127
                graph_walker, pack_data, progress)
117
128
        except GitProtocolError, e:
118
129
            raise BzrError(e)
119
130
 
120
131
    def send_pack(self, get_changed_refs, generate_pack_contents):
121
 
        client = self._get_client()
 
132
        client = self._get_client(thin_packs=False)
122
133
        try:
123
 
            return client.send_pack(self._get_path(), get_changed_refs, 
 
134
            return client.send_pack(self._get_path(), get_changed_refs,
124
135
                generate_pack_contents)
125
136
        except GitProtocolError, e:
126
137
            raise BzrError(e)
145
156
 
146
157
    _scheme = 'git'
147
158
 
148
 
    def _get_client(self):
 
159
    def _get_client(self, thin_packs):
149
160
        if self._client is not None:
150
161
            ret = self._client
151
162
            self._client = None
152
163
            return ret
153
 
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
 
164
        return git.client.TCPGitClient(self._host, self._port, thin_packs=thin_packs,
154
165
            report_activity=self._report_activity)
155
166
 
156
167
 
163
174
            return self._path[3:]
164
175
        return self._path
165
176
 
166
 
    def _get_client(self):
 
177
    def _get_client(self, thin_packs):
167
178
        if self._client is not None:
168
179
            ret = self._client
169
180
            self._client = None
170
181
            return ret
171
182
        return git.client.SSHGitClient(self._host, self._port, self._username,
172
 
            thin_packs=False, report_activity=self._report_activity)
 
183
            thin_packs=thin_packs, report_activity=self._report_activity)
173
184
 
174
185
 
175
186
class RemoteGitDir(GitDir):
184
195
    def open_repository(self):
185
196
        return RemoteGitRepository(self, self._lockfiles)
186
197
 
187
 
    def open_branch(self, ignore_fallbacks=False):
 
198
    def _open_branch(self, name=None, ignore_fallbacks=False, 
 
199
                    unsupported=False):
188
200
        repo = self.open_repository()
189
 
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
190
 
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
 
201
        refname = self._branch_name_to_ref(name)
 
202
        return RemoteGitBranch(self, repo, refname, self._lockfiles)
191
203
 
192
 
    def open_workingtree(self):
 
204
    def open_workingtree(self, recommend_upgrade=False):
193
205
        raise NotLocalUrl(self.transport.base)
194
206
 
195
207
 
206
218
        self.resolve_ext_ref = resolve_ext_ref
207
219
 
208
220
    @property
 
221
    def data(self):
 
222
        if self._data is None:
 
223
            self._data = PackData(self._data_path)
 
224
        return self._data
 
225
 
 
226
    @property
209
227
    def index(self):
210
228
        if self._idx is None:
211
 
            pb = ui.ui_factory.nested_progress_bar()
212
 
            try:
213
 
                def report_progress(cur, total):
214
 
                    pb.update("generating index", cur, total)
215
 
                self.data.create_index(self._idx_path, self.resolve_ext_ref,
216
 
                    progress=report_progress)
217
 
            finally:
218
 
                pb.finished()
 
229
            if not os.path.exists(self._idx_path):
 
230
                pb = ui.ui_factory.nested_progress_bar()
 
231
                try:
 
232
                    def report_progress(cur, total):
 
233
                        pb.update("generating index", cur, total)
 
234
                    self.data.create_index(self._idx_path, self.resolve_ext_ref,
 
235
                        progress=report_progress)
 
236
                finally:
 
237
                    pb.finished()
219
238
            self._idx = load_pack_index(self._idx_path)
220
239
        return self._idx
221
240
 
222
241
    def __del__(self):
223
 
        os.remove(self._data_path)
224
 
        os.remove(self._idx_path)
 
242
        if self._idx is not None:
 
243
            self._idx.close()
 
244
            os.remove(self._idx_path)
 
245
        if self._data is not None:
 
246
            self._data.close()
 
247
            os.remove(self._data_path)
225
248
 
226
249
 
227
250
class RemoteGitRepository(GitRepository):
245
268
    def get_refs(self):
246
269
        if self._refs is not None:
247
270
            return self._refs
248
 
        self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None, 
249
 
            lambda x: None, lambda x: mutter("git: %s" % x))
 
271
        self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None,
 
272
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
250
273
        return self._refs
251
274
 
252
 
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
 
275
    def fetch_pack(self, determine_wants, graph_walker, pack_data,
253
276
                   progress=None):
254
277
        return self._transport.fetch_pack(determine_wants, graph_walker,
255
278
                                          pack_data, progress)
257
280
    def send_pack(self, get_changed_refs, generate_pack_contents):
258
281
        return self._transport.send_pack(get_changed_refs, generate_pack_contents)
259
282
 
260
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
283
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
284
                      progress=None):
261
285
        fd, path = tempfile.mkstemp(suffix=".pack")
262
 
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
 
286
        self.fetch_pack(determine_wants, graph_walker,
 
287
            lambda x: os.write(fd, x), progress)
263
288
        os.close(fd)
264
289
        if os.path.getsize(path) == 0:
265
290
            return EmptyObjectStoreIterator()
266
291
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
267
292
 
268
 
    def lookup_git_revid(self, bzr_revid):
 
293
    def lookup_bzr_revision_id(self, bzr_revid):
269
294
        # This won't work for any round-tripped bzr revisions, but it's a start..
270
295
        try:
271
296
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
280
305
        self.repository = branch.repository
281
306
 
282
307
    def get_tag_dict(self):
283
 
        return extract_tags(self.repository.get_refs(), self.branch.mapping)
 
308
        tags = {}
 
309
        for k, v in extract_tags(self.repository.get_refs()).iteritems():
 
310
            tags[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
311
        return tags
284
312
 
285
313
    def set_tag(self, name, revid):
286
314
        # FIXME: Not supported yet, should do a push of a new ref
291
319
 
292
320
    def __init__(self, bzrdir, repository, name, lockfiles):
293
321
        self._ref = None
294
 
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, 
 
322
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
295
323
                lockfiles)
296
324
 
297
325
    def revision_history(self):
300
328
    def last_revision(self):
301
329
        return self.mapping.revision_id_foreign_to_bzr(self.head)
302
330
 
 
331
    def _get_config(self):
 
332
        class EmptyConfig(object):
 
333
 
 
334
            def _get_configobj(self):
 
335
                return config.ConfigObj()
 
336
 
 
337
        return EmptyConfig()
 
338
 
303
339
    @property
304
340
    def head(self):
305
341
        if self._ref is not None:
306
342
            return self._ref
307
343
        heads = self.repository.get_refs()
308
 
        if not self.name in heads:
309
 
            raise NoSuchRef(name)
310
 
        self._ref = heads[self.name]
 
344
        if self.name in heads:
 
345
            self._ref = heads[self.name]
 
346
        elif ("refs/heads/" + self.name) in heads:
 
347
            self._ref = heads["refs/heads/" + self.name]
 
348
        else:
 
349
            raise NoSuchRef(self.name)
311
350
        return self._ref
312
351
 
313
352
    def _synchronize_history(self, destination, revision_id):
314
353
        """See Branch._synchronize_history()."""
315
354
        destination.generate_revision_history(self.last_revision())
316
 
 
 
355
 
317
356
    def get_push_location(self):
318
357
        return None
319
358