/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
16
17
"""Test the smart client."""
18
0.358.3 by Jelmer Vernooij
Enable absolute import.
19
from __future__ import absolute_import
20
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
21
from io import BytesIO
0.406.3 by Jelmer Vernooij
Add extra tests.
22
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
23
import os
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
24
import time
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
25
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
26
from ...controldir import ControlDir
27
from ...errors import (
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
28
    DivergedBranches,
0.200.1275 by Jelmer Vernooij
recognize missing repositories
29
    NotBranchError,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
30
    NoSuchTag,
7103.1.2 by Jelmer Vernooij
Handle PermissionDenied.
31
    PermissionDenied,
0.200.1275 by Jelmer Vernooij
recognize missing repositories
32
    )
33
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
34
from ...tests import (
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
35
    TestCase,
36
    TestCaseWithTransport,
37
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
38
from ...tests.features import ExecutableFeature
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
39
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
40
from ..mapping import default_mapping
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
41
from ..remote import (
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
42
    split_git_url,
0.200.1275 by Jelmer Vernooij
recognize missing repositories
43
    parse_git_error,
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
44
    parse_git_hangup,
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
45
    HeadUpdateFailed,
46
    RemoteGitError,
0.295.1 by Jelmer Vernooij
Split up branch formats.
47
    RemoteGitBranchFormat,
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
48
    _git_url_and_path_from_transport,
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
49
    )
50
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
51
from dulwich import porcelain
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
52
from dulwich.errors import HangupException
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
53
from dulwich.repo import Repo as GitRepo
54
55
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
56
class SplitUrlTests(TestCase):
57
58
    def test_simple(self):
6964.2.3 by Jelmer Vernooij
Review comments.
59
        self.assertEqual(("foo", None, None, "/bar"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
                         split_git_url("git://foo/bar"))
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
61
62
    def test_port(self):
6964.2.3 by Jelmer Vernooij
Review comments.
63
        self.assertEqual(("foo", 343, None, "/bar"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
64
                         split_git_url("git://foo:343/bar"))
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
65
66
    def test_username(self):
6964.2.3 by Jelmer Vernooij
Review comments.
67
        self.assertEqual(("foo", None, "la", "/bar"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
68
                         split_git_url("git://la@foo/bar"))
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
69
7290.38.1 by Jelmer Vernooij
Backport python3.8 support patch to breezy 3.0.
70
    def test_username_password(self):
71
        self.assertEqual(
72
            ("foo", None, "la", "/bar"),
73
            split_git_url("git://la:passwd@foo/bar"))
74
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
75
    def test_nopath(self):
6964.2.3 by Jelmer Vernooij
Review comments.
76
        self.assertEqual(("foo", None, None, "/"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
77
                         split_git_url("git://foo/"))
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
78
79
    def test_slashpath(self):
6964.2.3 by Jelmer Vernooij
Review comments.
80
        self.assertEqual(("foo", None, None, "//bar"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
81
                         split_git_url("git://foo//bar"))
0.246.2 by Jelmer Vernooij
Improve the fix dealing with git repo's in home directories.
82
83
    def test_homedir(self):
6964.2.3 by Jelmer Vernooij
Review comments.
84
        self.assertEqual(("foo", None, None, "~bar"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
85
                         split_git_url("git://foo/~bar"))
0.200.1275 by Jelmer Vernooij
recognize missing repositories
86
7290.38.1 by Jelmer Vernooij
Backport python3.8 support patch to breezy 3.0.
87
    def test_file(self):
7290.38.2 by Jelmer Vernooij
More fixes.
88
        self.assertEqual(
89
            ("", None, None, "/bar"),
90
            split_git_url("file:///bar"))
7290.38.1 by Jelmer Vernooij
Backport python3.8 support patch to breezy 3.0.
91
0.200.1275 by Jelmer Vernooij
recognize missing repositories
92
93
class ParseGitErrorTests(TestCase):
94
95
    def test_unknown(self):
96
        e = parse_git_error("url", "foo")
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
97
        self.assertIsInstance(e, RemoteGitError)
0.200.1275 by Jelmer Vernooij
recognize missing repositories
98
99
    def test_notbrancherror(self):
100
        e = parse_git_error("url", "\n Could not find Repository foo/bar")
101
        self.assertIsInstance(e, NotBranchError)
0.295.1 by Jelmer Vernooij
Split up branch formats.
102
7104.2.1 by Jelmer Vernooij
Handle another way of formatting Git "repository not found" errors.
103
    def test_notbrancherror_launchpad(self):
104
        e = parse_git_error("url", "Repository 'foo/bar' not found.")
105
        self.assertIsInstance(e, NotBranchError)
106
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
107
    def test_notbrancherror_github(self):
108
        e = parse_git_error("url", "Repository not found.\n")
109
        self.assertIsInstance(e, NotBranchError)
110
7131.7.3 by Jelmer Vernooij
Handle one more error.
111
    def test_notbrancherror_normal(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
112
        e = parse_git_error(
113
            "url", "fatal: '/srv/git/lintian-brush' does not appear to be a git repository")
7131.7.3 by Jelmer Vernooij
Handle one more error.
114
        self.assertIsInstance(e, NotBranchError)
115
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
116
    def test_head_update(self):
117
        e = parse_git_error("url", "HEAD failed to update\n")
118
        self.assertIsInstance(e, HeadUpdateFailed)
119
7103.1.2 by Jelmer Vernooij
Handle PermissionDenied.
120
    def test_permission_dnied(self):
121
        e = parse_git_error(
122
            "url",
123
            "access denied or repository not exported: /debian/altermime.git")
124
        self.assertIsInstance(e, PermissionDenied)
125
7131.7.1 by Jelmer Vernooij
Handle permission denied by GitLab.
126
    def test_permission_denied_gitlab(self):
127
        e = parse_git_error(
128
            "url",
129
            'GitLab: You are not allowed to push code to this project.\n')
130
        self.assertIsInstance(e, PermissionDenied)
131
7131.7.2 by Jelmer Vernooij
Handle github PermissionDenied.
132
    def test_permission_denied_github(self):
133
        e = parse_git_error(
134
            "url",
135
            'Permission to porridge/gaduhistory.git denied to jelmer.')
136
        self.assertIsInstance(e, PermissionDenied)
137
        self.assertEqual(e.path, 'porridge/gaduhistory.git')
138
        self.assertEqual(e.extra, ': denied to jelmer')
139
7379.1.1 by Jelmer Vernooij
Handle invalid repository name on GitHub.
140
    def test_invalid_repo_name(self):
141
        e = parse_git_error(
142
            "url",
143
            """Gregwar/fatcat/tree/debian is not a valid repository name
144
Email support@github.com for help
145
""")
146
        self.assertIsInstance(e, NotBranchError)
147
0.295.1 by Jelmer Vernooij
Split up branch formats.
148
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
149
class ParseHangupTests(TestCase):
150
151
    def setUp(self):
152
        super(ParseHangupTests, self).setUp()
153
        try:
7490.49.3 by Jelmer Vernooij
Fix tests with newer dulwich.
154
            HangupException([b'foo'])
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
155
        except TypeError:
156
            self.skipTest('dulwich version too old')
157
158
    def test_not_set(self):
159
        self.assertIsInstance(
160
            parse_git_hangup('http://', HangupException()), HangupException)
161
162
    def test_single_line(self):
163
        self.assertEqual(
164
            RemoteGitError('foo bar'),
7490.49.3 by Jelmer Vernooij
Fix tests with newer dulwich.
165
            parse_git_hangup('http://', HangupException([b'foo bar'])))
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
166
167
    def test_multi_lines(self):
168
        self.assertEqual(
169
            RemoteGitError('foo bar\nbla bla'),
170
            parse_git_hangup(
7490.49.3 by Jelmer Vernooij
Fix tests with newer dulwich.
171
                'http://', HangupException([b'foo bar', b'bla bla'])))
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
172
173
    def test_filter_boring(self):
174
        self.assertEqual(
175
            RemoteGitError('foo bar'), parse_git_hangup('http://', HangupException(
7490.49.3 by Jelmer Vernooij
Fix tests with newer dulwich.
176
                [b'=======', b'foo bar', b'======'])))
7490.50.2 by Jelmer Vernooij
Strip 'remote: ' prefixes.
177
        self.assertEqual(
178
            RemoteGitError('foo bar'), parse_git_hangup('http://', HangupException(
179
                [b'remote: =======', b'remote: foo bar', b'remote: ======'])))
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
180
181
    def test_permission_denied(self):
182
        self.assertEqual(
183
            PermissionDenied('http://', 'You are not allowed to push code to this project.'),
184
            parse_git_hangup(
185
                'http://',
186
                HangupException(
7490.49.3 by Jelmer Vernooij
Fix tests with newer dulwich.
187
                    [b'=======',
188
                     b'You are not allowed to push code to this project.', b'', b'======'])))
7490.29.12 by Jelmer Vernooij
Parse stderr from remote git servers when they hang up.
189
190
0.295.1 by Jelmer Vernooij
Split up branch formats.
191
class TestRemoteGitBranchFormat(TestCase):
192
193
    def setUp(self):
194
        super(TestRemoteGitBranchFormat, self).setUp()
195
        self.format = RemoteGitBranchFormat()
196
197
    def test_get_format_description(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
198
        self.assertEqual("Remote Git Branch",
199
                         self.format.get_format_description())
0.295.1 by Jelmer Vernooij
Split up branch formats.
200
201
    def test_get_network_name(self):
6973.13.2 by Jelmer Vernooij
Fix some more tests.
202
        self.assertEqual(b"git", self.format.network_name())
0.295.1 by Jelmer Vernooij
Split up branch formats.
203
204
    def test_supports_tags(self):
205
        self.assertTrue(self.format.supports_tags())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
206
207
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
208
class TestRemoteGitBranch(TestCaseWithTransport):
209
7183.1.1 by Jelmer Vernooij
Add missing feature for git test that requires git executable.
210
    _test_needs_features = [ExecutableFeature('git')]
211
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
212
    def setUp(self):
213
        TestCaseWithTransport.setUp(self)
214
        self.remote_real = GitRepo.init('remote', mkdir=True)
215
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
216
        self.permit_url(self.remote_url)
217
218
    def test_set_last_revision_info(self):
219
        c1 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
220
            message=b'message 1',
221
            committer=b'committer <committer@example.com>',
222
            author=b'author <author@example.com>',
223
            ref=b'refs/heads/newbranch')
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
224
        c2 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
225
            message=b'message 2',
226
            committer=b'committer <committer@example.com>',
227
            author=b'author <author@example.com>',
228
            ref=b'refs/heads/newbranch')
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
229
230
        remote = ControlDir.open(self.remote_url)
231
        newbranch = remote.open_branch('newbranch')
232
        self.assertEqual(newbranch.lookup_foreign_revision_id(c2),
233
                         newbranch.last_revision())
234
        newbranch.set_last_revision_info(
235
            1, newbranch.lookup_foreign_revision_id(c1))
7131.12.2 by Jelmer Vernooij
Fix tests on python 3.
236
        self.assertEqual(c1, self.remote_real.refs[b'refs/heads/newbranch'])
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
237
        self.assertEqual(newbranch.last_revision(),
238
                         newbranch.lookup_foreign_revision_id(c1))
239
240
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
241
class FetchFromRemoteTestBase(object):
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
242
243
    _test_needs_features = [ExecutableFeature('git')]
244
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
245
    _to_format = None
246
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
247
    def setUp(self):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
248
        TestCaseWithTransport.setUp(self)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
249
        self.remote_real = GitRepo.init('remote', mkdir=True)
250
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
251
        self.permit_url(self.remote_url)
252
253
    def test_sprout_simple(self):
254
        self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
255
            message=b'message',
256
            committer=b'committer <committer@example.com>',
257
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
258
259
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
260
        self.make_controldir('local', format=self._to_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
261
        local = remote.sprout('local')
262
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
263
            default_mapping.revision_id_foreign_to_bzr(
264
                self.remote_real.head()),
265
            local.open_branch().last_revision())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
266
267
    def test_sprout_with_tags(self):
268
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
269
            message=b'message',
270
            committer=b'committer <committer@example.com>',
271
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
272
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
273
            message=b'another commit',
274
            committer=b'committer <committer@example.com>',
275
            author=b'author <author@example.com>',
276
            ref=b'refs/tags/another')
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
277
        self.remote_real.refs[b'refs/tags/blah'] = self.remote_real.head()
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
278
279
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
280
        self.make_controldir('local', format=self._to_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
281
        local = remote.sprout('local')
282
        local_branch = local.open_branch()
283
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
284
            default_mapping.revision_id_foreign_to_bzr(c1),
285
            local_branch.last_revision())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
286
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
287
            {'blah': local_branch.last_revision(),
288
             'another': default_mapping.revision_id_foreign_to_bzr(c2)},
289
            local_branch.tags.get_tag_dict())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
290
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
291
    def test_sprout_with_annotated_tag(self):
292
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
293
            message=b'message',
294
            committer=b'committer <committer@example.com>',
295
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
296
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
297
            message=b'another commit',
298
            committer=b'committer <committer@example.com>',
299
            author=b'author <author@example.com>',
300
            ref=b'refs/heads/another')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
301
        porcelain.tag_create(
7143.15.2 by Jelmer Vernooij
Run autopep8.
302
            self.remote_real,
303
            tag=b"blah",
304
            author=b'author <author@example.com>',
305
            objectish=c2,
306
            tag_time=int(time.time()),
307
            tag_timezone=0,
308
            annotated=True,
309
            message=b"Annotated tag")
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
310
311
        remote = ControlDir.open(self.remote_url)
312
        self.make_controldir('local', format=self._to_format)
7143.15.2 by Jelmer Vernooij
Run autopep8.
313
        local = remote.sprout(
314
            'local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
315
        local_branch = local.open_branch()
316
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
317
            default_mapping.revision_id_foreign_to_bzr(c1),
318
            local_branch.last_revision())
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
319
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
320
            {'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
321
            local_branch.tags.get_tag_dict())
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
322
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
323
    def test_sprout_with_annotated_tag_unreferenced(self):
324
        c1 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
325
            message=b'message',
326
            committer=b'committer <committer@example.com>',
327
            author=b'author <author@example.com>')
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
328
        c2 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
329
            message=b'another commit',
330
            committer=b'committer <committer@example.com>',
331
            author=b'author <author@example.com>')
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
332
        porcelain.tag_create(
7143.16.8 by Jelmer Vernooij
Fix E126
333
            self.remote_real,
334
            tag=b"blah",
335
            author=b'author <author@example.com>',
336
            objectish=c1,
337
            tag_time=int(time.time()),
338
            tag_timezone=0,
339
            annotated=True,
340
            message=b"Annotated tag")
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
341
342
        remote = ControlDir.open(self.remote_url)
343
        self.make_controldir('local', format=self._to_format)
344
        local = remote.sprout(
7143.16.8 by Jelmer Vernooij
Fix E126
345
            'local',
346
            revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
347
        local_branch = local.open_branch()
348
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
349
            default_mapping.revision_id_foreign_to_bzr(c1),
350
            local_branch.last_revision())
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
351
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
352
            {'blah': default_mapping.revision_id_foreign_to_bzr(c1)},
353
            local_branch.tags.get_tag_dict())
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
354
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
355
7143.16.21 by Jelmer Vernooij
Fix regressions.
356
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
357
358
    _to_format = '2a'
359
360
7143.15.2 by Jelmer Vernooij
Run autopep8.
361
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
362
363
    _to_format = 'git'
364
365
366
class PushToRemoteBase(object):
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
367
368
    _test_needs_features = [ExecutableFeature('git')]
369
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
370
    _from_format = None
371
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
372
    def setUp(self):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
373
        TestCaseWithTransport.setUp(self)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
374
        self.remote_real = GitRepo.init('remote', mkdir=True)
375
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
376
        self.permit_url(self.remote_url)
377
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
378
    def test_push_branch_new(self):
379
        remote = ControlDir.open(self.remote_url)
380
        wt = self.make_branch_and_tree('local', format=self._from_format)
381
        self.build_tree(['local/blah'])
382
        wt.add(['blah'])
383
        revid = wt.commit('blah')
384
385
        if self._from_format == 'git':
0.406.2 by Jelmer Vernooij
Add tests.
386
            result = remote.push_branch(wt.branch, name='newbranch')
387
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
388
            result = remote.push_branch(
389
                wt.branch, lossy=True, name='newbranch')
0.406.2 by Jelmer Vernooij
Add tests.
390
391
        self.assertEqual(0, result.old_revno)
392
        if self._from_format == 'git':
393
            self.assertEqual(1, result.new_revno)
394
        else:
395
            self.assertIs(None, result.new_revno)
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
396
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
397
        result.report(BytesIO())
0.406.3 by Jelmer Vernooij
Add extra tests.
398
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
399
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
400
            {b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
401
             },
402
            self.remote_real.get_refs())
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
403
7484.1.2 by Jelmer Vernooij
Add some tests.
404
    def test_push_branch_symref(self):
