/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 breezy/git/tests/test_remote.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 07:18:33 UTC
  • mfrom: (7141.3.3 fix1799482)
  • Revision ID: breezy.the.bot@gmail.com-20181116071833-e01b0833f3hkc3et
Report correct paths when running "brz add" in git repositories.

Merged from https://code.launchpad.net/~jelmer/brz/fix1799482/+merge/357734

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Test the smart client."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from io import BytesIO
 
22
 
 
23
import os
 
24
import time
 
25
 
 
26
from ...controldir import ControlDir
 
27
from ...errors import (
 
28
    BzrError,
 
29
    DivergedBranches,
 
30
    NotBranchError,
 
31
    NoSuchTag,
 
32
    PermissionDenied,
 
33
    )
 
34
 
 
35
from ...tests import (
 
36
    TestCase,
 
37
    TestCaseWithTransport,
 
38
    )
 
39
from ...tests.features import ExecutableFeature
 
40
 
 
41
from ..mapping import default_mapping
 
42
from ..remote import (
 
43
    split_git_url,
 
44
    parse_git_error,
 
45
    HeadUpdateFailed,
 
46
    RemoteGitError,
 
47
    RemoteGitBranchFormat,
 
48
    )
 
49
 
 
50
from dulwich import porcelain
 
51
from dulwich.repo import Repo as GitRepo
 
52
 
 
53
 
 
54
class SplitUrlTests(TestCase):
 
55
 
 
56
    def test_simple(self):
 
57
        self.assertEqual(("foo", None, None, "/bar"),
 
58
            split_git_url("git://foo/bar"))
 
59
 
 
60
    def test_port(self):
 
61
        self.assertEqual(("foo", 343, None, "/bar"),
 
62
            split_git_url("git://foo:343/bar"))
 
63
 
 
64
    def test_username(self):
 
65
        self.assertEqual(("foo", None, "la", "/bar"),
 
66
            split_git_url("git://la@foo/bar"))
 
67
 
 
68
    def test_nopath(self):
 
69
        self.assertEqual(("foo", None, None, "/"),
 
70
            split_git_url("git://foo/"))
 
71
 
 
72
    def test_slashpath(self):
 
73
        self.assertEqual(("foo", None, None, "//bar"),
 
74
            split_git_url("git://foo//bar"))
 
75
 
 
76
    def test_homedir(self):
 
77
        self.assertEqual(("foo", None, None, "~bar"),
 
78
            split_git_url("git://foo/~bar"))
 
79
 
 
80
 
 
81
class ParseGitErrorTests(TestCase):
 
82
 
 
83
    def test_unknown(self):
 
84
        e = parse_git_error("url", "foo")
 
85
        self.assertIsInstance(e, RemoteGitError)
 
86
 
 
87
    def test_notbrancherror(self):
 
88
        e = parse_git_error("url", "\n Could not find Repository foo/bar")
 
89
        self.assertIsInstance(e, NotBranchError)
 
90
 
 
91
    def test_notbrancherror_launchpad(self):
 
92
        e = parse_git_error("url", "Repository 'foo/bar' not found.")
 
93
        self.assertIsInstance(e, NotBranchError)
 
94
 
 
95
    def test_notbrancherror_github(self):
 
96
        e = parse_git_error("url", "Repository not found.\n")
 
97
        self.assertIsInstance(e, NotBranchError)
 
98
 
 
99
    def test_notbrancherror_normal(self):
 
100
        e = parse_git_error("url", "fatal: '/srv/git/lintian-brush' does not appear to be a git repository")
 
101
        self.assertIsInstance(e, NotBranchError)
 
102
 
 
103
    def test_head_update(self):
 
104
        e = parse_git_error("url", "HEAD failed to update\n")
 
105
        self.assertIsInstance(e, HeadUpdateFailed)
 
106
 
 
107
    def test_permission_dnied(self):
 
108
        e = parse_git_error(
 
109
            "url",
 
110
            "access denied or repository not exported: /debian/altermime.git")
 
