1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
# Copyright (C) 2011 Canonical Ltd.
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.
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.
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Tests for Git working trees."""
20
from __future__ import absolute_import
25
from dulwich.objects import (
31
from .... import conflicts as _mod_conflicts
32
from ..workingtree import (
34
changes_between_git_tree_and_working_copy,
36
from ....tests import TestCaseWithTransport
39
class GitWorkingTreeTests(TestCaseWithTransport):
42
super(GitWorkingTreeTests, self).setUp()
43
self.tree = self.make_branch_and_tree('.', format="git")
45
def test_conflict_list(self):
46
self.assertIsInstance(
47
self.tree.conflicts(),
48
_mod_conflicts.ConflictList)
50
def test_add_conflict(self):
51
self.build_tree(['conflicted'])
52
self.tree.add(['conflicted'])
53
with self.tree.lock_tree_write():
54
self.tree.index['conflicted'] = self.tree.index['conflicted'][:9] + (FLAG_STAGEMASK, )
55
conflicts = self.tree.conflicts()
56
self.assertEqual(1, len(conflicts))
58
def test_revert_empty(self):
59
self.build_tree(['a'])
61
self.assertTrue(self.tree.is_versioned('a'))
62
self.tree.revert(['a'])
63
self.assertFalse(self.tree.is_versioned('a'))
66
class ChangesBetweenGitTreeAndWorkingCopyTests(TestCaseWithTransport):
69
super(ChangesBetweenGitTreeAndWorkingCopyTests, self).setUp()
70
self.wt = self.make_branch_and_tree('.', format='git')
72
def expectDelta(self, expected_changes,
73
expected_extras=None, want_unversioned=False):
74
store = self.wt.branch.repository._git.object_store
76
tree_id = store[self.wt.branch.repository._git.head()].tree
79
changes, extras = changes_between_git_tree_and_working_copy(
80
store, tree_id, self.wt, want_unversioned=want_unversioned)
81
self.assertEqual(expected_changes, list(changes))
82
if expected_extras is None:
83
expected_extras = set()
84
self.assertEqual(set(expected_extras), set(extras))
88
[((None, ''), (None, stat.S_IFDIR), (None, Tree().id))])
90
def test_added_file(self):
91
self.build_tree(['a'])
93
a = Blob.from_string('contents of a\n')
95
t.add("a", stat.S_IFREG | 0o644, a.id)
97
[((None, ''), (None, stat.S_IFDIR), (None, t.id)),
98
((None, 'a'), (None, stat.S_IFREG | 0o644), (None, a.id))])
100
def test_added_unknown_file(self):
101
self.build_tree(['a'])
104
[((None, ''), (None, stat.S_IFDIR), (None, t.id))])
105
a = Blob.from_string('contents of a\n')
107
t.add("a", stat.S_IFREG | 0o644, a.id)
109
[((None, ''), (None, stat.S_IFDIR), (None, t.id)),
110
((None, 'a'), (None, stat.S_IFREG | 0o644), (None, a.id))],
112
want_unversioned=True)
114
def test_missing_added_file(self):
115
self.build_tree(['a'])
118
a = Blob.from_string('contents of a\n')
120
t.add("a", 0, ZERO_SHA)
122
[((None, ''), (None, stat.S_IFDIR), (None, t.id)),
123
((None, 'a'), (None, 0), (None, ZERO_SHA))],
126
def test_missing_versioned_file(self):
127
self.build_tree(['a'])
131
a = Blob.from_string('contents of a\n')
133
oldt.add("a", stat.S_IFREG | 0o644, a.id)
135
newt.add("a", 0, ZERO_SHA)
137
[(('', ''), (stat.S_IFDIR, stat.S_IFDIR), (oldt.id, newt.id)),
138
(('a', 'a'), (stat.S_IFREG|0o644, 0), (a.id, ZERO_SHA))])
140
def test_versioned_replace_by_dir(self):
141
self.build_tree(['a'])
146
olda = Blob.from_string('contents of a\n')
148
oldt.add("a", stat.S_IFREG | 0o644, olda.id)
151
newt.add("a", stat.S_IFDIR, newa.id)
154
(stat.S_IFDIR, stat.S_IFDIR),
156
(('a', 'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
157
], want_unversioned=False)
160
(stat.S_IFDIR, stat.S_IFDIR),
162
(('a', 'a'), (stat.S_IFREG | 0o644, stat.S_IFDIR), (olda.id, newa.id))
163
], want_unversioned=True)
165
def test_extra(self):
166
self.build_tree(['a'])
167
newa = Blob.from_string('contents of a\n')
169
newt.add("a", stat.S_IFREG | 0o644, newa.id)
172
(None, stat.S_IFDIR),
174
((None, 'a'), (None, stat.S_IFREG | 0o644), (None, newa.id))
175
], ['a'], want_unversioned=True)