13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
60
63
AttribFeature = _AttribFeature()
63
class _CompiledPatienceDiffFeature(Feature):
67
import bzrlib._patiencediff_c
72
def feature_name(self):
73
return 'bzrlib._patiencediff_c'
75
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
66
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
67
'bzrlib._patiencediff_c')
78
70
def udiff_lines(old, new, allow_binary=False):
397
389
self.wt.rename_one('file1', 'file1b')
398
390
old_tree = self.b.repository.revision_tree('rev-1')
399
391
new_tree = self.b.repository.revision_tree('rev-4')
400
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
392
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
401
393
working_tree=self.wt)
402
394
self.assertContainsRe(out, 'file1\t')
409
401
self.wt.rename_one('file1', 'dir1/file1')
410
402
old_tree = self.b.repository.revision_tree('rev-1')
411
403
new_tree = self.b.repository.revision_tree('rev-4')
412
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
404
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
413
405
working_tree=self.wt)
414
406
self.assertContainsRe(out, 'file1\t')
415
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
407
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
416
408
working_tree=self.wt)
417
409
self.assertNotContainsRe(out, 'file1\t')
787
779
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
788
780
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
789
781
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
790
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
782
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
792
784
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
808
800
test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
809
801
(4, 6), (5, 7), (6, 8)])
811
# recurse_matches doesn't match non-unique
803
# recurse_matches doesn't match non-unique
812
804
# lines surrounded by bogus text.
813
805
# The update has been done in patiencediff.SequenceMatcher instead
951
943
('delete', 1,2, 1,1),
952
944
('equal', 2,3, 1,2),
954
chk_ops('aBccDe', 'abccde',
946
chk_ops('aBccDe', 'abccde',
955
947
[('equal', 0,1, 0,1),
956
948
('replace', 1,5, 1,5),
957
949
('equal', 5,6, 5,6),
959
chk_ops('aBcDec', 'abcdec',
951
chk_ops('aBcDec', 'abcdec',
960
952
[('equal', 0,1, 0,1),
961
953
('replace', 1,2, 1,2),
962
954
('equal', 2,3, 2,3),
963
955
('replace', 3,4, 3,4),
964
956
('equal', 4,6, 4,6),
966
chk_ops('aBcdEcdFg', 'abcdecdfg',
958
chk_ops('aBcdEcdFg', 'abcdecdfg',
967
959
[('equal', 0,1, 0,1),
968
960
('replace', 1,8, 1,8),
969
961
('equal', 8,9, 8,9)
971
chk_ops('aBcdEeXcdFg', 'abcdecdfg',
963
chk_ops('aBcdEeXcdFg', 'abcdecdfg',
972
964
[('equal', 0,1, 0,1),
973
965
('replace', 1,2, 1,2),
974
966
('equal', 2,4, 2,4),
1296
1288
def test_from_string(self):
1297
1289
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1298
1290
self.addCleanup(diff_obj.finish)
1299
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1291
self.assertEqual(['diff', '@old_path', '@new_path'],
1300
1292
diff_obj.command_template)
1302
1294
def test_from_string_u5(self):
1303
1295
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1304
1296
self.addCleanup(diff_obj.finish)
1305
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1297
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1306
1298
diff_obj.command_template)
1307
1299
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1308
1300
diff_obj._get_command('old-path', 'new-path'))
1334
1326
tree.commit('old tree')
1335
1327
tree.lock_read()
1336
1328
self.addCleanup(tree.unlock)
1329
basis_tree = tree.basis_tree()
1330
basis_tree.lock_read()
1331
self.addCleanup(basis_tree.unlock)
1337
1332
diff_obj = DiffFromTool(['python', '-c',
1338
'print "%(old_path)s %(new_path)s"'],
1333
'print "@old_path @new_path"'],
1334
basis_tree, tree, output)
1340
1335
diff_obj._prepare_files('file-id', 'file', 'file')
1341
self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
1342
self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1336
# The old content should be readonly
1337
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1339
# The new content should use the tree object, not a 'new' file anymore
1340
self.assertEndsWith(tree.basedir, 'work/tree')
1341
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1344
1343
def assertReadableByAttrib(self, cwd, relpath, regex):
1345
1344
proc = subprocess.Popen(['attrib', relpath],
1346
1345
stdout=subprocess.PIPE,
1349
result = proc.stdout.read()
1350
self.assertContainsRe(result, regex)
1347
(result, err) = proc.communicate()
1348
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1352
1350
def test_prepare_files(self):
1353
1351
output = StringIO()
1376
1374
self.assertContainsRe(old_path, 'old/oldname$')
1377
1375
self.assertEqual(0, os.stat(old_path).st_mtime)
1378
self.assertContainsRe(new_path, 'new/newname$')
1376
self.assertContainsRe(new_path, 'tree/newname$')
1379
1377
self.assertFileEqual('oldcontent', old_path)
1380
1378
self.assertFileEqual('newcontent', new_path)
1381
1379
if osutils.host_os_dereferences_symlinks():
1382
1380
self.assertTrue(os.path.samefile('tree/newname', new_path))
1383
1381
# make sure we can create files with the same parent directories
1384
1382
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1385
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1387
def test_basic(self):
1388
tree = self.make_branch_and_tree('tree')
1389
(old_tree, new_tree,
1390
old_branch, new_branch,
1391
specific_files, extra_trees) = \
1392
get_trees_and_branches_to_diff(['tree'], None, None, None)
1394
self.assertIsInstance(old_tree, RevisionTree)
1395
#print dir (old_tree)
1396
self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1397
self.assertEqual(tree.basedir, new_tree.basedir)
1398
self.assertEqual(tree.branch.base, old_branch.base)
1399
self.assertEqual(tree.branch.base, new_branch.base)
1400
self.assertIs(None, specific_files)
1401
self.assertIs(None, extra_trees)
1403
def test_with_rev_specs(self):
1404
tree = self.make_branch_and_tree('tree')
1405
self.build_tree_contents([('tree/file', 'oldcontent')])
1406
tree.add('file', 'file-id')
1407
tree.commit('old tree', timestamp=0, rev_id="old-id")
1408
self.build_tree_contents([('tree/file', 'newcontent')])
1409
tree.commit('new tree', timestamp=0, rev_id="new-id")
1411
revisions = [RevisionSpec.from_string('1'),
1412
RevisionSpec.from_string('2')]
1413
(old_tree, new_tree,
1414
old_branch, new_branch,
1415
specific_files, extra_trees) = \
1416
get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1418
self.assertIsInstance(old_tree, RevisionTree)
1419
self.assertEqual("old-id", old_tree.get_revision_id())
1420
self.assertIsInstance(new_tree, RevisionTree)
1421
self.assertEqual("new-id", new_tree.get_revision_id())
1422
self.assertEqual(tree.branch.base, old_branch.base)
1423
self.assertEqual(tree.branch.base, new_branch.base)
1424
self.assertIs(None, specific_files)
1425
self.assertEqual(tree.basedir, extra_trees[0].basedir)