1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for interfacing with a Git Branch"""
22
from dulwich.objects import (
26
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
urllib.quote("refs/remotes/origin/unstable", safe='')
70
d = ControlDir.open(url)
72
self.assertEquals(b.ref, "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/', u'master')>" % (
86
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("a", committer="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'])
114
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
116
thebranch = Branch.open('.')
117
self.assertEquals((2, default_mapping.revision_id_foreign_to_bzr(revb)), thebranch.last_revision_info())
119
def test_tag_annotated(self):
120
reva = self.simple_commit_a()
123
o.tagger = "Jelmer <foo@example.com>"
124
o.message = "add tag"
125
o.object = (Commit, reva)
129
r.object_store.add_object(o)
130
r['refs/tags/foo'] = o.id
131
thebranch = Branch.open('.')
132
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
133
thebranch.tags.get_tag_dict())
136
reva = self.simple_commit_a()
138
r.refs["refs/tags/foo"] = reva
139
thebranch = Branch.open('.')
140
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
141
thebranch.tags.get_tag_dict())
145
class TestWithGitBranch(tests.TestCaseWithTransport):
148
tests.TestCaseWithTransport.setUp(self)
149
r = dulwich.repo.Repo.create(self.test_dir)
150
d = ControlDir.open(self.test_dir)
151
self.git_branch = d.create_branch()
153
def test_get_parent(self):
154
self.assertIs(None, self.git_branch.get_parent())
156
def test_get_stacked_on_url(self):
157
self.assertRaises(UnstackableBranchFormat,
158
self.git_branch.get_stacked_on_url)
160
def test_get_physical_lock_status(self):
161
self.assertFalse(self.git_branch.get_physical_lock_status())
164
class TestLocalGitBranchFormat(tests.TestCase):
167
super(TestLocalGitBranchFormat, self).setUp()
168
self.format = branch.LocalGitBranchFormat()
170
def test_get_format_description(self):
171
self.assertEquals("Local Git Branch", self.format.get_format_description())
173
def test_get_network_name(self):
174
self.assertEquals("git", self.format.network_name())
176
def test_supports_tags(self):
177
self.assertTrue(self.format.supports_tags())
180
class BranchTests(tests.TestCaseInTempDir):
182
def make_onerev_branch(self):
186
bb = tests.GitBranchBuilder()
187
bb.set_file("foobar", "foo\nbar\n", False)
188
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
189
gitsha = bb.finish()[mark]
191
return os.path.abspath("d"), gitsha
193
def make_tworev_branch(self):
197
bb = tests.GitBranchBuilder()
198
bb.set_file("foobar", "foo\nbar\n", False)
199
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
200
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
203
return "d", (marks[mark1], marks[mark2])
205
def clone_git_branch(self, from_url, to_url):
206
from_dir = ControlDir.open(from_url)
207
to_dir = from_dir.sprout(to_url)
208
return to_dir.open_branch()
210
def test_single_rev(self):
211
path, gitsha = self.make_onerev_branch()
212
oldrepo = Repository.open(path)
213
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
214
self.assertEquals(gitsha, oldrepo._git.get_refs()["refs/heads/master"])
215
newbranch = self.clone_git_branch(path, "f")
216
self.assertEquals([revid], newbranch.repository.all_revision_ids())
218
def test_sprouted_tags(self):
219
path, gitsha = self.make_onerev_branch()
221
r.refs["refs/tags/lala"] = r.head()
222
oldrepo = Repository.open(path)
223
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
224
newbranch = self.clone_git_branch(path, "f")
225
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
226
self.assertEquals([revid], newbranch.repository.all_revision_ids())
228
def test_interbranch_pull(self):
229
path, (gitsha1, gitsha2) = self.make_tworev_branch()
230
oldrepo = Repository.open(path)
231
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
232
newbranch = self.make_branch('g')
233
inter_branch = InterBranch.get(Branch.open(path), newbranch)
235
self.assertEquals(revid2, newbranch.last_revision())
237
def test_interbranch_pull_noop(self):
238
path, (gitsha1, gitsha2) = self.make_tworev_branch()
239
oldrepo = Repository.open(path)
240
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
241
newbranch = self.make_branch('g')
242
inter_branch = InterBranch.get(Branch.open(path), newbranch)
244
# This is basically "assertNotRaises"
246
self.assertEquals(revid2, newbranch.last_revision())
248
def test_interbranch_pull_stop_revision(self):
249
path, (gitsha1, gitsha2) = self.make_tworev_branch()
250
oldrepo = Repository.open(path)
251
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
252
newbranch = self.make_branch('g')
253
inter_branch = InterBranch.get(Branch.open(path), newbranch)
254
inter_branch.pull(stop_revision=revid1)
255
self.assertEquals(revid1, newbranch.last_revision())
257
def test_interbranch_pull_with_tags(self):
258
path, (gitsha1, gitsha2) = self.make_tworev_branch()
259
gitrepo = GitRepo(path)
260
gitrepo.refs["refs/tags/sometag"] = gitsha2
261
oldrepo = Repository.open(path)
262
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
263
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
264
newbranch = self.make_branch('g')
265
source_branch = Branch.open(path)
266
source_branch.get_config().set_user_option("branch.fetch_tags", True)
267
inter_branch = InterBranch.get(source_branch, newbranch)
268
inter_branch.pull(stop_revision=revid1)
269
self.assertEquals(revid1, newbranch.last_revision())
270
self.assertTrue(newbranch.repository.has_revision(revid2))
273
class ForeignTestsBranchFactory(object):
275
def make_empty_branch(self, transport):
276
d = LocalGitControlDirFormat().initialize_on_transport(transport)
277
return d.create_branch()
279
make_branch = make_empty_branch