111
        self.assertIsInstance(e, PermissionDenied)
 
112
 
 
113
    def test_permission_denied_gitlab(self):
 
114
        e = parse_git_error(
 
115
            "url",
 
116
            'GitLab: You are not allowed to push code to this project.\n')
 
117
        self.assertIsInstance(e, PermissionDenied)
 
118
 
 
119
    def test_permission_denied_github(self):
 
120
        e = parse_git_error(
 
121
            "url",
 
122
            'Permission to porridge/gaduhistory.git denied to jelmer.')
 
123
        self.assertIsInstance(e, PermissionDenied)
 
124
        self.assertEqual(e.path, 'porridge/gaduhistory.git')
 
125
        self.assertEqual(e.extra, ': denied to jelmer')
 
126
 
 
127
 
 
128
class TestRemoteGitBranchFormat(TestCase):
 
129
 
 
130
    def setUp(self):
 
131
        super(TestRemoteGitBranchFormat, self).setUp()
 
132
        self.format = RemoteGitBranchFormat()
 
133
 
 
134
    def test_get_format_description(self):
 
135
        self.assertEqual("Remote Git Branch", self.format.get_format_description())
 
136
 
 
137
    def test_get_network_name(self):
 
138
        self.assertEqual(b"git", self.format.network_name())
 
139
 
 
140
    def test_supports_tags(self):
 
141
        self.assertTrue(self.format.supports_tags())
 
142
 
 
143
 
 
144
class FetchFromRemoteTestBase(object):
 
145
 
 
146
    _test_needs_features = [ExecutableFeature('git')]
 
147
 
 
148
    _to_format = None
 
149
 
 
150
    def setUp(self):
 
151
        TestCaseWithTransport.setUp(self)
 
152
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
153
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
154
        self.permit_url(self.remote_url)
 
155
 
 
156
    def test_sprout_simple(self):
 
157
        self.remote_real.do_commit(
 
158
                message=b'message',
 
159
                committer=b'committer <committer@example.com>',
 
160
                author=b'author <author@example.com>')
 
161
 
 
162
        remote = ControlDir.open(self.remote_url)
 
163
        self.make_controldir('local', format=self._to_format)
 
164
        local = remote.sprout('local')
 
165
        self.assertEqual(
 
166
                default_mapping.revision_id_foreign_to_bzr(self.remote_real.head()),
 
167
                local.open_branch().last_revision())
 
168
 
 
169
    def test_sprout_with_tags(self):
 
170
        c1 = self.remote_real.do_commit(
 
171
                message=b'message',
 
172
                committer=b'committer <committer@example.com>',
 
173
                author=b'author <author@example.com>')
 
174
        c2 = self.remote_real.do_commit(
 
175
                message=b'another commit',
 
176
                committer=b'committer <committer@example.com>',
 
177
                author=b'author <author@example.com>',
 
178
                ref=b'refs/tags/another')
 
179
        self.remote_real.refs[b'refs/tags/blah'] = self.remote_real.head()
 
180
 
 
181
        remote = ControlDir.open(self.remote_url)
 
182
        self.make_controldir('local', format=self._to_format)
 
183
        local = remote.sprout('local')
 
184
        local_branch = local.open_branch()
 
185
        self.assertEqual(
 
186
                default_mapping.revision_id_foreign_to_bzr(c1),
 
187
                local_branch.last_revision())
 
188
        self.assertEqual(
 
189
                {'blah': local_branch.last_revision(),
 
190
                 'another': default_mapping.revision_id_foreign_to_bzr(c2)},
 
191
                local_branch.tags.get_tag_dict())
 
192
 
 
193
    def test_sprout_with_annotated_tag(self):
 
194
        c1 = self.remote_real.do_commit(
 
195
                message=b'message',
 
196
                committer=b'committer <committer@example.com>',
 
197
                author=b'author <author@example.com>')
 
198
        c2 = self.remote_real.do_commit(
 
199
                message=b'another commit',
 
200
                committer=b'committer <committer@example.com>',
 
201
                author=b'author <author@example.com>',
 
202
                ref=b'refs/heads/another')
 
