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 (
36
from bzrlib.branch import (
41
from bzrlib.controldir import (
46
from bzrlib.bzrdir import (
50
from bzrlib.repository import (
54
from bzrlib.plugins.git import (
55
LocalGitControlDirFormat,
59
from bzrlib.plugins.git.mapping import (
64
class TestGitBranch(tests.TestCaseInTempDir):
66
def test_open_existing(self):
68
d = ControlDir.open('.')
69
thebranch = d.create_branch()
70
self.assertIsInstance(thebranch, branch.GitBranch)
74
d = ControlDir.open('.')
75
thebranch = d.create_branch()
76
self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
78
def test_last_revision_is_null(self):
80
thedir = ControlDir.open('.')
81
thebranch = thedir.create_branch()
82
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
83
self.assertEqual((0, revision.NULL_REVISION),
84
thebranch.last_revision_info())
86
def simple_commit_a(self):
88
self.build_tree(['a'])
90
return r.do_commit("a", committer="Somebody <foo@example.com>")
92
def test_last_revision_is_valid(self):
93
head = self.simple_commit_a()
94
thebranch = Branch.open('.')
95
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
96
thebranch.last_revision())
98
def test_revision_history(self):
99
reva = self.simple_commit_a()
100
self.build_tree(['b'])
103
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
105
thebranch = Branch.open('.')
106
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
107
thebranch.revision_history())
109
def test_tag_annotated(self):
110
reva = self.simple_commit_a()
113
o.tagger = "Jelmer <foo@example.com>"
114
o.message = "add tag"
115
o.object = (Commit, reva)
119
r.object_store.add_object(o)
120
r['refs/tags/foo'] = o.id
121
thebranch = Branch.open('.')
122
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
123
thebranch.tags.get_tag_dict())
126
reva = self.simple_commit_a()
128
r.refs["refs/tags/foo"] = reva
129
thebranch = Branch.open('.')
130
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
131
thebranch.tags.get_tag_dict())
135
class TestWithGitBranch(tests.TestCaseWithTransport):
138
tests.TestCaseWithTransport.setUp(self)
139
dulwich.repo.Repo.create(self.test_dir)
140
d = ControlDir.open(self.test_dir)
141
self.git_branch = d.create_branch()
143
def test_get_parent(self):
144
self.assertIs(None, self.git_branch.get_parent())
146
def test_get_stacked_on_url(self):
147
self.assertRaises(errors.UnstackableBranchFormat,
148
self.git_branch.get_stacked_on_url)
150
def test_get_physical_lock_status(self):
151
self.assertFalse(self.git_branch.get_physical_lock_status())
154
class TestGitBranchFormat(tests.TestCase):
157
super(TestGitBranchFormat, self).setUp()
158
self.format = branch.GitBranchFormat()
160
def test_get_format_description(self):
161
self.assertEquals("Git Branch", self.format.get_format_description())
163
def test_get_network_name(self):
164
self.assertEquals("git", self.format.network_name())
166
def test_supports_tags(self):
167
self.assertTrue(self.format.supports_tags())
170
class BranchTests(tests.TestCaseInTempDir):
172
def make_onerev_branch(self):
176
bb = tests.GitBranchBuilder()
177
bb.set_file("foobar", "foo\nbar\n", False)
178
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
179
gitsha = bb.finish()[mark]
183
def make_tworev_branch(self):
187
bb = tests.GitBranchBuilder()
188
bb.set_file("foobar", "foo\nbar\n", False)
189
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
190
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
193
return "d", (marks[mark1], marks[mark2])
195
def clone_git_branch(self, from_url, to_url):
196
from_dir = ControlDir.open(from_url)
197
to_dir = from_dir.sprout(to_url)
198
return to_dir.open_branch()
200
def test_single_rev(self):
201
path, gitsha = self.make_onerev_branch()
202
oldrepo = Repository.open(path)
203
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
204
newbranch = self.clone_git_branch(path, "f")
205
self.assertEquals([revid], newbranch.repository.all_revision_ids())
207
def test_sprouted_tags(self):
208
path, gitsha = self.make_onerev_branch()
210
r.refs["refs/tags/lala"] = r.head()
211
oldrepo = Repository.open(path)
212
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
213
newbranch = self.clone_git_branch(path, "f")
214
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
215
self.assertEquals([revid], newbranch.repository.all_revision_ids())
217
def test_interbranch_pull(self):
218
path, (gitsha1, gitsha2) = self.make_tworev_branch()
219
oldrepo = Repository.open(path)
220
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
221
newbranch = self.make_branch('g')
222
inter_branch = InterBranch.get(Branch.open(path), newbranch)
224
self.assertEquals(revid2, newbranch.last_revision())
226
def test_interbranch_pull_noop(self):
227
path, (gitsha1, gitsha2) = self.make_tworev_branch()
228
oldrepo = Repository.open(path)
229
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
230
newbranch = self.make_branch('g')
231
inter_branch = InterBranch.get(Branch.open(path), newbranch)
233
# This is basically "assertNotRaises"
235
self.assertEquals(revid2, newbranch.last_revision())
237
def test_interbranch_pull_stop_revision(self):
238
path, (gitsha1, gitsha2) = self.make_tworev_branch()
239
oldrepo = Repository.open(path)
240
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
241
newbranch = self.make_branch('g')
242
inter_branch = InterBranch.get(Branch.open(path), newbranch)
243
inter_branch.pull(stop_revision=revid1)
244
self.assertEquals(revid1, newbranch.last_revision())
246
def test_interbranch_limited_pull(self):
247
path, (gitsha1, gitsha2) = self.make_tworev_branch()
248
oldrepo = Repository.open(path)
249
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
250
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
251
newbranch = self.make_branch('g')
252
inter_branch = InterBranch.get(Branch.open(path), newbranch)
253
inter_branch.pull(limit=1)
254
self.assertEquals(revid1, newbranch.last_revision())
255
inter_branch.pull(limit=1)
256
self.assertEquals(revid2, newbranch.last_revision())
259
class ForeignTestsBranchFactory(object):
261
def make_empty_branch(self, transport):
262
d = LocalGitControlDirFormat().initialize_on_transport(transport)
263
return d.create_branch()
265
make_branch = make_empty_branch