1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
# Copyright (C) 2007 Canonical Ltd
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.
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.
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
19
"""Tests for interfacing with a Git Branch"""
22
from dulwich.objects import (
26
from dulwich.repo import (
37
from ...branch import (
40
UnstackableBranchFormat,
42
from ...controldir import (
45
from ...repository import (
54
LocalGitControlDirFormat,
56
from ..mapping import (
61
class TestGitBranch(tests.TestCaseInTempDir):
63
def test_open_by_ref(self):
66
urlutils.local_path_to_url(self.test_dir),
67
urlutils.quote("refs/remotes/origin/unstable", safe='')
69
d = ControlDir.open(url)
71
self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
73
def test_open_existing(self):
75
d = ControlDir.open('.')
76
thebranch = d.create_branch()
77
self.assertIsInstance(thebranch, branch.GitBranch)
81
d = ControlDir.open('.')
82
thebranch = d.create_branch()
84
"<LocalGitBranch('%s/', %r)>" % (
85
urlutils.local_path_to_url(self.test_dir),
89
def test_last_revision_is_null(self):
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())
97
def simple_commit_a(self):
99
self.build_tree(['a'])
101
return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
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())
109
def test_last_revision_info(self):
110
reva = self.simple_commit_a()
111
self.build_tree(['b'])
113
self.addCleanup(r.close)
115
revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
117
thebranch = Branch.open('.')
118
self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(
119
revb)), thebranch.last_revision_info())
121
def test_tag_annotated(self):
122
reva = self.simple_commit_a()
125
o.tagger = b"Jelmer <foo@example.com>"
126
o.message = b"add tag"
127
o.object = (Commit, reva)
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())
139
reva = self.simple_commit_a()
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())
148
class TestWithGitBranch(tests.TestCaseWithTransport):
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()
156
def test_get_parent(self):
157
self.assertIs(None, self.git_branch.get_parent())
159
def test_get_stacked_on_url(self):
160
self.assertRaises(UnstackableBranchFormat,
161
self.git_branch.get_stacked_on_url)
163
def test_get_physical_lock_status(self):
164
self.assertFalse(self.git_branch.get_physical_lock_status())
167
class TestLocalGitBranchFormat(tests.TestCase):
170
super(TestLocalGitBranchFormat, self).setUp()
171
self.format = branch.LocalGitBranchFormat()
173
def test_get_format_description(self):
174
self.assertEqual("Local Git Branch",
175
self.format.get_format_description())
177
def test_get_network_name(self):
178
self.assertEqual(b"git", self.format.network_name())
180
def test_supports_tags(self):
181
self.assertTrue(self.format.supports_tags())
184
class BranchTests(tests.TestCaseInTempDir):
186
def make_onerev_branch(self):
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]
195
return os.path.abspath("d"), gitsha
197
def make_tworev_branch(self):
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")
207
return "d", (marks[mark1], marks[mark2])
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()
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())
222
def test_sprouted_tags(self):
223
path, gitsha = self.make_onerev_branch()
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())
233
def test_sprouted_ghost_tags(self):
234
path, gitsha = self.make_onerev_branch()
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])
246
def test_interbranch_pull_submodule(self):
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"""\
258
url = https://github.com/phhusson/QuasselC.git
260
mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
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)
270
self.assertEqual(revid2, newbranch.last_revision())
272
('https://github.com/phhusson/QuasselC.git', 'core'),
273
newbranch.get_reference_info(newbranch.basis_tree().path2id('core')))
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)
282
self.assertEqual(revid2, newbranch.last_revision())
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)
291
# This is basically "assertNotRaises"
293
self.assertEqual(revid2, newbranch.last_revision())
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())
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))
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')])
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())
331
wt.branch.get_master_branch().last_revision())
334
class ForeignTestsBranchFactory(object):
336
def make_empty_branch(self, transport):
337
d = LocalGitControlDirFormat().initialize_on_transport(transport)
338
return d.create_branch()
340
make_branch = make_empty_branch