/breezy-svn/trunk

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-svn/trunk
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
# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>

# 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
"""Checkouts and working trees (working copies)."""

from __future__ import absolute_import

from collections import (
    defaultdict,
    deque,
    )

import errno
import os
import stat
import subvertpy

from subvertpy import (
    ERR_BAD_FILENAME,
    ERR_WC_UNSUPPORTED_FORMAT,
    properties,
    )
from subvertpy.ra import (
    DEPTH_INFINITY,
    )
from subvertpy.wc import (
    SCHEDULE_ADD,
    SCHEDULE_DELETE,
    SCHEDULE_NORMAL,
    SCHEDULE_REPLACE,
    CommittedQueue,
    Adm,
    cleanup,
    get_adm_dir,
    is_adm_dir,
    match_ignore_list,
    revision_status,
    )

import breezy.add
from breezy import (
    conflicts as _mod_conflicts,
    errors as bzr_errors,
    hashcache,
    osutils,
    rio as _mod_rio,
    transport as _mod_transport,
    urlutils,
    )
from breezy.branch import BranchWriteLockResult
from breezy.errors import (
    BadFilenameEncoding,
    BzrError,
    MergeModifiedFormatError,
    NotBranchError,
    NoSuchFile,
    NoSuchId,
    NoSuchRevision,
    NoRepositoryPresent,
    NoWorkingTree,
    ReadOnlyError,
    TokenLockingNotSupported,
    TransportNotPossible,
    UnsupportedFormatError,
    UnsupportedOperation,
    UninitializableFormat,
    )
from breezy.bzr.inventory import (
    InventoryDirectory,
    InventoryFile,
    InventoryLink,
    )
from breezy.lock import (
    LogicalLockResult,
    )
from breezy.lockable_files import (
    TransportLock,
    )
from breezy.revision import (
    CURRENT_REVISION,
    NULL_REVISION,
    )
from breezy.sixish import (
    text_type,
    )
from breezy.trace import (
    mutter,
    note,
    )
from breezy.bzr.workingtree import (
    MERGE_MODIFIED_HEADER_1,
    )
from breezy.workingtree import (
    WorkingTree,
    WorkingTreeFormat,
    )

from breezy.plugins.svn import (
    SvnWorkingTreeProber,
    svk,
    )
from breezy.plugins.svn.commit import (
    _revision_id_to_svk_feature,
    )
from breezy.plugins.svn.errors import (
    convert_svn_error,
    NotSvnBranchPath,
    NoSvnRepositoryPresent,
    )
from breezy.plugins.svn.mapping import (
    escape_svn_path,
    )
from breezy.plugins.svn.transport import (
    SvnRaTransport,
    svn_config,
    )
from breezy.plugins.svn.tree import (
    BasisTreeIncomplete,
    SvnBasisTree,
    SubversionTree,
    )

# Compatibility with subvertpy < 0.8.9
ERR_WC_NODE_KIND_CHANGE = getattr(subvertpy, "ERR_WC_NODE_KIND_CHANGE", 155018)


from breezy.controldir import Converter
from breezy.controldir import (
    ControlDirFormat,
    ControlDir,
    format_registry,
    )


class RepositoryRootUnknown(BzrError):
    _fmt = "The working tree does not store the root of the Subversion repository."


class LocalRepositoryOpenFailed(BzrError):

    _fmt = ("Unable to open local repository at %(url)s")

    def __init__(self, url):
        self.url = url


class CorruptWorkingTree(BzrError):

    _fmt = ("Unable to open working tree at %(path)s: %(msg)s")

    def __init__(self, path, msg):
        self.path = path
        self.msg = msg


def update_wc(adm, basedir, conn, url, revnum):
    # FIXME: honor SVN_CONFIG_SECTION_HELPERS:SVN_CONFIG_OPTION_DIFF3_CMD
    # FIXME: honor SVN_CONFIG_SECTION_MISCELLANY:SVN_CONFIG_OPTION_USE_COMMIT_TIMES
    # FIXME: honor SVN_CONFIG_SECTION_MISCELLANY:SVN_CONFIG_OPTION_PRESERVED_CF_EXTS
    print(basedir)
    editor = adm.get_switch_editor(
            "", url, use_commit_times=False,
            depth=DEPTH_INFINITY, notify_func=None, diff3_cmd=None,
            depth_is_sticky=False, allow_unver_obstructions=True)
    reporter = conn.do_switch(revnum, "", True, url, editor)
    try:
        adm.crawl_revisions(basedir, reporter, restore_files=False,
                            recurse=True, use_commit_times=True)
    except subvertpy.SubversionException, (msg, num):
        if num == subvertpy.ERR_RA_ILLEGAL_URL:
            raise BzrError(msg)
        raise
    # FIXME: handle externals


def apply_prop_changes(orig_props, prop_changes):
    """Apply a set of property changes to a properties dictionary.

    :param orig_props: Dictionary with original properties (will be modified)
    :param prop_changes: Dictionary of new property values (None for deletion)
    :return: New dictionary
    """
    for k,v in prop_changes:
        if v is None:
            del orig_props[k]
        else:
            orig_props[k] = v
    return orig_props


class Walker(object):
    """Iterator of a Subversion working copy."""

    def __init__(self, workingtree, start=u"", recursive=True):
        """Create a new walker.

        :param workingtree: bzr-svn working tree to walk over
        :param start: Start path, relative to tree root
        :param recursive: Whether to be recursive
        """
        self.workingtree = workingtree
        self.todo = list([start])
        self.pending = deque()
        self.recursive = recursive

    def __iter__(self):
        return iter(self.next, None)

    def next(self):
        while not self.pending:
            try:
                p = self.todo.pop()
            except IndexError:
                return None
            try:
                wc = self.workingtree._get_wc(p)
            except subvertpy.SubversionException, (_, num):
                if num == subvertpy.ERR_WC_NOT_DIRECTORY:
                    # Turns out it's not a directory after all
                    wc = self.workingtree._get_wc(write_lock=False)
                    try:
                        try:
                            self.pending.append((p, self.workingtree._get_entry(wc, p)))
                        except KeyError:
                            pass
                    finally:
                        wc.close()
                    continue
                raise
            try:
                entries = wc.entries_read(True)
                for name in sorted(entries):
                    entry = entries[name]
                    subp = osutils.pathjoin(p, name.decode("utf-8")).rstrip("/")
                    if entry.kind == subvertpy.NODE_DIR and name != "":
                        if self.recursive:
                            self.todo.append(subp)
                    else:
                        self.pending.append((subp, entry))
            finally:
                wc.close()
        return self.pending.popleft()