203
        porcelain.tag_create(
 
204
                self.remote_real,
 
205
                tag=b"blah",
 
206
                author=b'author <author@example.com>',
 
207
                objectish=c2,
 
208
                tag_time=int(time.time()),
 
209
                tag_timezone=0,
 
210
                annotated=True,
 
211
                message=b"Annotated tag")
 
212
 
 
213
        remote = ControlDir.open(self.remote_url)
 
214
        self.make_controldir('local', format=self._to_format)
 
215
        local = remote.sprout('local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
 
216
        local_branch = local.open_branch()
 
217
        self.assertEqual(
 
218
                default_mapping.revision_id_foreign_to_bzr(c1),
 
219
                local_branch.last_revision())
 
220
        self.assertEqual(
 
221
                {'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
 
222
                local_branch.tags.get_tag_dict())
 
223
 
 
224
 
 
225
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase,TestCaseWithTransport):
 
226
 
 
227
    _to_format = '2a'
 
228
 
 
229
 
 
230
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase,TestCaseWithTransport):
 
231
 
 
232
    _to_format = 'git'
 
233
 
 
234
 
 
235
class PushToRemoteBase(object):
 
236
 
 
237
    _test_needs_features = [ExecutableFeature('git')]
 
238
 
 
239
    _from_format = None
 
240
 
 
241
    def setUp(self):
 
242
        TestCaseWithTransport.setUp(self)
 
243
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
244
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
245
        self.permit_url(self.remote_url)
 
246
 
 
247
    def test_push_branch_new(self):
 
248
        remote = ControlDir.open(self.remote_url)
 
249
        wt = self.make_branch_and_tree('local', format=self._from_format)
 
250
        self.build_tree(['local/blah'])
 
251
        wt.add(['blah'])
 
252
        revid = wt.commit('blah')
 
253
 
 
254
        if self._from_format == 'git':
 
255
            result = remote.push_branch(wt.branch, name='newbranch')
 
256
        else:
 
257
            result = remote.push_branch(wt.branch, lossy=True, name='newbranch')
 
258
 
 
259
        self.assertEqual(0, result.old_revno)
 
260
        if self._from_format == 'git':
 
261
            self.assertEqual(1, result.new_revno)
 
262
        else:
 
263
            self.assertIs(None, result.new_revno)
 
264
 
 
265
        result.report(BytesIO())
 
266
 
 
267
        self.assertEqual(
 
268
                {b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
 
269
                },
 
270
                self.remote_real.get_refs())
 
271
 
 
272
    def test_push(self):
 
273
        c1 = self.remote_real.do_commit(
 
274
                message=b'message',
 
275
                committer=b'committer <committer@example.com>',
 
276
                author=b'author <author@example.com>')
 
277
 
 
278
        remote = ControlDir.open(self.remote_url)
 
279
        self.make_controldir('local', format=self._from_format)
 
280
        local = remote.sprout('local')
 
281
        self.build_tree(['local/blah'])
 
282
        wt = local.open_workingtree()
 
283
        wt.add(['blah'])
 
284
        revid = wt.commit('blah')
 
285
        wt.branch.tags.set_tag('sometag', revid)
 
286
        wt.branch.get_config_stack().set('branch.fetch_tags', True)
 
287
 
 
288
        if self._from_format == 'git':
 
289
            result = wt.branch.push(remote.create_branch('newbranch'))
 
290
        else:
 
291
            result = wt.branch.push(remote.create_branch('newbranch'), lossy=True)
 
292
 
 
293
        self.assertEqual(0, result.old_revno)
 
294
        self.assertEqual(2, result.new_revno)
 
295
 
 
296
        result.report(BytesIO())
 
297
 
 
298
        self.assertEqual(
 
299
                {b'refs/heads/master': self.remote_real.head(),
 
300
                 b'HEAD': self.remote_real.head(),
 
301
                 b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
 
302
                 b'refs/tags/sometag': self.remote_real.refs[b'refs/heads/newbranch'],
 
303
                },
 
304
                self.remote_real.get_refs())
 
