1
# Copyright (C) 2005-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
24
revision as _mod_revision,
28
from bzrlib.tests import per_repository
31
class FileIdInvolvedBase(per_repository.TestCaseWithRepository):
33
def touch(self, tree, filename):
34
# use the trees transport to not depend on the tree's location or type.
35
tree.bzrdir.root_transport.append_bytes(filename, "appended line\n")
37
def compare_tree_fileids(self, branch, old_rev, new_rev):
38
old_tree = self.branch.repository.revision_tree(old_rev)
39
new_tree = self.branch.repository.revision_tree(new_rev)
40
delta = new_tree.changes_from(old_tree)
42
l2 = [id for path, id, kind in delta.added] + \
43
[id for oldpath, newpath, id, kind, text_modified, \
44
meta_modified in delta.renamed] + \
45
[id for path, id, kind, text_modified, meta_modified in \
50
class TestFileIdInvolved(FileIdInvolvedBase):
53
super(TestFileIdInvolved, self).setUp()
54
# create three branches, and merge it
56
# ,-->J------>K (branch2)
58
# A --->B --->C---->D-->G (main)
60
# '--->E---+---->F (branch1)
63
# B changes: 'a-file-id-2006-01-01-abcd'
64
# C changes: Nothing (perfect merge)
65
# D changes: 'b-file-id-2006-01-01-defg'
68
# G changes: 'b-file-id-2006-01-01-defg'
69
# J changes: 'b-file-id-2006-01-01-defg'
70
# K changes: 'c-funky<file-id>quiji%bo'
72
main_wt = self.make_branch_and_tree('main')
73
main_branch = main_wt.branch
74
self.build_tree(["main/a","main/b","main/c"])
76
main_wt.add(['a', 'b', 'c'], ['a-file-id-2006-01-01-abcd',
77
'b-file-id-2006-01-01-defg',
78
'c-funky<file-id>quiji%bo'])
80
main_wt.commit("Commit one", rev_id="rev-A")
81
except errors.IllegalPath:
82
# TODO: jam 20060701 Consider raising a different exception
83
# newer formats do support this, and nothin can done to
84
# correct this test - its not a bug.
85
if sys.platform == 'win32':
86
raise tests.TestSkipped('Old repository formats do not'
87
' support file ids with <> on win32')
88
# This is not a known error condition
91
#-------- end A -----------
93
bt1 = self.make_branch_and_tree('branch1')
96
self.build_tree(["branch1/d"])
97
bt1.add(['d'], ['file-d'])
98
bt1.commit("branch1, Commit one", rev_id="rev-E")
100
#-------- end E -----------
102
self.touch(main_wt, "a")
103
main_wt.commit("Commit two", rev_id="rev-B")
105
#-------- end B -----------
107
bt2 = self.make_branch_and_tree('branch2')
108
bt2.pull(main_branch)
109
branch2_branch = bt2.branch
110
set_executability(bt2, 'b', True)
111
bt2.commit("branch2, Commit one", rev_id="rev-J")
113
#-------- end J -----------
115
main_wt.merge_from_branch(b1)
116
main_wt.commit("merge branch1, rev-11", rev_id="rev-C")
118
#-------- end C -----------
120
bt1.rename_one("d","e")
121
bt1.commit("branch1, commit two", rev_id="rev-F")
123
#-------- end F -----------
126
bt2.commit("branch2, commit two", rev_id="rev-K")
128
#-------- end K -----------
130
main_wt.merge_from_branch(b1)
131
self.touch(main_wt, "b")
132
# D gets some funky characters to make sure the unescaping works
133
main_wt.commit("merge branch1, rev-12", rev_id="rev-<D>")
137
main_wt.merge_from_branch(branch2_branch)
138
main_wt.commit("merge branch1, rev-22", rev_id="rev-G")
141
self.branch = main_branch
143
def test_fileids_altered_between_two_revs(self):
144
self.branch.lock_read()
145
self.addCleanup(self.branch.unlock)
146
self.branch.repository.fileids_altered_by_revision_ids(["rev-J","rev-K"])
148
{'b-file-id-2006-01-01-defg':set(['rev-J']),
149
'c-funky<file-id>quiji%bo':set(['rev-K'])
151
self.branch.repository.fileids_altered_by_revision_ids(["rev-J","rev-K"]))
154
{'b-file-id-2006-01-01-defg': set(['rev-<D>']),
155
'file-d': set(['rev-F']),
157
self.branch.repository.fileids_altered_by_revision_ids(['rev-<D>', 'rev-F']))
161
'b-file-id-2006-01-01-defg': set(['rev-<D>', 'rev-G', 'rev-J']),
162
'c-funky<file-id>quiji%bo': set(['rev-K']),
163
'file-d': set(['rev-F']),
165
self.branch.repository.fileids_altered_by_revision_ids(
166
['rev-<D>', 'rev-G', 'rev-F', 'rev-K', 'rev-J']))
169
{'a-file-id-2006-01-01-abcd': set(['rev-B']),
170
'b-file-id-2006-01-01-defg': set(['rev-<D>', 'rev-G', 'rev-J']),
171
'c-funky<file-id>quiji%bo': set(['rev-K']),
172
'file-d': set(['rev-F']),
174
self.branch.repository.fileids_altered_by_revision_ids(
175
['rev-G', 'rev-F', 'rev-C', 'rev-B', 'rev-<D>', 'rev-K', 'rev-J']))
177
def fileids_altered_by_revision_ids(self, revision_ids):
178
"""This is a wrapper to strip TREE_ROOT if it occurs"""
179
repo = self.branch.repository
180
root_id = self.branch.basis_tree().get_root_id()
181
result = repo.fileids_altered_by_revision_ids(revision_ids)
182
if root_id in result:
186
def test_fileids_altered_by_revision_ids(self):
187
self.branch.lock_read()
188
self.addCleanup(self.branch.unlock)
190
{'a-file-id-2006-01-01-abcd':set(['rev-A']),
191
'b-file-id-2006-01-01-defg': set(['rev-A']),
192
'c-funky<file-id>quiji%bo': set(['rev-A']),
194
self.fileids_altered_by_revision_ids(["rev-A"]))
196
{'a-file-id-2006-01-01-abcd':set(['rev-B'])
198
self.branch.repository.fileids_altered_by_revision_ids(["rev-B"]))
200
{'b-file-id-2006-01-01-defg':set(['rev-<D>'])
202
self.branch.repository.fileids_altered_by_revision_ids(["rev-<D>"]))
204
def test_fileids_involved_full_compare(self):
205
# this tests that the result of each fileid_involved calculation
206
# along a revision history selects only the fileids selected by
207
# comparing the trees - no less, and no more. This is correct
208
# because in our sample data we do not revert any file ids along
209
# the revision history.
210
self.branch.lock_read()
211
self.addCleanup(self.branch.unlock)
213
history = self.branch.revision_history( )
215
if len(history) < 2: return
217
graph = self.branch.repository.get_graph()
218
for start in range(0,len(history)-1):
219
start_id = history[start]
220
for end in range(start+1,len(history)):
221
end_id = history[end]
222
unique_revs = graph.find_unique_ancestors(end_id, [start_id])
223
l1 = self.branch.repository.fileids_altered_by_revision_ids(
226
l2 = self.compare_tree_fileids(self.branch, start_id, end_id)
227
self.assertEquals(l1, l2)
230
class TestFileIdInvolvedNonAscii(FileIdInvolvedBase):
232
def test_utf8_file_ids_and_revision_ids(self):
233
main_wt = self.make_branch_and_tree('main')
234
main_branch = main_wt.branch
235
self.build_tree(["main/a"])
237
file_id = u'a-f\xedle-id'.encode('utf8')
238
main_wt.add(['a'], [file_id])
239
revision_id = u'r\xe9v-a'.encode('utf8')
241
main_wt.commit('a', rev_id=revision_id)
242
except errors.NonAsciiRevisionId:
243
raise tests.TestSkipped('non-ascii revision ids not supported by %s'
244
% self.repository_format)
246
repo = main_wt.branch.repository
248
self.addCleanup(repo.unlock)
249
file_ids = repo.fileids_altered_by_revision_ids([revision_id])
250
root_id = main_wt.basis_tree().get_root_id()
251
if root_id in file_ids:
252
self.assertEqual({file_id:set([revision_id]),
253
root_id:set([revision_id])
256
self.assertEqual({file_id:set([revision_id])}, file_ids)
259
class TestFileIdInvolvedSuperset(FileIdInvolvedBase):
262
super(TestFileIdInvolvedSuperset, self).setUp()
265
main_wt = self.make_branch_and_tree('main')
266
main_branch = main_wt.branch
267
self.build_tree(["main/a","main/b","main/c"])
269
main_wt.add(['a', 'b', 'c'], ['a-file-id-2006-01-01-abcd',
270
'b-file-id-2006-01-01-defg',
271
'c-funky<file-id>quiji\'"%bo'])
273
main_wt.commit("Commit one", rev_id="rev-A")
274
except errors.IllegalPath:
275
# TODO: jam 20060701 Consider raising a different exception
276
# newer formats do support this, and nothin can done to
277
# correct this test - its not a bug.
278
if sys.platform == 'win32':
279
raise tests.TestSkipped('Old repository formats do not'
280
' support file ids with <> on win32')
281
# This is not a known error condition
284
branch2_wt = self.make_branch_and_tree('branch2')
285
branch2_wt.pull(main_branch)
286
branch2_bzrdir = branch2_wt.bzrdir
287
branch2_branch = branch2_bzrdir.open_branch()
288
set_executability(branch2_wt, 'b', True)
289
branch2_wt.commit("branch2, Commit one", rev_id="rev-J")
291
main_wt.merge_from_branch(branch2_branch)
292
set_executability(main_wt, 'b', False)
293
main_wt.commit("merge branch1, rev-22", rev_id="rev-G")
296
self.branch = main_branch
298
def test_fileid_involved_full_compare2(self):
299
# this tests that fileids_altered_by_revision_ids returns
300
# more information than compare_tree can, because it
301
# sees each change rather than the aggregate delta.
302
self.branch.lock_read()
303
self.addCleanup(self.branch.unlock)
304
history = self.branch.revision_history()
307
graph = self.branch.repository.get_graph()
308
unique_revs = graph.find_unique_ancestors(new_rev, [old_rev])
310
l1 = self.branch.repository.fileids_altered_by_revision_ids(
314
l2 = self.compare_tree_fileids(self.branch, old_rev, new_rev)
315
self.assertNotEqual(l2, l1)
316
self.assertSubset(l2, l1)
319
class FileIdInvolvedWGhosts(per_repository.TestCaseWithRepository):
321
def create_branch_with_ghost_text(self):
322
builder = self.make_branch_builder('ghost')
323
builder.build_snapshot('A-id', None, [
324
('add', ('', 'root-id', 'directory', None)),
325
('add', ('a', 'a-file-id', 'file', 'some content\n'))])
326
b = builder.get_branch()
327
old_rt = b.repository.revision_tree('A-id')
328
new_inv = inventory.mutable_inventory_from_tree(old_rt)
329
new_inv.revision_id = 'B-id'
330
new_inv['a-file-id'].revision = 'ghost-id'
331
new_rev = _mod_revision.Revision('B-id',
332
timestamp=time.time(),
334
message='Committing against a ghost',
335
committer='Joe Foo <joe@foo.com>',
337
parent_ids=('A-id', 'ghost-id'),
340
self.addCleanup(b.unlock)
341
b.repository.start_write_group()
342
b.repository.add_revision('B-id', new_rev, new_inv)
343
self.disable_commit_write_group_paranoia(b.repository)
344
b.repository.commit_write_group()
347
def disable_commit_write_group_paranoia(self, repo):
348
if isinstance(repo, remote.RemoteRepository):
349
# We can't easily disable the checks in a remote repo.
350
repo.abort_write_group()
351
raise tests.TestSkipped(
352
"repository format does not support storing revisions with "
354
pack_coll = getattr(repo, '_pack_collection', None)
355
if pack_coll is not None:
356
# Monkey-patch the pack collection instance to allow storing
357
# incomplete revisions.
358
pack_coll._check_new_inventories = lambda: []
360
def test_file_ids_include_ghosts(self):
361
b = self.create_branch_with_ghost_text()
364
{'a-file-id':set(['ghost-id'])},
365
repo.fileids_altered_by_revision_ids(['B-id']))
367
def test_file_ids_uses_fallbacks(self):
368
builder = self.make_branch_builder('source',
369
format=self.bzrdir_format)
370
repo = builder.get_branch().repository
371
if not repo._format.supports_external_lookups:
372
raise tests.TestNotApplicable('format does not support stacking')
373
builder.start_series()
374
builder.build_snapshot('A-id', None, [
375
('add', ('', 'root-id', 'directory', None)),
376
('add', ('file', 'file-id', 'file', 'contents\n'))])
377
builder.build_snapshot('B-id', ['A-id'], [
378
('modify', ('file-id', 'new-content\n'))])
379
builder.build_snapshot('C-id', ['B-id'], [
380
('modify', ('file-id', 'yet more content\n'))])
381
builder.finish_series()
382
source_b = builder.get_branch()
384
self.addCleanup(source_b.unlock)
385
base = self.make_branch('base')
386
base.pull(source_b, stop_revision='B-id')
387
stacked = self.make_branch('stacked')
388
stacked.set_stacked_on_url('../base')
389
stacked.pull(source_b, stop_revision='C-id')
392
self.addCleanup(stacked.unlock)
393
repo = stacked.repository
394
keys = {'file-id': set(['A-id'])}
395
if stacked.repository.supports_rich_root():
396
keys['root-id'] = set(['A-id'])
397
self.assertEqual(keys, repo.fileids_altered_by_revision_ids(['A-id']))
400
def set_executability(wt, path, executable=True):
401
"""Set the executable bit for the file at path in the working tree
403
os.chmod() doesn't work on windows. But TreeTransform can mark or
404
unmark a file as executable.
406
file_id = wt.path2id(path)
407
tt = transform.TreeTransform(wt)
409
tt.set_executability(executable, tt.trans_id_tree_file_id(file_id))