1
# Copyright (C) 2005 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
from bzrlib.add import smart_add
20
from bzrlib.branch import Branch
21
from bzrlib.builtins import merge
22
from bzrlib.delta import compare_trees
23
from bzrlib.fetch import greedy_fetch
24
from bzrlib.merge import merge_inner
25
from bzrlib.revision import common_ancestor
26
from bzrlib.tests import TestCaseWithTransport
27
from bzrlib.workingtree import WorkingTree
29
class FileIdInvolvedBase(TestCaseWithTransport):
31
def touch(self,filename):
32
f = file(filename,"a")
33
f.write("appended line\n")
36
def merge(self, branch_from, wt_to):
37
# minimal ui-less merge.
38
greedy_fetch(to_branch=wt_to.branch, from_branch=branch_from,
39
revision=branch_from.last_revision())
40
base_rev = common_ancestor(branch_from.last_revision(),
41
wt_to.branch.last_revision(),
42
wt_to.branch.repository)
43
merge_inner(wt_to.branch, branch_from.working_tree(),
44
wt_to.branch.repository.revision_tree(base_rev),
46
wt_to.add_pending_merge(branch_from.last_revision())
48
def compare_tree_fileids(self, branch, old_rev, new_rev):
49
old_tree = self.branch.repository.revision_tree(old_rev)
50
new_tree = self.branch.repository.revision_tree(new_rev)
51
delta = compare_trees(old_tree, new_tree)
53
l2 = [id for path, id, kind in delta.added] + \
54
[id for oldpath, newpath, id, kind, text_modified, \
55
meta_modified in delta.renamed] + \
56
[id for path, id, kind, text_modified, meta_modified in \
61
class TestFileIdInvolved(FileIdInvolvedBase):
64
super(TestFileIdInvolved, self).setUp()
65
# create three branches, and merge it
67
# /-->J ------>K (branch2)
69
# A ---> B --->C ---->D->G (main)
71
# \---> E---/----> F (branch1)
73
main_wt = self.make_branch_and_tree('main')
74
main_branch = main_wt.branch
75
self.build_tree(["main/a","main/b","main/c"])
77
main_wt.add(['a', 'b', 'c'], ['a-file-id-2006-01-01-abcd',
78
'b-file-id-2006-01-01-defg',
79
'c-funky<file-id> quiji%bo'])
80
main_wt.commit("Commit one", rev_id="rev-A")
81
#-------- end A -----------
83
b1 = main_branch.clone("branch1")
84
self.build_tree(["branch1/d"])
85
b1.working_tree().add('d')
86
b1.working_tree().commit("branch1, Commit one", rev_id="rev-E")
88
#-------- end E -----------
91
main_wt.commit("Commit two", rev_id="rev-B")
93
#-------- end B -----------
95
branch2_branch = main_branch.clone("branch2")
96
os.chmod("branch2/b",0770)
97
branch2_branch.working_tree().commit("branch2, Commit one",
100
#-------- end J -----------
102
self.merge(b1, main_wt)
103
main_wt.commit("merge branch1, rev-11", rev_id="rev-C")
105
#-------- end C -----------
107
tree = WorkingTree('branch1', b1)
108
tree.rename_one("d","e")
109
tree.commit("branch1, commit two", rev_id="rev-F")
111
#-------- end F -----------
113
self.touch("branch2/c")
114
branch2_branch.working_tree().commit("branch2, commit two", rev_id="rev-K")
116
#-------- end K -----------
119
self.merge(b1, main_wt)
120
# D gets some funky characters to make sure the unescaping works
121
main_wt.commit("merge branch1, rev-12", rev_id="rev-<D>")
125
self.merge(branch2_branch, main_wt)
126
main_wt.commit("merge branch1, rev-22", rev_id="rev-G")
129
self.branch = main_branch
132
def test_fileid_involved_all_revs(self):
134
l = self.branch.fileid_involved( )
135
self.assertEquals( sorted(map( lambda x: x[0], l )), ["a","b","c","d"])
137
def test_fileid_involved_one_rev(self):
139
l = self.branch.fileid_involved("rev-B" )
140
self.assertEquals( sorted(map( lambda x: x[0], l )), ["a","b","c"])
142
def test_fileid_involved_two_revs(self):
144
l = self.branch.fileid_involved_between_revs("rev-B","rev-K" )
145
self.assertEquals( sorted(map( lambda x: x[0], l )), ["b","c"])
147
l = self.branch.fileid_involved_between_revs("rev-C","rev-<D>" )
148
self.assertEquals( sorted(map( lambda x: x[0], l )), ["b","d"])
150
l = self.branch.fileid_involved_between_revs("rev-C","rev-G" )
151
self.assertEquals( sorted(map( lambda x: x[0], l )), ["b","c","d"])
153
l = self.branch.fileid_involved_between_revs("rev-E","rev-G" )
154
self.assertEquals( sorted(map( lambda x: x[0], l )), ["a", "b","c","d"])
156
def test_fileid_involved_sets(self):
158
l = self.branch.fileid_involved_by_set(set(["rev-B"]))
159
self.assertEquals( sorted(map( lambda x: x[0], l )), ["a"])
161
l = self.branch.fileid_involved_by_set(set(["rev-<D>"]))
162
self.assertEquals( sorted(map( lambda x: x[0], l )), ["b"])
164
def test_fileid_involved_compare(self):
166
l1 = self.branch.fileid_involved_between_revs("rev-E", "rev-<D>")
167
l2 = self.branch.fileid_involved_by_set(set(["rev-<D>","rev-F","rev-C","rev-B"]))
168
self.assertEquals( l1, l2 )
170
l1 = self.branch.fileid_involved_between_revs("rev-C", "rev-G")
171
l2 = self.branch.fileid_involved_by_set(
172
set(["rev-G","rev-<D>","rev-F","rev-K","rev-J"]))
173
self.assertEquals( l1, l2 )
175
def test_fileid_involved_full_compare(self):
176
from bzrlib.tsort import topo_sort
178
history = self.branch.revision_history( )
180
if len(history) < 2: return
182
for start in range(0,len(history)-1):
183
start_id = history[start]
184
for end in range(start+1,len(history)):
185
end_id = history[end]
186
l1 = self.branch.fileid_involved_between_revs(start_id, end_id)
188
l2 = self.compare_tree_fileids(self.branch, start_id, end_id)
189
self.assertEquals(l1, l2)
192
class TestFileIdInvolvedSuperset(FileIdInvolvedBase):
195
super(TestFileIdInvolvedSuperset, self).setUp()
197
main_wt = self.make_branch_and_tree('main')
198
main_branch = main_wt.branch
199
self.build_tree(["main/a","main/b","main/c"])
201
main_wt.add(['a', 'b', 'c'], ['a-file-id-2006-01-01-abcd',
202
'b-file-id-2006-01-01-defg',
203
'c-funky<file-id> quiji%bo'])
204
main_wt.commit("Commit one", rev_id="rev-A")
206
branch2_branch = main_branch.clone("branch2")
207
os.chmod("branch2/b",0770)
208
branch2_branch.working_tree().commit("branch2, Commit one",
211
self.merge(branch2_branch, main_wt)
212
os.chmod("main/b",0660)
213
main_wt.commit("merge branch1, rev-22", rev_id="rev-G")
216
self.branch = main_branch
218
def test_fileid_involved_full_compare2(self):
219
history = self.branch.revision_history()
223
l1 = self.branch.fileid_involved_between_revs(old_rev, new_rev)
225
l2 = self.compare_tree_fileids(self.branch, old_rev, new_rev)
226
self.assertNotEqual(l2, l1)
227
self.AssertSubset(l2, l1)