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 (
40
from ....branch import (
43
UnstackableBranchFormat,
45
from ....controldir import (
48
from ....repository import (
57
LocalGitControlDirFormat,
59
from ..mapping import (
64
class TestGitBranch(tests.TestCaseInTempDir):
66
def test_open_by_ref(self):
69
urlutils.local_path_to_url(self.test_dir),
70
urllib.quote("refs/remotes/origin/unstable", safe='')
72
d = ControlDir.open(url)
74
self.assertEquals(b.ref, "refs/remotes/origin/unstable")
76
def test_open_existing(self):
78
d = ControlDir.open('.')
79
thebranch = d.create_branch()
80
self.assertIsInstance(thebranch, branch.GitBranch)
84
d = ControlDir.open('.')
85
thebranch = d.create_branch()
87
"<LocalGitBranch('%s/', u'master')>" % (
88
urlutils.local_path_to_url(self.test_dir),),
91
def test_last_revision_is_null(self):
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())
99
def simple_commit_a(self):
100
r = GitRepo.init('.')
101
self.build_tree(['a'])
103
return r.do_commit("a", committer="Somebody <foo@example.com>")
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())
111
def test_last_revision_info(self):
112
reva = self.simple_commit_a()
113
self.build_tree(['b'])
116
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
118
thebranch = Branch.open('.')
119
self.assertEquals((2, default_mapping.revision_id_foreign_to_bzr(revb)), thebranch.last_revision_info())
121
def test_tag_annotated(self):
122
reva = self.simple_commit_a()
125
o.tagger = "Jelmer <foo@example.com>"
126
o.message = "add tag"
127
o.object = (Commit, reva)
131
r.object_store.add_object(o)
132
r['refs/tags/foo'] = o.id
133
thebranch = Branch.open('.')
134
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
135
thebranch.tags.get_tag_dict())
138
reva = self.simple_commit_a()
140
r.refs["refs/tags/foo"] = reva
141
thebranch = Branch.open('.')
142
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
143
thebranch.tags.get_tag_dict())
147
class TestWithGitBranch(tests.TestCaseWithTransport):
150
tests.TestCaseWithTransport.setUp(self)
151
r = dulwich.repo.Repo.create(self.test_dir)
152
d = ControlDir.open(self.test_dir)
153
self.git_branch = d.create_branch()
155
def test_get_parent(self):
156
self.assertIs(None, self.git_branch.get_parent())
158
def test_get_stacked_on_url(self):
159
self.assertRaises(UnstackableBranchFormat,
160
self.git_branch.get_stacked_on_url)
162
def test_get_physical_lock_status(self):
163
self.assertFalse(self.git_branch.get_physical_lock_status())
166
class TestLocalGitBranchFormat(tests.TestCase):
169
super(TestLocalGitBranchFormat, self).setUp()
170
self.format = branch.LocalGitBranchFormat()
172
def test_get_format_description(self):
173
self.assertEquals("Local Git Branch", self.format.get_format_description())
175
def test_get_network_name(self):
176
self.assertEquals("git", self.format.network_name())
178
def test_supports_tags(self):
179
self.assertTrue(self.format.supports_tags())
182
class BranchTests(tests.TestCaseInTempDir):
184
def make_onerev_branch(self):
188
bb = tests.GitBranchBuilder()
189
bb.set_file("foobar", "foo\nbar\n", False)
190
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
191
gitsha = bb.finish()[mark]
193
return os.path.abspath("d"), gitsha
195
def make_tworev_branch(self):
199
bb = tests.GitBranchBuilder()
200
bb.set_file("foobar", "foo\nbar\n", False)
201
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
202
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
205
return "d", (marks[mark1], marks[mark2])
207
def clone_git_branch(self, from_url, to_url):
208
from_dir = ControlDir.open(from_url)
209
to_dir = from_dir.sprout(to_url)
210
return to_dir.open_branch()
212
def test_single_rev(self):
213
path, gitsha = self.make_onerev_branch()
214
oldrepo = Repository.open(path)
215
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
216
self.assertEquals(gitsha, oldrepo._git.get_refs()["refs/heads/master"])
217
newbranch = self.clone_git_branch(path, "f")
218
self.assertEquals([revid], newbranch.repository.all_revision_ids())
220
def test_sprouted_tags(self):
221
path, gitsha = self.make_onerev_branch()
223
r.refs["refs/tags/lala"] = r.head()
224
oldrepo = Repository.open(path)
225
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
226
newbranch = self.clone_git_branch(path, "f")
227
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
228
self.assertEquals([revid], newbranch.repository.all_revision_ids())
230
def test_interbranch_pull(self):
231
path, (gitsha1, gitsha2) = self.make_tworev_branch()
232
oldrepo = Repository.open(path)
233
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
234
newbranch = self.make_branch('g')
235
inter_branch = InterBranch.get(Branch.open(path), newbranch)
237
self.assertEquals(revid2, newbranch.last_revision())
239
def test_interbranch_pull_noop(self):
240
path, (gitsha1, gitsha2) = self.make_tworev_branch()
241
oldrepo = Repository.open(path)
242
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
243
newbranch = self.make_branch('g')
244
inter_branch = InterBranch.get(Branch.open(path), newbranch)
246
# This is basically "assertNotRaises"
248
self.assertEquals(revid2, newbranch.last_revision())
250
def test_interbranch_pull_stop_revision(self):
251
path, (gitsha1, gitsha2) = self.make_tworev_branch()
252
oldrepo = Repository.open(path)
253
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
254
newbranch = self.make_branch('g')
255
inter_branch = InterBranch.get(Branch.open(path), newbranch)
256
inter_branch.pull(stop_revision=revid1)
257
self.assertEquals(revid1, newbranch.last_revision())
259
def test_interbranch_pull_with_tags(self):
260
path, (gitsha1, gitsha2) = self.make_tworev_branch()
261
gitrepo = GitRepo(path)
262
gitrepo.refs["refs/tags/sometag"] = gitsha2
263
oldrepo = Repository.open(path)
264
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
265
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
266
newbranch = self.make_branch('g')
267
source_branch = Branch.open(path)
268
source_branch.get_config().set_user_option("branch.fetch_tags", True)
269
inter_branch = InterBranch.get(source_branch, newbranch)
270
inter_branch.pull(stop_revision=revid1)
271
self.assertEquals(revid1, newbranch.last_revision())
272
self.assertTrue(newbranch.repository.has_revision(revid2))
275
class ForeignTestsBranchFactory(object):
277
def make_empty_branch(self, transport):
278
d = LocalGitControlDirFormat().initialize_on_transport(transport)
279
return d.create_branch()
281
make_branch = make_empty_branch