/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
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
147
class TestRemoteGitBranchFormat(TestCase):
148
149
    def setUp(self):
150
        super(TestRemoteGitBranchFormat, self).setUp()
151
        self.format = RemoteGitBranchFormat()
152
153
    def test_get_format_description(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
154
        self.assertEqual("Remote Git Branch",
155
                         self.format.get_format_description())
0.295.1 by Jelmer Vernooij
Split up branch formats.
156
157
    def test_get_network_name(self):
6973.13.2 by Jelmer Vernooij
Fix some more tests.
158
        self.assertEqual(b"git", self.format.network_name())
0.295.1 by Jelmer Vernooij
Split up branch formats.
159
160
    def test_supports_tags(self):
161
        self.assertTrue(self.format.supports_tags())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
162
163
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
164
class TestRemoteGitBranch(TestCaseWithTransport):
165
7183.1.1 by Jelmer Vernooij
Add missing feature for git test that requires git executable.
166
    _test_needs_features = [ExecutableFeature('git')]
167
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
168
    def setUp(self):
169
        TestCaseWithTransport.setUp(self)
170
        self.remote_real = GitRepo.init('remote', mkdir=True)
171
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
172
        self.permit_url(self.remote_url)
173
174
    def test_set_last_revision_info(self):
175
        c1 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
176
            message=b'message 1',
177
            committer=b'committer <committer@example.com>',
178
            author=b'author <author@example.com>',
179
            ref=b'refs/heads/newbranch')
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
180
        c2 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
181
            message=b'message 2',
182
            committer=b'committer <committer@example.com>',
183
            author=b'author <author@example.com>',
184
            ref=b'refs/heads/newbranch')
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
185
186
        remote = ControlDir.open(self.remote_url)
187
        newbranch = remote.open_branch('newbranch')
188
        self.assertEqual(newbranch.lookup_foreign_revision_id(c2),
189
                         newbranch.last_revision())
190
        newbranch.set_last_revision_info(
191
            1, newbranch.lookup_foreign_revision_id(c1))
7131.12.2 by Jelmer Vernooij
Fix tests on python 3.
192
        self.assertEqual(c1, self.remote_real.refs[b'refs/heads/newbranch'])
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
193
        self.assertEqual(newbranch.last_revision(),
194
                         newbranch.lookup_foreign_revision_id(c1))
195
196
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
197
class FetchFromRemoteTestBase(object):
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
198
199
    _test_needs_features = [ExecutableFeature('git')]
200
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
201
    _to_format = None
202
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
203
    def setUp(self):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
204
        TestCaseWithTransport.setUp(self)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
205
        self.remote_real = GitRepo.init('remote', mkdir=True)
206
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
207
        self.permit_url(self.remote_url)
208
209
    def test_sprout_simple(self):
210
        self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
211
            message=b'message',
212
            committer=b'committer <committer@example.com>',
213
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
214
215
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
216
        self.make_controldir('local', format=self._to_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
217
        local = remote.sprout('local')
218
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
219
            default_mapping.revision_id_foreign_to_bzr(
220
                self.remote_real.head()),
221
            local.open_branch().last_revision())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
222
223
    def test_sprout_with_tags(self):
224
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
225
            message=b'message',
226
            committer=b'committer <committer@example.com>',
227
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
228
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
229
            message=b'another commit',
230
            committer=b'committer <committer@example.com>',
231
            author=b'author <author@example.com>',
232
            ref=b'refs/tags/another')
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
233
        self.remote_real.refs[b'refs/tags/blah'] = self.remote_real.head()
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
234
235
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
236
        self.make_controldir('local', format=self._to_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
237
        local = remote.sprout('local')
238
        local_branch = local.open_branch()
239
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
240
            default_mapping.revision_id_foreign_to_bzr(c1),
241
            local_branch.last_revision())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
242
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
243
            {'blah': local_branch.last_revision(),
244
             'another': default_mapping.revision_id_foreign_to_bzr(c2)},
245
            local_branch.tags.get_tag_dict())
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
246
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
247
    def test_sprout_with_annotated_tag(self):
248
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
249
            message=b'message',
250
            committer=b'committer <committer@example.com>',
251
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
252
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
253
            message=b'another commit',
254
            committer=b'committer <committer@example.com>',
255
            author=b'author <author@example.com>',
