1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test the smart client."""
19
from __future__ import absolute_import
24
from ....controldir import ControlDir
25
from ....errors import (
31
from ....tests import (
33
TestCaseWithTransport,
35
from ....tests.features import ExecutableFeature
37
from ..mapping import default_mapping
38
from ..remote import (
41
RemoteGitBranchFormat,
44
from dulwich import porcelain
45
from dulwich.repo import Repo as GitRepo
48
class SplitUrlTests(TestCase):
50
def test_simple(self):
51
self.assertEquals(("foo", None, None, "/bar"),
52
split_git_url("git://foo/bar"))
55
self.assertEquals(("foo", 343, None, "/bar"),
56
split_git_url("git://foo:343/bar"))
58
def test_username(self):
59
self.assertEquals(("foo", None, "la", "/bar"),
60
split_git_url("git://la@foo/bar"))
62
def test_nopath(self):
63
self.assertEquals(("foo", None, None, "/"),
64
split_git_url("git://foo/"))
66
def test_slashpath(self):
67
self.assertEquals(("foo", None, None, "//bar"),
68
split_git_url("git://foo//bar"))
70
def test_homedir(self):
71
self.assertEquals(("foo", None, None, "~bar"),
72
split_git_url("git://foo/~bar"))
75
class ParseGitErrorTests(TestCase):
77
def test_unknown(self):
78
e = parse_git_error("url", "foo")
79
self.assertIsInstance(e, BzrError)
81
def test_notbrancherror(self):
82
e = parse_git_error("url", "\n Could not find Repository foo/bar")
83
self.assertIsInstance(e, NotBranchError)
86
class TestRemoteGitBranchFormat(TestCase):
89
super(TestRemoteGitBranchFormat, self).setUp()
90
self.format = RemoteGitBranchFormat()
92
def test_get_format_description(self):
93
self.assertEquals("Remote Git Branch", self.format.get_format_description())
95
def test_get_network_name(self):
96
self.assertEquals("git", self.format.network_name())
98
def test_supports_tags(self):
99
self.assertTrue(self.format.supports_tags())
102
class FetchFromRemoteTestBase(object):
104
_test_needs_features = [ExecutableFeature('git')]
109
TestCaseWithTransport.setUp(self)
110
self.remote_real = GitRepo.init('remote', mkdir=True)
111
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
112
self.permit_url(self.remote_url)
114
def test_sprout_simple(self):
115
self.remote_real.do_commit(
117
committer='committer <committer@example.com>',
118
author='author <author@example.com>')
120
remote = ControlDir.open(self.remote_url)
121
self.make_controldir('local', format=self._to_format)
122
local = remote.sprout('local')
124
default_mapping.revision_id_foreign_to_bzr(self.remote_real.head()),
125
local.open_branch().last_revision())
127
def test_sprout_with_tags(self):
128
c1 = self.remote_real.do_commit(
130
committer='committer <committer@example.com>',
131
author='author <author@example.com>')
132
c2 = self.remote_real.do_commit(
133
message='another commit',
134
committer='committer <committer@example.com>',
135
author='author <author@example.com>',
136
ref='refs/tags/another')
137
self.remote_real.refs['refs/tags/blah'] = self.remote_real.head()
139
remote = ControlDir.open(self.remote_url)
140
self.make_controldir('local', format=self._to_format)
141
local = remote.sprout('local')
142
local_branch = local.open_branch()
144
default_mapping.revision_id_foreign_to_bzr(c1),
145
local_branch.last_revision())
147
{'blah': local_branch.last_revision(),
148
'another': default_mapping.revision_id_foreign_to_bzr(c2)},
149
local_branch.tags.get_tag_dict())
151
def test_sprout_with_annotated_tag(self):
152
c1 = self.remote_real.do_commit(
154
committer='committer <committer@example.com>',
155
author='author <author@example.com>')
156
c2 = self.remote_real.do_commit(
157
message='another commit',
158
committer='committer <committer@example.com>',
159
author='author <author@example.com>',
160
ref='refs/heads/another')
161
porcelain.tag_create(
164
author='author <author@example.com>',
166
tag_time=int(time.time()),
169
message="Annotated tag")
171
remote = ControlDir.open(self.remote_url)
172
self.make_controldir('local', format=self._to_format)
173
local = remote.sprout('local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
174
local_branch = local.open_branch()
176
default_mapping.revision_id_foreign_to_bzr(c1),
177
local_branch.last_revision())
179
{'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
180
local_branch.tags.get_tag_dict())
183
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase,TestCaseWithTransport):
188
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase,TestCaseWithTransport):
193
class PushToRemoteBase(object):
195
_test_needs_features = [ExecutableFeature('git')]
200
TestCaseWithTransport.setUp(self)
201
self.remote_real = GitRepo.init('remote', mkdir=True)
202
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
203
self.permit_url(self.remote_url)
205
def test_push_branch_new(self):
206
remote = ControlDir.open(self.remote_url)
207
wt = self.make_branch_and_tree('local', format=self._from_format)
208
self.build_tree(['local/blah'])
210
revid = wt.commit('blah')
212
if self._from_format == 'git':
213
remote.push_branch(wt.branch, name='newbranch')
215
remote.push_branch(wt.branch, lossy=True, name='newbranch')
218
{'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
220
self.remote_real.get_refs())
223
c1 = self.remote_real.do_commit(
225
committer='committer <committer@example.com>',
226
author='author <author@example.com>')
228
remote = ControlDir.open(self.remote_url)
229
self.make_controldir('local', format=self._from_format)
230
local = remote.sprout('local')
231
self.build_tree(['local/blah'])
232
wt = local.open_workingtree()
234
revid = wt.commit('blah')
235
wt.branch.tags.set_tag('sometag', revid)
236
wt.branch.get_config_stack().set('branch.fetch_tags', True)
238
if self._from_format == 'git':
239
wt.branch.push(remote.create_branch('newbranch'))
241
wt.branch.push(remote.create_branch('newbranch'), lossy=True)
244
{'refs/heads/master': self.remote_real.head(),
245
'HEAD': self.remote_real.head(),
246
'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
247
'refs/tags/sometag': self.remote_real.refs['refs/heads/newbranch'],
249
self.remote_real.get_refs())
252
class PushToRemoteFromBzrTests(PushToRemoteBase,TestCaseWithTransport):
257
class PushToRemoteFromGitTests(PushToRemoteBase,TestCaseWithTransport):
262
class RemoteControlDirTests(TestCaseWithTransport):
264
_test_needs_features = [ExecutableFeature('git')]
267
TestCaseWithTransport.setUp(self)
268
self.remote_real = GitRepo.init('remote', mkdir=True)
269
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
270
self.permit_url(self.remote_url)
272
def test_remove_branch(self):
273
c1 = self.remote_real.do_commit(
275
committer='committer <committer@example.com>',
276
author='author <author@example.com>')
277
c2 = self.remote_real.do_commit(
278
message='another commit',
279
committer='committer <committer@example.com>',
280
author='author <author@example.com>',
281
ref='refs/heads/blah')
283
remote = ControlDir.open(self.remote_url)
284
remote.destroy_branch(name='blah')
286
self.remote_real.get_refs(),
287
{'refs/heads/master': self.remote_real.head(),
288
'HEAD': self.remote_real.head(),
291
def test_list_branches(self):
292
c1 = self.remote_real.do_commit(
294
committer='committer <committer@example.com>',
295
author='author <author@example.com>')
296
c2 = self.remote_real.do_commit(
297
message='another commit',
298
committer='committer <committer@example.com>',
299
author='author <author@example.com>',
300
ref='refs/heads/blah')
302
remote = ControlDir.open(self.remote_url)
304
['master', 'blah', 'master'],
305
[b.name for b in remote.list_branches()])
307
def test_get_branches(self):
308
c1 = self.remote_real.do_commit(
310
committer='committer <committer@example.com>',
311
author='author <author@example.com>')
312
c2 = self.remote_real.do_commit(
313
message='another commit',
314
committer='committer <committer@example.com>',
315
author='author <author@example.com>',
316
ref='refs/heads/blah')
318
remote = ControlDir.open(self.remote_url)
320
{'': 'master', 'blah': 'blah', 'master': 'master'},
321
{n: b.name for (n, b) in remote.get_branches().items()})
323
def test_remove_tag(self):
324
c1 = self.remote_real.do_commit(
326
committer='committer <committer@example.com>',
327
author='author <author@example.com>')
328
c2 = self.remote_real.do_commit(
329
message='another commit',
330
committer='committer <committer@example.com>',
331
author='author <author@example.com>',
332
ref='refs/tags/blah')
334
remote = ControlDir.open(self.remote_url)
335
remote_branch = remote.open_branch()
336
remote_branch.tags.delete_tag('blah')
337
self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
339
self.remote_real.get_refs(),
340
{'refs/heads/master': self.remote_real.head(),
341
'HEAD': self.remote_real.head(),
344
def test_set_tag(self):
345
c1 = self.remote_real.do_commit(
347
committer='committer <committer@example.com>',
348
author='author <author@example.com>')
349
c2 = self.remote_real.do_commit(
350
message='another commit',
351
committer='committer <committer@example.com>',
352
author='author <author@example.com>')
354
remote = ControlDir.open(self.remote_url)
355
remote.open_branch().tags.set_tag(
356
'blah', default_mapping.revision_id_foreign_to_bzr(c1))
358
self.remote_real.get_refs(),
359
{'refs/heads/master': self.remote_real.head(),
360
'refs/tags/blah': c1,
361
'HEAD': self.remote_real.head(),
364
def test_annotated_tag(self):
365
c1 = self.remote_real.do_commit(
367
committer='committer <committer@example.com>',
368
author='author <author@example.com>')
369
c2 = self.remote_real.do_commit(
370
message='another commit',
371
committer='committer <committer@example.com>',
372
author='author <author@example.com>')
374
porcelain.tag_create(
377
author='author <author@example.com>',
379
tag_time=int(time.time()),
382
message="Annotated tag")
384
remote = ControlDir.open(self.remote_url)
385
remote_branch = remote.open_branch()
387
'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
388
remote_branch.tags.get_tag_dict())
390
def tetst_get_branch_reference(self):
391
c1 = self.remote_real.do_commit(
393
committer='committer <committer@example.com>',
394
author='author <author@example.com>')
395
c2 = self.remote_real.do_commit(
396
message='another commit',
397
committer='committer <committer@example.com>',
398
author='author <author@example.com>')
400
remote = ControlDir.open(self.remote_url)
401
self.assertEqual('refs/heads/master', remote.get_branch_reference(''))
402
self.assertEqual(None, remote.get_branch_reference('master'))