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('target').open_workingtree()
30
self.build_tree(['source/dir/', 'source/dir/contents'])
31
source_tree.add(['dir', 'dir/contents'], [b'dir-id', b'contents-id'])
32
source_tree.commit('added dir')
33
target_tree.lock_write()
34
self.addCleanup(target_tree.unlock)
35
merge.merge_inner(target_tree.branch, source_tree.basis_tree(),
36
target_tree.basis_tree(), this_tree=target_tree)
37
self.assertPathExists('target/dir')
38
self.assertPathExists('target/dir/contents')
40
self.assertPathDoesNotExist('target/dir/contents')
41
self.assertPathDoesNotExist('target/dir')
43
def test_revert_new(self):
44
"""Only locally-changed new files should be preserved when reverting
46
When a file isn't present in revert's target tree:
47
If a file hasn't been committed, revert should unversion it, but not
49
If a file has local changes, revert should unversion it, but not
51
If a file has no changes from the last commit, revert should delete it.
52
If a file has changes due to a merge, revert should delete it.
54
tree = self.make_branch_and_tree('tree')
55
tree.commit('empty tree')
56
merge_target = tree.controldir.sprout('merge_target').open_workingtree()
57
self.build_tree(['tree/new_file'])
59
# newly-added files should not be deleted
61
basis_tree = tree.branch.repository.revision_tree(tree.last_revision())
63
self.assertPathExists('tree/new_file')
65
# unchanged files should be deleted
67
tree.commit('add new_file')
68
tree.revert(old_tree=basis_tree)
69
self.assertPathDoesNotExist('tree/new_file')
71
# files should be deleted if their changes came from merges
72
merge_target.merge_from_branch(tree.branch)
73
self.assertPathExists('merge_target/new_file')
75
self.assertPathDoesNotExist('merge_target/new_file')
77
# files should not be deleted if changed after a merge
78
merge_target.merge_from_branch(tree.branch)
79
self.assertPathExists('merge_target/new_file')
80
self.build_tree_contents([('merge_target/new_file', b'new_contents')])
82
self.assertPathExists('merge_target/new_file')
84
def tree_with_executable(self):
85
tree = self.make_branch_and_tree('tree')
86
tt = transform.TreeTransform(tree)
87
tt.new_file('newfile', tt.root, [b'helooo!'], b'newfile-id', True)
89
with tree.lock_write():
90
self.assertTrue(tree.is_executable('newfile'))
91
tree.commit('added newfile')
94
def test_preserve_execute(self):
95
tree = self.tree_with_executable()
96
tt = transform.TreeTransform(tree)
97
newfile = tt.trans_id_tree_path('newfile')
98
tt.delete_contents(newfile)
99
tt.create_file([b'Woooorld!'], newfile)
101
tree = workingtree.WorkingTree.open('tree')
103
self.addCleanup(tree.unlock)
104
self.assertTrue(tree.is_executable('newfile'))
105
transform.revert(tree, tree.basis_tree(), None, backups=True)
106
self.assertEqual('helooo!', tree.get_file('newfile').read())
107
self.assertTrue(tree.is_executable('newfile'))
109
def test_revert_executable(self):
110
tree = self.tree_with_executable()
111
tt = transform.TreeTransform(tree)
112
newfile = tt.trans_id_tree_path('newfile')
113
tt.set_executability(False, newfile)
116
self.addCleanup(tree.unlock)
117
transform.revert(tree, tree.basis_tree(), None)
118
self.assertTrue(tree.is_executable('newfile'))
120
def test_revert_deletes_files_from_revert(self):
121
tree = self.make_branch_and_tree('.')
122
self.build_tree(['file'])
124
tree.commit('added file', rev_id=b'rev1')
126
tree.commit('removed file')
127
self.assertPathDoesNotExist('file')
128
tree.revert(old_tree=tree.branch.repository.revision_tree(b'rev1'))
129
self.assertPathExists('file')
131
self.assertPathDoesNotExist('file')
132
self.assertEqual({}, tree.merge_modified())
134
def test_revert_file_in_deleted_dir(self):
135
tree = self.make_branch_and_tree('.')
136
self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
137
tree.add(['dir', 'dir/file1', 'dir/file2'],
138
[b'dir-id', b'file1-id', b'file2-id'])
139
tree.commit("Added files")
140
os.unlink('dir/file1')
141
os.unlink('dir/file2')
143
tree.remove(['dir/', 'dir/file1', 'dir/file2'])
144
tree.revert(['dir/file1'])
145
self.assertPathExists('dir/file1')
146
self.assertPathDoesNotExist('dir/file2')
147
self.assertEqual(b'dir-id', tree.path2id('dir'))
149
def test_revert_root_id_change(self):
150
tree = self.make_branch_and_tree('.')
151
tree.set_root_id(b'initial-root-id')
152
self.build_tree(['file1'])
155
tree.set_root_id(b'temp-root-id')
156
self.assertEqual(b'temp-root-id', tree.get_root_id())
158
self.assertEqual(b'initial-root-id', tree.get_root_id())