/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) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.262.1 by Jelmer Vernooij
Fix WorkingTree.conflicts().
2
# Copyright (C) 2011 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.262.1 by Jelmer Vernooij
Fix WorkingTree.conflicts().
17
18
"""Tests for Git working trees."""
19
0.358.3 by Jelmer Vernooij
Enable absolute import.
20
from __future__ import absolute_import
21
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
22
import os
23
import stat
24
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
25
from dulwich.index import IndexEntry
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
26
from dulwich.objects import (
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
27
    S_IFGITLINK,
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
28
    Blob,
29
    Tree,
30
    ZERO_SHA,
31
    )
32
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
33
from ... import (
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
34
    conflicts as _mod_conflicts,
7131.3.1 by Jelmer Vernooij
When opening working trees with .git files, open the right control transport.
35
    workingtree as _mod_workingtree,
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
36
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
37
from ...delta import TreeDelta
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
38
from ...tree import TreeChange
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
39
from ..mapping import (
40
    default_mapping,
41
    )
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
42
from ..tree import (
43
    changes_between_git_tree_and_working_copy,
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
44
    tree_delta_from_git_changes,
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
45
    )
0.369.1 by Jelmer Vernooij
Implement conflict handling.
46
from ..workingtree import (
47
    FLAG_STAGEMASK,
0.262.1 by Jelmer Vernooij
Fix WorkingTree.conflicts().
48
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
49
from ...tests import (
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
50
    TestCase,
51
    TestCaseWithTransport,
52
    )
0.262.1 by Jelmer Vernooij
Fix WorkingTree.conflicts().
53
54
55
class GitWorkingTreeTests(TestCaseWithTransport):
56
57
    def setUp(self):
58
        super(GitWorkingTreeTests, self).setUp()
59
        self.tree = self.make_branch_and_tree('.', format="git")
60
0.369.1 by Jelmer Vernooij
Implement conflict handling.
61
    def test_conflict_list(self):
0.369.2 by Jelmer Vernooij
Fix tests.
62
        self.assertIsInstance(
7143.15.2 by Jelmer Vernooij
Run autopep8.
63
            self.tree.conflicts(),
64
            _mod_conflicts.ConflictList)
0.369.1 by Jelmer Vernooij
Implement conflict handling.
65
66
    def test_add_conflict(self):
67
        self.build_tree(['conflicted'])
68
        self.tree.add(['conflicted'])
69
        with self.tree.lock_tree_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
70
            self.tree.index[b'conflicted'] = self.tree.index[b'conflicted'][:9] + \
71
                (FLAG_STAGEMASK, )
0.415.3 by Jelmer Vernooij
Open index on demand.
72
            self.tree._index_dirty = True
0.369.1 by Jelmer Vernooij
Implement conflict handling.
73
        conflicts = self.tree.conflicts()
74
        self.assertEqual(1, len(conflicts))
0.385.1 by Jelmer Vernooij
Use specific_files argument to Tree.iter_entries_by_dir.
75
76
    def test_revert_empty(self):
77
        self.build_tree(['a'])
78
        self.tree.add(['a'])
79
        self.assertTrue(self.tree.is_versioned('a'))
80
        self.tree.revert(['a'])
81
        self.assertFalse(self.tree.is_versioned('a'))
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
82
7065.1.1 by Jelmer Vernooij
Properly handle ignored directories in Git.
83
    def test_is_ignored_directory(self):
84
        self.assertFalse(self.tree.is_ignored('a'))
85
        self.build_tree(['a/'])
86
        self.assertFalse(self.tree.is_ignored('a'))
87
        self.build_tree_contents([('.gitignore', 'a\n')])
88
        self.tree._ignoremanager = None
89
        self.assertTrue(self.tree.is_ignored('a'))
90
        self.build_tree_contents([('.gitignore', 'a/\n')])
91
        self.tree._ignoremanager = None
92
        self.assertTrue(self.tree.is_ignored('a'))
93
7265.3.1 by Jelmer Vernooij
Properly ignore .git files.
94
    def test_add_submodule_dir(self):
95
        subtree = self.make_branch_and_tree('asub', format='git')
96
        subtree.commit('Empty commit')
97
        self.tree.add(['asub'])
98
        with self.tree.lock_read():
99
            entry = self.tree.index[b'asub']
100
            self.assertEqual(entry.mode, S_IFGITLINK)
101
        self.assertEqual([], list(subtree.unknowns()))
102
103
    def test_add_submodule_file(self):
104
        os.mkdir('.git/modules')
105
        subbranch = self.make_branch('.git/modules/asub', format='git-bare')
106
        os.mkdir('asub')
107
        with open('asub/.git', 'w') as f:
108
            f.write('gitdir: ../.git/modules/asub\n')
109
        subtree = _mod_workingtree.WorkingTree.open('asub')
110
        subtree.commit('Empty commit')
111
        self.tree.add(['asub'])
112
        with self.tree.lock_read():
113
            entry = self.tree.index[b'asub']
114
            self.assertEqual(entry.mode, S_IFGITLINK)
115
        self.assertEqual([], list(subtree.unknowns()))
116
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
117
7131.3.1 by Jelmer Vernooij
When opening working trees with .git files, open the right control transport.
118
class GitWorkingTreeFileTests(TestCaseWithTransport):
119
120
    def setUp(self):
121
        super(GitWorkingTreeFileTests, self).setUp()
122
        self.tree = self.make_branch_and_tree('actual', format="git")
7143.15.2 by Jelmer Vernooij
Run autopep8.
123
        self.build_tree_contents(
124
            [('linked/',), ('linked/.git', 'gitdir: ../actual/.git')])
7131.3.1 by Jelmer Vernooij
When opening working trees with .git files, open the right control transport.
125
        self.wt = _mod_workingtree.WorkingTree.open('linked')
126
127
    def test_add(self):
128
        self.build_tree(['linked/somefile'])
129
        self.wt.add(["somefile"])
130
        self.wt.commit("Add somefile")
131
132
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
133
class TreeDeltaFromGitChangesTests(TestCase):
134
135
    def test_empty(self):
136
        delta = TreeDelta()
137
        changes = []
138
        self.assertEqual(
139
            delta,
7358.13.1 by Jelmer Vernooij
Drop file id roundtripping support in Git.
140
            tree_delta_from_git_changes(changes, (default_mapping, default_mapping)))
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
141
142
    def test_missing(self):
143
        delta = TreeDelta()
7358.11.6 by Jelmer Vernooij
merge trunk.
144
        delta.removed.append(TreeChange(b'git:a', ('a', 'a'), False, (True, True), (b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', None), (True, False)))
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
        changes = [((b'a', b'a'), (stat.S_IFREG | 0o755, 0),
146
                    (b'a' * 40, b'a' * 40))]
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
147
        self.assertEqual(
148
            delta,
7358.13.1 by Jelmer Vernooij
Drop file id roundtripping support in Git.
149
            tree_delta_from_git_changes(changes, (default_mapping, default_mapping)))
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
150
151
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
152
class ChangesBetweenGitTreeAndWorkingCopyTests(TestCaseWithTransport):
153
154
    def setUp(self):
155
        super(ChangesBetweenGitTreeAndWorkingCopyTests, self).setUp()
156
        self.wt = self.make_branch_and_tree('.', format='git')
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
157
        self.store = self.wt.branch.repository._git.object_store
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
158
159
    def expectDelta(self, expected_changes,
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
160
                    expected_extras=None, want_unversioned=False,
161
                    tree_id=None):
162
        if tree_id is None:
163
            try:
164
                tree_id = self.store[self.wt.branch.repository._git.head()].tree
165
            except KeyError:
166
                tree_id = None
0.415.3 by Jelmer Vernooij
Open index on demand.
167
        with self.wt.lock_read():
168
            changes, extras = changes_between_git_tree_and_working_copy(
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
169
                self.store, tree_id, self.wt, want_unversioned=want_unversioned)
0.415.3 by Jelmer Vernooij
Open index on demand.
170
            self.assertEqual(expected_changes, list(changes))
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
171
        if expected_extras is None:
172
            expected_extras = set()
173
        self.assertEqual(set(expected_extras), set(extras))
174
175
    def test_empty(self):
176
        self.expectDelta(
7018.3.1 by Jelmer Vernooij
Fix git cache handling.
177
            [((None, b''), (None, stat.S_IFDIR), (None, Tree().id))])
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
178
179
    def test_added_file(self):
180
        self.build_tree(['a'])
181
        self.wt.add(['a'])
6973.13.2 by Jelmer Vernooij
Fix some more tests.
182
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
183
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
184
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
185
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
186
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
187
             ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, a.id))])
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
188
189
    def test_added_unknown_file(self):
190
        self.build_tree(['a'])
191
        t = Tree()
192
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
193
            [((None, b''), (None, stat.S_IFDIR), (None, t.id))])
