/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-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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