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