1
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from breezy import merge, tests, transform, workingtree
22
class TestRevert(tests.TestCaseWithTransport):
23
"""Ensure that revert behaves as expected"""
25
def test_revert_merged_dir(self):
26
"""Reverting a merge that adds a directory deletes the directory"""
27
source_tree = self.make_branch_and_tree('source')
28
source_tree.commit('empty tree')
29
target_tree = source_tree.controldir.sprout(
30
'target').open_workingtree()
31
self.build_tree(['source/dir/', 'source/dir/contents'])
32
source_tree.add(['dir', 'dir/contents'], [b'dir-id', b'contents-id'])
33
source_tree.commit('added dir')
34
target_tree.lock_write()
35
self.addCleanup(target_tree.unlock)
36
merge.merge_inner(target_tree.branch, source_tree.basis_tree(),
37
target_tree.basis_tree(), this_tree=target_tree)
38
self.assertPathExists('target/dir')
39
self.assertPathExists('target/dir/contents')
41
self.assertPathDoesNotExist('target/dir/contents')
42
self.assertPathDoesNotExist('target/dir')
44
def test_revert_new(self):
45
"""Only locally-changed new files should be preserved when reverting
47
When a file isn't present in revert's target tree:
48
If a file hasn't been committed, revert should unversion it, but not
50
If a file has local changes, revert should unversion it, but not
52
If a file has no changes from the last commit, revert should delete it.
53
If a file has changes due to a merge, revert should delete it.
55
tree = self.make_branch_and_tree('tree')
56
tree.commit('empty tree')
57
merge_target = tree.controldir.sprout(
58
'merge_target').open_workingtree()
59
self.build_tree(['tree/new_file'])
61
# newly-added files should not be deleted
63
basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
65
self.assertPathExists('tree/new_file')
67
# unchanged files should be deleted
69
tree.commit('add new_file')
70
tree.revert(old_tree=basis_tree)
71
self.assertPathDoesNotExist('tree/new_file')
73
# files should be deleted if their changes came from merges
74
merge_target.merge_from_branch(tree.branch)
75
self.assertPathExists('merge_target/new_file')
77
self.assertPathDoesNotExist('merge_target/new_file')
79
# files should not be deleted if changed after a merge
80
merge_target.merge_from_branch(tree.branch)
81
self.assertPathExists('merge_target/new_file')
82
self.build_tree_contents([('merge_target/new_file', b'new_contents')])
84
self.assertPathExists('merge_target/new_file')
86
def tree_with_executable(self):
87
tree = self.make_branch_and_tree('tree')
88
tt = tree.get_transform()
89
tt.new_file('newfile', tt.root, [b'helooo!'], b'newfile-id', True)
91
with tree.lock_write():
92
self.assertTrue(tree.is_executable('newfile'))
93
tree.commit('added newfile')
96
def test_preserve_execute(self):
97
tree = self.tree_with_executable()
98
tt = tree.get_transform()
99
newfile = tt.trans_id_tree_path('newfile')
100
tt.delete_contents(newfile)
101
tt.create_file([b'Woooorld!'], newfile)
103
tree = workingtree.WorkingTree.open('tree')
105
self.addCleanup(tree.unlock)
106
self.assertTrue(tree.is_executable('newfile'))
107
transform.revert(tree, tree.basis_tree(), None, backups=True)
108
with tree.get_file('newfile', 'rb') as f:
109
self.assertEqual(b'helooo!', f.read())
110
self.assertTrue(tree.is_executable('newfile'))
112
def test_revert_executable(self):
113
tree = self.tree_with_executable()
114
tt = tree.get_transform()
115
newfile = tt.trans_id_tree_path('newfile')
116
tt.set_executability(False, newfile)
119
self.addCleanup(tree.unlock)
120
transform.revert(tree, tree.basis_tree(), None)
121
self.assertTrue(tree.is_executable('newfile'))
123
def test_revert_deletes_files_from_revert(self):
124
tree = self.make_branch_and_tree('.')
125
self.build_tree(['file'])
127
rev1 = tree.commit('added file')
128
with tree.lock_read():
129
file_sha = tree.get_file_sha1('file')
131
tree.commit('removed file')
132
self.assertPathDoesNotExist('file')
133
tree.revert(old_tree=tree.branch.repository.revision_tree(rev1))
134
self.assertEqual({'file': file_sha}, tree.merge_modified())
135
self.assertPathExists('file')
137
self.assertPathDoesNotExist('file')
138
self.assertEqual({}, tree.merge_modified())
140
def test_revert_file_in_deleted_dir(self):
141
tree = self.make_branch_and_tree('.')
142
self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
143
tree.add(['dir', 'dir/file1', 'dir/file2'],
144
[b'dir-id', b'file1-id', b'file2-id'])
145
tree.commit("Added files")
146
os.unlink('dir/file1')
147
os.unlink('dir/file2')
149
tree.remove(['dir/', 'dir/file1', 'dir/file2'])
150
tree.revert(['dir/file1'])
151
self.assertPathExists('dir/file1')
152
self.assertPathDoesNotExist('dir/file2')
153
self.assertEqual(b'dir-id', tree.path2id('dir'))
155
def test_revert_root_id_change(self):
156
tree = self.make_branch_and_tree('.')
157
tree.set_root_id(b'initial-root-id')
158
self.build_tree(['file1'])
161
tree.set_root_id(b'temp-root-id')
162
self.assertEqual(b'temp-root-id', tree.path2id(''))
164
self.assertEqual(b'initial-root-id', tree.path2id(''))