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