256
            ref=b'refs/heads/another')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
257
        porcelain.tag_create(
7143.15.2 by Jelmer Vernooij
Run autopep8.
258
            self.remote_real,
259
            tag=b"blah",
260
            author=b'author <author@example.com>',
261
            objectish=c2,
262
            tag_time=int(time.time()),
263
            tag_timezone=0,
264
            annotated=True,
265
            message=b"Annotated tag")
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
266
267
        remote = ControlDir.open(self.remote_url)
268
        self.make_controldir('local', format=self._to_format)
7143.15.2 by Jelmer Vernooij
Run autopep8.
269
        local = remote.sprout(
270
            'local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
271
        local_branch = local.open_branch()
272
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
273
            default_mapping.revision_id_foreign_to_bzr(c1),
274
            local_branch.last_revision())
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
275
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
276
            {'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
277
            local_branch.tags.get_tag_dict())
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
278
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
279
    def test_sprout_with_annotated_tag_unreferenced(self):
280
        c1 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
281
            message=b'message',
282
            committer=b'committer <committer@example.com>',
283
            author=b'author <author@example.com>')
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
284
        c2 = self.remote_real.do_commit(
7143.16.8 by Jelmer Vernooij
Fix E126
285
            message=b'another commit',
286
            committer=b'committer <committer@example.com>',
287
            author=b'author <author@example.com>')
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
288
        porcelain.tag_create(
7143.16.8 by Jelmer Vernooij
Fix E126
289
            self.remote_real,
290
            tag=b"blah",
291
            author=b'author <author@example.com>',
292
            objectish=c1,
293
            tag_time=int(time.time()),
294
            tag_timezone=0,
295
            annotated=True,
296
            message=b"Annotated tag")
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
297
298
        remote = ControlDir.open(self.remote_url)
299
        self.make_controldir('local', format=self._to_format)
300
        local = remote.sprout(
7143.16.8 by Jelmer Vernooij
Fix E126
301
            'local',
302
            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.
303
        local_branch = local.open_branch()
304
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
305
            default_mapping.revision_id_foreign_to_bzr(c1),
306
            local_branch.last_revision())
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
307
        self.assertEqual(
7143.16.8 by Jelmer Vernooij
Fix E126
308
            {'blah': default_mapping.revision_id_foreign_to_bzr(c1)},
309
            local_branch.tags.get_tag_dict())
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
310
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
311
7143.16.21 by Jelmer Vernooij
Fix regressions.
312
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
313
314
    _to_format = '2a'
315
316
7143.15.2 by Jelmer Vernooij
Run autopep8.
317
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
318
319
    _to_format = 'git'
320
321
322
class PushToRemoteBase(object):
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
323
324
    _test_needs_features = [ExecutableFeature('git')]
325
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
326
    _from_format = None
327
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
328
    def setUp(self):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
329
        TestCaseWithTransport.setUp(self)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
330
        self.remote_real = GitRepo.init('remote', mkdir=True)
331
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
332
        self.permit_url(self.remote_url)
333
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
334
    def test_push_branch_new(self):
335
        remote = ControlDir.open(self.remote_url)
336
        wt = self.make_branch_and_tree('local', format=self._from_format)
337
        self.build_tree(['local/blah'])
338
        wt.add(['blah'])
339
        revid = wt.commit('blah')
340
341
        if self._from_format == 'git':
0.406.2 by Jelmer Vernooij
Add tests.
342
            result = remote.push_branch(wt.branch, name='newbranch')
343
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
344
            result = remote.push_branch(
345
                wt.branch, lossy=True, name='newbranch')
0.406.2 by Jelmer Vernooij
Add tests.
346
347
        self.assertEqual(0, result.old_revno)
348
        if self._from_format == 'git':
349
            self.assertEqual(1, result.new_revno)
350
        else:
351
            self.assertIs(None, result.new_revno)
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
352
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
353
        result.report(BytesIO())
0.406.3 by Jelmer Vernooij
Add extra tests.
354
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
355
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
356
            {b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
357
             },
358
            self.remote_real.get_refs())
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
359
7484.1.2 by Jelmer Vernooij
Add some tests.
360
    def test_push_branch_symref(self):
361
        cfg = self.remote_real.get_config()
362
        cfg.set((b'core', ), b'bare', True)
