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"""
21
from __future__ import absolute_import
24
from dulwich.objects import (
28
from dulwich.repo import (
38
from ...branch import (
41
UnstackableBranchFormat,
43
from ...controldir import (
46
from ...repository import (
55
LocalGitControlDirFormat,
57
from ..mapping import (
62
class TestGitBranch(tests.TestCaseInTempDir):
64
def test_open_by_ref(self):
67
urlutils.local_path_to_url(self.test_dir),
68
urlutils.quote("refs/remotes/origin/unstable", safe='')
70
d = ControlDir.open(url)
72
self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
74
def test_open_existing(self):
76
d = ControlDir.open('.')
77
thebranch = d.create_branch()
78
self.assertIsInstance(thebranch, branch.GitBranch)
82
d = ControlDir.open('.')
83
thebranch = d.create_branch()
85
"<LocalGitBranch('%s/', %r)>" % (
86
urlutils.local_path_to_url(self.test_dir),
90
def test_last_revision_is_null(self):
92
thedir = ControlDir.open('.')
93
thebranch = thedir.create_branch()
94
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
95
self.assertEqual((0, revision.NULL_REVISION),
96
thebranch.last_revision_info())
98
def simple_commit_a(self):
100
self.build_tree(['a'])
102
return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
104
def test_last_revision_is_valid(self):
105
head = self.simple_commit_a()
106
thebranch = Branch.open('.')
107
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
108
thebranch.last_revision())
110
def test_last_revision_info(self):
111
reva = self.simple_commit_a()
112
self.build_tree(['b'])
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(revb)), thebranch.last_revision_info())
120
def test_tag_annotated(self):
121
reva = self.simple_commit_a()
124
o.tagger = b"Jelmer <foo@example.com>"
125
o.message = b"add tag"
126
o.object = (Commit, reva)
130
r.object_store.add_object(o)
131
r[b'refs/tags/foo'] = o.id
132
thebranch = Branch.open('.')
133
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
134
thebranch.tags.get_tag_dict())
137
reva = self.simple_commit_a()
139
r.refs[b"refs/tags/foo"] = reva
140
thebranch = Branch.open('.')
141
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
142
thebranch.tags.get_tag_dict())
146
class TestWithGitBranch(tests.TestCaseWithTransport):
149
tests.TestCaseWithTransport.setUp(self)
150
r = dulwich.repo.Repo.create(self.test_dir)
151
d = ControlDir.open(self.test_dir)
152
self.git_branch = d.create_branch()
154
def test_get_parent(self):
155
self.assertIs(None, self.git_branch.get_parent())
157
def test_get_stacked_on_url(self):
158
self.assertRaises(UnstackableBranchFormat,
159
self.git_branch.get_stacked_on_url)
161
def test_get_physical_lock_status(self):
162
self.assertFalse(self.git_branch.get_physical_lock_status())
165
class TestLocalGitBranchFormat(tests.TestCase):
168
super(TestLocalGitBranchFormat, self).setUp()
169
self.format = branch.LocalGitBranchFormat()
171
def test_get_format_description(self):
172
self.assertEqual("Local Git Branch", self.format.get_format_description())
174
def test_get_network_name(self):
175
self.assertEqual(b"git", self.format.network_name())
177
def test_supports_tags(self):
178
self.assertTrue(self.format.supports_tags())
181
class BranchTests(tests.TestCaseInTempDir):
183
def make_onerev_branch(self):
187
bb = tests.GitBranchBuilder()
188
bb.set_file("foobar", b"foo\nbar\n", False)
189
mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
190
gitsha = bb.finish()[mark]
192
return os.path.abspath("d"), gitsha
194
def make_tworev_branch(self):
198
bb = tests.GitBranchBuilder()
199
bb.set_file("foobar", b"foo\nbar\n", False)
200
mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
201
mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
204
return "d", (marks[mark1], marks[mark2])
206
def clone_git_branch(self, from_url, to_url):
207
from_dir = ControlDir.open(from_url)
208
to_dir = from_dir.sprout(to_url)
209
return to_dir.open_branch()
211
def test_single_rev(self):
212
path, gitsha = self.make_onerev_branch()
213
oldrepo = Repository.open(path)
214
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
215
self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
216
newbranch = self.clone_git_branch(path, "f")
217
self.assertEqual([revid], newbranch.repository.all_revision_ids())
219
def test_sprouted_tags(self):
220
path, gitsha = self.make_onerev_branch()
222
r.refs[b"refs/tags/lala"] = r.head()
223
oldrepo = Repository.open(path)
224
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
225
newbranch = self.clone_git_branch(path, "f")
226
self.assertEqual({"lala": revid}, newbranch.tags.get_tag_dict())
227
self.assertEqual([revid], newbranch.repository.all_revision_ids())
229
def test_interbranch_pull(self):
230
path, (gitsha1, gitsha2) = self.make_tworev_branch()
231
oldrepo = Repository.open(path)
232
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
233
newbranch = self.make_branch('g')
234
inter_branch = InterBranch.get(Branch.open(path), newbranch)
236
self.assertEqual(revid2, newbranch.last_revision())
238
def test_interbranch_pull_noop(self):
239
path, (gitsha1, gitsha2) = self.make_tworev_branch()
240
oldrepo = Repository.open(path)
241
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
242
newbranch = self.make_branch('g')
243
inter_branch = InterBranch.get(Branch.open(path), newbranch)
245
# This is basically "assertNotRaises"
247
self.assertEqual(revid2, newbranch.last_revision())
249
def test_interbranch_pull_stop_revision(self):
250
path, (gitsha1, gitsha2) = self.make_tworev_branch()
251
oldrepo = Repository.open(path)
252
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
253
newbranch = self.make_branch('g')
254
inter_branch = InterBranch.get(Branch.open(path), newbranch)
255
inter_branch.pull(stop_revision=revid1)
256
self.assertEqual(revid1, newbranch.last_revision())
258
def test_interbranch_pull_with_tags(self):
259
path, (gitsha1, gitsha2) = self.make_tworev_branch()
260
gitrepo = GitRepo(path)
261
gitrepo.refs[b"refs/tags/sometag"] = gitsha2
262
oldrepo = Repository.open(path)
263
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
264
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
265
newbranch = self.make_branch('g')
266
source_branch = Branch.open(path)
267
source_branch.get_config().set_user_option("branch.fetch_tags", True)
268
inter_branch = InterBranch.get(source_branch, newbranch)
269
inter_branch.pull(stop_revision=revid1)
270
self.assertEqual(revid1, newbranch.last_revision())
271
self.assertTrue(newbranch.repository.has_revision(revid2))
274
class ForeignTestsBranchFactory(object):
276
def make_empty_branch(self, transport):
277
d = LocalGitControlDirFormat().initialize_on_transport(transport)
278
return d.create_branch()
280
make_branch = make_empty_branch