/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/tests/test_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2007 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
"""Tests for interfacing with a Git Branch"""
 
20
 
 
21
import dulwich
 
22
from dulwich.objects import (
 
23
    Commit,
 
24
    Tag,
 
25
    )
 
26
from dulwich.repo import (
 
27
    Repo as GitRepo,
 
28
    )
 
29
 
 
30
import os
 
31
 
 
32
from ... import (
 
33
    errors,
 
34
    revision,
 
35
    urlutils,
 
36
    )
 
37
from ...branch import (
 
38
    Branch,
 
39
    InterBranch,
 
40
    UnstackableBranchFormat,
 
41
    )
 
42
from ...controldir import (
 
43
    ControlDir,
 
44
    )
 
45
from ...repository import (
 
46
    Repository,
 
47
    )
 
48
 
 
49
from .. import (
 
50
    branch,
 
51
    tests,
 
52
    )
 
53
from ..dir import (
 
54
    LocalGitControlDirFormat,
 
55
    )
 
56
from ..mapping import (
 
57
    default_mapping,
 
58
    )
 
59
 
 
60
 
 
61
class TestGitBranch(tests.TestCaseInTempDir):
 
62
 
 
63
    def test_open_by_ref(self):
 
64
        GitRepo.init('.')
 
65
        url = "%s,ref=%s" % (
 
66
            urlutils.local_path_to_url(self.test_dir),
 
67
            urlutils.quote("refs/remotes/origin/unstable", safe='')
 
68
            )
 
69
        d = ControlDir.open(url)
 
70
        b = d.create_branch()
 
71
        self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
 
72
 
 
73
    def test_open_existing(self):
 
74
        r = GitRepo.init('.')
 
75
        d = ControlDir.open('.')
 
76
        thebranch = d.create_branch()
 
77
        self.assertIsInstance(thebranch, branch.GitBranch)
 
78
 
 
79
    def test_repr(self):
 
80
        r = GitRepo.init('.')
 
81
        d = ControlDir.open('.')
 
82
        thebranch = d.create_branch()
 
83
        self.assertEqual(
 
84
            "<LocalGitBranch('%s/', %r)>" % (
 
85
                urlutils.local_path_to_url(self.test_dir),
 
86
                u'master'),
 
87
            repr(thebranch))
 
88
 
 
89
    def test_last_revision_is_null(self):
 
90
        r = GitRepo.init('.')
 
91
        thedir = ControlDir.open('.')
 
92
        thebranch = thedir.create_branch()
 
93
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
 
94
        self.assertEqual((0, revision.NULL_REVISION),
 
95
                         thebranch.last_revision_info())
 
96
 
 
97
    def simple_commit_a(self):
 
98
        r = GitRepo.init('.')
 
99
        self.build_tree(['a'])
 
100
        r.stage(["a"])
 
101
        return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
 
102
 
 
103
    def test_last_revision_is_valid(self):
 
104
        head = self.simple_commit_a()
 
105
        thebranch = Branch.open('.')
 
106
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
 
107
                         thebranch.last_revision())
 
108
 
 
109
    def test_last_revision_info(self):
 
110
        reva = self.simple_commit_a()
 
111
        self.build_tree(['b'])
 
112
        r = GitRepo(".")
 
113
        self.addCleanup(r.close)
 
114
        r.stage("b")
 
115
        revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
 
116
 
 
117
        thebranch = Branch.open('.')
 
118
        self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(
 
119
            revb)), thebranch.last_revision_info())
 
120
 
 
121
    def test_tag_annotated(self):
 
122
        reva = self.simple_commit_a()
 
123
        o = Tag()
 
124
        o.name = b"foo"
 
125
        o.tagger = b"Jelmer <foo@example.com>"
 
126
        o.message = b"add tag"
 
127
        o.object = (Commit, reva)
 
128
        o.tag_timezone = 0
 
129
        o.tag_time = 42
 
130
        r = GitRepo(".")
 
131
        self.addCleanup(r.close)
 
132
        r.object_store.add_object(o)
 
133
        r[b'refs/tags/foo'] = o.id
 
134
        thebranch = Branch.open('.')
 
135
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
136
                         thebranch.tags.get_tag_dict())
 
137
 
 
138
    def test_tag(self):
 
139
        reva = self.simple_commit_a()
 
140
        r = GitRepo(".")
 
141
        self.addCleanup(r.close)
 