363
        cfg.write_to_path()
364
        self.remote_real.refs.set_symbolic_ref(b'HEAD', b'refs/heads/master')
365
        c1 = self.remote_real.do_commit(
366
            message=b'message',
367
            committer=b'committer <committer@example.com>',
368
            author=b'author <author@example.com>',
369
            ref=b'refs/heads/master')
370
        remote = ControlDir.open(self.remote_url)
371
        wt = self.make_branch_and_tree('local', format=self._from_format)
372
        self.build_tree(['local/blah'])
373
        wt.add(['blah'])
374
        revid = wt.commit('blah')
375
376
        if self._from_format == 'git':
377
            result = remote.push_branch(wt.branch, overwrite=True)
378
        else:
379
            result = remote.push_branch(wt.branch, lossy=True, overwrite=True)
380
381
        self.assertEqual(None, result.old_revno)
382
        if self._from_format == 'git':
383
            self.assertEqual(1, result.new_revno)
384
        else:
385
            self.assertIs(None, result.new_revno)
386
387
        result.report(BytesIO())
388
389
        self.assertEqual(
7484.1.3 by Jelmer Vernooij
Fix formatting.
390
            {
391
                b'HEAD': self.remote_real.refs[b'refs/heads/master'],
392
                b'refs/heads/master': self.remote_real.refs[b'refs/heads/master'],
7484.1.2 by Jelmer Vernooij
Add some tests.
393
            },
394
            self.remote_real.get_refs())
395
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
396
    def test_push_branch_new_with_tags(self):
397
        remote = ControlDir.open(self.remote_url)
398
        builder = self.make_branch_builder('local', format=self._from_format)
399
        builder.start_series()
400
        rev_1 = builder.build_snapshot(None, [
401
            ('add', ('', None, 'directory', '')),
402
            ('add', ('filename', None, 'file', b'content'))])
403
        rev_2 = builder.build_snapshot(
404
            [rev_1], [('modify', ('filename', b'new-content\n'))])
405
        rev_3 = builder.build_snapshot(
406
            [rev_1], [('modify', ('filename', b'new-new-content\n'))])
407
        builder.finish_series()
408
        branch = builder.get_branch()
409
        try:
410
            branch.tags.set_tag('atag', rev_2)
411
        except TagsNotSupported:
412
            raise TestNotApplicable('source format does not support tags')
413
414
        branch.get_config_stack().set('branch.fetch_tags', True)
415
        if self._from_format == 'git':
416
            result = remote.push_branch(branch, name='newbranch')
417
        else:
418
            result = remote.push_branch(
419
                branch, lossy=True, name='newbranch')
420
421
        self.assertEqual(0, result.old_revno)
422
        if self._from_format == 'git':
423
            self.assertEqual(2, result.new_revno)
424
        else:
425
            self.assertIs(None, result.new_revno)
426
427
        result.report(BytesIO())
428
429
        self.assertEqual(
430
            {b'refs/heads/newbranch', b'refs/tags/atag'},
431
            set(self.remote_real.get_refs().keys()))
432
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
433
    def test_push(self):
434
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
435
            message=b'message',
436
            committer=b'committer <committer@example.com>',
437
            author=b'author <author@example.com>')
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
438
439
        remote = ControlDir.open(self.remote_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
440
        self.make_controldir('local', format=self._from_format)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
441
        local = remote.sprout('local')
442
        self.build_tree(['local/blah'])
443
        wt = local.open_workingtree()
444
        wt.add(['blah'])
445
        revid = wt.commit('blah')
446
        wt.branch.tags.set_tag('sometag', revid)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
447
        wt.branch.get_config_stack().set('branch.fetch_tags', True)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
448
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
449
        if self._from_format == 'git':
0.406.2 by Jelmer Vernooij
Add tests.
450
            result = wt.branch.push(remote.create_branch('newbranch'))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
451
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
452
            result = wt.branch.push(
453
                remote.create_branch('newbranch'), lossy=True)
0.406.2 by Jelmer Vernooij
Add tests.
454
455
        self.assertEqual(0, result.old_revno)
456
        self.assertEqual(2, result.new_revno)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
457
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
458
        result.report(BytesIO())
0.406.3 by Jelmer Vernooij
Add extra tests.
459
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
460
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
461
            {b'refs/heads/master': self.remote_real.head(),
462
             b'HEAD': self.remote_real.head(),
463
             b'refs/heads/newbranch': self.remote_real.refs[b'refs/heads/newbranch'],
464
             b'refs/tags/sometag': self.remote_real.refs[b'refs/heads/newbranch'],
465
             },
466
            self.remote_real.get_refs())
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
467
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
468
    def test_push_diverged(self):
