1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
|
# Copyright (C) 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
import bisect
import os
import time
from bzrlib import (
dirstate,
errors,
osutils,
)
from bzrlib.memorytree import MemoryTree
from bzrlib.osutils import has_symlinks
from bzrlib.tests import (
TestCase,
TestCaseWithTransport,
TestSkipped,
)
# TODO:
# TESTS to write:
# general checks for NOT_IN_MEMORY error conditions.
# set_path_id on a NOT_IN_MEMORY dirstate
# set_path_id unicode support
# set_path_id setting id of a path not root
# set_path_id setting id when there are parents without the id in the parents
# set_path_id setting id when there are parents with the id in the parents
# set_path_id setting id when state is not in memory
# set_path_id setting id when state is in memory unmodified
# set_path_id setting id when state is in memory modified
class TestCaseWithDirState(TestCaseWithTransport):
"""Helper functions for creating DirState objects with various content."""
def create_empty_dirstate(self):
"""Return a locked but empty dirstate"""
state = dirstate.DirState.initialize('dirstate')
return state
def create_dirstate_with_root(self):
"""Return a write-locked state with a single root entry."""
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
root_entry_direntry = ('', '', 'a-root-value'), [
('d', '', 0, False, packed_stat),
]
dirblocks = []
dirblocks.append(('', [root_entry_direntry]))
dirblocks.append(('', []))
state = self.create_empty_dirstate()
try:
state._set_data([], dirblocks)
state._validate()
except:
state.unlock()
raise
return state
def create_dirstate_with_root_and_subdir(self):
"""Return a locked DirState with a root and a subdir"""
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
subdir_entry = ('', 'subdir', 'subdir-id'), [
('d', '', 0, False, packed_stat),
]
state = self.create_dirstate_with_root()
try:
dirblocks = list(state._dirblocks)
dirblocks[1][1].append(subdir_entry)
state._set_data([], dirblocks)
except:
state.unlock()
raise
return state
def create_complex_dirstate(self):
"""This dirstate contains multiple files and directories.
/ a-root-value
a/ a-dir
b/ b-dir
c c-file
d d-file
a/e/ e-dir
a/f f-file
b/g g-file
b/h\xc3\xa5 h-\xc3\xa5-file #This is u'\xe5' encoded into utf-8
Notice that a/e is an empty directory.
:return: The dirstate, still write-locked.
"""
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
root_entry = ('', '', 'a-root-value'), [
('d', '', 0, False, packed_stat),
]
a_entry = ('', 'a', 'a-dir'), [
('d', '', 0, False, packed_stat),
]
b_entry = ('', 'b', 'b-dir'), [
('d', '', 0, False, packed_stat),
]
c_entry = ('', 'c', 'c-file'), [
('f', null_sha, 10, False, packed_stat),
]
d_entry = ('', 'd', 'd-file'), [
('f', null_sha, 20, False, packed_stat),
]
e_entry = ('a', 'e', 'e-dir'), [
('d', '', 0, False, packed_stat),
]
f_entry = ('a', 'f', 'f-file'), [
('f', null_sha, 30, False, packed_stat),
]
g_entry = ('b', 'g', 'g-file'), [
('f', null_sha, 30, False, packed_stat),
]
h_entry = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file'), [
('f', null_sha, 40, False, packed_stat),
]
dirblocks = []
dirblocks.append(('', [root_entry]))
dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
dirblocks.append(('a', [e_entry, f_entry]))
dirblocks.append(('b', [g_entry, h_entry]))
state = dirstate.DirState.initialize('dirstate')
state._validate()
try:
state._set_data([], dirblocks)
except:
state.unlock()
raise
return state
def check_state_with_reopen(self, expected_result, state):
"""Check that state has current state expected_result.
This will check the current state, open the file anew and check it
again.
This function expects the current state to be locked for writing, and
will unlock it before re-opening.
This is required because we can't open a lock_read() while something
else has a lock_write().
write => mutually exclusive lock
read => shared lock
"""
# The state should already be write locked, since we just had to do
# some operation to get here.
assert state._lock_token is not None
try:
self.assertEqual(expected_result[0], state.get_parent_ids())
# there should be no ghosts in this tree.
self.assertEqual([], state.get_ghosts())
# there should be one fileid in this tree - the root of the tree.
self.assertEqual(expected_result[1], list(state._iter_entries()))
state.save()
finally:
state.unlock()
del state
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
self.assertEqual(expected_result[1], list(state._iter_entries()))
finally:
state.unlock()
def create_basic_dirstate(self):
"""Create a dirstate with a few files and directories.
a
b/
c
d/
e
f
"""
tree = self.make_branch_and_tree('tree')
paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'f']
file_ids = ['a-id', 'b-id', 'c-id', 'd-id', 'e-id', 'f-id']
self.build_tree(['tree/' + p for p in paths])
tree.set_root_id('TREE_ROOT')
tree.add([p.rstrip('/') for p in paths], file_ids)
tree.commit('initial', rev_id='rev-1')
revision_id = 'rev-1'
# a_packed_stat = dirstate.pack_stat(os.stat('tree/a'))
t = self.get_transport().clone('tree')
a_text = t.get_bytes('a')
a_sha = osutils.sha_string(a_text)
a_len = len(a_text)
# b_packed_stat = dirstate.pack_stat(os.stat('tree/b'))
# c_packed_stat = dirstate.pack_stat(os.stat('tree/b/c'))
c_text = t.get_bytes('b/c')
c_sha = osutils.sha_string(c_text)
c_len = len(c_text)
# d_packed_stat = dirstate.pack_stat(os.stat('tree/b/d'))
# e_packed_stat = dirstate.pack_stat(os.stat('tree/b/d/e'))
e_text = t.get_bytes('b/d/e')
e_sha = osutils.sha_string(e_text)
e_len = len(e_text)
# f_packed_stat = dirstate.pack_stat(os.stat('tree/f'))
f_text = t.get_bytes('f')
f_sha = osutils.sha_string(f_text)
f_len = len(f_text)
null_stat = dirstate.DirState.NULLSTAT
expected = {
'':(('', '', 'TREE_ROOT'), [
('d', '', 0, False, null_stat),
('d', '', 0, False, revision_id),
]),
'a':(('', 'a', 'a-id'), [
('f', '', 0, False, null_stat),
('f', a_sha, a_len, False, revision_id),
]),
'b':(('', 'b', 'b-id'), [
('d', '', 0, False, null_stat),
('d', '', 0, False, revision_id),
]),
'b/c':(('b', 'c', 'c-id'), [
('f', '', 0, False, null_stat),
('f', c_sha, c_len, False, revision_id),
]),
'b/d':(('b', 'd', 'd-id'), [
('d', '', 0, False, null_stat),
('d', '', 0, False, revision_id),
]),
'b/d/e':(('b/d', 'e', 'e-id'), [
('f', '', 0, False, null_stat),
('f', e_sha, e_len, False, revision_id),
]),
'f':(('', 'f', 'f-id'), [
('f', '', 0, False, null_stat),
('f', f_sha, f_len, False, revision_id),
]),
}
state = dirstate.DirState.from_tree(tree, 'dirstate')
try:
state.save()
finally:
state.unlock()
# Use a different object, to make sure nothing is pre-cached in memory.
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
self.addCleanup(state.unlock)
self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
state._dirblock_state)
# This is code is only really tested if we actually have to make more
# than one read, so set the page size to something smaller.
# We want it to contain about 2.2 records, so that we have a couple
# records that we can read per attempt
state._bisect_page_size = 200
return tree, state, expected
def create_duplicated_dirstate(self):
"""Create a dirstate with a deleted and added entries.
This grabs a basic_dirstate, and then removes and re adds every entry
with a new file id.
"""
tree, state, expected = self.create_basic_dirstate()
# Now we will just remove and add every file so we get an extra entry
# per entry. Unversion in reverse order so we handle subdirs
tree.unversion(['f-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'f'],
['a-id2', 'b-id2', 'c-id2', 'd-id2', 'e-id2', 'f-id2'])
# Update the expected dictionary.
for path in ['a', 'b', 'b/c', 'b/d', 'b/d/e', 'f']:
orig = expected[path]
path2 = path + '2'
# This record was deleted in the current tree
expected[path] = (orig[0], [dirstate.DirState.NULL_PARENT_DETAILS,
orig[1][1]])
new_key = (orig[0][0], orig[0][1], orig[0][2]+'2')
# And didn't exist in the basis tree
expected[path2] = (new_key, [orig[1][0],
dirstate.DirState.NULL_PARENT_DETAILS])
# We will replace the 'dirstate' file underneath 'state', but that is
# okay as lock as we unlock 'state' first.
state.unlock()
try:
new_state = dirstate.DirState.from_tree(tree, 'dirstate')
try:
new_state.save()
finally:
new_state.unlock()
finally:
# But we need to leave state in a read-lock because we already have
# a cleanup scheduled
state.lock_read()
return tree, state, expected
def create_renamed_dirstate(self):
"""Create a dirstate with a few internal renames.
This takes the basic dirstate, and moves the paths around.
"""
tree, state, expected = self.create_basic_dirstate()
# Rename a file
tree.rename_one('a', 'b/g')
# And a directory
tree.rename_one('b/d', 'h')
old_a = expected['a']
expected['a'] = (old_a[0], [('r', 'b/g', 0, False, ''), old_a[1][1]])
expected['b/g'] = (('b', 'g', 'a-id'), [old_a[1][0],
('r', 'a', 0, False, '')])
old_d = expected['b/d']
expected['b/d'] = (old_d[0], [('r', 'h', 0, False, ''), old_d[1][1]])
expected['h'] = (('', 'h', 'd-id'), [old_d[1][0],
('r', 'b/d', 0, False, '')])
old_e = expected['b/d/e']
expected['b/d/e'] = (old_e[0], [('r', 'h/e', 0, False, ''),
old_e[1][1]])
expected['h/e'] = (('h', 'e', 'e-id'), [old_e[1][0],
('r', 'b/d/e', 0, False, '')])
state.unlock()
try:
new_state = dirstate.DirState.from_tree(tree, 'dirstate')
try:
new_state.save()
finally:
new_state.unlock()
finally:
state.lock_read()
return tree, state, expected
class TestTreeToDirState(TestCaseWithDirState):
def test_empty_to_dirstate(self):
"""We should be able to create a dirstate for an empty tree."""
# There are no files on disk and no parents
tree = self.make_branch_and_tree('tree')
expected_result = ([], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
])])
state = dirstate.DirState.from_tree(tree, 'dirstate')
state._validate()
self.check_state_with_reopen(expected_result, state)
def test_1_parents_empty_to_dirstate(self):
# create a parent by doing a commit
tree = self.make_branch_and_tree('tree')
rev_id = tree.commit('first post').encode('utf8')
root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
expected_result = ([rev_id], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
('d', '', 0, False, rev_id), # first parent details
])])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
state.lock_read()
try:
state._validate()
finally:
state.unlock()
def test_2_parents_empty_to_dirstate(self):
# create a parent by doing a commit
tree = self.make_branch_and_tree('tree')
rev_id = tree.commit('first post')
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
rev_id2 = tree2.commit('second post', allow_pointless=True)
tree.merge_from_branch(tree2.branch)
expected_result = ([rev_id, rev_id2], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
('d', '', 0, False, rev_id), # first parent details
('d', '', 0, False, rev_id2), # second parent details
])])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
state.lock_read()
try:
state._validate()
finally:
state.unlock()
def test_empty_unknowns_are_ignored_to_dirstate(self):
"""We should be able to create a dirstate for an empty tree."""
# There are no files on disk and no parents
tree = self.make_branch_and_tree('tree')
self.build_tree(['tree/unknown'])
expected_result = ([], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
])])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
def get_tree_with_a_file(self):
tree = self.make_branch_and_tree('tree')
self.build_tree(['tree/a file'])
tree.add('a file', 'a file id')
return tree
def test_non_empty_no_parents_to_dirstate(self):
"""We should be able to create a dirstate for an empty tree."""
# There are files on disk and no parents
tree = self.get_tree_with_a_file()
expected_result = ([], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
]),
(('', 'a file', 'a file id'), # common
[('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
]),
])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
def test_1_parents_not_empty_to_dirstate(self):
# create a parent by doing a commit
tree = self.get_tree_with_a_file()
rev_id = tree.commit('first post').encode('utf8')
# change the current content to be different this will alter stat, sha
# and length:
self.build_tree_contents([('tree/a file', 'new content\n')])
expected_result = ([rev_id], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
('d', '', 0, False, rev_id), # first parent details
]),
(('', 'a file', 'a file id'), # common
[('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
rev_id), # first parent
]),
])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
def test_2_parents_not_empty_to_dirstate(self):
# create a parent by doing a commit
tree = self.get_tree_with_a_file()
rev_id = tree.commit('first post').encode('utf8')
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
# change the current content to be different this will alter stat, sha
# and length:
self.build_tree_contents([('tree2/a file', 'merge content\n')])
rev_id2 = tree2.commit('second post').encode('utf8')
tree.merge_from_branch(tree2.branch)
# change the current content to be different this will alter stat, sha
# and length again, giving us three distinct values:
self.build_tree_contents([('tree/a file', 'new content\n')])
expected_result = ([rev_id, rev_id2], [
(('', '', tree.path2id('')), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
('d', '', 0, False, rev_id), # first parent details
('d', '', 0, False, rev_id2), # second parent details
]),
(('', 'a file', 'a file id'), # common
[('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
rev_id), # first parent
('f', '314d796174c9412647c3ce07dfb5d36a94e72958', 14, False,
rev_id2), # second parent
]),
])
state = dirstate.DirState.from_tree(tree, 'dirstate')
self.check_state_with_reopen(expected_result, state)
def test_colliding_fileids(self):
# test insertion of parents creating several entries at the same path.
# we used to have a bug where they could cause the dirstate to break
# its ordering invariants.
# create some trees to test from
parents = []
for i in range(7):
tree = self.make_branch_and_tree('tree%d' % i)
self.build_tree(['tree%d/name' % i,])
tree.add(['name'], ['file-id%d' % i])
revision_id = 'revid-%d' % i
tree.commit('message', rev_id=revision_id)
parents.append((revision_id,
tree.branch.repository.revision_tree(revision_id)))
# now fold these trees into a dirstate
state = dirstate.DirState.initialize('dirstate')
try:
state.set_parent_trees(parents, [])
state._validate()
finally:
state.unlock()
class TestDirStateOnFile(TestCaseWithDirState):
def test_construct_with_path(self):
tree = self.make_branch_and_tree('tree')
state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
# we want to be able to get the lines of the dirstate that we will
# write to disk.
lines = state.get_lines()
state.unlock()
self.build_tree_contents([('dirstate', ''.join(lines))])
# get a state object
# no parents, default tree content
expected_result = ([], [
(('', '', tree.path2id('')), # common details
# current tree details, but new from_tree skips statting, it
# uses set_state_from_inventory, and thus depends on the
# inventory state.
[('d', '', 0, False, dirstate.DirState.NULLSTAT),
])
])
state = dirstate.DirState.on_file('dirstate')
state.lock_write() # check_state_with_reopen will save() and unlock it
self.check_state_with_reopen(expected_result, state)
def test_can_save_clean_on_file(self):
tree = self.make_branch_and_tree('tree')
state = dirstate.DirState.from_tree(tree, 'dirstate')
try:
# doing a save should work here as there have been no changes.
state.save()
# TODO: stat it and check it hasn't changed; may require waiting
# for the state accuracy window.
finally:
state.unlock()
def test_can_save_in_read_lock(self):
self.build_tree(['a-file'])
state = dirstate.DirState.initialize('dirstate')
try:
# No stat and no sha1 sum.
state.add('a-file', 'a-file-id', 'file', None, '')
state.save()
finally:
state.unlock()
# Now open in readonly mode
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
entry = state._get_entry(0, path_utf8='a-file')
# The current sha1 sum should be empty
self.assertEqual('', entry[1][0][1])
# We should have a real entry.
self.assertNotEqual((None, None), entry)
# Make sure everything is old enough
state._sha_cutoff_time()
state._cutoff_time += 10
sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
# We should have gotten a real sha1
self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
sha1sum)
# The dirblock has been updated
self.assertEqual(sha1sum, entry[1][0][1])
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
del entry
# Now, since we are the only one holding a lock, we should be able
# to save and have it written to disk
state.save()
finally:
state.unlock()
# Re-open the file, and ensure that the state has been updated.
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
entry = state._get_entry(0, path_utf8='a-file')
self.assertEqual(sha1sum, entry[1][0][1])
finally:
state.unlock()
def test_save_fails_quietly_if_locked(self):
"""If dirstate is locked, save will fail without complaining."""
self.build_tree(['a-file'])
state = dirstate.DirState.initialize('dirstate')
try:
# No stat and no sha1 sum.
state.add('a-file', 'a-file-id', 'file', None, '')
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
entry = state._get_entry(0, path_utf8='a-file')
sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
# We should have gotten a real sha1
self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
sha1sum)
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
# Now, before we try to save, grab another dirstate, and take out a
# read lock.
# TODO: jam 20070315 Ideally this would be locked by another
# process. To make sure the file is really OS locked.
state2 = dirstate.DirState.on_file('dirstate')
state2.lock_read()
try:
# This won't actually write anything, because it couldn't grab
# a write lock. But it shouldn't raise an error, either.
# TODO: jam 20070315 We should probably distinguish between
# being dirty because of 'update_entry'. And dirty
# because of real modification. So that save() *does*
# raise a real error if it fails when we have real
# modifications.
state.save()
finally:
state2.unlock()
finally:
state.unlock()
# The file on disk should not be modified.
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
entry = state._get_entry(0, path_utf8='a-file')
self.assertEqual('', entry[1][0][1])
finally:
state.unlock()
class TestDirStateInitialize(TestCaseWithDirState):
def test_initialize(self):
expected_result = ([], [
(('', '', 'TREE_ROOT'), # common details
[('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
])
])
state = dirstate.DirState.initialize('dirstate')
try:
self.assertIsInstance(state, dirstate.DirState)
lines = state.get_lines()
finally:
state.unlock()
# On win32 you can't read from a locked file, even within the same
# process. So we have to unlock and release before we check the file
# contents.
self.assertFileEqual(''.join(lines), 'dirstate')
state.lock_read() # check_state_with_reopen will unlock
self.check_state_with_reopen(expected_result, state)
class TestDirStateManipulations(TestCaseWithDirState):
def test_set_state_from_inventory_no_content_no_parents(self):
# setting the current inventory is a slow but important api to support.
tree1 = self.make_branch_and_memory_tree('tree1')
tree1.lock_write()
try:
tree1.add('')
revid1 = tree1.commit('foo').encode('utf8')
root_id = tree1.inventory.root.file_id
inv = tree1.inventory
finally:
tree1.unlock()
expected_result = [], [
(('', '', root_id), [
('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
state = dirstate.DirState.initialize('dirstate')
try:
state.set_state_from_inventory(inv)
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._header_state)
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
except:
state.unlock()
raise
else:
# This will unlock it
self.check_state_with_reopen(expected_result, state)
def test_set_state_from_inventory_mixed_paths(self):
tree1 = self.make_branch_and_tree('tree1')
self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
'tree1/a/b/foo', 'tree1/a-b/bar'])
tree1.lock_write()
try:
tree1.add(['a', 'a/b', 'a-b', 'a/b/foo', 'a-b/bar'],
['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
tree1.commit('rev1', rev_id='rev1')
root_id = tree1.get_root_id()
inv = tree1.inventory
finally:
tree1.unlock()
expected_result1 = [('', '', root_id, 'd'),
('', 'a', 'a-id', 'd'),
('', 'a-b', 'a-b-id', 'd'),
('a', 'b', 'b-id', 'd'),
('a/b', 'foo', 'foo-id', 'f'),
('a-b', 'bar', 'bar-id', 'f'),
]
expected_result2 = [('', '', root_id, 'd'),
('', 'a', 'a-id', 'd'),
('', 'a-b', 'a-b-id', 'd'),
('a-b', 'bar', 'bar-id', 'f'),
]
state = dirstate.DirState.initialize('dirstate')
try:
state.set_state_from_inventory(inv)
values = []
for entry in state._iter_entries():
values.append(entry[0] + entry[1][0][:1])
self.assertEqual(expected_result1, values)
del inv['b-id']
state.set_state_from_inventory(inv)
values = []
for entry in state._iter_entries():
values.append(entry[0] + entry[1][0][:1])
self.assertEqual(expected_result2, values)
finally:
state.unlock()
def test_set_path_id_no_parents(self):
"""The id of a path can be changed trivally with no parents."""
state = dirstate.DirState.initialize('dirstate')
try:
# check precondition to be sure the state does change appropriately.
self.assertEqual(
[(('', '', 'TREE_ROOT'), [('d', '', 0, False,
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
list(state._iter_entries()))
state.set_path_id('', 'foobarbaz')
expected_rows = [
(('', '', 'foobarbaz'), [('d', '', 0, False,
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
self.assertEqual(expected_rows, list(state._iter_entries()))
# should work across save too
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
state._validate()
self.assertEqual(expected_rows, list(state._iter_entries()))
finally:
state.unlock()
def test_set_path_id_with_parents(self):
"""Set the root file id in a dirstate with parents"""
mt = self.make_branch_and_tree('mt')
# in case the default tree format uses a different root id
mt.set_root_id('TREE_ROOT')
mt.commit('foo', rev_id='parent-revid')
rt = mt.branch.repository.revision_tree('parent-revid')
state = dirstate.DirState.initialize('dirstate')
state._validate()
try:
state.set_parent_trees([('parent-revid', rt)], ghosts=[])
state.set_path_id('', 'foobarbaz')
state._validate()
# now see that it is what we expected
expected_rows = [
(('', '', 'TREE_ROOT'),
[('a', '', 0, False, ''),
('d', '', 0, False, 'parent-revid'),
]),
(('', '', 'foobarbaz'),
[('d', '', 0, False, ''),
('a', '', 0, False, ''),
]),
]
state._validate()
self.assertEqual(expected_rows, list(state._iter_entries()))
# should work across save too
state.save()
finally:
state.unlock()
# now flush & check we get the same
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
state._validate()
self.assertEqual(expected_rows, list(state._iter_entries()))
finally:
state.unlock()
# now change within an existing file-backed state
state.lock_write()
try:
state._validate()
state.set_path_id('', 'tree-root-2')
state._validate()
finally:
state.unlock()
def test_set_parent_trees_no_content(self):
# set_parent_trees is a slow but important api to support.
tree1 = self.make_branch_and_memory_tree('tree1')
tree1.lock_write()
try:
tree1.add('')
revid1 = tree1.commit('foo')
finally:
tree1.unlock()
branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
tree2 = MemoryTree.create_on_branch(branch2)
tree2.lock_write()
try:
revid2 = tree2.commit('foo')
root_id = tree2.inventory.root.file_id
finally:
tree2.unlock()
state = dirstate.DirState.initialize('dirstate')
try:
state.set_path_id('', root_id)
state.set_parent_trees(
((revid1, tree1.branch.repository.revision_tree(revid1)),
(revid2, tree2.branch.repository.revision_tree(revid2)),
('ghost-rev', None)),
['ghost-rev'])
# check we can reopen and use the dirstate after setting parent
# trees.
state._validate()
state.save()
state._validate()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_write()
try:
self.assertEqual([revid1, revid2, 'ghost-rev'],
state.get_parent_ids())
# iterating the entire state ensures that the state is parsable.
list(state._iter_entries())
# be sure that it sets not appends - change it
state.set_parent_trees(
((revid1, tree1.branch.repository.revision_tree(revid1)),
('ghost-rev', None)),
['ghost-rev'])
# and now put it back.
state.set_parent_trees(
((revid1, tree1.branch.repository.revision_tree(revid1)),
(revid2, tree2.branch.repository.revision_tree(revid2)),
('ghost-rev', tree2.branch.repository.revision_tree(None))),
['ghost-rev'])
self.assertEqual([revid1, revid2, 'ghost-rev'],
state.get_parent_ids())
# the ghost should be recorded as such by set_parent_trees.
self.assertEqual(['ghost-rev'], state.get_ghosts())
self.assertEqual(
[(('', '', root_id), [
('d', '', 0, False, dirstate.DirState.NULLSTAT),
('d', '', 0, False, revid1),
('d', '', 0, False, revid2)
])],
list(state._iter_entries()))
finally:
state.unlock()
def test_set_parent_trees_file_missing_from_tree(self):
# Adding a parent tree may reference files not in the current state.
# they should get listed just once by id, even if they are in two
# separate trees.
# set_parent_trees is a slow but important api to support.
tree1 = self.make_branch_and_memory_tree('tree1')
tree1.lock_write()
try:
tree1.add('')
tree1.add(['a file'], ['file-id'], ['file'])
tree1.put_file_bytes_non_atomic('file-id', 'file-content')
revid1 = tree1.commit('foo')
finally:
tree1.unlock()
branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
tree2 = MemoryTree.create_on_branch(branch2)
tree2.lock_write()
try:
tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
revid2 = tree2.commit('foo')
root_id = tree2.inventory.root.file_id
finally:
tree2.unlock()
# check the layout in memory
expected_result = [revid1.encode('utf8'), revid2.encode('utf8')], [
(('', '', root_id), [
('d', '', 0, False, dirstate.DirState.NULLSTAT),
('d', '', 0, False, revid1.encode('utf8')),
('d', '', 0, False, revid2.encode('utf8'))
]),
(('', 'a file', 'file-id'), [
('a', '', 0, False, ''),
('f', '2439573625385400f2a669657a7db6ae7515d371', 12, False,
revid1.encode('utf8')),
('f', '542e57dc1cda4af37cb8e55ec07ce60364bb3c7d', 16, False,
revid2.encode('utf8'))
])
]
state = dirstate.DirState.initialize('dirstate')
try:
state.set_path_id('', root_id)
state.set_parent_trees(
((revid1, tree1.branch.repository.revision_tree(revid1)),
(revid2, tree2.branch.repository.revision_tree(revid2)),
), [])
except:
state.unlock()
raise
else:
# check_state_with_reopen will unlock
self.check_state_with_reopen(expected_result, state)
### add a path via _set_data - so we dont need delta work, just
# raw data in, and ensure that it comes out via get_lines happily.
def test_add_path_to_root_no_parents_all_data(self):
# The most trivial addition of a path is when there are no parents and
# its in the root and all data about the file is supplied
self.build_tree(['a file'])
stat = os.lstat('a file')
# the 1*20 is the sha1 pretend value.
state = dirstate.DirState.initialize('dirstate')
expected_entries = [
(('', '', 'TREE_ROOT'), [
('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
]),
(('', 'a file', 'a file id'), [
('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
]),
]
try:
state.add('a file', 'a file id', 'file', stat, '1'*20)
# having added it, it should be in the output of iter_entries.
self.assertEqual(expected_entries, list(state._iter_entries()))
# saving and reloading should not affect this.
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
self.assertEqual(expected_entries, list(state._iter_entries()))
finally:
state.unlock()
def test_add_path_to_unversioned_directory(self):
"""Adding a path to an unversioned directory should error.
This is a duplicate of TestWorkingTree.test_add_in_unversioned,
once dirstate is stable and if it is merged with WorkingTree3, consider
removing this copy of the test.
"""
self.build_tree(['unversioned/', 'unversioned/a file'])
state = dirstate.DirState.initialize('dirstate')
try:
self.assertRaises(errors.NotVersionedError, state.add,
'unversioned/a file', 'a file id', 'file', None, None)
finally:
state.unlock()
def test_add_directory_to_root_no_parents_all_data(self):
# The most trivial addition of a dir is when there are no parents and
# its in the root and all data about the file is supplied
self.build_tree(['a dir/'])
stat = os.lstat('a dir')
expected_entries = [
(('', '', 'TREE_ROOT'), [
('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
]),
(('', 'a dir', 'a dir id'), [
('d', '', 0, False, dirstate.pack_stat(stat)), # current tree
]),
]
state = dirstate.DirState.initialize('dirstate')
try:
state.add('a dir', 'a dir id', 'directory', stat, None)
# having added it, it should be in the output of iter_entries.
self.assertEqual(expected_entries, list(state._iter_entries()))
# saving and reloading should not affect this.
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
state._validate()
try:
self.assertEqual(expected_entries, list(state._iter_entries()))
finally:
state.unlock()
def test_add_symlink_to_root_no_parents_all_data(self):
# The most trivial addition of a symlink when there are no parents and
# its in the root and all data about the file is supplied
# bzr doesn't support fake symlinks on windows, yet.
if not has_symlinks():
raise TestSkipped("No symlink support")
os.symlink('target', 'a link')
stat = os.lstat('a link')
expected_entries = [
(('', '', 'TREE_ROOT'), [
('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
]),
(('', 'a link', 'a link id'), [
('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
]),
]
state = dirstate.DirState.initialize('dirstate')
try:
state.add('a link', 'a link id', 'symlink', stat, 'target')
# having added it, it should be in the output of iter_entries.
self.assertEqual(expected_entries, list(state._iter_entries()))
# saving and reloading should not affect this.
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
self.assertEqual(expected_entries, list(state._iter_entries()))
finally:
state.unlock()
def test_add_directory_and_child_no_parents_all_data(self):
# after adding a directory, we should be able to add children to it.
self.build_tree(['a dir/', 'a dir/a file'])
dirstat = os.lstat('a dir')
filestat = os.lstat('a dir/a file')
expected_entries = [
(('', '', 'TREE_ROOT'), [
('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
]),
(('', 'a dir', 'a dir id'), [
('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
]),
(('a dir', 'a file', 'a file id'), [
('f', '1'*20, 25, False,
dirstate.pack_stat(filestat)), # current tree details
]),
]
state = dirstate.DirState.initialize('dirstate')
try:
state.add('a dir', 'a dir id', 'directory', dirstat, None)
state.add('a dir/a file', 'a file id', 'file', filestat, '1'*20)
# added it, it should be in the output of iter_entries.
self.assertEqual(expected_entries, list(state._iter_entries()))
# saving and reloading should not affect this.
state.save()
finally:
state.unlock()
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
self.assertEqual(expected_entries, list(state._iter_entries()))
finally:
state.unlock()
def test_add_tree_reference(self):
# make a dirstate and add a tree reference
state = dirstate.DirState.initialize('dirstate')
expected_entry = (
('', 'subdir', 'subdir-id'),
[('t', 'subtree-123123', 0, False,
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')],
)
try:
state.add('subdir', 'subdir-id', 'tree-reference', None, 'subtree-123123')
entry = state._get_entry(0, 'subdir-id', 'subdir')
self.assertEqual(entry, expected_entry)
state._validate()
state.save()
finally:
state.unlock()
# now check we can read it back
state.lock_read()
state._validate()
try:
entry2 = state._get_entry(0, 'subdir-id', 'subdir')
self.assertEqual(entry, entry2)
self.assertEqual(entry, expected_entry)
# and lookup by id should work too
entry2 = state._get_entry(0, fileid_utf8='subdir-id')
self.assertEqual(entry, expected_entry)
finally:
state.unlock()
def test_add_forbidden_names(self):
state = dirstate.DirState.initialize('dirstate')
self.addCleanup(state.unlock)
self.assertRaises(errors.BzrError,
state.add, '.', 'ass-id', 'directory', None, None)
self.assertRaises(errors.BzrError,
state.add, '..', 'ass-id', 'directory', None, None)
class TestGetLines(TestCaseWithDirState):
def test_get_line_with_2_rows(self):
state = self.create_dirstate_with_root_and_subdir()
try:
self.assertEqual(['#bazaar dirstate flat format 3\n',
'crc32: 41262208\n',
'num_entries: 2\n',
'0\x00\n\x00'
'0\x00\n\x00'
'\x00\x00a-root-value\x00'
'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
'\x00subdir\x00subdir-id\x00'
'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
], state.get_lines())
finally:
state.unlock()
def test_entry_to_line(self):
state = self.create_dirstate_with_root()
try:
self.assertEqual(
'\x00\x00a-root-value\x00d\x00\x000\x00n'
'\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk',
state._entry_to_line(state._dirblocks[0][1][0]))
finally:
state.unlock()
def test_entry_to_line_with_parent(self):
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
root_entry = ('', '', 'a-root-value'), [
('d', '', 0, False, packed_stat), # current tree details
# first: a pointer to the current location
('a', 'dirname/basename', 0, False, ''),
]
state = dirstate.DirState.initialize('dirstate')
try:
self.assertEqual(
'\x00\x00a-root-value\x00'
'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
'a\x00dirname/basename\x000\x00n\x00',
state._entry_to_line(root_entry))
finally:
state.unlock()
def test_entry_to_line_with_two_parents_at_different_paths(self):
# / in the tree, at / in one parent and /dirname/basename in the other.
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
root_entry = ('', '', 'a-root-value'), [
('d', '', 0, False, packed_stat), # current tree details
('d', '', 0, False, 'rev_id'), # first parent details
# second: a pointer to the current location
('a', 'dirname/basename', 0, False, ''),
]
state = dirstate.DirState.initialize('dirstate')
try:
self.assertEqual(
'\x00\x00a-root-value\x00'
'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
'd\x00\x000\x00n\x00rev_id\x00'
'a\x00dirname/basename\x000\x00n\x00',
state._entry_to_line(root_entry))
finally:
state.unlock()
def test_iter_entries(self):
# we should be able to iterate the dirstate entries from end to end
# this is for get_lines to be easy to read.
packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
dirblocks = []
root_entries = [(('', '', 'a-root-value'), [
('d', '', 0, False, packed_stat), # current tree details
])]
dirblocks.append(('', root_entries))
# add two files in the root
subdir_entry = ('', 'subdir', 'subdir-id'), [
('d', '', 0, False, packed_stat), # current tree details
]
afile_entry = ('', 'afile', 'afile-id'), [
('f', 'sha1value', 34, False, packed_stat), # current tree details
]
dirblocks.append(('', [subdir_entry, afile_entry]))
# and one in subdir
file_entry2 = ('subdir', '2file', '2file-id'), [
('f', 'sha1value', 23, False, packed_stat), # current tree details
]
dirblocks.append(('subdir', [file_entry2]))
state = dirstate.DirState.initialize('dirstate')
try:
state._set_data([], dirblocks)
expected_entries = [root_entries[0], subdir_entry, afile_entry,
file_entry2]
self.assertEqual(expected_entries, list(state._iter_entries()))
finally:
state.unlock()
class TestGetBlockRowIndex(TestCaseWithDirState):
def assertBlockRowIndexEqual(self, block_index, row_index, dir_present,
file_present, state, dirname, basename, tree_index):
self.assertEqual((block_index, row_index, dir_present, file_present),
state._get_block_entry_index(dirname, basename, tree_index))
if dir_present:
block = state._dirblocks[block_index]
self.assertEqual(dirname, block[0])
if dir_present and file_present:
row = state._dirblocks[block_index][1][row_index]
self.assertEqual(dirname, row[0][0])
self.assertEqual(basename, row[0][1])
def test_simple_structure(self):
state = self.create_dirstate_with_root_and_subdir()
self.addCleanup(state.unlock)
self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'subdir', 0)
self.assertBlockRowIndexEqual(1, 0, True, False, state, '', 'bdir', 0)
self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'zdir', 0)
self.assertBlockRowIndexEqual(2, 0, False, False, state, 'a', 'foo', 0)
self.assertBlockRowIndexEqual(2, 0, False, False, state,
'subdir', 'foo', 0)
def test_complex_structure_exists(self):
state = self.create_complex_dirstate()
self.addCleanup(state.unlock)
# Make sure we can find everything that exists
self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'a', 0)
self.assertBlockRowIndexEqual(1, 1, True, True, state, '', 'b', 0)
self.assertBlockRowIndexEqual(1, 2, True, True, state, '', 'c', 0)
self.assertBlockRowIndexEqual(1, 3, True, True, state, '', 'd', 0)
self.assertBlockRowIndexEqual(2, 0, True, True, state, 'a', 'e', 0)
self.assertBlockRowIndexEqual(2, 1, True, True, state, 'a', 'f', 0)
self.assertBlockRowIndexEqual(3, 0, True, True, state, 'b', 'g', 0)
self.assertBlockRowIndexEqual(3, 1, True, True, state,
'b', 'h\xc3\xa5', 0)
def test_complex_structure_missing(self):
state = self.create_complex_dirstate()
self.addCleanup(state.unlock)
# Make sure things would be inserted in the right locations
# '_' comes before 'a'
self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
self.assertBlockRowIndexEqual(1, 0, True, False, state, '', '_', 0)
self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'aa', 0)
self.assertBlockRowIndexEqual(1, 4, True, False, state,
'', 'h\xc3\xa5', 0)
self.assertBlockRowIndexEqual(2, 0, False, False, state, '_', 'a', 0)
self.assertBlockRowIndexEqual(3, 0, False, False, state, 'aa', 'a', 0)
self.assertBlockRowIndexEqual(4, 0, False, False, state, 'bb', 'a', 0)
# This would be inserted between a/ and b/
self.assertBlockRowIndexEqual(3, 0, False, False, state, 'a/e', 'a', 0)
# Put at the end
self.assertBlockRowIndexEqual(4, 0, False, False, state, 'e', 'a', 0)
class TestGetEntry(TestCaseWithDirState):
def assertEntryEqual(self, dirname, basename, file_id, state, path, index):
"""Check that the right entry is returned for a request to getEntry."""
entry = state._get_entry(index, path_utf8=path)
if file_id is None:
self.assertEqual((None, None), entry)
else:
cur = entry[0]
self.assertEqual((dirname, basename, file_id), cur[:3])
def test_simple_structure(self):
state = self.create_dirstate_with_root_and_subdir()
self.addCleanup(state.unlock)
self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
self.assertEntryEqual('', 'subdir', 'subdir-id', state, 'subdir', 0)
self.assertEntryEqual(None, None, None, state, 'missing', 0)
self.assertEntryEqual(None, None, None, state, 'missing/foo', 0)
self.assertEntryEqual(None, None, None, state, 'subdir/foo', 0)
def test_complex_structure_exists(self):
state = self.create_complex_dirstate()
self.addCleanup(state.unlock)
self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
self.assertEntryEqual('', 'a', 'a-dir', state, 'a', 0)
self.assertEntryEqual('', 'b', 'b-dir', state, 'b', 0)
self.assertEntryEqual('', 'c', 'c-file', state, 'c', 0)
self.assertEntryEqual('', 'd', 'd-file', state, 'd', 0)
self.assertEntryEqual('a', 'e', 'e-dir', state, 'a/e', 0)
self.assertEntryEqual('a', 'f', 'f-file', state, 'a/f', 0)
self.assertEntryEqual('b', 'g', 'g-file', state, 'b/g', 0)
self.assertEntryEqual('b', 'h\xc3\xa5', 'h-\xc3\xa5-file', state,
'b/h\xc3\xa5', 0)
def test_complex_structure_missing(self):
state = self.create_complex_dirstate()
self.addCleanup(state.unlock)
self.assertEntryEqual(None, None, None, state, '_', 0)
self.assertEntryEqual(None, None, None, state, '_\xc3\xa5', 0)
self.assertEntryEqual(None, None, None, state, 'a/b', 0)
self.assertEntryEqual(None, None, None, state, 'c/d', 0)
def test_get_entry_uninitialized(self):
"""Calling get_entry will load data if it needs to"""
state = self.create_dirstate_with_root()
try:
state.save()
finally:
state.unlock()
del state
state = dirstate.DirState.on_file('dirstate')
state.lock_read()
try:
self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
state._header_state)
self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
state._dirblock_state)
self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
finally:
state.unlock()
class TestDirstateSortOrder(TestCaseWithTransport):
"""Test that DirState adds entries in the right order."""
def test_add_sorting(self):
"""Add entries in lexicographical order, we get path sorted order.
This tests it to a depth of 4, to make sure we don't just get it right
at a single depth. 'a/a' should come before 'a-a', even though it
doesn't lexicographically.
"""
dirs = ['a', 'a/a', 'a/a/a', 'a/a/a/a',
'a-a', 'a/a-a', 'a/a/a-a', 'a/a/a/a-a',
]
null_sha = ''
state = dirstate.DirState.initialize('dirstate')
self.addCleanup(state.unlock)
fake_stat = os.stat('dirstate')
for d in dirs:
d_id = d.replace('/', '_')+'-id'
file_path = d + '/f'
file_id = file_path.replace('/', '_')+'-id'
state.add(d, d_id, 'directory', fake_stat, null_sha)
state.add(file_path, file_id, 'file', fake_stat, null_sha)
expected = ['', '', 'a',
'a/a', 'a/a/a', 'a/a/a/a',
'a/a/a/a-a', 'a/a/a-a', 'a/a-a', 'a-a',
]
split = lambda p:p.split('/')
self.assertEqual(sorted(expected, key=split), expected)
dirblock_names = [d[0] for d in state._dirblocks]
self.assertEqual(expected, dirblock_names)
def test_set_parent_trees_correct_order(self):
"""After calling set_parent_trees() we should maintain the order."""
dirs = ['a', 'a-a', 'a/a']
null_sha = ''
state = dirstate.DirState.initialize('dirstate')
self.addCleanup(state.unlock)
fake_stat = os.stat('dirstate')
for d in dirs:
d_id = d.replace('/', '_')+'-id'
file_path = d + '/f'
file_id = file_path.replace('/', '_')+'-id'
state.add(d, d_id, 'directory', fake_stat, null_sha)
state.add(file_path, file_id, 'file', fake_stat, null_sha)
expected = ['', '', 'a', 'a/a', 'a-a']
dirblock_names = [d[0] for d in state._dirblocks]
self.assertEqual(expected, dirblock_names)
# *really* cheesy way to just get an empty tree
repo = self.make_repository('repo')
empty_tree = repo.revision_tree(None)
state.set_parent_trees([('null:', empty_tree)], [])
dirblock_names = [d[0] for d in state._dirblocks]
self.assertEqual(expected, dirblock_names)
class InstrumentedDirState(dirstate.DirState):
"""An DirState with instrumented sha1 functionality."""
def __init__(self, path):
super(InstrumentedDirState, self).__init__(path)
self._time_offset = 0
self._log = []
def _sha_cutoff_time(self):
timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
self._cutoff_time = timestamp + self._time_offset
def _sha1_file(self, abspath, entry):
self._log.append(('sha1', abspath))
return super(InstrumentedDirState, self)._sha1_file(abspath, entry)
def _read_link(self, abspath, old_link):
self._log.append(('read_link', abspath, old_link))
return super(InstrumentedDirState, self)._read_link(abspath, old_link)
def _lstat(self, abspath, entry):
self._log.append(('lstat', abspath))
return super(InstrumentedDirState, self)._lstat(abspath, entry)
def _is_executable(self, mode, old_executable):
self._log.append(('is_exec', mode, old_executable))
return super(InstrumentedDirState, self)._is_executable(mode,
old_executable)
def adjust_time(self, secs):
"""Move the clock forward or back.
:param secs: The amount to adjust the clock by. Positive values make it
seem as if we are in the future, negative values make it seem like we
are in the past.
"""
self._time_offset += secs
self._cutoff_time = None
class _FakeStat(object):
"""A class with the same attributes as a real stat result."""
def __init__(self, size, mtime, ctime, dev, ino, mode):
self.st_size = size
self.st_mtime = mtime
self.st_ctime = ctime
self.st_dev = dev
self.st_ino = ino
self.st_mode = mode
class TestUpdateEntry(TestCaseWithDirState):
"""Test the DirState.update_entry functions"""
def get_state_with_a(self):
"""Create a DirState tracking a single object named 'a'"""
state = InstrumentedDirState.initialize('dirstate')
self.addCleanup(state.unlock)
state.add('a', 'a-id', 'file', None, '')
entry = state._get_entry(0, path_utf8='a')
return state, entry
def test_update_entry(self):
state, entry = self.get_state_with_a()
self.build_tree(['a'])
# Add one where we don't provide the stat or sha already
self.assertEqual(('', 'a', 'a-id'), entry[0])
self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
entry[1])
# Flush the buffers to disk
state.save()
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
link_or_sha1)
# The dirblock entry should not cache the file's sha1
self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
entry[1])
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
mode = stat_value.st_mode
self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
state.save()
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
# If we do it again right away, we don't know if the file has changed
# so we will re-read the file. Roll the clock back so the file is
# guaranteed to look too new.
state.adjust_time(-10)
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
('sha1', 'a'), ('is_exec', mode, False),
], state._log)
self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
link_or_sha1)
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
entry[1])
state.save()
# However, if we move the clock forward so the file is considered
# "stable", it should just cache the value.
state.adjust_time(+20)
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
link_or_sha1)
self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
('sha1', 'a'), ('is_exec', mode, False),
('sha1', 'a'), ('is_exec', mode, False),
], state._log)
self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
entry[1])
# Subsequent calls will just return the cached value
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
link_or_sha1)
self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
('sha1', 'a'), ('is_exec', mode, False),
('sha1', 'a'), ('is_exec', mode, False),
], state._log)
self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
entry[1])
def test_update_entry_symlink(self):
"""Update entry should read symlinks."""
if not osutils.has_symlinks():
# PlatformDeficiency / TestSkipped
raise TestSkipped("No symlink support")
state, entry = self.get_state_with_a()
state.save()
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
os.symlink('target', 'a')
state.adjust_time(-10) # Make the symlink look new
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('target', link_or_sha1)
self.assertEqual([('read_link', 'a', '')], state._log)
# Dirblock is not updated (the link is too new)
self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
entry[1])
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
# Because the stat_value looks new, we should re-read the target
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('target', link_or_sha1)
self.assertEqual([('read_link', 'a', ''),
('read_link', 'a', ''),
], state._log)
self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
entry[1])
state.adjust_time(+20) # Skip into the future, all files look old
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('target', link_or_sha1)
# We need to re-read the link because only now can we cache it
self.assertEqual([('read_link', 'a', ''),
('read_link', 'a', ''),
('read_link', 'a', ''),
], state._log)
self.assertEqual([('l', 'target', 6, False, packed_stat)],
entry[1])
# Another call won't re-read the link
self.assertEqual([('read_link', 'a', ''),
('read_link', 'a', ''),
('read_link', 'a', ''),
], state._log)
link_or_sha1 = state.update_entry(entry, abspath='a',
stat_value=stat_value)
self.assertEqual('target', link_or_sha1)
self.assertEqual([('l', 'target', 6, False, packed_stat)],
entry[1])
def do_update_entry(self, state, entry, abspath):
stat_value = os.lstat(abspath)
return state.update_entry(entry, abspath, stat_value)
def test_update_entry_dir(self):
state, entry = self.get_state_with_a()
self.build_tree(['a/'])
self.assertIs(None, self.do_update_entry(state, entry, 'a'))
def test_update_entry_dir_unchanged(self):
state, entry = self.get_state_with_a()
self.build_tree(['a/'])
state.adjust_time(+20)
self.assertIs(None, self.do_update_entry(state, entry, 'a'))
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
state.save()
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
self.assertIs(None, self.do_update_entry(state, entry, 'a'))
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
def test_update_entry_file_unchanged(self):
state, entry = self.get_state_with_a()
self.build_tree(['a'])
sha1sum = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
state.adjust_time(+20)
self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
state._dirblock_state)
state.save()
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
state._dirblock_state)
def create_and_test_file(self, state, entry):
"""Create a file at 'a' and verify the state finds it.
The state should already be versioning *something* at 'a'. This makes
sure that state.update_entry recognizes it as a file.
"""
self.build_tree(['a'])
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
link_or_sha1)
self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
entry[1])
return packed_stat
def create_and_test_dir(self, state, entry):
"""Create a directory at 'a' and verify the state finds it.
The state should already be versioning *something* at 'a'. This makes
sure that state.update_entry recognizes it as a directory.
"""
self.build_tree(['a/'])
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
self.assertIs(None, link_or_sha1)
self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
return packed_stat
def create_and_test_symlink(self, state, entry):
"""Create a symlink at 'a' and verify the state finds it.
The state should already be versioning *something* at 'a'. This makes
sure that state.update_entry recognizes it as a symlink.
This should not be called if this platform does not have symlink
support.
"""
# caller should care about skipping test on platforms without symlinks
os.symlink('path/to/foo', 'a')
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
self.assertEqual('path/to/foo', link_or_sha1)
self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
entry[1])
return packed_stat
def test_update_file_to_dir(self):
"""If a file changes to a directory we return None for the sha.
We also update the inventory record.
"""
state, entry = self.get_state_with_a()
# The file sha1 won't be cached unless the file is old
state.adjust_time(+10)
self.create_and_test_file(state, entry)
os.remove('a')
self.create_and_test_dir(state, entry)
def test_update_file_to_symlink(self):
"""File becomes a symlink"""
if not osutils.has_symlinks():
# PlatformDeficiency / TestSkipped
raise TestSkipped("No symlink support")
state, entry = self.get_state_with_a()
# The file sha1 won't be cached unless the file is old
state.adjust_time(+10)
self.create_and_test_file(state, entry)
os.remove('a')
self.create_and_test_symlink(state, entry)
def test_update_dir_to_file(self):
"""Directory becoming a file updates the entry."""
state, entry = self.get_state_with_a()
# The file sha1 won't be cached unless the file is old
state.adjust_time(+10)
self.create_and_test_dir(state, entry)
os.rmdir('a')
self.create_and_test_file(state, entry)
def test_update_dir_to_symlink(self):
"""Directory becomes a symlink"""
if not osutils.has_symlinks():
# PlatformDeficiency / TestSkipped
raise TestSkipped("No symlink support")
state, entry = self.get_state_with_a()
# The symlink target won't be cached if it isn't old
state.adjust_time(+10)
self.create_and_test_dir(state, entry)
os.rmdir('a')
self.create_and_test_symlink(state, entry)
def test_update_symlink_to_file(self):
"""Symlink becomes a file"""
if not has_symlinks():
raise TestSkipped("No symlink support")
state, entry = self.get_state_with_a()
# The symlink and file info won't be cached unless old
state.adjust_time(+10)
self.create_and_test_symlink(state, entry)
os.remove('a')
self.create_and_test_file(state, entry)
def test_update_symlink_to_dir(self):
"""Symlink becomes a directory"""
if not has_symlinks():
raise TestSkipped("No symlink support")
state, entry = self.get_state_with_a()
# The symlink target won't be cached if it isn't old
state.adjust_time(+10)
self.create_and_test_symlink(state, entry)
os.remove('a')
self.create_and_test_dir(state, entry)
def test__is_executable_win32(self):
state, entry = self.get_state_with_a()
self.build_tree(['a'])
# Make sure we are using the win32 implementation of _is_executable
state._is_executable = state._is_executable_win32
# The file on disk is not executable, but we are marking it as though
# it is. With _is_executable_win32 we ignore what is on disk.
entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
stat_value = os.lstat('a')
packed_stat = dirstate.pack_stat(stat_value)
state.adjust_time(-10) # Make sure everything is new
state.update_entry(entry, abspath='a', stat_value=stat_value)
# The row is updated, but the executable bit stays set.
self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
entry[1])
# Make the disk object look old enough to cache
state.adjust_time(+20)
digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
state.update_entry(entry, abspath='a', stat_value=stat_value)
self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
class TestPackStat(TestCaseWithTransport):
def assertPackStat(self, expected, stat_value):
"""Check the packed and serialized form of a stat value."""
self.assertEqual(expected, dirstate.pack_stat(stat_value))
def test_pack_stat_int(self):
st = _FakeStat(6859L, 1172758614, 1172758617, 777L, 6499538L, 0100644)
# Make sure that all parameters have an impact on the packed stat.
self.assertPackStat('AAAay0Xm4FZF5uBZAAADCQBjLNIAAIGk', st)
st.st_size = 7000L
# ay0 => bWE
self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
st.st_mtime = 1172758620
# 4FZ => 4Fx
self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
st.st_ctime = 1172758630
# uBZ => uBm
self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
st.st_dev = 888L
# DCQ => DeA
self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNIAAIGk', st)
st.st_ino = 6499540L
# LNI => LNQ
self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIGk', st)
st.st_mode = 0100744
# IGk => IHk
self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIHk', st)
def test_pack_stat_float(self):
"""On some platforms mtime and ctime are floats.
Make sure we don't get warnings or errors, and that we ignore changes <
1s
"""
st = _FakeStat(7000L, 1172758614.0, 1172758617.0,
777L, 6499538L, 0100644)
# These should all be the same as the integer counterparts
self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
st.st_mtime = 1172758620.0
# FZF5 => FxF5
self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
st.st_ctime = 1172758630.0
# uBZ => uBm
self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
# fractional seconds are discarded, so no change from above
st.st_mtime = 1172758620.453
self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
st.st_ctime = 1172758630.228
self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
class TestBisect(TestCaseWithDirState):
"""Test the ability to bisect into the disk format."""
def assertBisect(self, expected_map, map_keys, state, paths):
"""Assert that bisecting for paths returns the right result.
:param expected_map: A map from key => entry value
:param map_keys: The keys to expect for each path
:param state: The DirState object.
:param paths: A list of paths, these will automatically be split into
(dir, name) tuples, and sorted according to how _bisect
requires.
"""
dir_names = sorted(osutils.split(p) for p in paths)
result = state._bisect(dir_names)
# For now, results are just returned in whatever order we read them.
# We could sort by (dir, name, file_id) or something like that, but in
# the end it would still be fairly arbitrary, and we don't want the
# extra overhead if we can avoid it. So sort everything to make sure
# equality is true
assert len(map_keys) == len(dir_names)
expected = {}
for dir_name, keys in zip(dir_names, map_keys):
if keys is None:
# This should not be present in the output
continue
expected[dir_name] = sorted(expected_map[k] for k in keys)
for dir_name in result:
result[dir_name].sort()
self.assertEqual(expected, result)
def assertBisectDirBlocks(self, expected_map, map_keys, state, paths):
"""Assert that bisecting for dirbblocks returns the right result.
:param expected_map: A map from key => expected values
:param map_keys: A nested list of paths we expect to be returned.
Something like [['a', 'b', 'f'], ['b/c', 'b/d']]
:param state: The DirState object.
:param paths: A list of directories
"""
result = state._bisect_dirblocks(paths)
assert len(map_keys) == len(paths)
expected = {}
for path, keys in zip(paths, map_keys):
if keys is None:
# This should not be present in the output
continue
expected[path] = sorted(expected_map[k] for k in keys)
for path in result:
result[path].sort()
self.assertEqual(expected, result)
def assertBisectRecursive(self, expected_map, map_keys, state, paths):
"""Assert the return value of a recursive bisection.
:param expected_map: A map from key => entry value
:param map_keys: A list of paths we expect to be returned.
Something like ['a', 'b', 'f', 'b/d', 'b/d2']
:param state: The DirState object.
:param paths: A list of files and directories. It will be broken up
into (dir, name) pairs and sorted before calling _bisect_recursive.
"""
expected = {}
for key in map_keys:
entry = expected_map[key]
dir_name_id, trees_info = entry
expected[dir_name_id] = trees_info
dir_names = sorted(osutils.split(p) for p in paths)
result = state._bisect_recursive(dir_names)
self.assertEqual(expected, result)
def test_bisect_each(self):
"""Find a single record using bisect."""
tree, state, expected = self.create_basic_dirstate()
# Bisect should return the rows for the specified files.
self.assertBisect(expected, [['']], state, [''])
self.assertBisect(expected, [['a']], state, ['a'])
self.assertBisect(expected, [['b']], state, ['b'])
self.assertBisect(expected, [['b/c']], state, ['b/c'])
self.assertBisect(expected, [['b/d']], state, ['b/d'])
self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
self.assertBisect(expected, [['f']], state, ['f'])
def test_bisect_multi(self):
"""Bisect can be used to find multiple records at the same time."""
tree, state, expected = self.create_basic_dirstate()
# Bisect should be capable of finding multiple entries at the same time
self.assertBisect(expected, [['a'], ['b'], ['f']],
state, ['a', 'b', 'f'])
# ('', 'f') sorts before the others
self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
state, ['b/d', 'b/d/e', 'f'])
def test_bisect_one_page(self):
"""Test bisect when there is only 1 page to read"""
tree, state, expected = self.create_basic_dirstate()
state._bisect_page_size = 5000
self.assertBisect(expected,[['']], state, [''])
self.assertBisect(expected,[['a']], state, ['a'])
self.assertBisect(expected,[['b']], state, ['b'])
self.assertBisect(expected,[['b/c']], state, ['b/c'])
self.assertBisect(expected,[['b/d']], state, ['b/d'])
self.assertBisect(expected,[['b/d/e']], state, ['b/d/e'])
self.assertBisect(expected,[['f']], state, ['f'])
self.assertBisect(expected,[['a'], ['b'], ['f']],
state, ['a', 'b', 'f'])
# ('', 'f') sorts before the others
self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
state, ['b/d', 'b/d/e', 'f'])
def test_bisect_duplicate_paths(self):
"""When bisecting for a path, handle multiple entries."""
tree, state, expected = self.create_duplicated_dirstate()
# Now make sure that both records are properly returned.
self.assertBisect(expected, [['']], state, [''])
self.assertBisect(expected, [['a', 'a2']], state, ['a'])
self.assertBisect(expected, [['b', 'b2']], state, ['b'])
self.assertBisect(expected, [['b/c', 'b/c2']], state, ['b/c'])
self.assertBisect(expected, [['b/d', 'b/d2']], state, ['b/d'])
self.assertBisect(expected, [['b/d/e', 'b/d/e2']],
state, ['b/d/e'])
self.assertBisect(expected, [['f', 'f2']], state, ['f'])
def test_bisect_page_size_too_small(self):
"""If the page size is too small, we will auto increase it."""
tree, state, expected = self.create_basic_dirstate()
state._bisect_page_size = 50
self.assertBisect(expected, [None], state, ['b/e'])
self.assertBisect(expected, [['a']], state, ['a'])
self.assertBisect(expected, [['b']], state, ['b'])
self.assertBisect(expected, [['b/c']], state, ['b/c'])
self.assertBisect(expected, [['b/d']], state, ['b/d'])
self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
self.assertBisect(expected, [['f']], state, ['f'])
def test_bisect_missing(self):
"""Test that bisect return None if it cannot find a path."""
tree, state, expected = self.create_basic_dirstate()
self.assertBisect(expected, [None], state, ['foo'])
self.assertBisect(expected, [None], state, ['b/foo'])
self.assertBisect(expected, [None], state, ['bar/foo'])
self.assertBisect(expected, [['a'], None, ['b/d']],
state, ['a', 'foo', 'b/d'])
def test_bisect_rename(self):
"""Check that we find a renamed row."""
tree, state, expected = self.create_renamed_dirstate()
# Search for the pre and post renamed entries
self.assertBisect(expected, [['a']], state, ['a'])
self.assertBisect(expected, [['b/g']], state, ['b/g'])
self.assertBisect(expected, [['b/d']], state, ['b/d'])
self.assertBisect(expected, [['h']], state, ['h'])
# What about b/d/e? shouldn't that also get 2 directory entries?
self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
self.assertBisect(expected, [['h/e']], state, ['h/e'])
def test_bisect_dirblocks(self):
tree, state, expected = self.create_duplicated_dirstate()
self.assertBisectDirBlocks(expected,
[['', 'a', 'a2', 'b', 'b2', 'f', 'f2']], state, [''])
self.assertBisectDirBlocks(expected,
[['b/c', 'b/c2', 'b/d', 'b/d2']], state, ['b'])
self.assertBisectDirBlocks(expected,
[['b/d/e', 'b/d/e2']], state, ['b/d'])
self.assertBisectDirBlocks(expected,
[['', 'a', 'a2', 'b', 'b2', 'f', 'f2'],
['b/c', 'b/c2', 'b/d', 'b/d2'],
['b/d/e', 'b/d/e2'],
], state, ['', 'b', 'b/d'])
def test_bisect_dirblocks_missing(self):
tree, state, expected = self.create_basic_dirstate()
self.assertBisectDirBlocks(expected, [['b/d/e'], None],
state, ['b/d', 'b/e'])
# Files don't show up in this search
self.assertBisectDirBlocks(expected, [None], state, ['a'])
self.assertBisectDirBlocks(expected, [None], state, ['b/c'])
self.assertBisectDirBlocks(expected, [None], state, ['c'])
self.assertBisectDirBlocks(expected, [None], state, ['b/d/e'])
self.assertBisectDirBlocks(expected, [None], state, ['f'])
def test_bisect_recursive_each(self):
tree, state, expected = self.create_basic_dirstate()
self.assertBisectRecursive(expected, ['a'], state, ['a'])
self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
state, ['b/d'])
self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
state, ['b'])
self.assertBisectRecursive(expected, ['', 'a', 'b', 'f', 'b/c',
'b/d', 'b/d/e'],
state, [''])
def test_bisect_recursive_multiple(self):
tree, state, expected = self.create_basic_dirstate()
self.assertBisectRecursive(expected, ['a', 'b/c'], state, ['a', 'b/c'])
self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
state, ['b/d', 'b/d/e'])
def test_bisect_recursive_missing(self):
tree, state, expected = self.create_basic_dirstate()
self.assertBisectRecursive(expected, [], state, ['d'])
self.assertBisectRecursive(expected, [], state, ['b/e'])
self.assertBisectRecursive(expected, [], state, ['g'])
self.assertBisectRecursive(expected, ['a'], state, ['a', 'g'])
def test_bisect_recursive_renamed(self):
tree, state, expected = self.create_renamed_dirstate()
# Looking for either renamed item should find the other
self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['a'])
self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['b/g'])
# Looking in the containing directory should find the rename target,
# and anything in a subdir of the renamed target.
self.assertBisectRecursive(expected, ['a', 'b', 'b/c', 'b/d',
'b/d/e', 'b/g', 'h', 'h/e'],
state, ['b'])
class TestBisectDirblock(TestCase):
"""Test that bisect_dirblock() returns the expected values.
bisect_dirblock is intended to work like bisect.bisect_left() except it
knows it is working on dirblocks and that dirblocks are sorted by ('path',
'to', 'foo') chunks rather than by raw 'path/to/foo'.
"""
def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
"""Assert that bisect_split works like bisect_left on the split paths.
:param dirblocks: A list of (path, [info]) pairs.
:param split_dirblocks: A list of ((split, path), [info]) pairs.
:param path: The path we are indexing.
All other arguments will be passed along.
"""
bisect_split_idx = dirstate.bisect_dirblock(dirblocks, path,
*args, **kwargs)
split_dirblock = (path.split('/'), [])
bisect_left_idx = bisect.bisect_left(split_dirblocks, split_dirblock,
*args)
self.assertEqual(bisect_left_idx, bisect_split_idx,
'bisect_split disagreed. %s != %s'
' for key %s'
% (bisect_left_idx, bisect_split_idx, path)
)
def paths_to_dirblocks(self, paths):
"""Convert a list of paths into dirblock form.
Also, ensure that the paths are in proper sorted order.
"""
dirblocks = [(path, []) for path in paths]
split_dirblocks = [(path.split('/'), []) for path in paths]
self.assertEqual(sorted(split_dirblocks), split_dirblocks)
return dirblocks, split_dirblocks
def test_simple(self):
"""In the simple case it works just like bisect_left"""
paths = ['', 'a', 'b', 'c', 'd']
dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
for path in paths:
self.assertBisect(dirblocks, split_dirblocks, path)
self.assertBisect(dirblocks, split_dirblocks, '_')
self.assertBisect(dirblocks, split_dirblocks, 'aa')
self.assertBisect(dirblocks, split_dirblocks, 'bb')
self.assertBisect(dirblocks, split_dirblocks, 'cc')
self.assertBisect(dirblocks, split_dirblocks, 'dd')
self.assertBisect(dirblocks, split_dirblocks, 'a/a')
self.assertBisect(dirblocks, split_dirblocks, 'b/b')
self.assertBisect(dirblocks, split_dirblocks, 'c/c')
self.assertBisect(dirblocks, split_dirblocks, 'd/d')
def test_involved(self):
"""This is where bisect_left diverges slightly."""
paths = ['', 'a',
'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
'a-a', 'a-z',
'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
'z-a', 'z-z',
]
dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
for path in paths:
self.assertBisect(dirblocks, split_dirblocks, path)
def test_involved_cached(self):
"""This is where bisect_left diverges slightly."""
paths = ['', 'a',
'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
'a-a', 'a-z',
'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
'z-a', 'z-z',
]
cache = {}
dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
for path in paths:
self.assertBisect(dirblocks, split_dirblocks, path, cache=cache)
class TestDirstateValidation(TestCaseWithDirState):
def test_validate_correct_dirstate(self):
state = self.create_complex_dirstate()
state._validate()
state.unlock()
# and make sure we can also validate with a read lock
state.lock_read()
try:
state._validate()
finally:
state.unlock()
def test_dirblock_not_sorted(self):
tree, state, expected = self.create_renamed_dirstate()
state._read_dirblocks_if_needed()
last_dirblock = state._dirblocks[-1]
# we're appending to the dirblock, but this name comes before some of
# the existing names; that's wrong
last_dirblock[1].append(
(('h', 'aaaa', 'a-id'),
[('a', '', 0, False, ''),
('a', '', 0, False, '')]))
e = self.assertRaises(AssertionError,
state._validate)
self.assertContainsRe(str(e), 'not sorted')
def test_dirblock_name_mismatch(self):
tree, state, expected = self.create_renamed_dirstate()
state._read_dirblocks_if_needed()
last_dirblock = state._dirblocks[-1]
# add an entry with the wrong directory name
last_dirblock[1].append(
(('', 'z', 'a-id'),
[('a', '', 0, False, ''),
('a', '', 0, False, '')]))
e = self.assertRaises(AssertionError,
state._validate)
self.assertContainsRe(str(e),
"doesn't match directory name")
def test_dirblock_missing_rename(self):
tree, state, expected = self.create_renamed_dirstate()
state._read_dirblocks_if_needed()
last_dirblock = state._dirblocks[-1]
# make another entry for a-id, without a correct 'r' pointer to
# the real occurrence in the working tree
last_dirblock[1].append(
(('h', 'z', 'a-id'),
[('a', '', 0, False, ''),
('a', '', 0, False, '')]))
e = self.assertRaises(AssertionError,
state._validate)
self.assertContainsRe(str(e),
'file a-id is absent in row')
|