class SvnWorkingTree(SubversionTree, WorkingTree):
    """WorkingTree implementation that uses a svn working copy for storage."""

    def __init__(self, controldir, format, local_path, entry):
        self._reset_data()
        if not isinstance(local_path, text_type):
            raise TypeError(local_path)
        self.entry = entry
        self.basedir = local_path
        self._format = format
        self.controldir = controldir
        self._branch = None
        self._cached_base_tree = None
        self._detect_case_handling()
        self.bzr_controldir = os.path.join(controldir.local_path, controldir._adm_dir, 'bzr')
        try:
            os.makedirs(self.bzr_controldir)
            os.makedirs(os.path.join(self.bzr_controldir, 'lock'))
        except OSError:
            pass
        control_transport = controldir.transport.clone('bzr')
        self._transport = control_transport
        cache_filename = control_transport.local_abspath('stat-cache')
        self._hashcache = hashcache.HashCache(self.basedir, cache_filename,
            self.controldir._get_file_mode(),
            self._content_filter_stack_provider())
        self._hashcache.read()
        self._lock_count = 0
        self._lock_mode = None
        self._control_files = None
        self.views = self._make_views()

    def _cleanup(self):
        pass

    @property
    def mapping(self):
        """bzr-svn mapping to use."""
        return self.branch.mapping

    def stored_kind(self, path, file_id=None):
        assert path is not None
        try:
            return self.basis_tree().kind(path, file_id)
        except NoSuchId:
            return None

    def kind(self, path, file_id=None):
        abspath = self.abspath(path)
        try:
            return osutils.file_kind(abspath)
        except NoSuchFile:
            return None

    def _detect_case_handling(self):
        try:
            self.controldir.transport.stat("FoRmAt")
        except NoSuchFile:
            self.case_sensitive = True
        else:
            self.case_sensitive = False

    def _set_root_id(self, file_id):
        self._change_fileid_mapping(file_id, u"")

    def get_file_mtime(self, path, file_id=None):
        """See Tree.get_file_mtime."""
        return os.lstat(self.abspath(path)).st_mtime

    def _setup_directory_is_tree_reference(self):
        self._directory_is_tree_reference = self._directory_is_never_tree_reference

    def get_file_sha1(self, path, file_id=None, stat_value=None):
        return self._hashcache.get_sha1(path, stat_value)

    @property
    def basis_idmap(self):
        if self._cached_base_idmap is not None:
            return self._cached_base_idmap
        idmap = self.branch.repository.get_fileid_map(
                    self._get_base_revmeta(),
                    self.mapping)
        if self.is_locked():
            self._cached_base_idmap = idmap
        return idmap

    def get_branch_path(self, revnum=None):
        if revnum is None:
            try:
                return self.controldir.get_branch_path()
            except RepositoryRootUnknown:
                pass
        return self.branch.get_branch_path(revnum)

    @property
    def branch(self):
        if self._branch is None:
            self._branch = self.controldir.open_branch(revnum=self.base_revnum)
        return self._branch

    def __repr__(self):
        return "<%s of %s>" % (self.__class__.__name__, self.basedir.encode('utf-8'))

    def conflicts(self):
        # FIXME: Retrieve conflicts
        return _mod_conflicts.ConflictList()

    def get_ignore_list(self):
        """Obtain the list of ignore patterns for this working tree.

        :note: Will interpret the svn:ignore properties, rather than read
               .bzrignore
        """
        ignores = set([get_adm_dir()])
        ignores.update(svn_config.get_default_ignores())

        def dir_add(wc, prefix, patprefix):
            if not isinstance(prefix, text_type):
                raise TypeError(prefix)
            if not isinstance(patprefix, bytes):
                raise TypeError(patprefix)
            ignorestr = wc.prop_get(properties.PROP_IGNORE,
                self.abspath(prefix).rstrip("/").encode("utf-8"))
            if ignorestr is not None:
                for pat in ignorestr.splitlines():
                    ignores.add(urlutils.joinpath(patprefix, pat))

            entries = wc.entries_read(False)
            for entry in entries:
                if entry == "":
                    continue

                # Ignore ignores on things that aren't directories
                if entries[entry].kind != subvertpy.NODE_DIR:
                    continue

                subprefix = osutils.pathjoin(prefix, entry.decode("utf-8"))

                try:
                    subwc = self._get_wc(subprefix, base=wc)
                except subvertpy.SubversionException, (_, num):
                    if num == subvertpy.ERR_WC_NOT_DIRECTORY:
                        continue
                    raise
                try:
                    dir_add(subwc, subprefix, urlutils.joinpath(patprefix,
                        entry))
                finally:
                    subwc.close()

        with self._get_wc() as wc:
            dir_add(wc, u"", ".")

        return ignores

    def is_ignored(self, path):
        dirname = os.path.dirname(path)
        with self._get_wc(relpath=dirname) as wc:
            ignores = svn_config.get_default_ignores()
            ignorestr = wc.prop_get(properties.PROP_IGNORE,
                self.abspath(dirname))
            if ignorestr is not None:
                ignores.extend(ignorestr.splitlines())
            return match_ignore_list(os.path.basename(path), ignores)

    def flush(self):
        pass

    def is_control_filename(self, path):
        """Check whether path is a control file (used by bzr or svn)."""
        return is_adm_dir(path)

    def _update(self, branch_path, revnum, show_base):
        if revnum is None:
            # FIXME: should be able to use -1 here
            revnum = self.branch.get_revnum()
        old_branch_path = self.get_branch_path()
        with self._get_wc(write_lock=True, depth=-1) as adm:
            conn = self.branch.repository.transport.get_connection(old_branch_path)
            try:
                update_wc(adm, self.basedir.encode("utf-8"), conn,
                        urlutils.join(self.branch.repository.transport.svn_url, branch_path),
                        revnum)
            finally:
                self.branch.repository.transport.add_connection(conn)
        return revnum

    def update(self, change_reporter=None, possible_transports=None,
               revision=None, old_tip=None, show_base=False, revnum=None):
        """Update the workingtree to a new Bazaar revision number.

        """
        orig_revnum = self.base_revnum
        if revision is not None and revnum is not None:
            raise AssertionError("revision and revnum are mutually exclusive")
        if revision is not None:
            ((uuid, branch_path, revnum), mapping) = self.branch.lookup_bzr_revision_id(revision)
        else:
            branch_path = self.branch.get_branch_path()
        self._cached_base_revnum = self._update(branch_path, revnum,
            show_base=show_base)
        self._cached_base_revid = None
        self._cached_base_idmap = None
        return self.base_revnum - orig_revnum

    def remove(self, files, verbose=False, to_file=None, keep_files=True,
               force=False):
        """Remove files from the working tree."""
        # FIXME: Use to_file argument
        # FIXME: Use verbose argument
        assert isinstance(files, list)
        with self._get_wc(write_lock=True) as wc:
            for file in files:
                try:
                    wc.delete(osutils.safe_utf8(self.abspath(file)),
                              keep_local=keep_files)
                except subvertpy.SubversionException, (msg, num):
                    if num == ERR_BAD_FILENAME:
                        note("%s does not exist." % file)
                    else:
                        raise

        for file in files:
            self._change_fileid_mapping(None, file)

    def unversion(self, paths, file_ids=None):
        with self._get_wc(write_lock=True) as wc:
            for path in paths:
                wc.delete(osutils.safe_utf8(self.abspath(path)),
                          keep_local=True)

    def all_versioned_paths(self):
        ret = set()
        w = Walker(self)
        for path, entry in w:
            ret.add(path)
        return ret

    def all_file_ids(self):
        """See Tree.all_file_ids"""
        ret = set()
        w = Walker(self)
        for path, entry in w:
            try:
                ret.add(self.lookup_id(path)[0])
            except KeyError:
                pass
        return ret

    @convert_svn_error
    def _get_wc(self, relpath=u"", write_lock=False, depth=0, base=None):
        """Open a working copy handle."""
        return Adm(base,
            self.abspath(relpath).rstrip("/"),
            write_lock, depth)

    def _get_rel_wc(self, relpath, write_lock=False):
        if not isinstance(relpath, text_type):
            raise TypeError(relpath)
        dir = os.path.dirname(relpath)
        file = os.path.basename(relpath)
        return (self._get_wc(dir, write_lock), file)

    def _rename_fileid(self, old_path, new_path, wc=None):
        self._change_fileid_mapping(self.path2id(old_path), new_path, wc)
        self._change_fileid_mapping(None, old_path, wc)
        if not os.path.isdir(self.abspath(old_path)):
            return
        for from_subpath, entry in Walker(self, old_path):
            from_subpath = from_subpath.strip("/")
            to_subpath = osutils.pathjoin(new_path,
                from_subpath[len(old_path):].strip("/"))
            self._change_fileid_mapping(self.path2id(from_subpath), to_subpath,
                    wc)
            self._change_fileid_mapping(None, from_subpath, wc)

    def move(self, from_paths, to_dir=None, after=False, **kwargs):
        """Move files to a new location."""
        # FIXME: Use after argument
        if after:
            raise NotImplementedError("move after not supported")
        for from_path in from_paths:
            from_abspath = osutils.safe_utf8(self.abspath(from_path))
            new_path = osutils.pathjoin(
                osutils.safe_utf8(to_dir),
                osutils.safe_utf8(os.path.basename(from_path)))
            self._rename_fileid(from_path, new_path)
            with self._get_wc(osutils.safe_unicode(to_dir), write_lock=True) as to_wc:
                to_wc.copy(from_abspath,
                    osutils.safe_utf8(os.path.basename(from_path)))
            with self._get_wc(write_lock=True) as from_wc:
                from_wc.delete(from_abspath)

    def rename_one(self, from_rel, to_rel, after=False):
        from_rel = osutils.safe_unicode(from_rel)
        to_rel = osutils.safe_unicode(to_rel)
        # FIXME: Use after
        if after:
            raise NotImplementedError("rename_one after not supported")
        from_wc = None
        self._rename_fileid(from_rel, to_rel)
        (to_wc, to_file) = self._get_rel_wc(to_rel, write_lock=True)
        try:
            if os.path.dirname(from_rel) == os.path.dirname(to_rel):
                # Prevent lock contention
                from_wc = to_wc
            else:
                (from_wc, _) = self._get_rel_wc(from_rel, write_lock=True)
            try:
                to_wc.copy(self.abspath(from_rel), to_file)
                from_wc.delete(self.abspath(from_rel))
            finally:
                from_wc.close()
        finally:
            if from_wc != to_wc:
                to_wc.close()

    def path_to_file_id(self, revnum, current_revnum, path):
        """Generate a bzr file id from a Subversion file name.

        :param revnum: Revision number.
        :param path: Path of the file
        :return: Tuple with file id and revision id.
        """
        if not isinstance(path, text_type):
            raise TypeError(path)
        path = osutils.normpath(path)
        if path == u".":
            path = u""
        return self.lookup_id(path)

    def _find_ids(self, relpath, entry):
        if not isinstance(relpath, text_type):
            raise TypeError(relpath)
        assert entry.schedule in (SCHEDULE_NORMAL,
                                  SCHEDULE_DELETE,
                                  SCHEDULE_ADD,
                                  SCHEDULE_REPLACE)
        if entry.schedule == SCHEDULE_NORMAL:
            # Keep old id
            try:
                return self.path_to_file_id(entry.cmt_rev, entry.revision,
                        relpath)
            except KeyError:
                if entry.revision == self._cached_base_revnum:
                    raise AssertionError("file %s:%d not in fileid map for %d" % (
                        relpath, entry.revision, self._cached_base_revnum))
                # For some reason a file that doesn't exist in the current revision
                # has ended up here. Let's just generate a NEW- file id
        elif entry.schedule == SCHEDULE_DELETE:
            return (None, None)
        elif (entry.schedule == SCHEDULE_ADD or
              entry.schedule == SCHEDULE_REPLACE):
            ids = self._get_new_file_ids()
            if ids.has_key(relpath):
                return (ids[relpath], None)
        else:
            raise AssertionError("unknown schedule value %r for %s" % (
                entry.schedule, relpath))
        # FIXME: Generate more random but consistent file ids
        return ("NEW-" + escape_svn_path(osutils.safe_utf8(relpath).strip("/")),
                None)

    def get_root_id(self):
        return self.path2id("")

    def path2id(self, path):
        with self._get_wc() as wc:
            try:
                entry = self._get_entry(wc, path)
                (file_id, revision) = self._find_ids(osutils.safe_unicode(path), entry)
            except KeyError:
                return None
            else:
                return file_id

    def _get_entry(self, wc, path):
        path = osutils.safe_utf8(self.abspath(path))
        related_wc = wc.probe_try(path)
        if related_wc is None:
            raise KeyError
        try:
            return related_wc.entry(path)
        finally:
            related_wc.close()

    def filter_unversioned_files(self, paths):
        ret = set()
        with self._get_wc() as wc:
            for p in paths:
                try:
                    entry = self._get_entry(wc, p)
                except KeyError:
                    ret.add(p)
                else:
                    if entry.schedule == SCHEDULE_DELETE:
                        ret.add(p)
        return ret

    def has_or_had_id(self, file_id):
        if self.has_id(file_id):
            return True
        if self.basis_tree().has_id(file_id):
            return True
        return False

    def has_id(self, file_id):
        try:
            self.id2path(file_id)
        except NoSuchId:
            return False
        else:
            return True

    def id2path(self, file_id):
        ids = self._get_new_file_ids()
        for path, fid in ids.iteritems():
            if file_id == fid:
                return path
        try:
            return self.basis_idmap.reverse_lookup(self.mapping, file_id)
        except KeyError:
            pass
        if file_id.startswith("NEW-"):
            return urlutils.unescape(file_id[4:])
        if file_id == self.get_root_id():
            # Special case if self.last_revision() == 'null:'
            return ""
        raise NoSuchId(self, file_id)

    def _ie_from_entry(self, relpath, entry, parent_id):
        assert type(parent_id) is str or parent_id is None
        if not isinstance(relpath, text_type):
            raise TypeError(relpath)
        (file_id, revid) = self._find_ids(relpath, entry)
        abspath = self.abspath(relpath)
        basename = os.path.basename(relpath)
        if entry.kind == subvertpy.NODE_DIR:
            ie = InventoryDirectory(file_id, basename, parent_id)
            ie.revision = revid
            return ie
        elif os.path.islink(abspath):
            ie = InventoryLink(file_id, basename, parent_id)
            ie.revision = revid
            target_path = os.readlink(abspath.encode(osutils._fs_enc))
            ie.symlink_target = target_path.decode(osutils._fs_enc)
            return ie
        else:
            ie = InventoryFile(file_id, basename, parent_id)
            ie.revision = revid
            try:
                data = osutils.fingerprint_file(
                    open(abspath.encode(osutils._fs_enc)))
            except IOError, e:
                if e.errno == errno.EISDIR:
                    ie = InventoryDirectory(file_id, basename, parent_id)
                    ie.revision = None
                    return ie
                elif e.errno == errno.ENOENT:
                    return None
                raise
            else:
                ie.text_sha1 = data['sha1']
                ie.text_size = data['size']
                ie.executable = self.is_executable(relpath)
                return ie

    def iter_child_entries(self, path, file_id=None):
        """See Tree.iter_child_entries."""
        entry = self.iter_entries_by_dir(specific_files=[path]).next()[1]
        for child in getattr(entry, 'children', {}).itervalues():
            yield child

    def iter_entries_by_dir(self, specific_files=None):
        """See WorkingTree.iter_entries_by_dir."""
        if specific_files is not None:
            with self._get_wc() as wc:
                for path in specific_files:
                    parent = os.path.dirname(path)
                    parent_id = self.lookup_id(parent)
                    entry = self._get_entry(wc, path)
                    if entry.schedule == SCHEDULE_DELETE:
                        continue
                    ie = self._ie_from_entry(path, entry, parent_id[0])
                    if ie is not None:
                        yield path, ie
        else:
            fileids = {}
            w = Walker(self)
            for relpath, entry in w:
                if entry.schedule == SCHEDULE_DELETE:
                    continue
                if not isinstance(relpath, text_type):
                    raise TypeError(relpath)
                if relpath == u"":
                    parent_id = None
                else:
                    parent_id = fileids[os.path.dirname(relpath)]
                ie = self._ie_from_entry(relpath, entry, parent_id)
                if ie is not None:
                    fileids[relpath] = ie.file_id
                    yield relpath, ie

    def extras(self):
        """See WorkingTree.extras."""
        w = Walker(self)
        versioned_files = set()
        all_files = set()
        for path, dir_entry in w:
            versioned_files.add(path)
            dirabs = self.abspath(path)
            if not osutils.isdir(dirabs):
                # e.g. directory deleted
                continue
            for subf in os.listdir(dirabs):
                if self.controldir.is_control_filename(subf):
                    continue
                try:
                    (subf_norm, can_access) = osutils.normalized_filename(subf)
                except UnicodeDecodeError:
                    path_os_enc = path.encode(osutils._fs_enc)
                    relpath = path_os_enc + '/' + subf
                    raise BadFilenameEncoding(relpath, osutils._fs_enc)
                subp = os.path.join(path, subf_norm)
                all_files.add(subp)
        return iter(all_files - versioned_files)

    def set_last_revision(self, revid):
        mutter('setting last revision to %r', revid)
        if revid == NULL_REVISION:
            # If there is a hidden revision at the beginning of the branch,
            # use that as the NULL revision.
            foreign_revid = None
            for revmeta, hidden, mapping in self.branch._iter_revision_meta_ancestry():
                if hidden:
                    foreign_revid = revmeta.metarev.get_foreign_revid()
                else:
                    foreign_revid = None
            if foreign_revid is None:
                raise NotImplementedError("unable to set NULL_REVISION if there is "
                    "no hidden initial revision")
            self._cached_base_revnum = foreign_revid[2]
            self._cached_base_idmap = None
        else:
            (foreign_revid, mapping) = self.branch.lookup_bzr_revision_id(revid)
            self._cached_base_revnum = foreign_revid[2]
            self._cached_base_idmap = None
        # FIXME: set any items that are no longer versioned to SCHEDULE_ADD,
        # but remove their versioning information
        self._cached_base_revid = revid

    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
        """See MutableTree.set_parent_trees."""
        self.set_parent_ids([rev for (rev, tree) in parents_list])

    def get_parent_ids(self):
        """See Tree.get_parent_ids.

        This implementation reads the pending merges list and last_revision
        value and uses that to decide what the parents list should be.
        """
        last_rev = self._last_revision()
        if NULL_REVISION == last_rev:
            parents = []
        else:
            parents = [last_rev]
        if not self._cached_merges:
            try:
                merges_bytes = self._transport.get_bytes('pending-merges')
            except NoSuchFile:
                merges = []
            else:
                merges = []
                for l in osutils.split_lines(merges_bytes):
                    revision_id = l.rstrip('\n')
                    merges.append(revision_id)
            if self.is_locked():
                self._cached_merges = merges
        else:
            merges = self._cached_merges
        parents.extend(merges)
        return parents

    def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
        """See MutableTree.set_parent_ids."""
        if parent_ids == []:
            merges = []
            self.set_last_revision(NULL_REVISION)
        else:
            merges = parent_ids[1:]
            self.set_last_revision(parent_ids[0])
        if self.is_locked():
            self._cached_merges = merges
        self._transport.put_bytes('pending-merges', '\n'.join(merges),
            mode=self.controldir._get_file_mode())
        with self._get_wc(write_lock=True) as adm:
            old_svk_merges = svk.parse_svk_features(self._get_svk_merges(
                self._get_base_branch_props()))
            svk_merges = set(old_svk_merges)

            # Set svk:merge
            for merge in merges:
                try:
                    svk_merges.add(_revision_id_to_svk_feature(merge,
                        self.branch.repository.lookup_bzr_revision_id))
                except NoSuchRevision:
                    pass
            if old_svk_merges != svk_merges:
                adm.prop_set(
                    svk.SVN_PROP_SVK_MERGE,
                    svk.serialize_svk_features(svk_merges),
                    self.basedir.encode("utf-8"))

    def smart_add(self, file_list, recurse=True, action=None, save=True):
        """See MutableTree.smart_add()."""
        assert isinstance(recurse, bool)
        if action is None:
            action = breezy.add.AddAction()
        # TODO: use action
        if not file_list:
            # no paths supplied: add the entire tree.
            file_list = [u'.']
        ignored = defaultdict(list)
        added = []

        for file_path in file_list:
            todo = []
            file_path = os.path.abspath(osutils.safe_unicode(file_path))
            f = self.relpath(file_path)
            with self._get_wc(os.path.dirname(f.encode(osutils._fs_enc)).decode(osutils._fs_enc), write_lock=True) as wc:
                if self.filter_unversioned_files([f]):
                    if save:
                        mutter('adding %r', file_path)
                        wc.add(file_path)
                        self._fix_special(wc, file_path, f)
                    added.append(file_path)
                if (recurse and
                    osutils.file_kind(file_path.encode(osutils._fs_enc)) == 'directory'):
                    # Filter out ignored files and update ignored
                    for c in os.listdir(file_path.encode(osutils._fs_enc)):
                        if self.is_control_filename(c):
                            continue
                        c = c.decode(osutils._fs_enc)
                        c_path = osutils.pathjoin(file_path, c)
                        ignore_glob = self.is_ignored(c)
                        if ignore_glob is not None:
                            ignored[ignore_glob].append(c_path)
                        todo.append(c_path)
            if todo != []:
                cadded, cignored = self.smart_add(todo, recurse, action, save)
                added.extend(cadded)
                ignored.update(cignored)
        return added, ignored

    def _fix_special(self, adm, abspath, relpath, kind=None):
        if kind is None:
            kind = self.kind(relpath)
        if kind == "file":
            value = None
        elif kind == "symlink":
            value = properties.PROP_SPECIAL_VALUE
        else:
            return
        adm.prop_set(properties.PROP_SPECIAL, value, abspath)

    def _fix_kind(self, adm, abspath, relpath, entry):
        kind = self.kind(relpath)
        if ((entry.kind == subvertpy.NODE_DIR and kind in ('file', 'symlink')) or
            (entry.kind == subvertpy.NODE_FILE and kind == 'directory')):
            try:
                adm.delete(abspath, keep_local=True)
                adm.add(abspath)
            except subvertpy.SubversionException, (_, num):
                if num == ERR_WC_NODE_KIND_CHANGE:
                    if entry.kind == subvertpy.NODE_DIR:
                        from_kind = "directory"
                    else:
                        from_kind = "file or symlink"
                    raise bzr_errors.UnsupportedKindChange(relpath,
                        from_kind, kind, self._format)
                raise
        self._fix_special(adm, abspath, relpath, kind)

    def add(self, files, ids=None, kinds=None, _copyfrom=None):
        """Add files to the working tree."""
        # TODO: Use kinds
        if isinstance(files, basestring):
            files = [files]
            if isinstance(ids, str):
                ids = [ids]
        if ids is None:
            ids = [None] * len(files)
        if _copyfrom is None:
            _copyfrom = [(None, -1)] * len(files)
        if kinds is None:
            kinds = [self.kind(file) for file in files]
        assert isinstance(files, list)
        for f, kind, file_id, copyfrom in zip(files, kinds, ids, _copyfrom):
            with self._get_wc(os.path.dirname(osutils.safe_unicode(f)),
                    write_lock=True) as wc:
                try:
                    utf8_abspath = osutils.safe_utf8(self.abspath(f))
                    wc.add(utf8_abspath, copyfrom[0], copyfrom[1])
                except subvertpy.SubversionException, (_, num):
                    if num in (subvertpy.ERR_ENTRY_EXISTS,
                               subvertpy.ERR_WC_SCHEDULE_CONFLICT):
                        continue
                    elif num == subvertpy.ERR_WC_PATH_NOT_FOUND:
                        raise NoSuchFile(path=f)
                    raise
                self._fix_special(wc, utf8_abspath, f)
            if file_id is not None:
                self._change_fileid_mapping(file_id, f)

    def basis_tree(self):
        """Return the basis tree for a working tree."""
        try:
            return SvnBasisTree(self)
        except BasisTreeIncomplete:
            return self.branch.basis_tree()

    def list_files(self, include_root=False, from_dir=None, recursive=True):
        """See ``Tree.list_files``."""
        # TODO: This doesn't sort the output
        # TODO: This ignores unversioned files at the moment
        if from_dir is None:
            from_dir = u""
        else:
            from_dir = osutils.safe_unicode(from_dir)
        w = Walker(self, from_dir, recursive=recursive)
        fileids = {}
        for path, entry in w:
            relpath = path.decode("utf-8")
            if entry.schedule in (SCHEDULE_NORMAL, SCHEDULE_ADD,
                    SCHEDULE_REPLACE):
                versioned = 'V'
            else:
                versioned = '?'
            if relpath == u"":
                parent_id = None
            else:
                parent_path = os.path.dirname(relpath)
                try:
                    parent_id = fileids[parent_path]
                except KeyError:
                    parent_id = self.path2id(parent_path)
                    assert type(parent_id) is str
                    fileids[parent_path] = parent_id
            ie = self._ie_from_entry(relpath, entry, parent_id)
            if ie is not None:
                fileids[relpath] = ie.file_id
            if include_root or relpath != u"":
                yield relpath, versioned, ie.kind, ie.file_id, ie

    def revision_tree(self, revid):
        return self.branch.repository.revision_tree(revid)

    def unprefix(self, relpath):
        """Remove the branch path from a relpath.

        :param relpath: path from the repository root.
        """
        assert relpath.startswith(self.get_branch_path()), \
                "expected %s prefix, got %s" % (self.get_branch_path(), relpath)
        return relpath[len(self.get_branch_path()):].strip("/")

    def pull(self, source, overwrite=False, stop_revision=None,
             delta_reporter=None, possible_transports=None, local=False,
             show_base=False):
        """Pull in changes from another branch into this working tree."""
        # FIXME: Use delta_reporter
        result = self.branch.pull(source, overwrite=overwrite,
            stop_revision=stop_revision, local=local)
        fetched = self._update(self.branch.get_branch_path(),
            self.branch.get_revnum(), show_base=show_base)
        self._cached_base_revnum = fetched
        self._cached_base_revid = None
        self._cached_base_idmap = None
        self._cached_merges = None
        return result

    def get_file_properties(self, path, file_id=None):
        abspath = self.abspath(path)
        with self._get_wc(write_lock=False) as root_adm, \
             root_adm.probe_try(abspath.encode("utf-8"), False, 2) as adm:
            try:
                (prop_changes, orig_props) = adm.get_prop_diffs(
                    abspath.encode("utf-8"))
            except subvertpy.SubversionException, (_, num):
                if num in (subvertpy.ERR_WC_NOT_DIRECTORY,
                           subvertpy.ERR_WC_NOT_LOCKED):
                    return {}
                raise
        return apply_prop_changes(orig_props, prop_changes)

    def _change_fileid_mapping(self, id, path, wc=None):
        """Change the file id mapping for a particular path."""
        if wc is None:
            subwc = self._get_wc(write_lock=True)
        else:
            subwc = wc
        try:
            new_entries = self._get_new_file_ids()
            if id is None:
                if new_entries.has_key(path):
                    del new_entries[path]
            else:
                assert isinstance(id, str)
                new_entries[path] = id
            fileprops = self._get_branch_props()
            self.mapping.export_fileid_map_fileprops(new_entries, fileprops)
            self._set_branch_props(subwc, fileprops)
        finally:
            if wc is None:
                subwc.close()

    def lookup_id(self, path, entry=None):
        ids = self._get_new_file_ids()
        if path in ids:
            return (ids[path], None)
        try:
            return self.basis_idmap.lookup(self.mapping, path)[:2]
        except KeyError:
            if path == "":
                return ("root-%s-%s" % (
                    escape_svn_path(self.get_branch_path()),
                    self.entry.uuid), None)
            mutter("fileid map: %r", self._get_new_file_ids())
            raise

    def _get_changed_branch_props(self):
        with self._get_wc() as wc:
            ret = {}
            (prop_changes, orig_props) = wc.get_prop_diffs(
                self.basedir.encode("utf-8"))
            for k,v in prop_changes:
                ret[k] = (orig_props.get(k), v)
            return ret

    def _get_branch_props(self):
        return self.get_file_properties("")

    def _set_branch_props(self, wc, fileprops):
        for k, v in fileprops.iteritems():
            wc.prop_set(k, v, self.basedir.encode("utf-8"))

    def _get_base_branch_props(self):
        with self._get_wc() as wc:
            (prop_changes, orig_props) = wc.get_prop_diffs(
                self.basedir.encode("utf-8"))
            return orig_props

    def _get_new_file_ids(self):
        return self.mapping.import_fileid_map_fileprops(
            self._get_changed_branch_props())

    def _get_svk_merges(self, base_branch_props):
        return base_branch_props.get(svk.SVN_PROP_SVK_MERGE, "")

    def _apply_inventory_delta_change(self, base_tree, old_path, new_path,
                                      file_id, ie):
        already_there = (
            old_path == new_path and
            base_tree.kind(old_path) == ie.kind)
        if old_path is not None:
            old_abspath = osutils.safe_utf8(self.abspath(old_path))
            if not already_there:
                self.remove([old_path], keep_files=True)
            copyfrom = (urlutils.join(self.entry.url, old_path),
                        self.base_revnum)
        else:
            try:
                old_path = base_tree.id2path(file_id)
            except NoSuchId:
                copyfrom = (None, -1)
            else:
                copyfrom = (urlutils.join(self.entry.url, old_path),
                            self.base_revnum)
        if new_path is not None:
            if not already_there:
                # if base_tree.id2path(file_id) == new_path and base_tree._inventory[file_id].kind == ie.kind
                # FIXME: Revert
                # else:
                self.add([new_path], [file_id], _copyfrom=[copyfrom])

    def apply_inventory_delta(self, delta):
        """Apply an inventory delta."""
        def cmp_delta_changes(a, b):
            # Only sort on new_path for now
            return cmp(a[1], b[1])
        delta.sort(cmp_delta_changes)
        base_tree = self.basis_tree()
        for (old_path, new_path, file_id, ie) in delta:
            self._apply_inventory_delta_change(base_tree, old_path, new_path,
                file_id, ie)

    @property
    def base_revnum(self):
        if self._cached_base_revnum is not None:
            return self._cached_base_revnum
        max_rev = revision_status(self.basedir.encode("utf-8"), None, True)[1]
        if self.is_locked():
            self._cached_base_revnum = max_rev
        return max_rev

    def _last_revision(self):
        if self._cached_base_revid is not None:
            return self._cached_base_revid
        try:
            revid = self.branch.generate_revision_id(self.base_revnum)
        except NoSuchRevision:
            # Current revision is no longer on the mainline?
            revid = self.branch.repository.generate_revision_id(self.base_revnum,
                self.get_branch_path(), self.mapping)
        if self.is_locked():
            self._cached_base_revid = revid
        return revid

    def path_content_summary(self, path, _lstat=os.lstat,
        _mapper=osutils.file_kind_from_stat_mode):
        """See Tree.path_content_summary."""
        abspath = self.abspath(path)
        try:
            stat_result = _lstat(abspath.encode(osutils._fs_enc))
        except OSError, e:
            import errno
            if getattr(e, 'errno', None) == errno.ENOENT:
                # no file.
                return ('missing', None, None, None)
            # propagate other errors
            raise
        kind = _mapper(stat_result.st_mode)
        if kind == 'file':
            size = stat_result.st_size
            # try for a stat cache lookup
            executable = self._is_executable_from_path_and_stat(path,
                stat_result)
            return (kind, size, executable, self._sha_from_stat(
                path, stat_result))
        elif kind == 'directory':
            return kind, None, None, None
        elif kind == 'symlink':
            target = os.readlink(abspath.encode(osutils._fs_enc))
            return ('symlink', None, None, target.decode(osutils._fs_enc))
        else:
            return (kind, None, None, None)

    def _get_base_revmeta(self):
        return self.branch.repository._revmeta_provider.lookup_revision(
            self.get_branch_path(), self.base_revnum)

    def _reset_data(self):
        self._cached_base_revnum = None
        self._cached_base_idmap = None
        self._cached_base_revid = None
        self._cached_merges = None

    def break_lock(self):
        """Break a lock if one is present from another instance.

        Uses the ui factory to ask for confirmation if the lock may be from
        an active process.

        This will probe the repository for its lock as well.
        """
        cleanup(self.basedir.encode("utf-8"))

    def is_locked(self):
        return (self._lock_mode is not None)

    def _lock_write(self):
        if self._lock_mode:
            if self._lock_mode == 'r':
                raise ReadOnlyError(self)
            self._lock_count += 1
        else:
            self._lock_mode = 'w'
            self._lock_count = 1
        return BranchWriteLockResult(self.unlock, None)

    def lock_tree_write(self):
        ret = self._lock_write()
        try:
            self.branch.lock_read()
        except:
            ret.unlock()
            raise
        return ret

    def get_physical_lock_status(self):
        return False

    def lock_write(self, token=None):
        """See Branch.lock_write()."""
        # TODO: Obtain lock on the remote server?
        if token is not None:
            raise TokenLockingNotSupported(self)
        ret = self._lock_write()
        try:
            self.branch.lock_write()
        except:
            ret.unlock()
            raise
        return ret

    def lock_read(self):
        """See Branch.lock_read()."""
        if self._lock_mode:
            assert self._lock_mode in ('r', 'w')
            self._lock_count += 1
        else:
            self._lock_mode = 'r'
            self._lock_count = 1
        self.branch.lock_read()
        return LogicalLockResult(self.unlock)

    def unlock(self):
        self._lock_count -= 1
        if self._lock_count == 0:
            self._lock_mode = None
            self._cleanup()
            self._reset_data()
        self.branch.unlock()

    def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
        mode = stat_result.st_mode
        return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)

    if not osutils.supports_executable():
        def is_executable(self, path, file_id=None):
            basis_tree = self.basis_tree()
            if file_id in basis_tree:
                return basis_tree.is_executable(path, file_id)
            # Default to not executable
            return False
    else:
        def is_executable(self, path, file_id=None):
            mode = os.lstat(self.abspath(path)).st_mode
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)

        _is_executable_from_path_and_stat = \
            _is_executable_from_path_and_stat_from_stat

    def transmit_svn_dir_deltas(self, file_id, editor):
        path = self.id2path(file_id)
        encoded_path = self.abspath(path).encoded("utf-8")
        with self._get_wc(write_lock=True) as root_adm, \
            root_adm.probe_try(encoded_path, True, 1) as adm:
            entry = adm.entry(encoded_path)
            self._fix_kind(root_adm, encoded_path, path, entry)
            root_adm.transmit_prop_deltas(encoded_path, True, editor)

    def transmit_svn_file_deltas(self, path, editor):
        encoded_path = self.abspath(path).encode("utf-8")
        with self._get_wc(write_lock=True) as root_adm, \
            root_adm.probe_try(encoded_path, True, 1) as adm:
            entry = adm.entry(encoded_path)
            self._fix_kind(adm, encoded_path, path, entry)
            root_adm.transmit_prop_deltas(encoded_path, entry, editor)
            root_adm.transmit_text_deltas(encoded_path, True, editor)

    def merge_modified(self):
        """Return a dictionary of files modified by a merge.

        The list is initialized by WorkingTree.set_merge_modified, which is
        typically called after we make some automatic updates to the tree
        because of a merge.

        This returns a map of file_id->sha1, containing only files which are
        still in the working inventory and have that text hash.
        """
        with self.lock_read():
            try:
                hashfile = self._transport.get('merge-hashes')
            except NoSuchFile:
                return {}
            try:
                merge_hashes = {}
                try:
                    if hashfile.next() != MERGE_MODIFIED_HEADER_1 + '\n':
                        raise MergeModifiedFormatError()
                except StopIteration:
                    raise MergeModifiedFormatError()
                for s in _mod_rio.RioReader(hashfile):
                    # RioReader reads in Unicode, so convert file_ids back to utf8
                    file_id = osutils.safe_file_id(s.get("file_id"), warn=False)
                    if not self.has_id(file_id):
                        continue
                    text_hash = s.get("hash")
                    if text_hash == self.get_file_sha1(self.id2path(file_id), file_id):
                        merge_hashes[file_id] = text_hash
                return merge_hashes
            finally:
                hashfile.close()

    def set_merge_modified(self, modified_hashes):
        def iter_stanzas():
            for file_id, hash in modified_hashes.iteritems():
                yield _mod_rio.Stanza(file_id=file_id.decode('utf8'),
                    hash=hash)
        with self.lock_tree_write():
            my_file = _mod_rio.rio_file(iter_stanzas(), MERGE_MODIFIED_HEADER_1)
            self._transport.put_file('merge-hashes', my_file,
                mode=self.controldir._get_file_mode())

    def update_basis_by_delta(self, new_revid, delta):
        """Update the parents of this tree after a commit.

        This gives the tree one parent, with revision id new_revid. The
        inventory delta is applied to the current basis tree to generate the
        inventory for the parent new_revid, and all other parent trees are
        discarded.

        All the changes in the delta should be changes synchronising the basis
        tree with some or all of the working tree, with a change to a directory
        requiring that its contents have been recursively included. That is,
        this is not a general purpose tree modification routine, but a helper
        for commit which is not required to handle situations that do not arise
        outside of commit.

        :param new_revid: The new revision id for the trees parent.
        :param delta: An inventory delta (see apply_inventory_delta) describing
            the changes from the current left most parent revision to new_revid.
        """
        revmeta, mapping = self.branch.last_revmeta(True)
        assert revmeta.get_revision_id(mapping) == new_revid, \
                "%r != %r" % (revmeta.get_revision_id(mapping), new_revid)
        rev = revmeta.metarev.revnum
        self._cached_base_revnum = rev
        self._cached_base_revid = new_revid
        self._cached_base_idmap = None
        self._cached_merges = []

        newrev = self.branch.repository.get_revision(new_revid)
        svn_revprops = revmeta.metarev.revprops

        class DummyEditor(object):

            def apply_textdelta(self, checksum):
                def window_handler(window):
                    pass
                return window_handler

            def close(self):
                pass

        adms_to_close = set()
        def update_entry(cq, path, root_adm, md5sum=None):
            mutter('updating entry for %s', path)
            adm = root_adm.probe_try(
                self.abspath(path), True, 1)
            adms_to_close.add(adm)
            cq.queue(self.abspath(path).rstrip("/"), adm,
                True, None, False, False, md5sum)

        cq = CommittedQueue()
        root_adm = self._get_wc(self.abspath("."), write_lock=True, depth=-1)
        adms_to_close.add(root_adm)
        try:
            for repos_path, changes in revmeta.metarev.paths.iteritems():
                if changes[0] == 'D':
                    continue
                assert changes[0] in ('A', 'M', 'R')
                path = repos_path[len(revmeta.metarev.branch_path):].strip("/")
                try:
                    kind = self.kind(path)
                except NoSuchFile:
                    md5sum = None
                else:
                    if kind == "file":
                        md5sum = osutils.md5(self.get_file_text(path)).digest()
                    else:
                        md5sum = None
                update_entry(cq, path, root_adm, md5sum)
            root_adm.process_committed_queue(cq,
                rev, svn_revprops[properties.PROP_REVISION_DATE],
                svn_revprops[properties.PROP_REVISION_AUTHOR])
        finally:
            for adm in adms_to_close:
                adm.close()
        self.set_parent_ids([new_revid])

    def annotate_iter(self, path, default_revision=CURRENT_REVISION, file_id=None):
        from .annotate import Annotater
        with self.lock_read():
            annotater = Annotater(self.branch.repository)
            parent_lines = []
            if len(self.get_parent_ids()) > 1:
                graph = self.branch.repository.get_graph()
                head_parents = graph.heads(self.get_parent_ids())
            else:
                head_parents = self.get_parent_ids()
            for revid in head_parents:
                foreign_revid, mapping = self.branch.repository.lookup_bzr_revision_id(
                    revid)
                parent_lines.append(annotater.check_file_revs(
                    revid, foreign_revid[1], foreign_revid[2], self.mapping, path))
            return annotater.check_file(
                self.get_file_lines(path, file_id),
                default_revision, parent_lines)

    def find_related_paths_across_trees(self, paths, trees=[],
            require_versioned=True):
        """Find related paths in tree corresponding to specified filenames in any
        of `lookup_trees`.

        All matches in all trees will be used, and all children of matched
        directories will be used.

        :param paths: The filenames to find related paths for (if None, returns
            None)
        :param trees: The trees to find file_ids within
        :param require_versioned: if true, all specified filenames must occur in
            at least one tree.
        :return: a set of paths for the specified filenames and their children
            in `tree`
        """
        if paths is None:
            return None;
        file_ids = self.paths2ids(
                paths, trees, require_versioned=require_versioned)
        ret = set()
        for file_id in file_ids:
            try:
                ret.add(self.id2path(file_id))
            except errors.NoSuchId:
                pass
        return ret