194
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
195
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
196
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
197
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
198
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
199
             ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, a.id))],
200
            [b'a'],
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
201
            want_unversioned=True)
202
203
    def test_missing_added_file(self):
204
        self.build_tree(['a'])
205
        self.wt.add(['a'])
206
        os.unlink('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
207
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
208
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
209
        t.add(b"a", 0, ZERO_SHA)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
210
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
211
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
212
             ((None, b'a'), (None, 0), (None, ZERO_SHA))],
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
213
            [])
214
215
    def test_missing_versioned_file(self):
216
        self.build_tree(['a'])
217
        self.wt.add(['a'])
218
        self.wt.commit('')
219
        os.unlink('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
220
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
221
        oldt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
222
        oldt.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
223
        newt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
224
        newt.add(b"a", 0, ZERO_SHA)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
225
        self.expectDelta(
7143.15.2 by Jelmer Vernooij
Run autopep8.
226
            [((b'', b''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)),
227
             ((b'a', b'a'), (stat.S_IFREG | 0o644, 0), (a.id, ZERO_SHA))])
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
228
229
    def test_versioned_replace_by_dir(self):
230
        self.build_tree(['a'])
231
        self.wt.add(['a'])
232
        self.wt.commit('')
233
        os.unlink('a')
234
        os.mkdir('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
235
        olda = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
236
        oldt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
237
        oldt.add(b"a", stat.S_IFREG | 0o644, olda.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
238
        newt = Tree()
239
        newa = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
240
        newt.add(b"a", stat.S_IFDIR, newa.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
241
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
242
            ((b'', b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
243
             (stat.S_IFDIR, stat.S_IFDIR),
244
             (oldt.id, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
245
            ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
246
            ], want_unversioned=False)
247
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
248
            ((b'', b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
249
             (stat.S_IFDIR, stat.S_IFDIR),
250
             (oldt.id, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
251
            ((b'a', b'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
252
            ], want_unversioned=True)
253
254
    def test_extra(self):
255
        self.build_tree(['a'])
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
256
        newa = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
257
        newt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
258
        newt.add(b"a", stat.S_IFREG | 0o644, newa.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
259
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
260
            ((None, b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
261
             (None, stat.S_IFDIR),
262
             (None, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
263
            ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, newa.id))
264
            ], [b'a'], want_unversioned=True)
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
265
266
    def test_submodule(self):
7296.1.3 by Jelmer Vernooij
Fix submodule test.
267
        self.subtree = self.make_branch_and_tree('a', format="git")
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
268
        a = Blob.from_string(b'irrelevant\n')
7296.1.3 by Jelmer Vernooij
Fix submodule test.
269
        self.build_tree_contents([('a/.git/HEAD', a.id)])
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
270
        with self.wt.lock_tree_write():
7131.13.3 by Jelmer Vernooij
Fix python3 compatibility.
271
            (index, index_path) = self.wt._lookup_index(b'a')
7183.2.1 by Martin
Fix E999 lint error for Python 2 flake8
272
            index[b'a'] = IndexEntry(0, 0, 0, 0, S_IFGITLINK, 0, 0, 0, a.id, 0)
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
273
            self.wt._index_dirty = True
274
        t = Tree()
7183.2.1 by Martin
Fix E999 lint error for Python 2 flake8
275
        t.add(b"a", S_IFGITLINK, a.id)
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
276
        self.store.add_object(t)
277
        self.expectDelta([], tree_id=t.id)
7452.1.1 by Jelmer Vernooij
Don't show submodules that are not checked out as deltas.
278
279
    def test_submodule_not_checked_out(self):
280
        a = Blob.from_string(b'irrelevant\n')
281
        with self.wt.lock_tree_write():
282
            (index, index_path) = self.wt._lookup_index(b'a')
283
            index[b'a'] = IndexEntry(0, 0, 0, 0, S_IFGITLINK, 0, 0, 0, a.id, 0)
284
            self.wt._index_dirty = True
285
        os.mkdir(self.wt.abspath('a'))
286
        t = Tree()
287
        t.add(b"a", S_IFGITLINK, a.id)
288
        self.store.add_object(t)
289
        self.expectDelta([], tree_id=t.id)