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
17
19
"""Tests for interfacing with a Git Branch"""
19
from bzrlib import revision
20
from bzrlib.branch import Branch
22
from bzrlib.plugins.git import (
22
from dulwich.objects import (
26
from dulwich.repo import (
37
from ...branch import (
40
UnstackableBranchFormat,
42
from ...controldir import (
45
from ...repository import (
26
from bzrlib.plugins.git.mapping import default_mapping
27
from bzrlib.plugins.git import git
54
LocalGitControlDirFormat,
56
from ..mapping import (
31
61
class TestGitBranch(tests.TestCaseInTempDir):
33
_test_needs_features = [tests.GitCommandFeature]
63
def test_open_by_ref(self):
66
urlutils.local_path_to_url(self.test_dir),
67
urlutils.quote("refs/remotes/origin/unstable", safe='')
69
d = ControlDir.open(url)
71
self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
35
73
def test_open_existing(self):
38
thebranch = Branch.open('.')
75
d = ControlDir.open('.')
76
thebranch = d.create_branch()
39
77
self.assertIsInstance(thebranch, branch.GitBranch)
81
d = ControlDir.open('.')
82
thebranch = d.create_branch()
84
"<LocalGitBranch('%s/', %r)>" % (
85
urlutils.local_path_to_url(self.test_dir),
41
89
def test_last_revision_is_null(self):
44
thebranch = Branch.open('.')
91
thedir = ControlDir.open('.')
92
thebranch = thedir.create_branch()
45
93
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
46
94
self.assertEqual((0, revision.NULL_REVISION),
47
95
thebranch.last_revision_info())
49
97
def simple_commit_a(self):
51
99
self.build_tree(['a'])
52
tests.run_git('add', 'a')
53
tests.run_git('commit', '-m', 'a')
101
return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
55
103
def test_last_revision_is_valid(self):
56
self.simple_commit_a()
57
head = tests.run_git('rev-parse', 'HEAD').strip()
104
head = self.simple_commit_a()
59
105
thebranch = Branch.open('.')
60
106
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
61
107
thebranch.last_revision())
63
def test_revision_history(self):
64
self.simple_commit_a()
65
reva = tests.run_git('rev-parse', 'HEAD').strip()
109
def test_last_revision_info(self):
110
reva = self.simple_commit_a()
66
111
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()
71
thebranch = Branch.open('.')
72
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
73
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
newid = open('.git/refs/tags/foo').read().rstrip()
83
thebranch = Branch.open('.')
84
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(newid)},
85
thebranch.tags.get_tag_dict())
113
self.addCleanup(r.close)
115
revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
117
thebranch = Branch.open('.')
118
self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(
119
revb)), thebranch.last_revision_info())
121
def test_tag_annotated(self):
122
reva = self.simple_commit_a()
125
o.tagger = b"Jelmer <foo@example.com>"
126
o.message = b"add tag"
127
o.object = (Commit, reva)
131
self.addCleanup(r.close)
132
r.object_store.add_object(o)
133
r[b'refs/tags/foo'] = o.id
134
thebranch = Branch.open('.')
135
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
136
thebranch.tags.get_tag_dict())
139
reva = self.simple_commit_a()
141
self.addCleanup(r.close)
142
r.refs[b"refs/tags/foo"] = reva
143
thebranch = Branch.open('.')
144
self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
145
thebranch.tags.get_tag_dict())
88
148
class TestWithGitBranch(tests.TestCaseWithTransport):
91
151
tests.TestCaseWithTransport.setUp(self)
92
git.repo.Repo.create(self.test_dir)
93
self.git_branch = Branch.open(self.test_dir)
152
r = dulwich.repo.Repo.create(self.test_dir)
153
d = ControlDir.open(self.test_dir)
154
self.git_branch = d.create_branch()
95
156
def test_get_parent(self):
96
157
self.assertIs(None, self.git_branch.get_parent())
98
159
def test_get_stacked_on_url(self):
99
self.assertIs(None, self.git_branch.get_stacked_on_url())
160
self.assertRaises(UnstackableBranchFormat,
161
self.git_branch.get_stacked_on_url)
101
163
def test_get_physical_lock_status(self):
102
164
self.assertFalse(self.git_branch.get_physical_lock_status())
105
class TestGitBranchFormat(tests.TestCase):
167
class TestLocalGitBranchFormat(tests.TestCase):
108
super(TestGitBranchFormat, self).setUp()
109
self.format = branch.GitBranchFormat()
170
super(TestLocalGitBranchFormat, self).setUp()
171
self.format = branch.LocalGitBranchFormat()
111
173
def test_get_format_description(self):
112
self.assertEquals("Git Branch", self.format.get_format_description())
174
self.assertEqual("Local Git Branch",
175
self.format.get_format_description())
177
def test_get_network_name(self):
178
self.assertEqual(b"git", self.format.network_name())
180
def test_supports_tags(self):
181
self.assertTrue(self.format.supports_tags())
184
class BranchTests(tests.TestCaseInTempDir):
186
def make_onerev_branch(self):
190
bb = tests.GitBranchBuilder()
191
bb.set_file("foobar", b"foo\nbar\n", False)
192
mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
193
gitsha = bb.finish()[mark]
195
return os.path.abspath("d"), gitsha
197
def make_tworev_branch(self):
201
bb = tests.GitBranchBuilder()
202
bb.set_file("foobar", b"foo\nbar\n", False)
203
mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
204
mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
207
return "d", (marks[mark1], marks[mark2])
209
def clone_git_branch(self, from_url, to_url):
210
from_dir = ControlDir.open(from_url)
211
to_dir = from_dir.sprout(to_url)
212
return to_dir.open_branch()
214
def test_single_rev(self):
215
path, gitsha = self.make_onerev_branch()
216
oldrepo = Repository.open(path)
217
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
218
self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
219
newbranch = self.clone_git_branch(path, "f")
220
self.assertEqual([revid], newbranch.repository.all_revision_ids())
222
def test_sprouted_tags(self):
223
path, gitsha = self.make_onerev_branch()
225
self.addCleanup(r.close)
226
r.refs[b"refs/tags/lala"] = r.head()
227
oldrepo = Repository.open(path)
228
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
229
newbranch = self.clone_git_branch(path, "f")
230
self.assertEqual({"lala": revid}, newbranch.tags.get_tag_dict())
231
self.assertEqual([revid], newbranch.repository.all_revision_ids())
233
def test_sprouted_ghost_tags(self):
234
path, gitsha = self.make_onerev_branch()
236
self.addCleanup(r.close)
237
r.refs[b"refs/tags/lala"] = b"aa" * 20
238
oldrepo = Repository.open(path)
239
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
240
warnings, newbranch = self.callCatchWarnings(
241
self.clone_git_branch, path, "f")
242
self.assertEqual({}, newbranch.tags.get_tag_dict())
243
# Dulwich raises a UserWarning for tags with invalid target
244
self.assertIn(('ref refs/tags/lala points at non-present sha ' + ("aa" * 20), ), [w.args for w in warnings])
246
def test_interbranch_pull_submodule(self):
251
bb = tests.GitBranchBuilder()
252
bb.set_file("foobar", b"foo\nbar\n", False)
253
mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
254
bb.set_submodule("core", b'102ee7206ebc4227bec8ac02450972e6738f4a33')
255
bb.set_file('.gitmodules', b"""\
258
url = https://github.com/phhusson/QuasselC.git
260
mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
263
gitsha1 = marks[mark1]
264
gitsha2 = marks[mark2]
265
oldrepo = Repository.open(path)
266
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
267
newbranch = self.make_branch('g')
268
inter_branch = InterBranch.get(Branch.open(path), newbranch)
270
self.assertEqual(revid2, newbranch.last_revision())
272
('https://github.com/phhusson/QuasselC.git', 'core'),
273
newbranch.get_reference_info(newbranch.basis_tree().path2id('core')))
275
def test_interbranch_pull(self):
276
path, (gitsha1, gitsha2) = self.make_tworev_branch()
277
oldrepo = Repository.open(path)
278
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
279
newbranch = self.make_branch('g')
280
inter_branch = InterBranch.get(Branch.open(path), newbranch)
282
self.assertEqual(revid2, newbranch.last_revision())
284
def test_interbranch_pull_noop(self):
285
path, (gitsha1, gitsha2) = self.make_tworev_branch()
286
oldrepo = Repository.open(path)
287
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
288
newbranch = self.make_branch('g')
289
inter_branch = InterBranch.get(Branch.open(path), newbranch)
291
# This is basically "assertNotRaises"
293
self.assertEqual(revid2, newbranch.last_revision())
295
def test_interbranch_pull_stop_revision(self):
296
path, (gitsha1, gitsha2) = self.make_tworev_branch()
297
oldrepo = Repository.open(path)
298
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
299
newbranch = self.make_branch('g')
300
inter_branch = InterBranch.get(Branch.open(path), newbranch)
301
inter_branch.pull(stop_revision=revid1)
302
self.assertEqual(revid1, newbranch.last_revision())
304
def test_interbranch_pull_with_tags(self):
305
path, (gitsha1, gitsha2) = self.make_tworev_branch()
306
gitrepo = GitRepo(path)
307
self.addCleanup(gitrepo.close)
308
gitrepo.refs[b"refs/tags/sometag"] = gitsha2
309
oldrepo = Repository.open(path)
310
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
311
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
312
newbranch = self.make_branch('g')
313
source_branch = Branch.open(path)
314
source_branch.get_config().set_user_option("branch.fetch_tags", True)
315
inter_branch = InterBranch.get(source_branch, newbranch)
316
inter_branch.pull(stop_revision=revid1)
317
self.assertEqual(revid1, newbranch.last_revision())
318
self.assertTrue(newbranch.repository.has_revision(revid2))
320
def test_bzr_branch_bound_to_git(self):
321
path, (gitsha1, gitsha2) = self.make_tworev_branch()
322
wt = Branch.open(path).create_checkout('co')
323
self.build_tree_contents([('co/foobar', b'blah')])
325
errors.NoRoundtrippingSupport, wt.commit,
326
'commit from bound branch.')
327
revid = wt.commit('commit from bound branch.', lossy=True)
328
self.assertEqual(revid, wt.branch.last_revision())
331
wt.branch.get_master_branch().last_revision())
334
class ForeignTestsBranchFactory(object):
336
def make_empty_branch(self, transport):
337
d = LocalGitControlDirFormat().initialize_on_transport(transport)
338
return d.create_branch()
340
make_branch = make_empty_branch