/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: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
1
2
# Copyright (C) 2007 Canonical Ltd
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
12
13
#
13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
 
17
18
 
18
19
"""Tests for interfacing with a Git Branch"""
19
20
 
 
21
from __future__ import absolute_import
20
22
 
21
23
import dulwich
22
24
from dulwich.objects import (
29
31
 
30
32
import os
31
33
 
32
 
from bzrlib import (
 
34
from ... import (
33
35
    errors,
34
36
    revision,
 
37
    urlutils,
35
38
    )
36
 
from bzrlib.branch import (
 
39
from ...branch import (
37
40
    Branch,
38
41
    InterBranch,
39
 
    )
40
 
from bzrlib.bzrdir import (
41
 
    BzrDir,
42
 
    )
43
 
from bzrlib.repository import (
 
42
    UnstackableBranchFormat,
 
43
    )
 
44
from ...controldir import (
 
45
    ControlDir,
 
46
    )
 
47
from ...repository import (
44
48
    Repository,
45
49
    )
46
50
 
47
 
from bzrlib.plugins.git import (
48
 
    LocalGitControlDirFormat,
 
51
from .. import (
49
52
    branch,
50
53
    tests,
51
54
    )
52
 
from bzrlib.plugins.git.mapping import (
 
55
from ..dir import (
 
56
    LocalGitControlDirFormat,
 
57
    )
 
58
from ..mapping import (
53
59
    default_mapping,
54
60
    )
55
61
 
56
62
 
57
63
class TestGitBranch(tests.TestCaseInTempDir):
58
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
 
59
75
    def test_open_existing(self):
60
 
        GitRepo.init('.')
61
 
        d = BzrDir.open('.')
 
76
        r = GitRepo.init('.')
 
77
        d = ControlDir.open('.')
62
78
        thebranch = d.create_branch()
63
79
        self.assertIsInstance(thebranch, branch.GitBranch)
64
80
 
65
81
    def test_repr(self):
66
 
        GitRepo.init('.')
67
 
        d = BzrDir.open('.')
 
82
        r = GitRepo.init('.')
 
83
        d = ControlDir.open('.')
68
84
        thebranch = d.create_branch()
69
 
        self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
 
85
        self.assertEqual(
 
86
            "<LocalGitBranch('%s/', %r)>" % (
 
87
                urlutils.local_path_to_url(self.test_dir),
 
88
                u'master'),
 
89
            repr(thebranch))
70
90
 
71
91
    def test_last_revision_is_null(self):
72
 
        GitRepo.init('.')
73
 
        thedir = BzrDir.open('.')
 
92
        r = GitRepo.init('.')
 
93
        thedir = ControlDir.open('.')
74
94
        thebranch = thedir.create_branch()
75
95
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
76
96
        self.assertEqual((0, revision.NULL_REVISION),
80
100
        r = GitRepo.init('.')
81
101
        self.build_tree(['a'])
82
102
        r.stage(["a"])
83
 
        return r.do_commit("a", committer="Somebody <foo@example.com>")
 
103
        return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
84
104
 
85
105
    def test_last_revision_is_valid(self):
86
106
        head = self.simple_commit_a()
88
108
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
89
109
                         thebranch.last_revision())
90
110
 
91
 
    def test_revision_history(self):
 
111
    def test_last_revision_info(self):
92
112
        reva = self.simple_commit_a()
93
113
        self.build_tree(['b'])
94
114
        r = GitRepo(".")
 
115
        self.addCleanup(r.close)
95
116
        r.stage("b")
96
 
        revb = r.do_commit("b", committer="Somebody <foo@example.com>")
 
117
        revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
97
118
 
98
119
        thebranch = Branch.open('.')
99
 
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
100
 
                         thebranch.revision_history())
 
120
        self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(
 
121
            revb)), thebranch.last_revision_info())
101
122
 
102
123
    def test_tag_annotated(self):
103
124
        reva = self.simple_commit_a()
104
125
        o = Tag()
105
 
        o.name = "foo"
106
 
        o.tagger = "Jelmer <foo@example.com>"
107
 
        o.message = "add tag"
 
126
        o.name = b"foo"
 
127
        o.tagger = b"Jelmer <foo@example.com>"
 
128
        o.message = b"add tag"
108
129
        o.object = (Commit, reva)
109
130
        o.tag_timezone = 0
110
131
        o.tag_time = 42
111
132
        r = GitRepo(".")
 
133
        self.addCleanup(r.close)
112
134
        r.object_store.add_object(o)
113
 
        r['refs/tags/foo'] = o.id
 
135
        r[b'refs/tags/foo'] = o.id
114
136
        thebranch = Branch.open('.')
115
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
116
 
                          thebranch.tags.get_tag_dict())
 
137
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
138
                         thebranch.tags.get_tag_dict())