405
        cfg = self.remote_real.get_config()
406
        cfg.set((b'core', ), b'bare', True)
407
        cfg.write_to_path()
408
        self.remote_real.refs.set_symbolic_ref(b'HEAD', b'refs/heads/master')
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
            ref=b'refs/heads/master')
414
        remote = ControlDir.open(self.remote_url)
415
        wt = self.make_branch_and_tree('local', format=self._from_format)
416
        self.build_tree(['local/blah'])
417
        wt.add(['blah'])
418
        revid = wt.commit('blah')
419
420
        if self._from_format == 'git':
421
            result = remote.push_branch(wt.branch, overwrite=True)
422
        else:
423
            result = remote.push_branch(wt.branch, lossy=True, overwrite=True)
424
425
        self.assertEqual(None, result.old_revno)
426
        if self._from_format == 'git':
427
            self.assertEqual(1, result.new_revno)
428
        else:
429
            self.assertIs(None, result.new_revno)
430
431
        result.report(BytesIO())
432
433
        self.assertEqual(
7484.1.3 by Jelmer Vernooij
Fix formatting.
434
            {
435
                b'HEAD': self.remote_real.refs[b'refs/heads/master'],
436
                b'refs/heads/master': self.remote_real.refs[b'refs/heads/master'],
7484.1.2 by Jelmer Vernooij
Add some tests.
437
            },
438
            self.remote_real.get_refs())