class SvnWorkingTreeFormat(WorkingTreeFormat):
    """Subversion working copy format."""

    supports_versioned_directories = True

    supports_store_uncommitted = False

    def __init__(self, version=None):
        self.version = version

    def __get_matchingcontroldir(self):
        return SvnWorkingTreeDirFormat(self.version)

    _matchingcontroldir = property(__get_matchingcontroldir)

    def get_format_description(self):
        if self.version is not None:
            return "Subversion Working Copy (version %d)" % self.version
        else:
            return "Subversion Working Copy"

    def get_format_string(self):
        raise NotImplementedError

    def initialize(self, a_controldir, revision_id=None):
        raise NotImplementedError(self.initialize)

    def open(self, a_controldir):
        raise NotImplementedError(self.initialize)

    def get_controldir_for_branch(self):
        from breezy.plugins.svn.remote import SvnRemoteFormat
        return SvnRemoteFormat()


class SvnWorkingTreeDirFormat(ControlDirFormat):
    """Working Tree implementation that uses Subversion working copies."""

    _lock_class = TransportLock

    def __init__(self, version=None):
        super(SvnWorkingTreeDirFormat, self).__init__()
        self.version = version

    def open(self, transport, _found=False):
        import subvertpy
        try:
            return SvnCheckout(transport, self)
        except subvertpy.SubversionException, (_, num):
            if num in (subvertpy.ERR_RA_LOCAL_REPOS_OPEN_FAILED,):
                raise NoSvnRepositoryPresent(transport.base)
            if num in (subvertpy.ERR_WC_NOT_DIRECTORY,):
                raise NotBranchError(path=transport.base)
            raise

    def get_format_string(self):
        raise NotImplementedError(self.get_format_string)

    def get_format_description(self):
        return 'Subversion Local Checkout'

    def is_initializable(self):
        return False

    def is_supported(self):
        return True

    def initialize_on_transport(self, transport):
        raise UninitializableFormat(self)

    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
        create_prefix=False, force_new_repo=False, stacked_on=None,
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
        shared_repo=False, vfs_only=False):
        raise UninitializableFormat(self)

    def get_converter(self, format):
        """See ControlDirFormat.get_converter()."""
        return SvnCheckoutConverter(format)

    @property
    def repository_format(self):
        from breezy.plugins.svn.repository import SvnRepositoryFormat
        return SvnRepositoryFormat()

    def get_branch_format(self):
        from breezy.plugins.svn.branch import SvnBranchFormat
        return SvnBranchFormat()

    def supports_transport(self, transport):
        from breezy.transport.local import LocalTransport
        return isinstance(transport, LocalTransport)