117
139
 
118
140
    def test_tag(self):
119
141
        reva = self.simple_commit_a()
120
142
        r = GitRepo(".")
121
 
        r.refs["refs/tags/foo"] = reva
 
143
        self.addCleanup(r.close)
 
144
        r.refs[b"refs/tags/foo"] = reva
122
145
        thebranch = Branch.open('.')
123
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
124
 
                          thebranch.tags.get_tag_dict())
125
 
 
 
146
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
147
                         thebranch.tags.get_tag_dict())
126
148
 
127
149
 
128
150
class TestWithGitBranch(tests.TestCaseWithTransport):
129
151
 
130
152
    def setUp(self):
131
153
        tests.TestCaseWithTransport.setUp(self)
132
 
        dulwich.repo.Repo.create(self.test_dir)
133
 
        d = BzrDir.open(self.test_dir)
 
154
        r = dulwich.repo.Repo.create(self.test_dir)
 
155
        d = ControlDir.open(self.test_dir)
134
156
        self.git_branch = d.create_branch()
135
157
 
136
158
    def test_get_parent(self):
137
159
        self.assertIs(None, self.git_branch.get_parent())
138
160
 
139
161
    def test_get_stacked_on_url(self):
140
 
        self.assertRaises(errors.UnstackableBranchFormat,
141
 
            self.git_branch.get_stacked_on_url)
 
162
        self.assertRaises(UnstackableBranchFormat,
 
163
                          self.git_branch.get_stacked_on_url)
142
164
 
143
165
    def test_get_physical_lock_status(self):
144
166
        self.assertFalse(self.git_branch.get_physical_lock_status())
145
167
 
146
168
 
147
 
class TestGitBranchFormat(tests.TestCase):
 
169
class TestLocalGitBranchFormat(tests.TestCase):
148
170
 
149
171
    def setUp(self):
150
 
        super(TestGitBranchFormat, self).setUp()
151
 
        self.format = branch.GitBranchFormat()
 
172
        super(TestLocalGitBranchFormat, self).setUp()
 
173
        self.format = branch.LocalGitBranchFormat()
152
174
 
153
175
    def test_get_format_description(self):
154
 
        self.assertEquals("Git Branch", self.format.get_format_description())
 
176
        self.assertEqual("Local Git Branch",
 
177
                         self.format.get_format_description())
155
178
 
156
179
    def test_get_network_name(self):
157
 
        self.assertEquals("git", self.format.network_name())
 
180
        self.assertEqual(b"git", self.format.network_name())
158
181
 
159
182
    def test_supports_tags(self):
160
183
        self.assertTrue(self.format.supports_tags())
167
190
        os.chdir("d")
168
191
        GitRepo.init('.')
169
192
        bb = tests.GitBranchBuilder()
170
 
        bb.set_file("foobar", "foo\nbar\n", False)
171
 
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
193
        bb.set_file("foobar", b"foo\nbar\n", False)
 
194
        mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
172
195
        gitsha = bb.finish()[mark]
173
196
        os.chdir("..")
174
 
        return "d", gitsha
 
197
        return os.path.abspath("d"), gitsha
175
198
 
176
199
    def make_tworev_branch(self):
177
200
        os.mkdir("d")
178
201
        os.chdir("d")
179
202
        GitRepo.init('.')
180
203
        bb = tests.GitBranchBuilder()
181
 
        bb.set_file("foobar", "foo\nbar\n", False)
182
 
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
183
 
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
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")
184
207
        marks = bb.finish()
185
208
        os.chdir("..")