439
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
440
    def test_push_branch_new_with_tags(self):
441
        remote = ControlDir.open(self.remote_url)
442
        builder = self.make_branch_builder('local', format=self._from_format)
443
        builder.start_series()
444
        rev_1 = builder.build_snapshot(None, [
445
            ('add', ('', None, 'directory', '')),
446
            ('add', ('filename', None, 'file', b'content'))])
447
        rev_2 = builder.build_snapshot(
448
            [rev_1], [('modify', ('filename', b'new-content\n'))])
449
        rev_3 = builder.build_snapshot(
450
            [rev_1], [('modify', ('filename', b'new-new-content\n'))])
451
        builder.finish_series()
452
        branch = builder.get_branch()
453
        try:
454
            branch.tags.set_tag('atag', rev_2)
455
        except TagsNotSupported:
456
            raise TestNotApplicable('source format does not support tags')
457
458
        branch.get_config_stack().set('branch.fetch_tags', True)
459
        if self._from_format == 'git':
460
            result = remote.push_branch(branch, name='newbranch')
461
        else:
462
            result = remote.push_branch(
463
                branch, lossy=True, name='newbranch')
464
465
        self.assertEqual(0, result.old_revno)
466
        if self._from_format == 'git':
467
            self.assertEqual(2, result.new_revno)