305
 
 
306
    def test_push_diverged(self):
 
307
        c1 = self.remote_real.do_commit(
 
308
                message=b'message',
 
309
                committer=b'committer <committer@example.com>',
 
310
                author=b'author <author@example.com>',
 
311
                ref=b'refs/heads/newbranch')
 
312
 
 
313
        remote = ControlDir.open(self.remote_url)
 
314
        wt = self.make_branch_and_tree('local', format=self._from_format)
 
315
        self.build_tree(['local/blah'])
 
316
        wt.add(['blah'])
 
317
        revid = wt.commit('blah')
 
318
 
 
319
        newbranch = remote.open_branch('newbranch')
 
320
        if self._from_format == 'git':
 
321
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch)
 
322
        else:
 
323
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch, lossy=True)
 
324
 
 
325
        self.assertEqual(
 
326
                {b'refs/heads/newbranch': c1 },
 
327
                self.remote_real.get_refs())
 
328
 
 
329
        if self._from_format == 'git':
 
330
            wt.branch.push(newbranch, overwrite=True)
 
331
        else:
 
332
            wt.branch.push(newbranch, lossy=True, overwrite=True)
 
333
 
 
334
        self.assertNotEqual(c1, self.remote_real.refs[b'refs/heads/newbranch'])
 
335
 
 
336
 
 
337
class PushToRemoteFromBzrTests(PushToRemoteBase,TestCaseWithTransport):
 
338
 
 
339
    _from_format = '2a'
 
340
 
 
341
 
 
342
class PushToRemoteFromGitTests(PushToRemoteBase,TestCaseWithTransport):
 
343
 
 
344
    _from_format = 'git'
 
345
 
 
346
 
 
347
class RemoteControlDirTests(TestCaseWithTransport):
 
348
 
 
349
    _test_needs_features = [ExecutableFeature('git')]
 
350
 
 
351
    def setUp(self):
 
352
        TestCaseWithTransport.setUp(self)
 
353
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
354
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
355
        self.permit_url(self.remote_url)
 
356
 
 
357
    def test_remove_branch(self):
 
358
        c1 = self.remote_real.do_commit(
 
359
                message=b'message',
 
360
                committer=b'committer <committer@example.com>',
 
361
                author=b'author <author@example.com>')
 
362
        c2 = self.remote_real.do_commit(
 
363
                message=b'another commit',
 
364
                committer=b'committer <committer@example.com>',
 
365
                author=b'author <author@example.com>',
 
366
                ref=b'refs/heads/blah')
 
367
 
 
368
        remote = ControlDir.open(self.remote_url)
 
369
        remote.destroy_branch(name='blah')
 
370
        self.assertEqual(
 
371
                self.remote_real.get_refs(),
 
372
                {b'refs/heads/master': self.remote_real.head(),
 
373
                 b'HEAD': self.remote_real.head(),
 
374
                })
 
375
 
 
376
    def test_list_branches(self):
 
377
        c1 = self.remote_real.do_commit(
 
378
                message=b'message',
 
379
                committer=b'committer <committer@example.com>',
 
380
                author=b'author <author@example.com>')
 
381
        c2 = self.remote_real.do_commit(
 
382
                message=b'another commit',
 
383
                committer=b'committer <committer@example.com>',
 
384
                author=b'author <author@example.com>',
 
385
                ref=b'refs/heads/blah')
 
386
 
 
387
        remote = ControlDir.open(self.remote_url)
 
388
        self.assertEqual(
 
389
                set(['master', 'blah', 'master']),
 
390
                set([b.name for b in remote.list_branches()]))
 
391
 
 
392
    def test_get_branches(self):
 
393
        c1 = self.remote_real.do_commit(
 
394
                message=b'message',
 
395
                committer=b'committer <committer@example.com>',
 
396
                author=b'author <author@example.com>')
 
