/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
17
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
18
"""Tests for interfacing with a Git Branch"""
19
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
20
21
import dulwich
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
22
from dulwich.objects import (
23
    Commit,
24
    Tag,
25
    )
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
26
from dulwich.repo import (
27
    Repo as GitRepo,
28
    )
29
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
30
import os
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
31
import urllib
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
32
33
from bzrlib import (
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
34
    errors,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
35
    revision,
0.266.1 by Martin
Expect native-style file urls in branch object reprs
36
    urlutils,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
37
    )
38
from bzrlib.branch import (
39
    Branch,
0.247.1 by Michael Hudson
test
40
    InterBranch,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
41
    )
0.200.1014 by Jelmer Vernooij
Fix tests.
42
from bzrlib.bzrdir import (
43
    BzrDir,
44
    )
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
45
from bzrlib.repository import (
46
    Repository,
47
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
48
0.200.27 by David Allouche
Flat is better than nested, remove the gitlib hierarchy.
49
from bzrlib.plugins.git import (
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
50
    branch,
0.200.97 by Jelmer Vernooij
use mapping object.
51
    tests,
0.200.20 by John Arbash Meinel
All tests are passing again
52
    )
0.200.1140 by Jelmer Vernooij
Update now that the control dir formats are no longer in __init__.
53
from bzrlib.plugins.git.dir import (
54
    LocalGitControlDirFormat,
55
    )
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
56
from bzrlib.plugins.git.mapping import (
57
    default_mapping,
58
    )
0.200.123 by Jelmer Vernooij
Use central git module.
59
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
60
61
class TestGitBranch(tests.TestCaseInTempDir):
62
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
63
    def test_open_by_ref(self):
64
        GitRepo.init('.')
0.200.1427 by Jelmer Vernooij
fix 2.3 and 2.4 compatibility.
65
        url = "%s,ref=%s" % (
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
66
            urlutils.local_path_to_url(self.test_dir),
67
            urllib.quote("refs/remotes/origin/unstable", safe='')
0.200.1427 by Jelmer Vernooij
fix 2.3 and 2.4 compatibility.
68
            )
69
        d = BzrDir.open(url)
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
70
        b = d.create_branch()
71
        self.assertEquals(b.ref, "refs/remotes/origin/unstable")
72
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
73
    def test_open_existing(self):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
74
        r = GitRepo.init('.')
75
        del r.refs["HEAD"]
0.200.1014 by Jelmer Vernooij
Fix tests.
76
        d = BzrDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
77
        thebranch = d.create_branch()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
78
        self.assertIsInstance(thebranch, branch.GitBranch)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
79
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
80
    def test_repr(self):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
81
        r = GitRepo.init('.')
82
        del r.refs["HEAD"]
0.200.1014 by Jelmer Vernooij
Fix tests.
83
        d = BzrDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
84
        thebranch = d.create_branch()
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
85
        self.assertEquals(
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
86
            "<LocalGitBranch('%s/', u'')>" % (
0.266.1 by Martin
Expect native-style file urls in branch object reprs
87
                urlutils.local_path_to_url(self.test_dir),),
88
            repr(thebranch))
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
89
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
90
    def test_last_revision_is_null(self):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
91
        r = GitRepo.init('.')
92
        del r.refs["HEAD"]
0.200.1014 by Jelmer Vernooij
Fix tests.
93
        thedir = BzrDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
94
        thebranch = thedir.create_branch()
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
95
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
96
        self.assertEqual((0, revision.NULL_REVISION),
97
                         thebranch.last_revision_info())
0.200.20 by John Arbash Meinel
All tests are passing again
98
0.200.82 by Jelmer Vernooij
Support listing tags.
99
    def simple_commit_a(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
100
        r = GitRepo.init('.')
0.200.82 by Jelmer Vernooij
Support listing tags.
101
        self.build_tree(['a'])
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
102
        r.stage(["a"])
103
        return r.do_commit("a", committer="Somebody <foo@example.com>")
0.200.82 by Jelmer Vernooij
Support listing tags.
104
0.200.20 by John Arbash Meinel
All tests are passing again
105
    def test_last_revision_is_valid(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
106
        head = self.simple_commit_a()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
107
        thebranch = Branch.open('.')
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
108
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
0.200.20 by John Arbash Meinel
All tests are passing again
109
                         thebranch.last_revision())
0.200.58 by Jelmer Vernooij
Fix remaining tests.
110
0.200.1602 by Jelmer Vernooij
Remove Branch.revision_history.
111
    def test_last_revision_info(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
112
        reva = self.simple_commit_a()
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
113
        self.build_tree(['b'])
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
114
        r = GitRepo(".")
115
        r.stage("b")
116
        revb = r.do_commit("b", committer="Somebody <foo@example.com>")
0.200.58 by Jelmer Vernooij
Fix remaining tests.
117
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
118
        thebranch = Branch.open('.')
0.200.1602 by Jelmer Vernooij
Remove Branch.revision_history.
119
        self.assertEquals((2, default_mapping.revision_id_foreign_to_bzr(revb)), thebranch.last_revision_info())
0.200.82 by Jelmer Vernooij
Support listing tags.
120
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
121
    def test_tag_annotated(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
122
        reva = self.simple_commit_a()
123
        o = Tag()
124
        o.name = "foo"
125
        o.tagger = "Jelmer <foo@example.com>"
126
        o.message = "add tag"
127
        o.object = (Commit, reva)
128
        o.tag_timezone = 0
129
        o.tag_time = 42
130
        r = GitRepo(".")
131
        r.object_store.add_object(o)
132
        r['refs/tags/foo'] = o.id
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
133
        thebranch = Branch.open('.')
134
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
135
                          thebranch.tags.get_tag_dict())
136
137
    def test_tag(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
138
        reva = self.simple_commit_a()
139
        r = GitRepo(".")
140
        r.refs["refs/tags/foo"] = reva
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
141
        thebranch = Branch.open('.')
142
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
143
                          thebranch.tags.get_tag_dict())
144
0.200.956 by Jelmer Vernooij
Add some more format tests.
145
0.200.66 by Jelmer Vernooij
Implement branch.get_parent().
146
147
class TestWithGitBranch(tests.TestCaseWithTransport):
148
149
    def setUp(self):
150
        tests.TestCaseWithTransport.setUp(self)
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
151
        r = dulwich.repo.Repo.create(self.test_dir)
152
        del r.refs["HEAD"]
0.200.1014 by Jelmer Vernooij
Fix tests.
153
        d = BzrDir.open(self.test_dir)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
154
        self.git_branch = d.create_branch()
0.200.66 by Jelmer Vernooij
Implement branch.get_parent().
155
156
    def test_get_parent(self):
157
        self.assertIs(None, self.git_branch.get_parent())
0.200.67 by Jelmer Vernooij
Implement Branch.get_stacked_on_url.
158
159
    def test_get_stacked_on_url(self):
0.200.956 by Jelmer Vernooij
Add some more format tests.
160
        self.assertRaises(errors.UnstackableBranchFormat,
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
161
            self.git_branch.get_stacked_on_url)
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
162
0.200.72 by Jelmer Vernooij
Implement Branch.get_physical_lock_status.
163
    def test_get_physical_lock_status(self):
164
        self.assertFalse(self.git_branch.get_physical_lock_status())
165
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
166
167
class TestGitBranchFormat(tests.TestCase):
168
169
    def setUp(self):
170
        super(TestGitBranchFormat, self).setUp()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
171
        self.format = branch.GitBranchFormat()
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
172
173
    def test_get_format_description(self):
174
        self.assertEquals("Git Branch", self.format.get_format_description())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
175
0.200.956 by Jelmer Vernooij
Add some more format tests.
176
    def test_get_network_name(self):
177
        self.assertEquals("git", self.format.network_name())
178
179
    def test_supports_tags(self):
180
        self.assertTrue(self.format.supports_tags())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
181
182
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
183
class BranchTests(tests.TestCaseInTempDir):
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
184
185
    def make_onerev_branch(self):
186
        os.mkdir("d")
187
        os.chdir("d")
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
188
        GitRepo.init('.')
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
189
        bb = tests.GitBranchBuilder()
190
        bb.set_file("foobar", "foo\nbar\n", False)
191
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
192
        gitsha = bb.finish()[mark]
193
        os.chdir("..")
0.200.1373 by Jelmer Vernooij
Prevent accidentally removing branch.
194
        return os.path.abspath("d"), gitsha
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
195
0.247.1 by Michael Hudson
test
196
    def make_tworev_branch(self):
197
        os.mkdir("d")
198
        os.chdir("d")
199
        GitRepo.init('.')
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")
204
        marks = bb.finish()
205
        os.chdir("..")
206
        return "d", (marks[mark1], marks[mark2])
207
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
208
    def clone_git_branch(self, from_url, to_url):
0.200.1014 by Jelmer Vernooij
Fix tests.
209
        from_dir = BzrDir.open(from_url)
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
210
        to_dir = from_dir.sprout(to_url)
211
        return to_dir.open_branch()
212
213
    def test_single_rev(self):
214
        path, gitsha = self.make_onerev_branch()
215
        oldrepo = Repository.open(path)
216
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
0.200.1373 by Jelmer Vernooij
Prevent accidentally removing branch.
217
        self.assertEquals(gitsha, oldrepo._git.get_refs()["refs/heads/master"])
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
218
        newbranch = self.clone_git_branch(path, "f")
219
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
220
221
    def test_sprouted_tags(self):
222
        path, gitsha = self.make_onerev_branch()
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
223
        r = GitRepo(path)
224
        r.refs["refs/tags/lala"] = r.head()
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
225
        oldrepo = Repository.open(path)
226
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
227
        newbranch = self.clone_git_branch(path, "f")
228
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
229
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
230
0.247.1 by Michael Hudson
test
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)
237
        inter_branch.pull()
238
        self.assertEquals(revid2, newbranch.last_revision())
239
0.247.5 by Michael Hudson
test and fix for noop pull case
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)
246
        inter_branch.pull()
247
        # This is basically "assertNotRaises"
248
        inter_branch.pull()
249
        self.assertEquals(revid2, newbranch.last_revision())
250
0.247.3 by Michael Hudson
oh, so it wasn't (particularly) wrong, but it was a bit obscure
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())
259
0.259.8 by Jelmer Vernooij
Add test.
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')
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
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)
0.259.8 by Jelmer Vernooij
Add test.
271
        inter_branch.pull(stop_revision=revid1)
272
        self.assertEquals(revid1, newbranch.last_revision())
273
        self.assertTrue(newbranch.repository.has_revision(revid2))
274
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
275
276
class ForeignTestsBranchFactory(object):
277
278
    def make_empty_branch(self, transport):
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
279
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
280
        return d.create_branch()
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
281
282
    make_branch = make_empty_branch