468
        else:
469
            self.assertIs(None, result.new_revno)
470
471
        result.report(BytesIO())
472
473
        self.assertEqual(
474
            {b'refs/heads/newbranch', b'refs/tags/atag'},
475
            set(self.remote_real.get_refs().keys()))
476
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
477
    def test_push(self):
478
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
479
            message=b'message',
480
            committer=b'committer <committer@example.com>',
481
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
482
483
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
484
        self.make_controldir('local', format=self._from_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
485
        local = remote.sprout('local')
486
        self.build_tree(['local/blah'])
487
        wt = local.open_workingtree()
488
        wt.add(['blah'])
489
        revid = wt.commit('blah')
490
        wt.branch.tags.set_tag('sometag', revid)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
491
        wt.branch.get_config_stack().set('branch.fetch_tags', True)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
492
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
493
        if self._from_format == 'git':
0.406.2 by Jelmer Vernooij
Add tests.
494
            result = wt.branch.push(remote.create_branch('newbranch'))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
495
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
496
            result = wt.branch.push(
497
                remote.create_branch('newbranch'), lossy=True)
0.406.2 by Jelmer Vernooij
Add tests.
498
499
        self.assertEqual(0, result.old_revno)
500
        self.assertEqual(2, result.new_revno)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
501
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
502
        result.report(BytesIO())
0.406.3 by Jelmer Vernooij
Add extra tests.
503
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
504
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
505
            {b'refs/heads/master': self.remote_real.head(),
506
             b'HEAD': self.remote_real.head(),
507
             b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
508
             b'refs/tags/sometag': self.remote_real.refs[b'refs/heads/newbranch'],
509
             },
510
            self.remote_real.get_refs())
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
511
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
512
    def test_push_diverged(self):
