14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
"""Tests for interfacing with a Git Branch"""
19
from bzrlib import revision
20
from bzrlib.branch import Branch
22
from dulwich.objects import (
26
from dulwich.repo import (
36
from bzrlib.branch import (
40
from bzrlib.bzrdir import (
43
from bzrlib.repository import (
22
47
from bzrlib.plugins.git import (
48
LocalGitControlDirFormat,
26
from bzrlib.plugins.git.mapping import default_mapping
52
from bzrlib.plugins.git.mapping import (
31
57
class TestGitBranch(tests.TestCaseInTempDir):
33
_test_needs_features = [tests.GitCommandFeature]
35
59
def test_open_existing(self):
38
thebranch = Branch.open('.')
62
thebranch = d.create_branch()
39
63
self.assertIsInstance(thebranch, branch.GitBranch)
68
thebranch = d.create_branch()
69
self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
41
71
def test_last_revision_is_null(self):
44
thebranch = Branch.open('.')
73
thedir = BzrDir.open('.')
74
thebranch = thedir.create_branch()
45
75
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
46
76
self.assertEqual((0, revision.NULL_REVISION),
47
77
thebranch.last_revision_info())
49
79
def simple_commit_a(self):
51
81
self.build_tree(['a'])
52
tests.run_git('add', 'a')
53
tests.run_git('commit', '-m', 'a')
83
return r.do_commit("a", committer="Somebody <foo@example.com>")
55
85
def test_last_revision_is_valid(self):
56
self.simple_commit_a()
57
head = tests.run_git('rev-parse', 'HEAD').strip()
86
head = self.simple_commit_a()
59
87
thebranch = Branch.open('.')
60
88
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
61
89
thebranch.last_revision())
63
91
def test_revision_history(self):
64
self.simple_commit_a()
65
reva = tests.run_git('rev-parse', 'HEAD').strip()
92
reva = self.simple_commit_a()
66
93
self.build_tree(['b'])
67
tests.run_git('add', 'b')
68
tests.run_git('commit', '-m', 'b')
69
revb = tests.run_git('rev-parse', 'HEAD').strip()
96
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
71
98
thebranch = Branch.open('.')
72
99
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
73
100
thebranch.revision_history())
76
self.simple_commit_a()
77
reva = tests.run_git('rev-parse', 'HEAD').strip()
79
tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
81
thebranch = Branch.open('.')
82
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
83
thebranch.tags.get_tag_dict())
102
def test_tag_annotated(self):
103
reva = self.simple_commit_a()
106
o.tagger = "Jelmer <foo@example.com>"
107
o.message = "add tag"
108
o.object = (Commit, reva)
112
r.object_store.add_object(o)
113
r['refs/tags/foo'] = o.id
114
thebranch = Branch.open('.')
115
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
116
thebranch.tags.get_tag_dict())
119
reva = self.simple_commit_a()
121
r.refs["refs/tags/foo"] = reva
122
thebranch = Branch.open('.')
123
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
124
thebranch.tags.get_tag_dict())
86
128
class TestWithGitBranch(tests.TestCaseWithTransport):
89
131
tests.TestCaseWithTransport.setUp(self)
90
git.repo.Repo.create(self.test_dir)
91
self.git_branch = Branch.open(self.test_dir)
132
dulwich.repo.Repo.create(self.test_dir)
133
d = BzrDir.open(self.test_dir)
134
self.git_branch = d.create_branch()
93
136
def test_get_parent(self):
94
137
self.assertIs(None, self.git_branch.get_parent())
96
139
def test_get_stacked_on_url(self):
97
self.assertIs(None, self.git_branch.get_stacked_on_url())
140
self.assertRaises(errors.UnstackableBranchFormat,
141
self.git_branch.get_stacked_on_url)
99
143
def test_get_physical_lock_status(self):
100
144
self.assertFalse(self.git_branch.get_physical_lock_status())
109
153
def test_get_format_description(self):
110
154
self.assertEquals("Git Branch", self.format.get_format_description())
156
def test_get_network_name(self):
157
self.assertEquals("git", self.format.network_name())
159
def test_supports_tags(self):
160
self.assertTrue(self.format.supports_tags())
163
class BranchTests(tests.TestCaseInTempDir):
165
def make_onerev_branch(self):
169
bb = tests.GitBranchBuilder()
170
bb.set_file("foobar", "foo\nbar\n", False)
171
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
172
gitsha = bb.finish()[mark]
176
def make_tworev_branch(self):
180
bb = tests.GitBranchBuilder()
181
bb.set_file("foobar", "foo\nbar\n", False)
182
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
183
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
186
return "d", (marks[mark1], marks[mark2])
188
def clone_git_branch(self, from_url, to_url):
189
from_dir = BzrDir.open(from_url)
190
to_dir = from_dir.sprout(to_url)
191
return to_dir.open_branch()
193
def test_single_rev(self):
194
path, gitsha = self.make_onerev_branch()
195
oldrepo = Repository.open(path)
196
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
197
newbranch = self.clone_git_branch(path, "f")
198
self.assertEquals([revid], newbranch.repository.all_revision_ids())
200
def test_sprouted_tags(self):
201
path, gitsha = self.make_onerev_branch()
203
r.refs["refs/tags/lala"] = r.head()
204
oldrepo = Repository.open(path)
205
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
206
newbranch = self.clone_git_branch(path, "f")
207
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
208
self.assertEquals([revid], newbranch.repository.all_revision_ids())
210
def test_interbranch_pull(self):
211
path, (gitsha1, gitsha2) = self.make_tworev_branch()
212
oldrepo = Repository.open(path)
213
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
214
newbranch = self.make_branch('g')
215
inter_branch = InterBranch.get(Branch.open(path), newbranch)
217
self.assertEquals(revid2, newbranch.last_revision())
219
def test_interbranch_pull_noop(self):
220
path, (gitsha1, gitsha2) = self.make_tworev_branch()
221
oldrepo = Repository.open(path)
222
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
223
newbranch = self.make_branch('g')
224
inter_branch = InterBranch.get(Branch.open(path), newbranch)
226
# This is basically "assertNotRaises"
228
self.assertEquals(revid2, newbranch.last_revision())
230
def test_interbranch_pull_stop_revision(self):
231
path, (gitsha1, gitsha2) = self.make_tworev_branch()
232
oldrepo = Repository.open(path)
233
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
234
newbranch = self.make_branch('g')
235
inter_branch = InterBranch.get(Branch.open(path), newbranch)
236
inter_branch.pull(stop_revision=revid1)
237
self.assertEquals(revid1, newbranch.last_revision())
239
def test_interbranch_limited_pull(self):
240
path, (gitsha1, gitsha2) = self.make_tworev_branch()
241
oldrepo = Repository.open(path)
242
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
243
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
244
newbranch = self.make_branch('g')
245
inter_branch = InterBranch.get(Branch.open(path), newbranch)
246
inter_branch.pull(limit=1)
247
self.assertEquals(revid1, newbranch.last_revision())
248
inter_branch.pull(limit=1)
249
self.assertEquals(revid2, newbranch.last_revision())
252
class ForeignTestsBranchFactory(object):
254
def make_empty_branch(self, transport):
255
d = LocalGitControlDirFormat().initialize_on_transport(transport)
256
return d.create_branch()
258
make_branch = make_empty_branch