class SvnCheckout(ControlDir):
    """ControlDir implementation for Subversion checkouts (directories
    containing a .svn subdirectory."""

    @property
    def control_transport(self):
        return self.transport

    @property
    def control_url(self):
        return urlutils.join(self.user_url, get_adm_dir())

    @property
    def user_transport(self):
        return self.root_transport

    def break_lock(self):
        raise NotImplementedError(self.break_lock)

    def __init__(self, transport, format):
        self._format = format
        self._mode_check_done = False
        self._config = None
        if transport.base.startswith("readonly+"):
            real_transport = transport._decorated
        else:
            real_transport = transport
        SvnWorkingTreeProber().probe_transport(real_transport)
        self.local_path = real_transport.local_abspath(".")

        # Open related remote repository + branch
        try:
            wc = Adm(None, self.local_path)
        except subvertpy.SubversionException, (msg, num):
            if num == ERR_WC_UNSUPPORTED_FORMAT:
                raise UnsupportedFormatError(msg, kind='workingtree')
            else:
                raise
        try:
            try:
                self.entry = wc.entry(self.local_path, True)
            except subvertpy.SubversionException, (msg, num):
                if num in (subvertpy.ERR_ENTRY_NOT_FOUND,
                           subvertpy.ERR_NODE_UNKNOWN_KIND):
                    raise CorruptWorkingTree(self.local_path, msg)
                else:
                    raise
        finally:
            wc.close()

        self.svn_root_url = self.entry.repos
        self.svn_url = self.entry.url
        self._remote_branch_transport = None
        self._remote_repo_transport = None
        self._remote_controldir = None
        self._adm_dir = get_adm_dir()
        self.root_transport = transport
        self.transport = self.root_transport.clone(get_adm_dir())

    def backup_bzrdir(self):
        self.root_transport.copy_tree(".svn", ".svn.backup")
        return (self.root_transport.abspath(".svn"),
                self.root_transport.abspath(".svn.backup"))

    def is_control_filename(self, filename):
        return filename == self._adm_dir or filename.startswith(self._adm_dir+'/')

    def get_remote_controldir(self):
        from breezy.plugins.svn.remote import SvnRemoteAccess
        if self._remote_controldir is None:
            self._remote_controldir = SvnRemoteAccess(self.get_remote_transport())
        return self._remote_controldir

    def get_remote_transport(self):
        if self._remote_branch_transport is None:
            self._remote_branch_transport = SvnRaTransport(self.entry.url,
                from_transport=self._remote_repo_transport)
        return self._remote_branch_transport

    def clone(self, location, revision_id=None, force_new_repo=False,
              preserve_stacking=False):
        wt = self.open_workingtree()
        if revision_id is None:
            revision_id = wt.last_revision()
        url = _mod_transport.location_to_url(location)
        path = urlutils.local_path_from_url(url)
        return wt.branch.create_checkout(path, lightweight=True,
            revision_id=revision_id).controldir

    def open_workingtree(self, unsupported=False, recommend_upgrade=False):
        wt_format = SvnWorkingTreeFormat(self._format.version)
        ret = SvnWorkingTree(self, wt_format, self.local_path, self.entry)
        try:
            # FIXME: This could potentially be done without actually
            # opening the branch?
            ret.branch # Trigger NotSvnBranchPath error
        except NotSvnBranchPath, e:
            raise NoWorkingTree(self.local_path)
        return ret

    def sprout(self, *args, **kwargs):
        return self.get_remote_controldir().sprout(*args, **kwargs)

    def create_repository(self, shared=False):
        raise UninitializableFormat(self._format)

    def open_repository(self):
        raise NoRepositoryPresent(self)

    def find_repository(self, _ignore_branch_path=False):
        raise NoRepositoryPresent(self)

    def _find_repository(self):
        return self.get_remote_controldir().find_repository()

    def get_branch_path(self):
        if self.entry.repos is None:
            raise RepositoryRootUnknown()
        assert self.entry.url.startswith(self.entry.repos)
        return self.entry.url[len(self.entry.repos):].strip("/").decode('utf-8')

    def needs_format_conversion(self, format):
        return not isinstance(self._format, format.__class__)

    def create_workingtree(self, revision_id=None, hardlink=None):
        """See ControlDir.create_workingtree().

        Not implemented for Subversion because having a .svn directory
        implies having a working copy.
        """
        raise NotImplementedError(self.create_workingtree)

    def destroy_workingtree(self):
        raise UnsupportedOperation(self.destroy_workingtree, self)

    def create_branch(self):
        """See ControlDir.create_branch()."""
        raise NotImplementedError(self.create_branch)

    def open_branch(self, name=None, unsupported=True, ignore_fallbacks=False,
            mapping=None, revnum=None, possible_transports=None):
        """See ControlDir.open_branch()."""
        from breezy.plugins.svn.branch import SvnBranch
        repos = self._find_repository()
        if mapping is None:
            mapping = repos.get_mapping()

        try:
            return SvnBranch(repos, self.get_remote_controldir(),
                self.get_branch_path(), mapping, revnum=revnum)
        except RepositoryRootUnknown:
            return self.get_remote_controldir().open_branch(revnum=revnum,
                possible_transports=possible_transports)
        except subvertpy.SubversionException, (_, num):
            if num == subvertpy.ERR_WC_NOT_DIRECTORY:
                raise NotBranchError(path=self.base)
            raise

    def _find_creation_modes(self):
        """Determine the appropriate modes for files and directories.

        They're always set to be consistent with the base directory,
        assuming that this transport allows setting modes.
        """
        # TODO: Do we need or want an option (maybe a config setting) to turn
        # this off or override it for particular locations? -- mbp 20080512
        if self._mode_check_done:
            return
        self._mode_check_done = True
        try:
            st = self.transport.stat('.')
        except TransportNotPossible:
            self._dir_mode = None
            self._file_mode = None
        else:
            # Check the directory mode, but also make sure the created
            # directories and files are read-write for this user. This is
            # mostly a workaround for filesystems which lie about being able to
            # write to a directory (cygwin & win32)
            if (st.st_mode & 07777 == 00000):
                # FTP allows stat but does not return dir/file modes
                self._dir_mode = None
                self._file_mode = None
            else:
                self._dir_mode = (st.st_mode & 07777) | 00700
                # Remove the sticky and execute bits for files
                self._file_mode = self._dir_mode & ~07111

    def _get_file_mode(self):
        """Return Unix mode for newly created files, or None.
        """
        if not self._mode_check_done:
            self._find_creation_modes()
        return self._file_mode

    def _get_dir_mode(self):
        """Return Unix mode for newly created directories, or None.
        """
        if not self._mode_check_done:
            self._find_creation_modes()
        return self._dir_mode

    def get_config(self):
        from breezy.plugins.svn.config import SvnRepositoryConfig
        if self._config is None:
            self._config = SvnRepositoryConfig(self.entry.url, self.entry.uuid)
        return self._config


class SvnCheckoutConverter(Converter):
    """Converts from a Subversion directory to a bzr dir."""

    def __init__(self, target_format):
        """Create a SvnCheckoutConverter.
        :param target_format: The format the resulting repository should be.
        """
        super(SvnCheckoutConverter, self).__init__()
        self.target_format = target_format

    def convert(self, to_convert, pb):
        """See Converter.convert()."""
        from breezy.branch import BranchReferenceFormat
        remote_branch = to_convert.open_branch()
        controldir = self.target_format.initialize(to_convert.root_transport.base)
        branch = BranchReferenceFormat().initialize(controldir, remote_branch)
        wt = controldir.create_workingtree()
        # FIXME: Convert working tree
        to_convert.root_transport.delete_tree(".svn")
        return controldir