513
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
514
            message=b'message',
515
            committer=b'committer <committer@example.com>',
516
            author=b'author <author@example.com>',
517
            ref=b'refs/heads/newbranch')
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
518
519
        remote = ControlDir.open(self.remote_url)
520
        wt = self.make_branch_and_tree('local', format=self._from_format)
521
        self.build_tree(['local/blah'])
522
        wt.add(['blah'])
523
        revid = wt.commit('blah')
524
525
        newbranch = remote.open_branch('newbranch')
526
        if self._from_format == 'git':
527
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch)
528
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
529
            self.assertRaises(DivergedBranches, wt.branch.push,
530
                              newbranch, lossy=True)
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
531
532
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
533
            {b'refs/heads/newbranch': c1},
534
            self.remote_real.get_refs())
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
535
536
        if self._from_format == 'git':
537
            wt.branch.push(newbranch, overwrite=True)
538
        else:
539
            wt.branch.push(newbranch, lossy=True, overwrite=True)
540
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
541
        self.assertNotEqual(c1, self.remote_real.refs[b'refs/heads/newbranch'])
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
542
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
543
7143.15.2 by Jelmer Vernooij
Run autopep8.
544
class PushToRemoteFromBzrTests(PushToRemoteBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
545
546
    _from_format = '2a'
547
548
7143.15.2 by Jelmer Vernooij
Run autopep8.
549
class PushToRemoteFromGitTests(PushToRemoteBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
550
551
    _from_format = 'git'
552
553
554
class RemoteControlDirTests(TestCaseWithTransport):
555
556
    _test_needs_features = [ExecutableFeature('git')]
557
558
    def setUp(self):
559
        TestCaseWithTransport.setUp(self)
560
        self.remote_real = GitRepo.init('remote', mkdir=True)
561
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
562
        self.permit_url(self.remote_url)
563
564
    def test_remove_branch(self):
565
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
566
            message=b'message',
567
            committer=b'committer <committer@example.com>',
568
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
569
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
570
            message=b'another commit',
571
            committer=b'committer <committer@example.com>',
572
            author=b'author <author@example.com>',
573
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
574
575
        remote = ControlDir.open(self.remote_url)
576
        remote.destroy_branch(name='blah')
577
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
578
            self.remote_real.get_refs(),
579
            {b'refs/heads/master': self.remote_real.head(),
580
             b'HEAD': self.remote_real.head(),
581
             })
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
582
583
    def test_list_branches(self):
584
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
585
            message=b'message',
586
            committer=b'committer <committer@example.com>',
587
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
588
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
589
            message=b'another commit',
590
            committer=b'committer <committer@example.com>',
591
            author=b'author <author@example.com>',
592
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
593
594
        remote = ControlDir.open(self.remote_url)
595
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
596
            set(['master', 'blah', 'master']),
597
            set([b.name for b in remote.list_branches()]))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
