13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
"""Tests for interfacing with a Git Branch"""
21
from __future__ import absolute_import
22
24
from dulwich.objects import (
36
from bzrlib.branch import (
39
from ...branch import (
40
from bzrlib.bzrdir import (
43
from bzrlib.repository import (
42
UnstackableBranchFormat,
44
from ...controldir import (
47
from ...repository import (
47
from bzrlib.plugins.git import (
48
LocalGitControlDirFormat,
52
from bzrlib.plugins.git.mapping import (
56
LocalGitControlDirFormat,
58
from ..mapping import (
57
63
class TestGitBranch(tests.TestCaseInTempDir):
65
def test_open_by_ref(self):
68
urlutils.local_path_to_url(self.test_dir),
69
urlutils.quote("refs/remotes/origin/unstable", safe='')
71
d = ControlDir.open(url)
73
self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
59
75
def test_open_existing(self):
77
d = ControlDir.open('.')
62
78
thebranch = d.create_branch()
63
79
self.assertIsInstance(thebranch, branch.GitBranch)
65
81
def test_repr(self):
83
d = ControlDir.open('.')
68
84
thebranch = d.create_branch()
69
self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
86
"<LocalGitBranch('%s/', %r)>" % (
87
urlutils.local_path_to_url(self.test_dir),
71
91
def test_last_revision_is_null(self):
73
thedir = BzrDir.open('.')
93
thedir = ControlDir.open('.')
74
94
thebranch = thedir.create_branch()
75
95
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
76
96
self.assertEqual((0, revision.NULL_REVISION),
88
108
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
89
109
thebranch.last_revision())
91
def test_revision_history(self):
111
def test_last_revision_info(self):
92
112
reva = self.simple_commit_a()
93
113
self.build_tree(['b'])
115
self.addCleanup(r.close)
96
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
117
revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
98
119
thebranch = Branch.open('.')
99
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
100
thebranch.revision_history())
120
self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(
121
revb)), thebranch.last_revision_info())
102
123
def test_tag_annotated(self):
103
124
reva = self.simple_commit_a()
106
o.tagger = "Jelmer <foo@example.com>"
107
o.message = "add tag"
127
o.tagger = b"Jelmer <foo@example.com>"
128
o.message = b"add tag"
108
129
o.object = (Commit, reva)
109
130
o.tag_timezone = 0
133
self.addCleanup(r.close)
112
134
r.object_store.add_object(o)
113
r['refs/tags/foo'] = o.id
135
r[b'refs/tags/foo'] = o.id
114
136
thebranch = Branch.open('.')
115
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
116
thebranch.tags.get_tag_dict())
137
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
138
thebranch.tags.get_tag_dict())
118
140
def test_tag(self):
119
141
reva = self.simple_commit_a()
121
r.refs["refs/tags/foo"] = reva
143
self.addCleanup(r.close)
144
r.refs[b"refs/tags/foo"] = reva
122
145
thebranch = Branch.open('.')
123
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
124
thebranch.tags.get_tag_dict())
146
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
147
thebranch.tags.get_tag_dict())
128
150
class TestWithGitBranch(tests.TestCaseWithTransport):
131
153
tests.TestCaseWithTransport.setUp(self)
132
dulwich.repo.Repo.create(self.test_dir)
133
d = BzrDir.open(self.test_dir)
154
r = dulwich.repo.Repo.create(self.test_dir)
155
d = ControlDir.open(self.test_dir)
134
156
self.git_branch = d.create_branch()
136
158
def test_get_parent(self):
137
159
self.assertIs(None, self.git_branch.get_parent())
139
161
def test_get_stacked_on_url(self):
140
self.assertRaises(errors.UnstackableBranchFormat,
141
self.git_branch.get_stacked_on_url)
162
self.assertRaises(UnstackableBranchFormat,
163
self.git_branch.get_stacked_on_url)
143
165
def test_get_physical_lock_status(self):
144
166
self.assertFalse(self.git_branch.get_physical_lock_status())
147
class TestGitBranchFormat(tests.TestCase):
169
class TestLocalGitBranchFormat(tests.TestCase):
150
super(TestGitBranchFormat, self).setUp()
151
self.format = branch.GitBranchFormat()
172
super(TestLocalGitBranchFormat, self).setUp()
173
self.format = branch.LocalGitBranchFormat()
153
175
def test_get_format_description(self):
154
self.assertEquals("Git Branch", self.format.get_format_description())
176
self.assertEqual("Local Git Branch",
177
self.format.get_format_description())
156
179
def test_get_network_name(self):
157
self.assertEquals("git", self.format.network_name())
180
self.assertEqual(b"git", self.format.network_name())
159
182
def test_supports_tags(self):
160
183
self.assertTrue(self.format.supports_tags())
168
191
GitRepo.init('.')
169
192
bb = tests.GitBranchBuilder()
170
bb.set_file("foobar", "foo\nbar\n", False)
171
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
193
bb.set_file("foobar", b"foo\nbar\n", False)
194
mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
172
195
gitsha = bb.finish()[mark]
197
return os.path.abspath("d"), gitsha
176
199
def make_tworev_branch(self):
179
202
GitRepo.init('.')
180
203
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")
204
bb.set_file("foobar", b"foo\nbar\n", False)
205
mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
206
mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
184
207
marks = bb.finish()
186
209
return "d", (marks[mark1], marks[mark2])
188
211
def clone_git_branch(self, from_url, to_url):
189
from_dir = BzrDir.open(from_url)
212
from_dir = ControlDir.open(from_url)
190
213
to_dir = from_dir.sprout(to_url)
191
214
return to_dir.open_branch()
194
217
path, gitsha = self.make_onerev_branch()
195
218
oldrepo = Repository.open(path)
196
219
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
220
self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
197
221
newbranch = self.clone_git_branch(path, "f")
198
self.assertEquals([revid], newbranch.repository.all_revision_ids())
222
self.assertEqual([revid], newbranch.repository.all_revision_ids())
200
224
def test_sprouted_tags(self):
201
225
path, gitsha = self.make_onerev_branch()
202
226
r = GitRepo(path)
203
r.refs["refs/tags/lala"] = r.head()
227
self.addCleanup(r.close)
228
r.refs[b"refs/tags/lala"] = r.head()
204
229
oldrepo = Repository.open(path)
205
230
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
206
231
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())
232
self.assertEqual({"lala": revid}, newbranch.tags.get_tag_dict())
233
self.assertEqual([revid], newbranch.repository.all_revision_ids())
235
def test_sprouted_ghost_tags(self):
236
path, gitsha = self.make_onerev_branch()
238
self.addCleanup(r.close)
239
r.refs[b"refs/tags/lala"] = b"aa" * 20
240
oldrepo = Repository.open(path)
241
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
242
warnings, newbranch = self.callCatchWarnings(
243
self.clone_git_branch, path, "f")
244
self.assertEqual({}, newbranch.tags.get_tag_dict())
245
# Dulwich raises a UserWarning for tags with invalid target
246
self.assertIn(('ref refs/tags/lala points at non-present sha ' + ("aa" * 20), ), [w.args for w in warnings])
210
248
def test_interbranch_pull(self):
211
249
path, (gitsha1, gitsha2) = self.make_tworev_branch()
234
272
newbranch = self.make_branch('g')
235
273
inter_branch = InterBranch.get(Branch.open(path), newbranch)
236
274
inter_branch.pull(stop_revision=revid1)
237
self.assertEquals(revid1, newbranch.last_revision())
275
self.assertEqual(revid1, newbranch.last_revision())
239
def test_interbranch_limited_pull(self):
277
def test_interbranch_pull_with_tags(self):
240
278
path, (gitsha1, gitsha2) = self.make_tworev_branch()
279
gitrepo = GitRepo(path)
280
self.addCleanup(gitrepo.close)
281
gitrepo.refs[b"refs/tags/sometag"] = gitsha2
241
282
oldrepo = Repository.open(path)
242
283
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
243
284
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
244
285
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())
286
source_branch = Branch.open(path)
287
source_branch.get_config().set_user_option("branch.fetch_tags", True)
288
inter_branch = InterBranch.get(source_branch, newbranch)
289
inter_branch.pull(stop_revision=revid1)
290
self.assertEqual(revid1, newbranch.last_revision())
291
self.assertTrue(newbranch.repository.has_revision(revid2))
293
def test_bzr_branch_bound_to_git(self):
294
path, (gitsha1, gitsha2) = self.make_tworev_branch()
295
wt = Branch.open(path).create_checkout('co')
296
self.build_tree_contents([('co/foobar', b'blah')])
298
errors.NoRoundtrippingSupport, wt.commit,
299
'commit from bound branch.')
300
revid = wt.commit('commit from bound branch.', lossy=True)
301
self.assertEqual(revid, wt.branch.last_revision())
304
wt.branch.get_master_branch().last_revision())
252
307
class ForeignTestsBranchFactory(object):