142
        r.refs[b"refs/tags/foo"] = reva
 
143
        thebranch = Branch.open('.')
 
144
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
145
                         thebranch.tags.get_tag_dict())
 
146
 
 
147
 
 
148
class TestWithGitBranch(tests.TestCaseWithTransport):
 
149
 
 
150
    def setUp(self):
 
151
        tests.TestCaseWithTransport.setUp(self)
 
152
        r = dulwich.repo.Repo.create(self.test_dir)
 
153
        d = ControlDir.open(self.test_dir)
 
154
        self.git_branch = d.create_branch()
 
155
 
 
156
    def test_get_parent(self):
 
157
        self.assertIs(None, self.git_branch.get_parent())
 
158
 
 
159
    def test_get_stacked_on_url(self):
 
160
        self.assertRaises(UnstackableBranchFormat,
 
161
                          self.git_branch.get_stacked_on_url)
 
162
 
 
163
    def test_get_physical_lock_status(self):
 
164
        self.assertFalse(self.git_branch.get_physical_lock_status())
 
165
 
 
166
 
 
167
class TestLocalGitBranchFormat(tests.TestCase):
 
168
 
 
169
    def setUp(self):
 
170
        super(TestLocalGitBranchFormat, self).setUp()
 
171
        self.format = branch.LocalGitBranchFormat()
 
172
 
 
173
    def test_get_format_description(self):
 
174
        self.assertEqual("Local Git Branch",
 
175
                         self.format.get_format_description())
 
176
 
 
177
    def test_get_network_name(self):
 
178
        self.assertEqual(b"git", self.format.network_name())
 
179
 
 
180
    def test_supports_tags(self):
 
181
        self.assertTrue(self.format.supports_tags())
 
182
 
 
183
 
 
184
class BranchTests(tests.TestCaseInTempDir):
 
185
 
 
186
    def make_onerev_branch(self):
 
187
        os.mkdir("d")
 
188
        os.chdir("d")
 
189
        GitRepo.init('.')
 
190
        bb = tests.GitBranchBuilder()
 
191
        bb.set_file("foobar", b"foo\nbar\n", False)
 
192
        mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
193
        gitsha = bb.finish()[mark]
 
194
        os.chdir("..")
 
195
        return os.path.abspath("d"), gitsha
 
196
 
 
197
    def make_tworev_branch(self):
 
198
        os.mkdir("d")
 
199
        os.chdir("d")
 
200
        GitRepo.init('.')
 
201
        bb = tests.GitBranchBuilder()
 
202
        bb.set_file("foobar", b"foo\nbar\n", False)
 
203
        mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
204
        mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
205
        marks = bb.finish()
 
206
        os.chdir("..")
 
207
        return "d", (marks[mark1], marks[mark2])
 
208
 
 
209
    def clone_git_branch(self, from_url, to_url):
 
210
        from_dir = ControlDir.open(from_url)
 
211
        to_dir = from_dir.sprout(to_url)
 
212
        return to_dir.open_branch()
 
213
 
 
214
    def test_single_rev(self):
 
215
        path, gitsha = self.make_onerev_branch()
 
216
        oldrepo = Repository.open(path)
 
217
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
218
        self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
 
219
        newbranch = self.clone_git_branch(path, "f")
 
220
        self.assertEqual([revid], newbranch.repository.all_revision_ids())
 
221
 
 
222
    def test_sprouted_tags(self):
 
223
        path, gitsha = self.make_onerev_branch()
 
224
        r = GitRepo(path)
 
225
        self.addCleanup(r.close)
 
226
        r.refs[b"refs/tags/lala"] = r.head()
 
227
        oldrepo = Repository.open(path)
 
228
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
229
        newbranch = self.clone_git_branch(path, "f")
 
230
        self.assertEqual({"lala": revid}, newbranch.tags.get_tag_dict())
 
231
        self.assertEqual([revid], newbranch.repository.all_revision_ids())
 
232
 
 
233
    def test_sprouted_ghost_tags(self):
 
234
        path, gitsha = self.make_onerev_branch()
 
235
        r = GitRepo(path)
 
236
        self.addCleanup(r.close)
 
237
        r.refs[b"refs/tags/lala"] = b"aa" * 20
 
238
        oldrepo = Repository.open(path)
 
239
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
240
        warnings, newbranch = self.callCatchWarnings(
 
241
            self.clone_git_branch, path, "f")
 
