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 (
32
from ....tests import (
34
TestCaseWithTransport,
36
from ....tests.features import ExecutableFeature
38
from ..mapping import default_mapping
39
from ..remote import (
42
RemoteGitBranchFormat,
45
from dulwich import porcelain
46
from dulwich.repo import Repo as GitRepo
49
class SplitUrlTests(TestCase):
51
def test_simple(self):
52
self.assertEquals(("foo", None, None, "/bar"),
53
split_git_url("git://foo/bar"))
56
self.assertEquals(("foo", 343, None, "/bar"),
57
split_git_url("git://foo:343/bar"))
59
def test_username(self):
60
self.assertEquals(("foo", None, "la", "/bar"),
61
split_git_url("git://la@foo/bar"))
63
def test_nopath(self):
64
self.assertEquals(("foo", None, None, "/"),
65
split_git_url("git://foo/"))
67
def test_slashpath(self):
68
self.assertEquals(("foo", None, None, "//bar"),
69
split_git_url("git://foo//bar"))
71
def test_homedir(self):
72
self.assertEquals(("foo", None, None, "~bar"),
73
split_git_url("git://foo/~bar"))
76
class ParseGitErrorTests(TestCase):
78
def test_unknown(self):
79
e = parse_git_error("url", "foo")
80
self.assertIsInstance(e, BzrError)
82
def test_notbrancherror(self):
83
e = parse_git_error("url", "\n Could not find Repository foo/bar")
84
self.assertIsInstance(e, NotBranchError)
87
class TestRemoteGitBranchFormat(TestCase):
90
super(TestRemoteGitBranchFormat, self).setUp()
91
self.format = RemoteGitBranchFormat()
93
def test_get_format_description(self):
94
self.assertEquals("Remote Git Branch", self.format.get_format_description())
96
def test_get_network_name(self):
97
self.assertEquals("git", self.format.network_name())
99
def test_supports_tags(self):
100
self.assertTrue(self.format.supports_tags())
103
class FetchFromRemoteTestBase(object):
105
_test_needs_features = [ExecutableFeature('git')]
110
TestCaseWithTransport.setUp(self)
111
self.remote_real = GitRepo.init('remote', mkdir=True)
112
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
113
self.permit_url(self.remote_url)
115
def test_sprout_simple(self):
116
self.remote_real.do_commit(
118
committer='committer <committer@example.com>',
119
author='author <author@example.com>')
121
remote = ControlDir.open(self.remote_url)
122
self.make_controldir('local', format=self._to_format)
123
local = remote.sprout('local')
125
default_mapping.revision_id_foreign_to_bzr(self.remote_real.head()),
126
local.open_branch().last_revision())
128
def test_sprout_with_tags(self):
129
c1 = self.remote_real.do_commit(
131
committer='committer <committer@example.com>',
132
author='author <author@example.com>')
133
c2 = self.remote_real.do_commit(
134
message='another commit',
135
committer='committer <committer@example.com>',
136
author='author <author@example.com>',
137
ref='refs/tags/another')
138
self.remote_real.refs['refs/tags/blah'] = self.remote_real.head()
140
remote = ControlDir.open(self.remote_url)
141
self.make_controldir('local', format=self._to_format)
142
local = remote.sprout('local')
143
local_branch = local.open_branch()
145
default_mapping.revision_id_foreign_to_bzr(c1),
146
local_branch.last_revision())
148
{'blah': local_branch.last_revision(),
149
'another': default_mapping.revision_id_foreign_to_bzr(c2)},
150
local_branch.tags.get_tag_dict())
152
def test_sprout_with_annotated_tag(self):
153
c1 = self.remote_real.do_commit(
155
committer='committer <committer@example.com>',
156
author='author <author@example.com>')
157
c2 = self.remote_real.do_commit(
158
message='another commit',
159
committer='committer <committer@example.com>',
160
author='author <author@example.com>',
161
ref='refs/heads/another')
162
porcelain.tag_create(
165
author='author <author@example.com>',
167
tag_time=int(time.time()),
170
message="Annotated tag")
172
remote = ControlDir.open(self.remote_url)
173
self.make_controldir('local', format=self._to_format)
174
local = remote.sprout('local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
175
local_branch = local.open_branch()
177
default_mapping.revision_id_foreign_to_bzr(c1),
178
local_branch.last_revision())
180
{'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
181
local_branch.tags.get_tag_dict())
184
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase,TestCaseWithTransport):
189
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase,TestCaseWithTransport):
194
class PushToRemoteBase(object):
196
_test_needs_features = [ExecutableFeature('git')]
201
TestCaseWithTransport.setUp(self)
202
self.remote_real = GitRepo.init('remote', mkdir=True)
203
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
204
self.permit_url(self.remote_url)
206
def test_push_branch_new(self):
207
remote = ControlDir.open(self.remote_url)
208
wt = self.make_branch_and_tree('local', format=self._from_format)
209
self.build_tree(['local/blah'])
211
revid = wt.commit('blah')
213
if self._from_format == 'git':
214
remote.push_branch(wt.branch, name='newbranch')
216
remote.push_branch(wt.branch, lossy=True, name='newbranch')
219
{'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
221
self.remote_real.get_refs())
224
c1 = self.remote_real.do_commit(
226
committer='committer <committer@example.com>',
227
author='author <author@example.com>')
229
remote = ControlDir.open(self.remote_url)
230
self.make_controldir('local', format=self._from_format)
231
local = remote.sprout('local')
232
self.build_tree(['local/blah'])
233
wt = local.open_workingtree()
235
revid = wt.commit('blah')
236
wt.branch.tags.set_tag('sometag', revid)
237
wt.branch.get_config_stack().set('branch.fetch_tags', True)
239
if self._from_format == 'git':
240
wt.branch.push(remote.create_branch('newbranch'))
242
wt.branch.push(remote.create_branch('newbranch'), lossy=True)
245
{'refs/heads/master': self.remote_real.head(),
246
'HEAD': self.remote_real.head(),
247
'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
248
'refs/tags/sometag': self.remote_real.refs['refs/heads/newbranch'],
250
self.remote_real.get_refs())
252
def test_push_diverged(self):
253
c1 = self.remote_real.do_commit(
255
committer='committer <committer@example.com>',
256
author='author <author@example.com>',
257
ref='refs/heads/newbranch')
259
remote = ControlDir.open(self.remote_url)
260
wt = self.make_branch_and_tree('local', format=self._from_format)
261
self.build_tree(['local/blah'])
263
revid = wt.commit('blah')
265
newbranch = remote.open_branch('newbranch')
266
if self._from_format == 'git':
267
self.assertRaises(DivergedBranches, wt.branch.push, newbranch)
269
self.assertRaises(DivergedBranches, wt.branch.push, newbranch, lossy=True)
272
{'refs/heads/newbranch': c1 },
273
self.remote_real.get_refs())
275
if self._from_format == 'git':
276
wt.branch.push(newbranch, overwrite=True)
278
wt.branch.push(newbranch, lossy=True, overwrite=True)
280
self.assertNotEqual(c1, self.remote_real.refs['refs/heads/newbranch'])
283
class PushToRemoteFromBzrTests(PushToRemoteBase,TestCaseWithTransport):
288
class PushToRemoteFromGitTests(PushToRemoteBase,TestCaseWithTransport):
293
class RemoteControlDirTests(TestCaseWithTransport):
295
_test_needs_features = [ExecutableFeature('git')]
298
TestCaseWithTransport.setUp(self)
299
self.remote_real = GitRepo.init('remote', mkdir=True)
300
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
301
self.permit_url(self.remote_url)
303
def test_remove_branch(self):
304
c1 = self.remote_real.do_commit(
306
committer='committer <committer@example.com>',
307
author='author <author@example.com>')
308
c2 = self.remote_real.do_commit(
309
message='another commit',
310
committer='committer <committer@example.com>',
311
author='author <author@example.com>',
312
ref='refs/heads/blah')
314
remote = ControlDir.open(self.remote_url)
315
remote.destroy_branch(name='blah')
317
self.remote_real.get_refs(),
318
{'refs/heads/master': self.remote_real.head(),
319
'HEAD': self.remote_real.head(),
322
def test_list_branches(self):
323
c1 = self.remote_real.do_commit(
325
committer='committer <committer@example.com>',
326
author='author <author@example.com>')
327
c2 = self.remote_real.do_commit(
328
message='another commit',
329
committer='committer <committer@example.com>',
330
author='author <author@example.com>',
331
ref='refs/heads/blah')
333
remote = ControlDir.open(self.remote_url)
335
['master', 'blah', 'master'],
336
[b.name for b in remote.list_branches()])
338
def test_get_branches(self):
339
c1 = self.remote_real.do_commit(
341
committer='committer <committer@example.com>',
342
author='author <author@example.com>')
343
c2 = self.remote_real.do_commit(
344
message='another commit',
345
committer='committer <committer@example.com>',
346
author='author <author@example.com>',
347
ref='refs/heads/blah')
349
remote = ControlDir.open(self.remote_url)
351
{'': 'master', 'blah': 'blah', 'master': 'master'},
352
{n: b.name for (n, b) in remote.get_branches().items()})
354
def test_remove_tag(self):
355
c1 = self.remote_real.do_commit(
357
committer='committer <committer@example.com>',
358
author='author <author@example.com>')
359
c2 = self.remote_real.do_commit(
360
message='another commit',
361
committer='committer <committer@example.com>',
362
author='author <author@example.com>',
363
ref='refs/tags/blah')
365
remote = ControlDir.open(self.remote_url)
366
remote_branch = remote.open_branch()
367
remote_branch.tags.delete_tag('blah')
368
self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
370
self.remote_real.get_refs(),
371
{'refs/heads/master': self.remote_real.head(),
372
'HEAD': self.remote_real.head(),
375
def test_set_tag(self):
376
c1 = self.remote_real.do_commit(
378
committer='committer <committer@example.com>',
379
author='author <author@example.com>')
380
c2 = self.remote_real.do_commit(
381
message='another commit',
382
committer='committer <committer@example.com>',
383
author='author <author@example.com>')
385
remote = ControlDir.open(self.remote_url)
386
remote.open_branch().tags.set_tag(
387
'blah', default_mapping.revision_id_foreign_to_bzr(c1))
389
self.remote_real.get_refs(),
390
{'refs/heads/master': self.remote_real.head(),
391
'refs/tags/blah': c1,
392
'HEAD': self.remote_real.head(),
395
def test_annotated_tag(self):
396
c1 = self.remote_real.do_commit(
398
committer='committer <committer@example.com>',
399
author='author <author@example.com>')
400
c2 = self.remote_real.do_commit(
401
message='another commit',
402
committer='committer <committer@example.com>',
403
author='author <author@example.com>')
405
porcelain.tag_create(
408
author='author <author@example.com>',
410
tag_time=int(time.time()),
413
message="Annotated tag")
415
remote = ControlDir.open(self.remote_url)
416
remote_branch = remote.open_branch()
418
'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
419
remote_branch.tags.get_tag_dict())
421
def tetst_get_branch_reference(self):
422
c1 = self.remote_real.do_commit(
424
committer='committer <committer@example.com>',
425
author='author <author@example.com>')
426
c2 = self.remote_real.do_commit(
427
message='another commit',
428
committer='committer <committer@example.com>',
429
author='author <author@example.com>')
431
remote = ControlDir.open(self.remote_url)
432
self.assertEqual('refs/heads/master', remote.get_branch_reference(''))
433
self.assertEqual(None, remote.get_branch_reference('master'))