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.repo import (
32
from bzrlib.branch import (
36
from bzrlib.bzrdir import (
39
from bzrlib.repository import (
43
from bzrlib.plugins.git import (
48
from bzrlib.plugins.git.mapping import (
53
class TestGitBranch(tests.TestCaseInTempDir):
55
_test_needs_features = [tests.GitCommandFeature]
57
def test_open_existing(self):
60
thebranch = Branch.open('.')
61
self.assertIsInstance(thebranch, branch.GitBranch)
65
thebranch = Branch.open('.')
66
self.assertEquals("LocalGitBranch('file://%s/', 'HEAD')" % self.test_dir, repr(thebranch))
68
def test_last_revision_is_null(self):
71
thebranch = Branch.open('.')
72
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
73
self.assertEqual((0, revision.NULL_REVISION),
74
thebranch.last_revision_info())
76
def simple_commit_a(self):
78
self.build_tree(['a'])
79
tests.run_git('add', 'a')
80
tests.run_git('commit', '-m', 'a')
82
def test_last_revision_is_valid(self):
83
self.simple_commit_a()
84
head = tests.run_git('rev-parse', 'HEAD').strip()
86
thebranch = Branch.open('.')
87
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
88
thebranch.last_revision())
90
def test_revision_history(self):
91
self.simple_commit_a()
92
reva = tests.run_git('rev-parse', 'HEAD').strip()
93
self.build_tree(['b'])
94
tests.run_git('add', 'b')
95
tests.run_git('commit', '-m', 'b')
96
revb = tests.run_git('rev-parse', 'HEAD').strip()
98
thebranch = Branch.open('.')
99
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
100
thebranch.revision_history())
102
def test_tag_annotated(self):
103
self.simple_commit_a()
104
reva = tests.run_git('rev-parse', 'HEAD').strip()
105
tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
106
thebranch = Branch.open('.')
107
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
108
thebranch.tags.get_tag_dict())
111
self.simple_commit_a()
112
reva = tests.run_git('rev-parse', 'HEAD').strip()
113
tests.run_git('tag', '-m', 'add tag', 'foo')
114
thebranch = Branch.open('.')
115
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
116
thebranch.tags.get_tag_dict())
120
class TestWithGitBranch(tests.TestCaseWithTransport):
123
tests.TestCaseWithTransport.setUp(self)
124
dulwich.repo.Repo.create(self.test_dir)
125
self.git_branch = Branch.open(self.test_dir)
127
def test_get_parent(self):
128
self.assertIs(None, self.git_branch.get_parent())
130
def test_get_stacked_on_url(self):
131
self.assertRaises(errors.UnstackableBranchFormat,
132
self.git_branch.get_stacked_on_url)
134
def test_get_physical_lock_status(self):
135
self.assertFalse(self.git_branch.get_physical_lock_status())
138
class TestGitBranchFormat(tests.TestCase):
141
super(TestGitBranchFormat, self).setUp()
142
self.format = branch.GitBranchFormat()
144
def test_get_format_description(self):
145
self.assertEquals("Git Branch", self.format.get_format_description())
149
class BranchTests(tests.TestCaseInTempDir):
151
def make_onerev_branch(self):
155
bb = tests.GitBranchBuilder()
156
bb.set_file("foobar", "foo\nbar\n", False)
157
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
158
gitsha = bb.finish()[mark]
162
def make_tworev_branch(self):
166
bb = tests.GitBranchBuilder()
167
bb.set_file("foobar", "foo\nbar\n", False)
168
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
169
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
172
return "d", (marks[mark1], marks[mark2])
174
def clone_git_branch(self, from_url, to_url):
175
from_dir = BzrDir.open(from_url)
176
to_dir = from_dir.sprout(to_url)
177
return to_dir.open_branch()
179
def test_single_rev(self):
180
path, gitsha = self.make_onerev_branch()
181
oldrepo = Repository.open(path)
182
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
183
newbranch = self.clone_git_branch(path, "f")
184
self.assertEquals([revid], newbranch.repository.all_revision_ids())
186
def test_sprouted_tags(self):
187
path, gitsha = self.make_onerev_branch()
189
tests.run_git("tag", "lala")
190
os.chdir(self.test_dir)
191
oldrepo = Repository.open(path)
192
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
193
newbranch = self.clone_git_branch(path, "f")
194
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
195
self.assertEquals([revid], newbranch.repository.all_revision_ids())
197
def test_interbranch_pull(self):
198
path, (gitsha1, gitsha2) = self.make_tworev_branch()
199
oldrepo = Repository.open(path)
200
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
201
newbranch = self.make_branch('g')
202
inter_branch = InterBranch.get(Branch.open(path), newbranch)
204
self.assertEquals(revid2, newbranch.last_revision())
206
def test_interbranch_pull_noop(self):
207
path, (gitsha1, gitsha2) = self.make_tworev_branch()
208
oldrepo = Repository.open(path)
209
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
210
newbranch = self.make_branch('g')
211
inter_branch = InterBranch.get(Branch.open(path), newbranch)
213
# This is basically "assertNotRaises"
215
self.assertEquals(revid2, newbranch.last_revision())
217
def test_interbranch_pull_stop_revision(self):
218
path, (gitsha1, gitsha2) = self.make_tworev_branch()
219
oldrepo = Repository.open(path)
220
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
221
newbranch = self.make_branch('g')
222
inter_branch = InterBranch.get(Branch.open(path), newbranch)
223
inter_branch.pull(stop_revision=revid1)
224
self.assertEquals(revid1, newbranch.last_revision())
226
def test_interbranch_limited_pull(self):
227
path, (gitsha1, gitsha2) = self.make_tworev_branch()
228
oldrepo = Repository.open(path)
229
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
230
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
231
newbranch = self.make_branch('g')
232
inter_branch = InterBranch.get(Branch.open(path), newbranch)
233
inter_branch.pull(limit=1)
234
self.assertEquals(revid1, newbranch.last_revision())
235
inter_branch.pull(limit=1)
236
self.assertEquals(revid2, newbranch.last_revision())
239
class ForeignTestsBranchFactory(object):
241
def make_empty_branch(self, transport):
242
return LocalGitBzrDirFormat().initialize_on_transport(transport).open_branch()
244
make_branch = make_empty_branch