469
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
470
            message=b'message',
471
            committer=b'committer <committer@example.com>',
472
            author=b'author <author@example.com>',
473
            ref=b'refs/heads/newbranch')
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
474
475
        remote = ControlDir.open(self.remote_url)
476
        wt = self.make_branch_and_tree('local', format=self._from_format)
477
        self.build_tree(['local/blah'])
478
        wt.add(['blah'])
479
        revid = wt.commit('blah')
480
481
        newbranch = remote.open_branch('newbranch')
482
        if self._from_format == 'git':
483
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch)
484
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
485
            self.assertRaises(DivergedBranches, wt.branch.push,
486
                              newbranch, lossy=True)
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
487
488
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
489
            {b'refs/heads/newbranch': c1},
490
            self.remote_real.get_refs())
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
491
492
        if self._from_format == 'git':
493
            wt.branch.push(newbranch, overwrite=True)
494
        else:
495
            wt.branch.push(newbranch, lossy=True, overwrite=True)
496
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
497
        self.assertNotEqual(c1, self.remote_real.refs[b'refs/heads/newbranch'])
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
498
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
499
7143.15.2 by Jelmer Vernooij
Run autopep8.
500
class PushToRemoteFromBzrTests(PushToRemoteBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
501
502
    _from_format = '2a'
503
504
7143.15.2 by Jelmer Vernooij
Run autopep8.
505
class PushToRemoteFromGitTests(PushToRemoteBase, TestCaseWithTransport):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
506
507
    _from_format = 'git'
508
509
510
class RemoteControlDirTests(TestCaseWithTransport):
511
512
    _test_needs_features = [ExecutableFeature('git')]
513
514
    def setUp(self):
515
        TestCaseWithTransport.setUp(self)
516
        self.remote_real = GitRepo.init('remote', mkdir=True)
517
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
518
        self.permit_url(self.remote_url)
519
520
    def test_remove_branch(self):
521
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
522
            message=b'message',
523
            committer=b'committer <committer@example.com>',
524
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
525
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
526
            message=b'another commit',
527
            committer=b'committer <committer@example.com>',
528
            author=b'author <author@example.com>',
529
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
530
531
        remote = ControlDir.open(self.remote_url)
532
        remote.destroy_branch(name='blah')
533
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
534
            self.remote_real.get_refs(),
535
            {b'refs/heads/master': self.remote_real.head(),
536
             b'HEAD': self.remote_real.head(),
537
             })
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
538
539
    def test_list_branches(self):
540
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
541
            message=b'message',
542
            committer=b'committer <committer@example.com>',
543
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
544
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
545
            message=b'another commit',
546
            committer=b'committer <committer@example.com>',
547
            author=b'author <author@example.com>',
548
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
549
550
        remote = ControlDir.open(self.remote_url)
551
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
552
            set(['master', 'blah', 'master']),
553
            set([b.name for b in remote.list_branches()]))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
554
555
    def test_get_branches(self):
556
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
557
            message=b'message',
558
            committer=b'committer <committer@example.com>',
559
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
560
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
561
            message=b'another commit',
562
            committer=b'committer <committer@example.com>',
563
            author=b'author <author@example.com>',
564
            ref=b'refs/heads/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
565
566
        remote = ControlDir.open(self.remote_url)
567
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
568
            {'': 'master', 'blah': 'blah', 'master': 'master'},
569
            {n: b.name for (n, b) in remote.get_branches().items()})
7490.13.6 by Jelmer Vernooij
Fix tests.
570
        self.assertEqual(
571
            set(['', 'blah', 'master']), set(remote.branch_names()))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
572
573
    def test_remove_tag(self):
574
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
575
            message=b'message',
576
            committer=b'committer <committer@example.com>',
577
            author=b'author <author@example.com>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
578
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
579
            message=b'another commit',
580
            committer=b'committer <committer@example.com>',
581
            author=b'author <author@example.com>',
