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