242
        self.assertEqual({}, newbranch.tags.get_tag_dict())
 
243
        # Dulwich raises a UserWarning for tags with invalid target
 
244
        self.assertIn(('ref refs/tags/lala points at non-present sha ' + ("aa" * 20), ), [w.args for w in warnings])
 
245
 
 
246
    def test_interbranch_pull_submodule(self):
 
247
        path = "d"
 
248
        os.mkdir(path)
 
249
        os.chdir(path)
 
250
        GitRepo.init('.')
 
251
        bb = tests.GitBranchBuilder()
 
252
        bb.set_file("foobar", b"foo\nbar\n", False)
 
253
        mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
254
        bb.set_submodule("core", b'102ee7206ebc4227bec8ac02450972e6738f4a33')
 
255
        bb.set_file('.gitmodules', b"""\
 
256
[submodule "core"]
 
257
  path = core
 
258
  url = https://github.com/phhusson/QuasselC.git
 
259
""", False)
 
260
        mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
261
        marks = bb.finish()
 
262
        os.chdir("..")
 
263
        gitsha1 = marks[mark1]
 
264
        gitsha2 = marks[mark2]
 
265
        oldrepo = Repository.open(path)
 
266
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
267
        newbranch = self.make_branch('g')
 
268
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
269
        inter_branch.pull()
 
270
        self.assertEqual(revid2, newbranch.last_revision())
 
271
        self.assertEqual(
 
272
            ('https://github.com/phhusson/QuasselC.git', 'core'),
 
273
            newbranch.get_reference_info(newbranch.basis_tree().path2id('core')))
 
274
 
 
275
    def test_interbranch_pull(self):
 
276
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
277
        oldrepo = Repository.open(path)
 
278
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
279
        newbranch = self.make_branch('g')
 
280
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
281
        inter_branch.pull()
 
282
        self.assertEqual(revid2, newbranch.last_revision())
 
283
 
 
284
    def test_interbranch_pull_noop(self):
 
285
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
286
        oldrepo = Repository.open(path)
 
287
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
288
        newbranch = self.make_branch('g')
 
289
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
290
        inter_branch.pull()
 
291
        # This is basically "assertNotRaises"
 
292
        inter_branch.pull()
 
293
        self.assertEqual(revid2, newbranch.last_revision())
 
294
 
 
295
    def test_interbranch_pull_stop_revision(self):
 
296
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
297
        oldrepo = Repository.open(path)
 
298
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
299
        newbranch = self.make_branch('g')
 
300
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
301
        inter_branch.pull(stop_revision=revid1)
 
302
        self.assertEqual(revid1, newbranch.last_revision())
 
303
 
 
304
    def test_interbranch_pull_with_tags(self):
 
305
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
306
        gitrepo = GitRepo(path)
 
307
        self.addCleanup(gitrepo.close)
 
308
        gitrepo.refs[b"refs/tags/sometag"] = gitsha2
 
309
        oldrepo = Repository.open(path)
 
310
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
311
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
312
        newbranch = self.make_branch('g')
 
313
        source_branch = Branch.open(path)
 
314
        source_branch.get_config().set_user_option("branch.fetch_tags", True)
 
315
        inter_branch = InterBranch.get(source_branch, newbranch)
 
316
        inter_branch.pull(stop_revision=revid1)
 
317
        self.assertEqual(revid1, newbranch.last_revision())
 
318
        self.assertTrue(newbranch.repository.has_revision(revid2))
 
319
 
 
320
    def test_bzr_branch_bound_to_git(self):
 
321
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
322
        wt = Branch.open(path).create_checkout('co')
 
323
        self.build_tree_contents([('co/foobar', b'blah')])
 
324
        self.assertRaises(
 
325
            errors.NoRoundtrippingSupport, wt.commit,
 
326
            'commit from bound branch.')
 
327
        revid = wt.commit('commit from bound branch.', lossy=True)
 
328
        self.assertEqual(revid, wt.branch.last_revision())
 
329
        self.assertEqual(
 
330
            revid,
 
331
            wt.branch.get_master_branch().last_revision())
 
332
 
 
333
 
 
334
class ForeignTestsBranchFactory(object):
 
335
 
 
336
    def make_empty_branch(self, transport):
 
337
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
 
338
        return d.create_branch()
 
339
 
 
340
    make_branch = make_empty_branch