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