598
599
    def test_get_branches(self):
600
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
601
            message=b'message',
602
            committer=b'committer <committer@example.com>',
603
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
604
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
605
            message=b'another commit',
606
            committer=b'committer <committer@example.com>',
607
            author=b'author <author@example.com>',
608
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
609
610
        remote = ControlDir.open(self.remote_url)
611
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
612
            {'': 'master', 'blah': 'blah', 'master': 'master'},
613
            {n: b.name for (n, b) in remote.get_branches().items()})
7490.13.6 by Jelmer Vernooij
Fix tests.
614
        self.assertEqual(
615
            set(['', 'blah', 'master']), set(remote.branch_names()))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
616
617
    def test_remove_tag(self):
618
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
619
            message=b'message',
620
            committer=b'committer <committer@example.com>',
621
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
622
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
623
            message=b'another commit',
624
            committer=b'committer <committer@example.com>',
625
            author=b'author <author@example.com>',
626
            ref=b'refs/tags/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
627
628
        remote = ControlDir.open(self.remote_url)
629
        remote_branch = remote.open_branch()
630
        remote_branch.tags.delete_tag('blah')
631
        self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
632
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
633
            self.remote_real.get_refs(),
634
            {b'refs/heads/master': self.remote_real.head(),
635
             b'HEAD': self.remote_real.head(),
636
             })
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
637
638
    def test_set_tag(self):
