/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
2
# Copyright (C) 2007 Canonical Ltd
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
0.358.1 by Jelmer Vernooij
Fix FSF address.
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
17
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
18
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
19
"""Tests for interfacing with a Git Branch"""
20
0.358.3 by Jelmer Vernooij
Enable absolute import.
21
from __future__ import absolute_import
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
22
23
import dulwich
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
24
from dulwich.objects import (
25
    Commit,
26
    Tag,
27
    )
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
28
from dulwich.repo import (
29
    Repo as GitRepo,
30
    )
31
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
32
import os
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
33
import urllib
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
34
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
35
from .... import (
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
36
    errors,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
37
    revision,
0.266.1 by Martin
Expect native-style file urls in branch object reprs
38
    urlutils,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
39
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
40
from ....branch import (
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
41
    Branch,
0.247.1 by Michael Hudson
test
42
    InterBranch,
0.200.1660 by Jelmer Vernooij
Fix imports.
43
    UnstackableBranchFormat,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
44
    )
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
45
from ....controldir import (
46
    ControlDir,
0.200.1014 by Jelmer Vernooij
Fix tests.
47
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
48
from ....repository import (
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
49
    Repository,
50
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
51
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
52
from .. import (
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
53
    branch,
0.200.97 by Jelmer Vernooij
use mapping object.
54
    tests,
0.200.20 by John Arbash Meinel
All tests are passing again
55
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
56
from ..dir import (
0.200.1140 by Jelmer Vernooij
Update now that the control dir formats are no longer in __init__.
57
    LocalGitControlDirFormat,
58
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
59
from ..mapping import (
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
60
    default_mapping,
61
    )
0.200.123 by Jelmer Vernooij
Use central git module.
62
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
63
64
class TestGitBranch(tests.TestCaseInTempDir):
65
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
66
    def test_open_by_ref(self):
67
        GitRepo.init('.')
0.200.1427 by Jelmer Vernooij
fix 2.3 and 2.4 compatibility.
68
        url = "%s,ref=%s" % (
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
69
            urlutils.local_path_to_url(self.test_dir),
70
            urllib.quote("refs/remotes/origin/unstable", safe='')
0.200.1427 by Jelmer Vernooij
fix 2.3 and 2.4 compatibility.
71
            )
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
72
        d = ControlDir.open(url)
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
73
        b = d.create_branch()
74
        self.assertEquals(b.ref, "refs/remotes/origin/unstable")
75
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
76
    def test_open_existing(self):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
77
        r = GitRepo.init('.')
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
78
        d = ControlDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
79
        thebranch = d.create_branch()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
80
        self.assertIsInstance(thebranch, branch.GitBranch)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
81
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
82
    def test_repr(self):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
83
        r = GitRepo.init('.')
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.310.10 by Jelmer Vernooij
Fix format opener fails on empty dir.
87
            "<LocalGitBranch('%s/', u'master')>" % (
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('.')
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
93
        thedir = ControlDir.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)
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
152
        d = ControlDir.open(self.test_dir)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
153
        self.git_branch = d.create_branch()
0.200.66 by Jelmer Vernooij
Implement branch.get_parent().
154
155
    def test_get_parent(self):
156
        self.assertIs(None, self.git_branch.get_parent())
0.200.67 by Jelmer Vernooij
Implement Branch.get_stacked_on_url.
157
158
    def test_get_stacked_on_url(self):
0.200.1660 by Jelmer Vernooij
Fix imports.
159
        self.assertRaises(UnstackableBranchFormat,
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
160
            self.git_branch.get_stacked_on_url)
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
161
0.200.72 by Jelmer Vernooij
Implement Branch.get_physical_lock_status.
162
    def test_get_physical_lock_status(self):
163
        self.assertFalse(self.git_branch.get_physical_lock_status())
164
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
165
0.295.1 by Jelmer Vernooij
Split up branch formats.
166
class TestLocalGitBranchFormat(tests.TestCase):
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
167
168
    def setUp(self):
0.295.1 by Jelmer Vernooij
Split up branch formats.
169
        super(TestLocalGitBranchFormat, self).setUp()
170
        self.format = branch.LocalGitBranchFormat()
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
171
172
    def test_get_format_description(self):
0.295.1 by Jelmer Vernooij
Split up branch formats.
173
        self.assertEquals("Local Git Branch", self.format.get_format_description())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
174
0.200.956 by Jelmer Vernooij
Add some more format tests.
175
    def test_get_network_name(self):
176
        self.assertEquals("git", self.format.network_name())
177
178
    def test_supports_tags(self):
179
        self.assertTrue(self.format.supports_tags())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
180
181
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
182
class BranchTests(tests.TestCaseInTempDir):
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
183
184
    def make_onerev_branch(self):
185
        os.mkdir("d")
186
        os.chdir("d")
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
187
        GitRepo.init('.')
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
188
        bb = tests.GitBranchBuilder()
189
        bb.set_file("foobar", "foo\nbar\n", False)
190
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
191
        gitsha = bb.finish()[mark]
192
        os.chdir("..")
0.200.1373 by Jelmer Vernooij
Prevent accidentally removing branch.
193
        return os.path.abspath("d"), gitsha
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
194
0.247.1 by Michael Hudson
test
195
    def make_tworev_branch(self):
196
        os.mkdir("d")
197
        os.chdir("d")
198
        GitRepo.init('.')
199
        bb = tests.GitBranchBuilder()
200
        bb.set_file("foobar", "foo\nbar\n", False)
201
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
202
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
203
        marks = bb.finish()
204
        os.chdir("..")
205
        return "d", (marks[mark1], marks[mark2])
206
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
207
    def clone_git_branch(self, from_url, to_url):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
208
        from_dir = ControlDir.open(from_url)
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
209
        to_dir = from_dir.sprout(to_url)
210
        return to_dir.open_branch()
211
212
    def test_single_rev(self):
213
        path, gitsha = self.make_onerev_branch()
214
        oldrepo = Repository.open(path)
215
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
0.200.1373 by Jelmer Vernooij
Prevent accidentally removing branch.
216
        self.assertEquals(gitsha, oldrepo._git.get_refs()["refs/heads/master"])
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
217
        newbranch = self.clone_git_branch(path, "f")
218
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
219
220
    def test_sprouted_tags(self):
221
        path, gitsha = self.make_onerev_branch()
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
222
        r = GitRepo(path)
223
        r.refs["refs/tags/lala"] = r.head()
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
224
        oldrepo = Repository.open(path)
225
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
226
        newbranch = self.clone_git_branch(path, "f")
227
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
228
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
229
0.247.1 by Michael Hudson
test
230
    def test_interbranch_pull(self):
231
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
232
        oldrepo = Repository.open(path)
233
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
234
        newbranch = self.make_branch('g')
235
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
236
        inter_branch.pull()
237
        self.assertEquals(revid2, newbranch.last_revision())
238
0.247.5 by Michael Hudson
test and fix for noop pull case
239
    def test_interbranch_pull_noop(self):
240
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
241
        oldrepo = Repository.open(path)
242
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
243
        newbranch = self.make_branch('g')
244
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
245
        inter_branch.pull()
246
        # This is basically "assertNotRaises"
247
        inter_branch.pull()
248
        self.assertEquals(revid2, newbranch.last_revision())
249
0.247.3 by Michael Hudson
oh, so it wasn't (particularly) wrong, but it was a bit obscure
250
    def test_interbranch_pull_stop_revision(self):
251
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
252
        oldrepo = Repository.open(path)
253
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
254
        newbranch = self.make_branch('g')
255
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
256
        inter_branch.pull(stop_revision=revid1)
257
        self.assertEquals(revid1, newbranch.last_revision())
258
0.259.8 by Jelmer Vernooij
Add test.
259
    def test_interbranch_pull_with_tags(self):
260
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
261
        gitrepo = GitRepo(path)
262
        gitrepo.refs["refs/tags/sometag"] = gitsha2
263
        oldrepo = Repository.open(path)
264
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
265
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
266
        newbranch = self.make_branch('g')
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
267
        source_branch = Branch.open(path)
268
        source_branch.get_config().set_user_option("branch.fetch_tags", True)
269
        inter_branch = InterBranch.get(source_branch, newbranch)
0.259.8 by Jelmer Vernooij
Add test.
270
        inter_branch.pull(stop_revision=revid1)
271
        self.assertEquals(revid1, newbranch.last_revision())
272
        self.assertTrue(newbranch.repository.has_revision(revid2))
273
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
274
275
class ForeignTestsBranchFactory(object):
276
277
    def make_empty_branch(self, transport):
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
278
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
279
        return d.create_branch()
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
280
281
    make_branch = make_empty_branch