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