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