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