186
209
        return "d", (marks[mark1], marks[mark2])
187
210
 
188
211
    def clone_git_branch(self, from_url, to_url):
189
 
        from_dir = BzrDir.open(from_url)
 
212
        from_dir = ControlDir.open(from_url)
190
213
        to_dir = from_dir.sprout(to_url)
191
214
        return to_dir.open_branch()
192
215
 
194
217
        path, gitsha = self.make_onerev_branch()
195
218
        oldrepo = Repository.open(path)
196
219
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
220
        self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
197
221
        newbranch = self.clone_git_branch(path, "f")
198
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
222
        self.assertEqual([revid], newbranch.repository.all_revision_ids())
199
223
 
200
224
    def test_sprouted_tags(self):
201
225
        path, gitsha = self.make_onerev_branch()
202
226
        r = GitRepo(path)
203
 
        r.refs["refs/tags/lala"] = r.head()
 
227
        self.addCleanup(r.close)
 
228
        r.refs[b"refs/tags/lala"] = r.head()
204
229
        oldrepo = Repository.open(path)
205
230
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
206
231
        newbranch = self.clone_git_branch(path, "f")
207
 
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
208
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
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])
209
247
 
210
248
    def test_interbranch_pull(self):
211
249
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
214
252
        newbranch = self.make_branch('g')
215
253
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
216
254
        inter_branch.pull()
217
 
        self.assertEquals(revid2, newbranch.last_revision())
 
255
        self.assertEqual(revid2, newbranch.last_revision())
218
256
 
219
257
    def test_interbranch_pull_noop(self):
220
258
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
225
263
        inter_branch.pull()
226
264
        # This is basically "assertNotRaises"
227
265
        inter_branch.pull()
228
 
        self.assertEquals(revid2, newbranch.last_revision())
 
266
        self.assertEqual(revid2, newbranch.last_revision())
229
267
 
230
268
    def test_interbranch_pull_stop_revision(self):
231
269
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
234
272
        newbranch = self.make_branch('g')
235
273
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
236
274
        inter_branch.pull(stop_revision=revid1)
237
 
        self.assertEquals(revid1, newbranch.last_revision())
 
275
        self.assertEqual(revid1, newbranch.last_revision())
238
276
 
239
 
    def test_interbranch_limited_pull(self):
 
277
    def test_interbranch_pull_with_tags(self):
240
278
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
279
        gitrepo = GitRepo(path)
 
280
        self.addCleanup(gitrepo.close)
 
281
        gitrepo.refs[b"refs/tags/sometag"] = gitsha2
241
282
        oldrepo = Repository.open(path)
242
283
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
243
284
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
244
285
        newbranch = self.make_branch('g')
245
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
246
 
        inter_branch.pull(limit=1)
247
 
        self.assertEquals(revid1, newbranch.last_revision())
248
 
        inter_branch.pull(limit=1)
249
 
        self.assertEquals(revid2, newbranch.last_revision())
 
286
        source_branch = Branch.open(path)
 
287
        source_branch.get_config().set_user_option("branch.fetch_tags", True)
 
288
        inter_branch = InterBranch.get(source_branch, newbranch)
 
289
        inter_branch.pull(stop_revision=revid1)
 
290
        self.assertEqual(revid1, newbranch.last_revision())
 
291
        self.assertTrue(newbranch.repository.has_revision(revid2))
 
292
 
 
293
    def test_bzr_branch_bound_to_git(self):
 
294
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
295
        wt = Branch.open(path).create_checkout('co')
 
296
        self.build_tree_contents([('co/foobar', b'blah')])
 
297
        self.assertRaises(
 
298
            errors.NoRoundtrippingSupport, wt.commit,
 
299
            'commit from bound branch.')
 
300
        revid = wt.commit('commit from bound branch.', lossy=True)
 
301
        self.assertEqual(revid, wt.branch.last_revision())
 
302
        self.assertEqual(
 
303
            revid,
 
304
            wt.branch.get_master_branch().last_revision())
250
305
 
251
306
 
252
307
class ForeignTestsBranchFactory(object):
256
311
        return d.create_branch()
257
312
 
258
313
    make_branch = make_empty_branch
259
 
 
260
 
 
261