/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
    GitFileIdMap,
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,
140
            tree_delta_from_git_changes(changes, default_mapping,
7143.15.2 by Jelmer Vernooij
Run autopep8.
141
                                        (GitFileIdMap({}, default_mapping),
142
                                         GitFileIdMap({}, default_mapping))))
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
143
144
    def test_missing(self):
145
        delta = TreeDelta()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
146
        delta.removed.append(('a', b'a-id', 'file'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
        changes = [((b'a', b'a'), (stat.S_IFREG | 0o755, 0),
148
                    (b'a' * 40, b'a' * 40))]
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
149
        self.assertEqual(
150
            delta,
151
            tree_delta_from_git_changes(changes, default_mapping,
7143.15.2 by Jelmer Vernooij
Run autopep8.
152
                                        (GitFileIdMap({u'a': b'a-id'}, default_mapping),
153
                                         GitFileIdMap({u'a': b'a-id'}, default_mapping))))
6977.1.2 by Jelmer Vernooij
Deal with missing files properly in 'bzr st'.
154
155
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
156
class ChangesBetweenGitTreeAndWorkingCopyTests(TestCaseWithTransport):
157
158
    def setUp(self):
159
        super(ChangesBetweenGitTreeAndWorkingCopyTests, self).setUp()
160
        self.wt = self.make_branch_and_tree('.', format='git')
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
161
        self.store = self.wt.branch.repository._git.object_store
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
162
163
    def expectDelta(self, expected_changes,
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
164
                    expected_extras=None, want_unversioned=False,
165
                    tree_id=None):
166
        if tree_id is None:
167
            try:
168
                tree_id = self.store[self.wt.branch.repository._git.head()].tree
169
            except KeyError:
170
                tree_id = None
0.415.3 by Jelmer Vernooij
Open index on demand.
171
        with self.wt.lock_read():
172
            changes, extras = changes_between_git_tree_and_working_copy(
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
173
                self.store, tree_id, self.wt, want_unversioned=want_unversioned)
0.415.3 by Jelmer Vernooij
Open index on demand.
174
            self.assertEqual(expected_changes, list(changes))
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
175
        if expected_extras is None:
176
            expected_extras = set()
177
        self.assertEqual(set(expected_extras), set(extras))
178
179
    def test_empty(self):
180
        self.expectDelta(
7018.3.1 by Jelmer Vernooij
Fix git cache handling.
181
            [((None, b''), (None, stat.S_IFDIR), (None, Tree().id))])
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
182
183
    def test_added_file(self):
184
        self.build_tree(['a'])
185
        self.wt.add(['a'])
6973.13.2 by Jelmer Vernooij
Fix some more tests.
186
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
187
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
188
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
189
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
190
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
191
             ((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.
192
193
    def test_added_unknown_file(self):
194
        self.build_tree(['a'])
195
        t = Tree()
196
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
197
            [((None, b''), (None, stat.S_IFDIR), (None, t.id))])
198
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
199
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
200
        t.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
201
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
202
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
203
             ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, a.id))],
204
            [b'a'],
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
205
            want_unversioned=True)
206
207
    def test_missing_added_file(self):
208
        self.build_tree(['a'])
209
        self.wt.add(['a'])
210
        os.unlink('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
211
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
212
        t = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
213
        t.add(b"a", 0, ZERO_SHA)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
214
        self.expectDelta(
6973.13.2 by Jelmer Vernooij
Fix some more tests.
215
            [((None, b''), (None, stat.S_IFDIR), (None, t.id)),
216
             ((None, b'a'), (None, 0), (None, ZERO_SHA))],
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
217
            [])
218
219
    def test_missing_versioned_file(self):
220
        self.build_tree(['a'])
221
        self.wt.add(['a'])
222
        self.wt.commit('')
223
        os.unlink('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
224
        a = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
225
        oldt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
226
        oldt.add(b"a", stat.S_IFREG | 0o644, a.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
227
        newt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
228
        newt.add(b"a", 0, ZERO_SHA)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
229
        self.expectDelta(
7143.15.2 by Jelmer Vernooij
Run autopep8.
230
            [((b'', b''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)),
231
             ((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.
232
233
    def test_versioned_replace_by_dir(self):
234
        self.build_tree(['a'])
235
        self.wt.add(['a'])
236
        self.wt.commit('')
237
        os.unlink('a')
238
        os.mkdir('a')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
239
        olda = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
240
        oldt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
241
        oldt.add(b"a", stat.S_IFREG | 0o644, olda.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
242
        newt = Tree()
243
        newa = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
244
        newt.add(b"a", stat.S_IFDIR, newa.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
245
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
246
            ((b'', b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
247
             (stat.S_IFDIR, stat.S_IFDIR),
248
             (oldt.id, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
249
            ((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.
250
            ], want_unversioned=False)
251
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
252
            ((b'', b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
253
             (stat.S_IFDIR, stat.S_IFDIR),
254
             (oldt.id, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
255
            ((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.
256
            ], want_unversioned=True)
257
258
    def test_extra(self):
259
        self.build_tree(['a'])
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
260
        newa = Blob.from_string(b'contents of a\n')
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
261
        newt = Tree()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
262
        newt.add(b"a", stat.S_IFREG | 0o644, newa.id)
0.391.7 by Jelmer Vernooij
Fix reporting of missing files in .iter_changes.
263
        self.expectDelta([
6973.13.2 by Jelmer Vernooij
Fix some more tests.
264
            ((None, b''),
7143.15.2 by Jelmer Vernooij
Run autopep8.
265
             (None, stat.S_IFDIR),
266
             (None, newt.id)),
6973.13.2 by Jelmer Vernooij
Fix some more tests.
267
            ((None, b'a'), (None, stat.S_IFREG | 0o644), (None, newa.id))
268
            ], [b'a'], want_unversioned=True)
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
269
270
    def test_submodule(self):
271
        self.build_tree(['a/'])
272
        a = Blob.from_string(b'irrelevant\n')
273
        with self.wt.lock_tree_write():
7131.13.3 by Jelmer Vernooij
Fix python3 compatibility.
274
            (index, index_path) = self.wt._lookup_index(b'a')
7183.2.1 by Martin
Fix E999 lint error for Python 2 flake8
275
            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.
276
            self.wt._index_dirty = True
277
        t = Tree()
7183.2.1 by Martin
Fix E999 lint error for Python 2 flake8
278
        t.add(b"a", S_IFGITLINK, a.id)
7131.13.1 by Jelmer Vernooij
Don't show a delta for unchanged submodules.
279
        self.store.add_object(t)
280
        self.expectDelta([], tree_id=t.id)