582
            ref=b'refs/tags/blah')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
583
584
        remote = ControlDir.open(self.remote_url)
585
        remote_branch = remote.open_branch()
586
        remote_branch.tags.delete_tag('blah')
587
        self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
588
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
589
            self.remote_real.get_refs(),
590
            {b'refs/heads/master': self.remote_real.head(),
591
             b'HEAD': self.remote_real.head(),
592
             })
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
593
594
    def test_set_tag(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>')
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
603
604
        remote = ControlDir.open(self.remote_url)
605
        remote.open_branch().tags.set_tag(
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
606
            b'blah', default_mapping.revision_id_foreign_to_bzr(c1))
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
607
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
608
            self.remote_real.get_refs(),
609
            {b'refs/heads/master': self.remote_real.head(),
610
             b'refs/tags/blah': c1,
611
             b'HEAD': self.remote_real.head(),
612
             })
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
613
614
    def test_annotated_tag(self):
615
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
616
            message=b'message',
617
            committer=b'committer <committer@example.com>',
618
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
619
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
620
            message=b'another commit',
621
            committer=b'committer <committer@example.com>',
622
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
623
624
        porcelain.tag_create(
7143.15.2 by Jelmer Vernooij
Run autopep8.
625
            self.remote_real,
626
            tag=b"blah",
627
            author=b'author <author@example.com>',
628
            objectish=c2,
629
            tag_time=int(time.time()),
630
            tag_timezone=0,
631
            annotated=True,
632
            message=b"Annotated tag")
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
633
634
        remote = ControlDir.open(self.remote_url)
635
        remote_branch = remote.open_branch()
636
        self.assertEqual({
6973.13.2 by Jelmer Vernooij
Fix some more tests.
637
            'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
638
            remote_branch.tags.get_tag_dict())
639
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
640
    def test_get_branch_reference(self):
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
641
        c1 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
642
            message=b'message',
643
            committer=b'committer <committer@example.com>',
644
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
645
        c2 = self.remote_real.do_commit(
7143.15.2 by Jelmer Vernooij
Run autopep8.
646
            message=b'another commit',
647
            committer=b'committer <committer@example.com>',
648
            author=b'author <author@example.com>')
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
649
650
        remote = ControlDir.open(self.remote_url)
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
651
        self.assertEqual(b'refs/heads/master', remote.get_branch_reference(''))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
652
        self.assertEqual(None, remote.get_branch_reference('master'))
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
653
654
    def test_get_branch_nick(self):
655
        c1 = self.remote_real.do_commit(
7143.16.21 by Jelmer Vernooij
Fix regressions.
656
            message=b'message',
657
            committer=b'committer <committer@example.com>',
658
            author=b'author <author@example.com>')
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
659
        remote = ControlDir.open(self.remote_url)
660
        self.assertEqual('master', remote.open_branch().nick)
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
661
662
663
class GitUrlAndPathFromTransportTests(TestCase):
664
665
    def test_file(self):
666
        split_url = _git_url_and_path_from_transport('file:///home/blah')
667
        self.assertEqual(split_url.scheme, 'file')
668
        self.assertEqual(split_url.path, '/home/blah')
669
670
    def test_file_segment_params(self):
671
        split_url = _git_url_and_path_from_transport('file:///home/blah,branch=master')
672
        self.assertEqual(split_url.scheme, 'file')
673
        self.assertEqual(split_url.path, '/home/blah')
674
675
    def test_git_smart(self):
676
        split_url = _git_url_and_path_from_transport(
677
            'git://github.com/dulwich/dulwich,branch=master')
678
        self.assertEqual(split_url.scheme, 'git')
679
        self.assertEqual(split_url.path, '/dulwich/dulwich')
680
681
    def test_https(self):
682
        split_url = _git_url_and_path_from_transport(
683
            'https://github.com/dulwich/dulwich')
684
        self.assertEqual(split_url.scheme, 'https')
685
        self.assertEqual(split_url.path, '/dulwich/dulwich')
686
687
    def test_https_segment_params(self):
688
        split_url = _git_url_and_path_from_transport(
689
            'https://github.com/dulwich/dulwich,branch=master')
690
        self.assertEqual(split_url.scheme, 'https')
691
        self.assertEqual(split_url.path, '/dulwich/dulwich')