39
from bzrlib.plugins.git.mapping import default_mapping
53
from bzrlib.plugins.git.dir import (
54
LocalGitControlDirFormat,
56
from bzrlib.plugins.git.mapping import (
42
61
class TestGitBranch(tests.TestCaseInTempDir):
44
_test_needs_features = [tests.GitCommandFeature]
63
def test_open_by_ref(self):
66
urlutils.local_path_to_url(self.test_dir),
67
urllib.quote("refs/remotes/origin/unstable", safe='')
71
self.assertEquals(b.ref, "refs/remotes/origin/unstable")
46
73
def test_open_existing(self):
49
thebranch = Branch.open('.')
77
thebranch = d.create_branch()
50
78
self.assertIsInstance(thebranch, branch.GitBranch)
84
thebranch = d.create_branch()
86
"<LocalGitBranch('%s/', u'')>" % (
87
urlutils.local_path_to_url(self.test_dir),),
52
90
def test_last_revision_is_null(self):
55
thebranch = Branch.open('.')
93
thedir = BzrDir.open('.')
94
thebranch = thedir.create_branch()
56
95
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
57
96
self.assertEqual((0, revision.NULL_REVISION),
58
97
thebranch.last_revision_info())
60
99
def simple_commit_a(self):
100
r = GitRepo.init('.')
62
101
self.build_tree(['a'])
63
tests.run_git('add', 'a')
64
tests.run_git('commit', '-m', 'a')
103
return r.do_commit("a", committer="Somebody <foo@example.com>")
66
105
def test_last_revision_is_valid(self):
67
self.simple_commit_a()
68
head = tests.run_git('rev-parse', 'HEAD').strip()
106
head = self.simple_commit_a()
70
107
thebranch = Branch.open('.')
71
108
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
72
109
thebranch.last_revision())
74
def test_revision_history(self):
75
self.simple_commit_a()
76
reva = tests.run_git('rev-parse', 'HEAD').strip()
111
def test_last_revision_info(self):
112
reva = self.simple_commit_a()
77
113
self.build_tree(['b'])
78
tests.run_git('add', 'b')
79
tests.run_git('commit', '-m', 'b')
80
revb = tests.run_git('rev-parse', 'HEAD').strip()
116
revb = r.do_commit("b", committer="Somebody <foo@example.com>")
82
118
thebranch = Branch.open('.')
83
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
84
thebranch.revision_history())
119
self.assertEquals((2, default_mapping.revision_id_foreign_to_bzr(revb)), thebranch.last_revision_info())
86
121
def test_tag_annotated(self):
87
self.simple_commit_a()
88
reva = tests.run_git('rev-parse', 'HEAD').strip()
89
tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
122
reva = self.simple_commit_a()
125
o.tagger = "Jelmer <foo@example.com>"
126
o.message = "add tag"
127
o.object = (Commit, reva)
131
r.object_store.add_object(o)
132
r['refs/tags/foo'] = o.id
90
133
thebranch = Branch.open('.')
91
134
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
92
135
thebranch.tags.get_tag_dict())
94
137
def test_tag(self):
95
self.simple_commit_a()
96
reva = tests.run_git('rev-parse', 'HEAD').strip()
97
tests.run_git('tag', '-m', 'add tag', 'foo')
138
reva = self.simple_commit_a()
140
r.refs["refs/tags/foo"] = reva
98
141
thebranch = Branch.open('.')
99
142
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
100
143
thebranch.tags.get_tag_dict())
104
147
class TestWithGitBranch(tests.TestCaseWithTransport):
107
150
tests.TestCaseWithTransport.setUp(self)
108
git.repo.Repo.create(self.test_dir)
109
self.git_branch = Branch.open(self.test_dir)
151
r = dulwich.repo.Repo.create(self.test_dir)
153
d = BzrDir.open(self.test_dir)
154
self.git_branch = d.create_branch()
111
156
def test_get_parent(self):
112
157
self.assertIs(None, self.git_branch.get_parent())
114
159
def test_get_stacked_on_url(self):
115
self.assertIs(None, self.git_branch.get_stacked_on_url())
160
self.assertRaises(errors.UnstackableBranchFormat,
161
self.git_branch.get_stacked_on_url)
117
163
def test_get_physical_lock_status(self):
118
164
self.assertFalse(self.git_branch.get_physical_lock_status())
134
185
def make_onerev_branch(self):
137
tests.run_git("init")
138
189
bb = tests.GitBranchBuilder()
139
190
bb.set_file("foobar", "foo\nbar\n", False)
140
191
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
141
192
gitsha = bb.finish()[mark]
194
return os.path.abspath("d"), gitsha
196
def make_tworev_branch(self):
200
bb = tests.GitBranchBuilder()
201
bb.set_file("foobar", "foo\nbar\n", False)
202
mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
203
mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
206
return "d", (marks[mark1], marks[mark2])
145
208
def clone_git_branch(self, from_url, to_url):
146
209
from_dir = BzrDir.open(from_url)
151
214
path, gitsha = self.make_onerev_branch()
152
215
oldrepo = Repository.open(path)
153
216
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
217
self.assertEquals(gitsha, oldrepo._git.get_refs()["refs/heads/master"])
154
218
newbranch = self.clone_git_branch(path, "f")
155
219
self.assertEquals([revid], newbranch.repository.all_revision_ids())
157
221
def test_sprouted_tags(self):
158
222
path, gitsha = self.make_onerev_branch()
160
tests.run_git("tag", "lala")
161
os.chdir(self.test_dir)
224
r.refs["refs/tags/lala"] = r.head()
162
225
oldrepo = Repository.open(path)
163
226
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
164
227
newbranch = self.clone_git_branch(path, "f")
165
228
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
166
229
self.assertEquals([revid], newbranch.repository.all_revision_ids())
231
def test_interbranch_pull(self):
232
path, (gitsha1, gitsha2) = self.make_tworev_branch()
233
oldrepo = Repository.open(path)
234
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
235
newbranch = self.make_branch('g')
236
inter_branch = InterBranch.get(Branch.open(path), newbranch)
238
self.assertEquals(revid2, newbranch.last_revision())
240
def test_interbranch_pull_noop(self):
241
path, (gitsha1, gitsha2) = self.make_tworev_branch()
242
oldrepo = Repository.open(path)
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)
247
# This is basically "assertNotRaises"
249
self.assertEquals(revid2, newbranch.last_revision())
251
def test_interbranch_pull_stop_revision(self):
252
path, (gitsha1, gitsha2) = self.make_tworev_branch()
253
oldrepo = Repository.open(path)
254
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
255
newbranch = self.make_branch('g')
256
inter_branch = InterBranch.get(Branch.open(path), newbranch)
257
inter_branch.pull(stop_revision=revid1)
258
self.assertEquals(revid1, newbranch.last_revision())
260
def test_interbranch_pull_with_tags(self):
261
path, (gitsha1, gitsha2) = self.make_tworev_branch()
262
gitrepo = GitRepo(path)
263
gitrepo.refs["refs/tags/sometag"] = gitsha2
264
oldrepo = Repository.open(path)
265
revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
266
revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
267
newbranch = self.make_branch('g')
268
source_branch = Branch.open(path)
269
source_branch.get_config().set_user_option("branch.fetch_tags", True)
270
inter_branch = InterBranch.get(source_branch, newbranch)
271
inter_branch.pull(stop_revision=revid1)
272
self.assertEquals(revid1, newbranch.last_revision())
273
self.assertTrue(newbranch.repository.has_revision(revid2))
276
class ForeignTestsBranchFactory(object):
278
def make_empty_branch(self, transport):
279
d = LocalGitControlDirFormat().initialize_on_transport(transport)
280
return d.create_branch()
282
make_branch = make_empty_branch