639
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
640
            message=b'message',
641
            committer=b'committer <committer@example.com>',
642
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
643
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
644
            message=b'another commit',
645
            committer=b'committer <committer@example.com>',
646
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
647
648
        remote = ControlDir.open(self.remote_url)
649
        remote.open_branch().tags.set_tag(
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
650
            b'blah', default_mapping.revision_id_foreign_to_bzr(c1))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
651
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
652
            self.remote_real.get_refs(),
653
            {b'refs/heads/master': self.remote_real.head(),
654
             b'refs/tags/blah': c1,
655
             b'HEAD': self.remote_real.head(),
656
             })
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
657
658
    def test_annotated_tag(self):
659
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
660
            message=b'message',
661
            committer=b'committer <committer@example.com>',
662
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
663
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
664
            message=b'another commit',
665
            committer=b'committer <committer@example.com>',
666
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
667
668
        porcelain.tag_create(
7143.15.2 by Jelmer Vernooij
Run autopep8.
669
            self.remote_real,
670
            tag=b"blah",
671
            author=b'author <author@example.com>',
672
            objectish=c2,
673
            tag_time=int(time.time()),
674
            tag_timezone=0,
675
            annotated=True,
676
            message=b"Annotated tag")
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
677
678
        remote = ControlDir.open(self.remote_url)
679
        remote_branch = remote.open_branch()
680
        self.assertEqual({
6973.13.2 by Jelmer Vernooij
Fix some more tests.
681
            'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
682
            remote_branch.tags.get_tag_dict())
683
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
684
    def test_get_branch_reference(self):
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
685
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
686
            message=b'message',
687
            committer=b'committer <committer@example.com>',
688
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
689
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
690
            message=b'another commit',
691
            committer=b'committer <committer@example.com>',
692
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
693
694
        remote = ControlDir.open(self.remote_url)
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
695
        self.assertEqual(b'refs/heads/master', remote.get_branch_reference(''))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
696
        self.assertEqual(None, remote.get_branch_reference('master'))
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
697
698
    def test_get_branch_nick(self):
699
        c1 = self.remote_real.do_commit(
7143.16.21 by Jelmer Vernooij
Fix regressions.
700
            message=b'message',
701
            committer=b'committer <committer@example.com>',
702
            author=b'author <author@example.com>')
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
703
        remote = ControlDir.open(self.remote_url)
704
        self.assertEqual('master', remote.open_branch().nick)
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
705
706
707
class GitUrlAndPathFromTransportTests(TestCase):
708
709
    def test_file(self):
710
        split_url = _git_url_and_path_from_transport('file:///home/blah')
711
        self.assertEqual(split_url.scheme, 'file')
712
        self.assertEqual(split_url.path, '/home/blah')
713
714
    def test_file_segment_params(self):
715
        split_url = _git_url_and_path_from_transport('file:///home/blah,branch=master')
716
        self.assertEqual(split_url.scheme, 'file')
717
        self.assertEqual(split_url.path, '/home/blah')
718
719
    def test_git_smart(self):
720
        split_url = _git_url_and_path_from_transport(
721
            'git://github.com/dulwich/dulwich,branch=master')
722
        self.assertEqual(split_url.scheme, 'git')
723
        self.assertEqual(split_url.path, '/dulwich/dulwich')
724
725
    def test_https(self):
726
        split_url = _git_url_and_path_from_transport(
727
            'https://github.com/dulwich/dulwich')
728
        self.assertEqual(split_url.scheme, 'https')
729
        self.assertEqual(split_url.path, '/dulwich/dulwich')
730
731
    def test_https_segment_params(self):
732
        split_url = _git_url_and_path_from_transport(
733
            'https://github.com/dulwich/dulwich,branch=master')
734
        self.assertEqual(split_url.scheme, 'https')
735
        self.assertEqual(split_url.path, '/dulwich/dulwich')