/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
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
2
# Copyright (C) 2007-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.
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
18
"""Tests for interfacing with a Git Repository"""
19
0.358.3 by Jelmer Vernooij
Enable absolute import.
20
from __future__ import absolute_import
21
0.200.445 by Jelmer Vernooij
Import dulwich as dulwich, not as git.
22
import dulwich
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
23
from dulwich.repo import (
24
    Repo as GitRepo,
25
    )
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
26
import os
27
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
28
from .... import (
0.386.1 by Jelmer Vernooij
Support signing commits.
29
    config,
0.200.61 by Jelmer Vernooij
Fix tests.
30
    errors,
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
31
    revision,
32
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
33
from ....repository import (
0.200.357 by Jelmer Vernooij
Move push code to push.py.
34
    InterRepository,
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
35
    Repository,
36
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
37
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
38
from .. import (
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
39
    dir,
40
    repository,
0.200.97 by Jelmer Vernooij
use mapping object.
41
    tests,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
42
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
43
from ..mapping import (
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
44
    default_mapping,
45
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
46
from ..object_store import (
0.235.2 by Jelmer Vernooij
Fix tests.
47
    BazaarObjectStore,
48
    )
0.200.1642 by Jelmer Vernooij
Use relative imports in tests.
49
from ..push import (
0.200.368 by Jelmer Vernooij
Cope with more granular timezones.
50
    MissingObjectsIterator,
51
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
52
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
53
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
54
    """Feature tests for GitRepository."""
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
55
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
56
    def _do_commit(self):
57
        builder = tests.GitBranchBuilder()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
58
        builder.set_file(b'a', b'text for a\n', False)
59
        commit_handle = builder.commit(b'Joe Foo <joe@foo.com>', b'message')
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
60
        mapping = builder.finish()
61
        return mapping[commit_handle]
62
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
63
    def test_open_existing(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
64
        GitRepo.init(self.test_dir)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
65
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
66
        repo = Repository.open('.')
67
        self.assertIsInstance(repo, repository.GitRepository)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
68
0.200.56 by Jelmer Vernooij
Switch to using GitPython rather than our own in-house stuff.
69
    def test_has_git_repo(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
70
        GitRepo.init(self.test_dir)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
71
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
72
        repo = Repository.open('.')
0.257.1 by Jelmer Vernooij
use transport repo objects even for local access.
73
        self.assertIsInstance(repo._git, dulwich.repo.BaseRepo)
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
74
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
75
    def test_has_revision(self):
76
        GitRepo.init(self.test_dir)
77
        commit_id = self._do_commit()
78
        repo = Repository.open('.')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
79
        self.assertFalse(repo.has_revision(b'foobar'))
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
80
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
81
        self.assertTrue(repo.has_revision(revid))
82
83
    def test_has_revisions(self):
84
        GitRepo.init(self.test_dir)
85
        commit_id = self._do_commit()
86
        repo = Repository.open('.')
6964.2.3 by Jelmer Vernooij
Review comments.
87
        self.assertEqual(set(), repo.has_revisions([b'foobar']))
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
88
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
6964.2.3 by Jelmer Vernooij
Review comments.
89
        self.assertEqual(set([revid]), repo.has_revisions([b'foobar', revid]))
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
90
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
91
    def test_get_revision(self):
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
92
        # GitRepository.get_revision gives a Revision object.
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
93
94
        # Create a git repository with a revision.
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
95
        GitRepo.init(self.test_dir)
0.200.902 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
96
        commit_id = self._do_commit()
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
97
98
        # Get the corresponding Revision object.
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
99
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
100
        repo = Repository.open('.')
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
101
        rev = repo.get_revision(revid)
102
        self.assertIsInstance(rev, revision.Revision)
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
103
0.200.106 by Jelmer Vernooij
Test that getting an unknown revision fails.
104
    def test_get_revision_unknown(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
105
        GitRepo.init(self.test_dir)
0.200.106 by Jelmer Vernooij
Test that getting an unknown revision fails.
106
107
        repo = Repository.open('.')
7018.3.2 by Jelmer Vernooij
Fix some git tests.
108
        self.assertRaises(errors.NoSuchRevision, repo.get_revision, b"bla")
0.200.106 by Jelmer Vernooij
Test that getting an unknown revision fails.
109
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
110
    def simple_commit(self):
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
111
        # Create a git repository with some interesting files in a revision.
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
112
        GitRepo.init(self.test_dir)
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
113
        builder = tests.GitBranchBuilder()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
114
        builder.set_file(b'data', b'text\n', False)
115
        builder.set_file(b'executable', b'content', True)
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
116
        builder.set_symlink(b'link', b'broken')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
117
        builder.set_file(b'subdir/subfile', b'subdir text\n', False)
118
        commit_handle = builder.commit(b'Joe Foo <joe@foo.com>', b'message',
0.204.4 by James Westby
Make the inventory test match what the code produces.
119
            timestamp=1205433193)
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
120
        mapping = builder.finish()
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
121
        return mapping[commit_handle]
122
0.257.1 by Jelmer Vernooij
use transport repo objects even for local access.
123
    def test_pack(self):
124
        commit_id = self.simple_commit()
125
        repo = Repository.open('.')
126
        repo.pack()
127
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
128
    def test_revision_tree(self):
129
        commit_id = self.simple_commit()
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
130
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
131
        repo = Repository.open('.')
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
132
        tree = repo.revision_tree(revid)
6964.2.3 by Jelmer Vernooij
Review comments.
133
        self.assertEqual(tree.get_revision_id(), revid)
6973.7.3 by Jelmer Vernooij
Fix some more tests.
134
        self.assertEqual(b"text\n", tree.get_file_text("data"))
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
135
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
136
0.204.2 by James Westby
Switch to TestCaseWithTransport so that the cache dir can be
137
class TestGitRepository(tests.TestCaseWithTransport):
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
138
0.252.41 by Jelmer Vernooij
Fix Repository.all_revision_ids.
139
    def _do_commit(self):
140
        builder = tests.GitBranchBuilder()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
141
        builder.set_file(b'a', b'text for a\n', False)
142
        commit_handle = builder.commit(b'Joe Foo <joe@foo.com>', b'message')
0.252.41 by Jelmer Vernooij
Fix Repository.all_revision_ids.
143
        mapping = builder.finish()
144
        return mapping[commit_handle]
145
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
146
    def setUp(self):
0.204.2 by James Westby
Switch to TestCaseWithTransport so that the cache dir can be
147
        tests.TestCaseWithTransport.setUp(self)
0.200.445 by Jelmer Vernooij
Import dulwich as dulwich, not as git.
148
        dulwich.repo.Repo.create(self.test_dir)
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
149
        self.git_repo = Repository.open(self.test_dir)
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
150
0.200.40 by David Allouche
GitRepository.supports_rich_root() => False
151
    def test_supports_rich_root(self):
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
152
        repo = self.git_repo
0.200.131 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
153
        self.assertEqual(repo.supports_rich_root(), True)
0.200.40 by David Allouche
GitRepository.supports_rich_root() => False
154
0.200.60 by Jelmer Vernooij
Support signature functions.
155
    def test_get_signature_text(self):
0.200.61 by Jelmer Vernooij
Fix tests.
156
        self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
0.200.60 by Jelmer Vernooij
Support signature functions.
157
158
    def test_has_signature_for_revision_id(self):
6964.2.3 by Jelmer Vernooij
Review comments.
159
        self.assertEqual(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
0.200.60 by Jelmer Vernooij
Support signature functions.
160
0.200.75 by Jelmer Vernooij
Implement Repository.all_revision_ids().
161
    def test_all_revision_ids_none(self):
6964.2.3 by Jelmer Vernooij
Review comments.
162
        self.assertEqual([], self.git_repo.all_revision_ids())
0.200.75 by Jelmer Vernooij
Implement Repository.all_revision_ids().
163
0.200.1282 by Jelmer Vernooij
Add tests for get_known_graph_ancestry.
164
    def test_get_known_graph_ancestry(self):
165
        cid = self._do_commit()
166
        revid = default_mapping.revision_id_foreign_to_bzr(cid)
167
        g = self.git_repo.get_known_graph_ancestry([revid])
6964.2.3 by Jelmer Vernooij
Review comments.
168
        self.assertEqual(frozenset([revid]),
0.200.1329 by Jelmer Vernooij
Fix more tests.
169
            g.heads([revid]))
170
        self.assertEqual([(revid, 0, (1,), True)],
0.200.1284 by Jelmer Vernooij
Fix merge_sort test.
171
            [(n.key, n.merge_depth, n.revno, n.end_of_merge)
172
                 for n in g.merge_sort(revid)])
0.200.1282 by Jelmer Vernooij
Add tests for get_known_graph_ancestry.
173
0.252.41 by Jelmer Vernooij
Fix Repository.all_revision_ids.
174
    def test_all_revision_ids(self):
175
        commit_id = self._do_commit()
6964.2.3 by Jelmer Vernooij
Review comments.
176
        self.assertEqual(
0.200.1727 by Jelmer Vernooij
Change all_revision_ids type to list.
177
                [default_mapping.revision_id_foreign_to_bzr(commit_id)],
0.252.41 by Jelmer Vernooij
Fix Repository.all_revision_ids.
178
                self.git_repo.all_revision_ids())
179
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
180
    def assertIsNullInventory(self, inv):
181
        self.assertEqual(inv.root, None)
182
        self.assertEqual(inv.revision_id, revision.NULL_REVISION)
183
        self.assertEqual(list(inv.iter_entries()), [])
184
185
    def test_revision_tree_none(self):
186
        # GitRepository.revision_tree(None) returns the null tree.
187
        repo = self.git_repo
0.200.57 by Jelmer Vernooij
Fix more tests.
188
        tree = repo.revision_tree(revision.NULL_REVISION)
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
189
        self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
0.378.1 by Jelmer Vernooij
Return None from Tree.get_root_id() when encountering an empty tree.
190
        self.assertIs(None, tree.get_root_id())
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
191
0.200.77 by Jelmer Vernooij
Handle NULL_REVISION in get_parent_map.
192
    def test_get_parent_map_null(self):
6964.2.3 by Jelmer Vernooij
Review comments.
193
        self.assertEqual({revision.NULL_REVISION: ()},
0.200.77 by Jelmer Vernooij
Handle NULL_REVISION in get_parent_map.
194
                           self.git_repo.get_parent_map([revision.NULL_REVISION]))
195
0.200.71 by Jelmer Vernooij
Implement GitRepositoryFormat.get_format_description.
196
0.386.1 by Jelmer Vernooij
Support signing commits.
197
class SigningGitRepository(tests.TestCaseWithTransport):
198
199
    def test_signed_commit(self):
200
        import breezy.gpg
201
        oldstrategy = breezy.gpg.GPGStrategy
202
        wt = self.make_branch_and_tree('.', format='git')
203
        branch = wt.branch
204
        revid = wt.commit("base", allow_pointless=True)
205
        self.assertFalse(branch.repository.has_signature_for_revision_id(revid))
206
        try:
207
            breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
7018.3.7 by Jelmer Vernooij
Fix remaining git tests.
208
            conf = config.MemoryStack(b'''
0.386.1 by Jelmer Vernooij
Support signing commits.
209
create_signatures=always
210
''')
211
            revid2 = wt.commit(config=conf, message="base", allow_pointless=True)
212
            def sign(text):
213
                return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
7018.3.7 by Jelmer Vernooij
Fix remaining git tests.
214
            self.assertIsInstance(branch.repository.get_signature_text(revid2), bytes)
0.386.1 by Jelmer Vernooij
Support signing commits.
215
        finally:
216
            breezy.gpg.GPGStrategy = oldstrategy
217
218
0.200.71 by Jelmer Vernooij
Implement GitRepositoryFormat.get_format_description.
219
class GitRepositoryFormat(tests.TestCase):
220
221
    def setUp(self):
222
        super(GitRepositoryFormat, self).setUp()
0.200.290 by Jelmer Vernooij
Fix tests.
223
        self.format = repository.GitRepositoryFormat()
0.200.71 by Jelmer Vernooij
Implement GitRepositoryFormat.get_format_description.
224
225
    def test_get_format_description(self):
6964.2.3 by Jelmer Vernooij
Review comments.
226
        self.assertEqual("Git Repository",
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
227
                          self.format.get_format_description())
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
228
229
230
class RevisionGistImportTests(tests.TestCaseWithTransport):
231
232
    def setUp(self):
233
        tests.TestCaseWithTransport.setUp(self)
234
        self.git_path = os.path.join(self.test_dir, "git")
235
        os.mkdir(self.git_path)
0.200.445 by Jelmer Vernooij
Import dulwich as dulwich, not as git.
236
        dulwich.repo.Repo.create(self.git_path)
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
237
        self.git_repo = Repository.open(self.git_path)
238
        self.bzr_tree = self.make_branch_and_tree("bzr")
239
0.200.357 by Jelmer Vernooij
Move push code to push.py.
240
    def get_inter(self):
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
241
        return InterRepository.get(self.bzr_tree.branch.repository,
0.200.357 by Jelmer Vernooij
Move push code to push.py.
242
                                   self.git_repo)
243
0.200.368 by Jelmer Vernooij
Cope with more granular timezones.
244
    def object_iter(self):
0.235.2 by Jelmer Vernooij
Fix tests.
245
        store = BazaarObjectStore(self.bzr_tree.branch.repository, default_mapping)
0.254.8 by Jelmer Vernooij
Fix tests.
246
        store_iterator = MissingObjectsIterator(store, self.bzr_tree.branch.repository)
247
        return store, store_iterator
0.200.368 by Jelmer Vernooij
Cope with more granular timezones.
248
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
249
    def import_rev(self, revid, parent_lookup=None):
0.254.8 by Jelmer Vernooij
Fix tests.
250
        store, store_iter = self.object_iter()
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
251
        store._cache.idmap.start_write_group()
0.254.8 by Jelmer Vernooij
Fix tests.
252
        try:
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
253
            return store_iter.import_revision(revid, lossy=True)
0.254.8 by Jelmer Vernooij
Fix tests.
254
        except:
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
255
            store._cache.idmap.abort_write_group()
0.254.8 by Jelmer Vernooij
Fix tests.
256
            raise
257
        else:
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
258
            store._cache.idmap.commit_write_group()
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
259
260
    def test_pointless(self):
0.200.257 by Jelmer Vernooij
Add more details for commit, to avoid checksum from changing.
261
        revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
262
                timezone=0, committer="Jelmer Vernooij <jelmer@samba.org>")
6964.2.3 by Jelmer Vernooij
Review comments.
263
        self.assertEqual(b"2caa8094a5b794961cd9bf582e3e2bb090db0b14",
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
264
                self.import_rev(revid))
6964.2.3 by Jelmer Vernooij
Review comments.
265
        self.assertEqual(b"2caa8094a5b794961cd9bf582e3e2bb090db0b14",
0.200.256 by Jelmer Vernooij
Add tests for import_revision_gist.
266
                self.import_rev(revid))
0.200.658 by Jelmer Vernooij
Provide right infrastructure for foreign repository tests from bzrlib.
267
268
269
class ForeignTestsRepositoryFactory(object):
270
271
    def make_repository(self, transport):
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
272
        return dir.LocalGitControlDirFormat().initialize_on_transport(transport).open_repository()