397
        c2 = self.remote_real.do_commit(
 
398
                message=b'another commit',
 
399
                committer=b'committer <committer@example.com>',
 
400
                author=b'author <author@example.com>',
 
401
                ref=b'refs/heads/blah')
 
402
 
 
403
        remote = ControlDir.open(self.remote_url)
 
404
        self.assertEqual(
 
405
                {'': 'master', 'blah': 'blah', 'master': 'master'},
 
406
                {n: b.name for (n, b) in remote.get_branches().items()})
 
407
 
 
408
    def test_remove_tag(self):
 
409
        c1 = self.remote_real.do_commit(
 
410
                message=b'message',
 
411
                committer=b'committer <committer@example.com>',
 
412
                author=b'author <author@example.com>')
 
413
        c2 = self.remote_real.do_commit(
 
414
                message=b'another commit',
 
415
                committer=b'committer <committer@example.com>',
 
416
                author=b'author <author@example.com>',
 
417
                ref=b'refs/tags/blah')
 
418
 
 
419
        remote = ControlDir.open(self.remote_url)
 
420
        remote_branch = remote.open_branch()
 
421
        remote_branch.tags.delete_tag('blah')
 
422
        self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
 
423
        self.assertEqual(
 
424
                self.remote_real.get_refs(),
 
425
                {b'refs/heads/master': self.remote_real.head(),
 
426
                 b'HEAD': self.remote_real.head(),
 
427
                })
 
428
 
 
429
    def test_set_tag(self):
 
430
        c1 = self.remote_real.do_commit(
 
431
                message=b'message',
 
432
                committer=b'committer <committer@example.com>',
 
433
                author=b'author <author@example.com>')
 
434
        c2 = self.remote_real.do_commit(
 
435
                message=b'another commit',
 
436
                committer=b'committer <committer@example.com>',
 
437
                author=b'author <author@example.com>')
 
438
 
 
439
        remote = ControlDir.open(self.remote_url)
 
440
        remote.open_branch().tags.set_tag(
 
441
            b'blah', default_mapping.revision_id_foreign_to_bzr(c1))
 
442
        self.assertEqual(
 
443
                self.remote_real.get_refs(),
 
444
                {b'refs/heads/master': self.remote_real.head(),
 
445
                 b'refs/tags/blah': c1,
 
446
                 b'HEAD': self.remote_real.head(),
 
447
                })
 
448
 
 
449
    def test_annotated_tag(self):
 
450
        c1 = self.remote_real.do_commit(
 
451
                message=b'message',
 
452
                committer=b'committer <committer@example.com>',
 
453
                author=b'author <author@example.com>')
 
454
        c2 = self.remote_real.do_commit(
 
455
                message=b'another commit',
 
456
                committer=b'committer <committer@example.com>',
 
457
                author=b'author <author@example.com>')
 
458
 
 
459
        porcelain.tag_create(
 
460
                self.remote_real,
 
461
                tag=b"blah",
 
462
                author=b'author <author@example.com>',
 
463
                objectish=c2,
 
464
                tag_time=int(time.time()),
 
465
                tag_timezone=0,
 
466
                annotated=True,
 
467
                message=b"Annotated tag")
 
468
 
 
469
        remote = ControlDir.open(self.remote_url)
 
470
        remote_branch = remote.open_branch()
 
471
        self.assertEqual({
 
472
            'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
 
473
            remote_branch.tags.get_tag_dict())
 
474
 
 
475
    def tetst_get_branch_reference(self):
 
476
        c1 = self.remote_real.do_commit(
 
477
                message=b'message',
 
478
                committer=b'committer <committer@example.com>',
 
479
                author=b'author <author@example.com>')
 
480
        c2 = self.remote_real.do_commit(
 
481
                message=b'another commit',
 
482
                committer=b'committer <committer@example.com>',
 
483
                author=b'author <author@example.com>')
 
484
 
 
485
        remote = ControlDir.open(self.remote_url)
 
486
        self.assertEqual(b'refs/heads/master', remote.get_branch_reference(''))
 
487
        self.assertEqual(None, remote.get_branch_reference('master'))