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
|
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Git Trees."""
from __future__ import absolute_import
from collections import deque
import errno
from io import BytesIO
import os
from dulwich.config import (
parse_submodules,
ConfigFile as GitConfigFile,
)
from dulwich.diff_tree import tree_changes
from dulwich.errors import NotTreeError
from dulwich.index import (
blob_from_path_and_stat,
cleanup_mode,
commit_tree,
index_entry_from_stat,
Index,
)
from dulwich.object_store import (
tree_lookup_path,
OverlayObjectStore,
)
from dulwich.objects import (
Blob,
Tree,
ZERO_SHA,
S_IFGITLINK,
S_ISGITLINK,
)
import stat
import posixpath
from .. import (
controldir as _mod_controldir,
delta,
errors,
mutabletree,
osutils,
revisiontree,
trace,
tree as _mod_tree,
workingtree,
)
from ..revision import (
CURRENT_REVISION,
NULL_REVISION,
)
from .mapping import (
mode_is_executable,
mode_kind,
default_mapping,
)
from .transportgit import (
TransportObjectStore,
TransportRepo,
)
class GitTreeDirectory(_mod_tree.TreeDirectory):
__slots__ = ['file_id', 'name', 'parent_id', 'children']
def __init__(self, file_id, name, parent_id):
self.file_id = file_id
self.name = name
self.parent_id = parent_id
# TODO(jelmer)
self.children = {}
@property
def kind(self):
return 'directory'
@property
def executable(self):
return False
def copy(self):
return self.__class__(
self.file_id, self.name, self.parent_id)
def __repr__(self):
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
self.__class__.__name__, self.file_id, self.name,
self.parent_id)
def __eq__(self, other):
return (self.kind == other.kind and
self.file_id == other.file_id and
self.name == other.name and
self.parent_id == other.parent_id)
class GitTreeFile(_mod_tree.TreeFile):
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
'executable']
def __init__(self, file_id, name, parent_id, text_size=None,
text_sha1=None, executable=None):
self.file_id = file_id
self.name = name
self.parent_id = parent_id
self.text_size = text_size
self.text_sha1 = text_sha1
self.executable = executable
@property
def kind(self):
return 'file'
def __eq__(self, other):
return (self.kind == other.kind and
self.file_id == other.file_id and
self.name == other.name and
self.parent_id == other.parent_id and
self.text_sha1 == other.text_sha1 and
self.text_size == other.text_size and
self.executable == other.executable)
def __repr__(self):
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
"text_sha1=%r, executable=%r)") % (
type(self).__name__, self.file_id, self.name, self.parent_id,
self.text_size, self.text_sha1, self.executable)
def copy(self):
ret = self.__class__(
self.file_id, self.name, self.parent_id)
ret.text_sha1 = self.text_sha1
ret.text_size = self.text_size
ret.executable = self.executable
return ret
class GitTreeSymlink(_mod_tree.TreeLink):
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
def __init__(self, file_id, name, parent_id,
symlink_target=None):
self.file_id = file_id
self.name = name
self.parent_id = parent_id
self.symlink_target = symlink_target
@property
def kind(self):
return 'symlink'
@property
def executable(self):
return False
@property
def text_size(self):
return None
def __repr__(self):
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
type(self).__name__, self.file_id, self.name, self.parent_id,
self.symlink_target)
def __eq__(self, other):
return (self.kind == other.kind and
self.file_id == other.file_id and
self.name == other.name and
self.parent_id == other.parent_id and
self.symlink_target == other.symlink_target)
def copy(self):
return self.__class__(
self.file_id, self.name, self.parent_id,
self.symlink_target)
class GitTreeSubmodule(_mod_tree.TreeReference):
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
def __init__(self, file_id, name, parent_id, reference_revision=None):
self.file_id = file_id
self.name = name
self.parent_id = parent_id
self.reference_revision = reference_revision
@property
def executable(self):
return False
@property
def kind(self):
return 'tree-reference'
def __repr__(self):
return ("%s(file_id=%r, name=%r, parent_id=%r, "
"reference_revision=%r)") % (
type(self).__name__, self.file_id, self.name, self.parent_id,
self.reference_revision)
def __eq__(self, other):
return (self.kind == other.kind and
self.file_id == other.file_id and
self.name == other.name and
self.parent_id == other.parent_id and
self.reference_revision == other.reference_revision)
def copy(self):
return self.__class__(
self.file_id, self.name, self.parent_id,
self.reference_revision)
entry_factory = {
'directory': GitTreeDirectory,
'file': GitTreeFile,
'symlink': GitTreeSymlink,
'tree-reference': GitTreeSubmodule,
}
def ensure_normalized_path(path):
"""Check whether path is normalized.
:raises InvalidNormalization: When path is not normalized, and cannot be
accessed on this platform by the normalized path.
:return: The NFC normalised version of path.
"""
norm_path, can_access = osutils.normalized_filename(path)
if norm_path != path:
if can_access:
return norm_path
else:
raise errors.InvalidNormalization(path)
return path
class GitRevisionTree(revisiontree.RevisionTree):
"""Revision tree implementation based on Git objects."""
def __init__(self, repository, revision_id):
self._revision_id = revision_id
self._repository = repository
self._submodules = None
self.store = repository._git.object_store
if not isinstance(revision_id, bytes):
raise TypeError(revision_id)
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
revision_id)
if revision_id == NULL_REVISION:
self.tree = None
self.mapping = default_mapping
else:
try:
commit = self.store[self.commit_id]
except KeyError:
raise errors.NoSuchRevision(repository, revision_id)
self.tree = commit.tree
def _submodule_info(self):
if self._submodules is None:
try:
with self.get_file('.gitmodules') as f:
config = GitConfigFile.from_file(f)
self._submodules = {
path: (url, section)
for path, url, section in parse_submodules(config)}
except errors.NoSuchFile:
self._submodules = {}
return self._submodules
def _get_submodule_repository(self, relpath):
if not isinstance(relpath, bytes):
raise TypeError(relpath)
try:
info = self._submodule_info()[relpath]
except KeyError:
nested_repo_transport = self._repository.controldir.user_transport.clone(
relpath.decode('utf-8'))
else:
nested_repo_transport = self._repository.controldir.control_transport.clone(
posixpath.join('modules', info[1].decode('utf-8')))
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
nested_repo_transport)
return nested_controldir.find_repository()
def _get_submodule_store(self, relpath):
return self._get_submodule_repository(relpath)._git.object_store
def get_nested_tree(self, path):
encoded_path = path.encode('utf-8')
nested_repo = self._get_submodule_repository(encoded_path)
ref_rev = self.get_reference_revision(path)
return nested_repo.revision_tree(ref_rev)
def supports_rename_tracking(self):
return False
def get_file_revision(self, path):
change_scanner = self._repository._file_change_scanner
if self.commit_id == ZERO_SHA:
return NULL_REVISION
(unused_path, commit_id) = change_scanner.find_last_change_revision(
path.encode('utf-8'), self.commit_id)
return self._repository.lookup_foreign_revision_id(
commit_id, self.mapping)
def get_file_mtime(self, path):
try:
revid = self.get_file_revision(path)
except KeyError:
raise errors.NoSuchFile(path)
try:
rev = self._repository.get_revision(revid)
except errors.NoSuchRevision:
raise _mod_tree.FileTimestampUnavailable(path)
return rev.timestamp
def id2path(self, file_id, recurse='down'):
try:
path = self.mapping.parse_file_id(file_id)
except ValueError:
raise errors.NoSuchId(self, file_id)
if self.is_versioned(path):
return path
raise errors.NoSuchId(self, file_id)
def is_versioned(self, path):
return self.has_filename(path)
def path2id(self, path):
if self.mapping.is_special_file(path):
return None
if not self.is_versioned(path):
return None
return self.mapping.generate_file_id(osutils.safe_unicode(path))
def all_file_ids(self):
raise errors.UnsupportedOperation(self.all_file_ids, self)
def all_versioned_paths(self):
ret = {u''}
todo = [(self.store, b'', self.tree)]
while todo:
(store, path, tree_id) = todo.pop()
if tree_id is None:
continue
tree = store[tree_id]
for name, mode, hexsha in tree.items():
subpath = posixpath.join(path, name)
ret.add(subpath.decode('utf-8'))
if stat.S_ISDIR(mode):
todo.append((store, subpath, hexsha))
return ret
def _lookup_path(self, path):
if self.tree is None:
raise errors.NoSuchFile(path)
encoded_path = path.encode('utf-8')
parts = encoded_path.split(b'/')
hexsha = self.tree
store = self.store
mode = None
for i, p in enumerate(parts):
if not p:
continue
obj = store[hexsha]
if not isinstance(obj, Tree):
raise NotTreeError(hexsha)
try:
mode, hexsha = obj[p]
except KeyError:
raise errors.NoSuchFile(path)
if S_ISGITLINK(mode) and i != len(parts) - 1:
store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
hexsha = store[hexsha].tree
return (store, mode, hexsha)
def is_executable(self, path):
(store, mode, hexsha) = self._lookup_path(path)
if mode is None:
# the tree root is a directory
return False
return mode_is_executable(mode)
def kind(self, path):
(store, mode, hexsha) = self._lookup_path(path)
if mode is None:
# the tree root is a directory
return "directory"
return mode_kind(mode)
def has_filename(self, path):
try:
self._lookup_path(path)
except errors.NoSuchFile:
return False
else:
return True
def _submodule_info(self):
if self._submodules is None:
try:
with self.get_file('.gitmodules') as f:
config = GitConfigFile.from_file(f)
self._submodules = {
path: (url, section)
for path, url, section in parse_submodules(config)}
except errors.NoSuchFile:
self._submodules = {}
return self._submodules
def list_files(self, include_root=False, from_dir=None, recursive=True,
recurse_nested=False):
if self.tree is None:
return
if from_dir is None or from_dir == '.':
from_dir = u""
(store, mode, hexsha) = self._lookup_path(from_dir)
if mode is None: # Root
root_ie = self._get_dir_ie(b"", None)
else:
parent_path = posixpath.dirname(from_dir)
parent_id = self.mapping.generate_file_id(parent_path)
if mode_kind(mode) == 'directory':
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
else:
root_ie = self._get_file_ie(
store, from_dir.encode("utf-8"),
posixpath.basename(from_dir), mode, hexsha)
if include_root:
yield (from_dir, "V", root_ie.kind, root_ie)
todo = []
if root_ie.kind == 'directory':
todo.append((store, from_dir.encode("utf-8"),
b"", hexsha, root_ie.file_id))
while todo:
(store, path, relpath, hexsha, parent_id) = todo.pop()
tree = store[hexsha]
for name, mode, hexsha in tree.iteritems():
if self.mapping.is_special_file(name):
continue
child_path = posixpath.join(path, name)
child_relpath = posixpath.join(relpath, name)
if S_ISGITLINK(mode) and recurse_nested:
mode = stat.S_IFDIR
store = self._get_submodule_store(child_relpath)
hexsha = store[hexsha].tree
if stat.S_ISDIR(mode):
ie = self._get_dir_ie(child_path, parent_id)
if recursive:
todo.append(
(store, child_path, child_relpath, hexsha,
ie.file_id))
else:
ie = self._get_file_ie(
store, child_path, name, mode, hexsha, parent_id)
yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
if not isinstance(path, bytes):
raise TypeError(path)
if not isinstance(name, bytes):
raise TypeError(name)
kind = mode_kind(mode)
path = path.decode('utf-8')
name = name.decode("utf-8")
file_id = self.mapping.generate_file_id(path)
ie = entry_factory[kind](file_id, name, parent_id)
if kind == 'symlink':
ie.symlink_target = store[hexsha].data.decode('utf-8')
elif kind == 'tree-reference':
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
hexsha)
else:
data = store[hexsha].data
ie.text_sha1 = osutils.sha_string(data)
ie.text_size = len(data)
ie.executable = mode_is_executable(mode)
return ie
def _get_dir_ie(self, path, parent_id):
path = path.decode('utf-8')
file_id = self.mapping.generate_file_id(path)
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
def iter_child_entries(self, path):
(store, mode, tree_sha) = self._lookup_path(path)
if mode is not None and not stat.S_ISDIR(mode):
return
encoded_path = path.encode('utf-8')
file_id = self.path2id(path)
tree = store[tree_sha]
for name, mode, hexsha in tree.iteritems():
if self.mapping.is_special_file(name):
continue
child_path = posixpath.join(encoded_path, name)
if stat.S_ISDIR(mode):
yield self._get_dir_ie(child_path, file_id)
else:
yield self._get_file_ie(store, child_path, name, mode, hexsha,
file_id)
def iter_entries_by_dir(self, specific_files=None,
recurse_nested=False):
if self.tree is None:
return
if specific_files is not None:
if specific_files in ([""], []):
specific_files = None
else:
specific_files = set([p.encode('utf-8')
for p in specific_files])
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
if specific_files is None or u"" in specific_files:
yield u"", self._get_dir_ie(b"", None)
while todo:
store, path, tree_sha, parent_id = todo.popleft()
tree = store[tree_sha]
extradirs = []
for name, mode, hexsha in tree.iteritems():
if self.mapping.is_special_file(name):
continue
child_path = posixpath.join(path, name)
child_path_decoded = child_path.decode('utf-8')
if recurse_nested and S_ISGITLINK(mode):
mode = stat.S_IFDIR
store = self._get_submodule_store(child_path)
hexsha = store[hexsha].tree
if stat.S_ISDIR(mode):
if (specific_files is None or
any([p for p in specific_files if p.startswith(
child_path)])):
extradirs.append(
(store, child_path, hexsha,
self.path2id(child_path_decoded)))
if specific_files is None or child_path in specific_files:
if stat.S_ISDIR(mode):
yield (child_path_decoded,
self._get_dir_ie(child_path, parent_id))
else:
yield (child_path_decoded,
self._get_file_ie(store, child_path, name, mode,
hexsha, parent_id))
todo.extendleft(reversed(extradirs))
def iter_references(self):
if self.supports_tree_reference():
for path, entry in self.iter_entries_by_dir():
if entry.kind == 'tree-reference':
yield path
def get_revision_id(self):
"""See RevisionTree.get_revision_id."""
return self._revision_id
def get_file_sha1(self, path, stat_value=None):
if self.tree is None:
raise errors.NoSuchFile(path)
return osutils.sha_string(self.get_file_text(path))
def get_file_verifier(self, path, stat_value=None):
(store, mode, hexsha) = self._lookup_path(path)
return ("GIT", hexsha)
def get_file_size(self, path):
(store, mode, hexsha) = self._lookup_path(path)
if stat.S_ISREG(mode):
return len(store[hexsha].data)
return None
def get_file_text(self, path):
"""See RevisionTree.get_file_text."""
(store, mode, hexsha) = self._lookup_path(path)
if stat.S_ISREG(mode):
return store[hexsha].data
else:
return b""
def get_symlink_target(self, path):
"""See RevisionTree.get_symlink_target."""
(store, mode, hexsha) = self._lookup_path(path)
if stat.S_ISLNK(mode):
return store[hexsha].data.decode('utf-8')
else:
return None
def get_reference_revision(self, path):
"""See RevisionTree.get_symlink_target."""
(store, mode, hexsha) = self._lookup_path(path)
if S_ISGITLINK(mode):
try:
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
except errors.NotBranchError:
return self.mapping.revision_id_foreign_to_bzr(hexsha)
else:
return nested_repo.lookup_foreign_revision_id(hexsha)
else:
return None
def _comparison_data(self, entry, path):
if entry is None:
return None, False, None
return entry.kind, entry.executable, None
def path_content_summary(self, path):
"""See Tree.path_content_summary."""
try:
(store, mode, hexsha) = self._lookup_path(path)
except errors.NoSuchFile:
return ('missing', None, None, None)
kind = mode_kind(mode)
if kind == 'file':
executable = mode_is_executable(mode)
contents = store[hexsha].data
return (kind, len(contents), executable,
osutils.sha_string(contents))
elif kind == 'symlink':
return (kind, None, None, store[hexsha].data.decode('utf-8'))
elif kind == 'tree-reference':
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
return (kind, None, None,
nested_repo.lookup_foreign_revision_id(hexsha))
else:
return (kind, None, None, None)
def find_related_paths_across_trees(self, paths, trees=[],
require_versioned=True):
if paths is None:
return None
if require_versioned:
trees = [self] + (trees if trees is not None else [])
unversioned = set()
for p in paths:
for t in trees:
if t.is_versioned(p):
break
else:
unversioned.add(p)
if unversioned:
raise errors.PathsNotVersionedError(unversioned)
return filter(self.is_versioned, paths)
def _iter_tree_contents(self, include_trees=False):
if self.tree is None:
return iter([])
return self.store.iter_tree_contents(
self.tree, include_trees=include_trees)
def annotate_iter(self, path, default_revision=CURRENT_REVISION):
"""Return an iterator of revision_id, line tuples.
For working trees (and mutable trees in general), the special
revision_id 'current:' will be used for lines that are new in this
tree, e.g. uncommitted changes.
:param default_revision: For lines that don't match a basis, mark them
with this revision id. Not all implementations will make use of
this value.
"""
with self.lock_read():
# Now we have the parents of this content
from breezy.annotate import Annotator
from .annotate import AnnotateProvider
annotator = Annotator(AnnotateProvider(
self._repository._file_change_scanner))
this_key = (path, self.get_file_revision(path))
annotations = [(key[-1], line)
for key, line in annotator.annotate_flat(this_key)]
return annotations
def _get_rules_searcher(self, default_searcher):
return default_searcher
def walkdirs(self, prefix=u""):
(store, mode, hexsha) = self._lookup_path(prefix)
todo = deque(
[(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
while todo:
store, path, tree_sha, parent_id = todo.popleft()
path_decoded = path.decode('utf-8')
tree = store[tree_sha]
children = []
for name, mode, hexsha in tree.iteritems():
if self.mapping.is_special_file(name):
continue
child_path = posixpath.join(path, name)
file_id = self.path2id(child_path.decode('utf-8'))
if stat.S_ISDIR(mode):
todo.append((store, child_path, hexsha, file_id))
children.append(
(child_path.decode('utf-8'), name.decode('utf-8'),
mode_kind(mode), None,
file_id, mode_kind(mode)))
yield (path_decoded, parent_id), children
def tree_delta_from_git_changes(changes, mappings,
specific_files=None,
require_versioned=False, include_root=False,
target_extras=None):
"""Create a TreeDelta from two git trees.
source and target are iterators over tuples with:
(filename, sha, mode)
"""
(old_mapping, new_mapping) = mappings
if target_extras is None:
target_extras = set()
ret = delta.TreeDelta()
added = []
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
if newpath == b'' and not include_root:
continue
if oldpath is not None:
oldpath_decoded = oldpath.decode('utf-8')
else:
oldpath_decoded = None
if newpath is not None:
newpath_decoded = newpath.decode('utf-8')
else:
newpath_decoded = None
if not (specific_files is None or
(oldpath is not None and
osutils.is_inside_or_parent_of_any(
specific_files, oldpath_decoded)) or
(newpath is not None and
osutils.is_inside_or_parent_of_any(
specific_files, newpath_decoded))):
continue
if oldpath_decoded is None:
fileid = new_mapping.generate_file_id(newpath_decoded)
oldexe = None
oldkind = None
oldname = None
oldparent = None
oldversioned = False
else:
oldversioned = True
if oldmode:
oldexe = mode_is_executable(oldmode)
oldkind = mode_kind(oldmode)
else:
oldexe = False
oldkind = None
if oldpath_decoded == u'':
oldparent = None
oldname = u''
else:
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
oldparent = old_mapping.generate_file_id(oldparentpath)
fileid = old_mapping.generate_file_id(oldpath_decoded)
if newpath_decoded is None:
newexe = None
newkind = None
newname = None
newparent = None
newversioned = False
else:
newversioned = (newpath_decoded not in target_extras)
if newmode:
newexe = mode_is_executable(newmode)
newkind = mode_kind(newmode)
else:
newexe = False
newkind = None
if newpath_decoded == u'':
newparent = None
newname = u''
else:
newparentpath, newname = osutils.split(newpath_decoded)
newparent = new_mapping.generate_file_id(newparentpath)
if old_mapping.is_special_file(oldpath):
oldpath = None
if new_mapping.is_special_file(newpath):
newpath = None
if oldpath is None and newpath is None:
continue
change = _mod_tree.TreeChange(
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
(oldversioned, newversioned),
(oldparent, newparent), (oldname, newname),
(oldkind, newkind), (oldexe, newexe))
if oldpath is None:
added.append((newpath, newkind))
elif newpath is None or newmode == 0:
ret.removed.append(change)
elif oldpath != newpath:
ret.renamed.append(change)
elif mode_kind(oldmode) != mode_kind(newmode):
ret.kind_changed.append(change)
elif oldsha != newsha or oldmode != newmode:
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
continue
ret.modified.append(change)
else:
ret.unchanged.append(change)
implicit_dirs = {b''}
for path, kind in added:
if kind == 'directory' or path in target_extras:
continue
implicit_dirs.update(osutils.parent_directories(path))
for path, kind in added:
if kind == 'directory' and path not in implicit_dirs:
continue
path_decoded = osutils.normalized_filename(path)[0]
parent_path, basename = osutils.split(path_decoded)
parent_id = new_mapping.generate_file_id(parent_path)
if path in target_extras:
ret.unversioned.append(_mod_tree.TreeChange(
None, (None, path_decoded),
True, (False, False), (None, parent_id),
(None, basename), (None, kind), (None, False)))
else:
file_id = new_mapping.generate_file_id(path_decoded)
ret.added.append(
_mod_tree.TreeChange(
file_id, (None, path_decoded), True,
(False, True),
(None, parent_id),
(None, basename), (None, kind), (None, False)))
return ret
def changes_from_git_changes(changes, mapping, specific_files=None,
include_unchanged=False, target_extras=None):
"""Create a iter_changes-like generator from a git stream.
source and target are iterators over tuples with:
(filename, sha, mode)
"""
if target_extras is None:
target_extras = set()
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
if oldpath is not None:
oldpath_decoded = oldpath.decode('utf-8')
else:
oldpath_decoded = None
if newpath is not None:
newpath_decoded = newpath.decode('utf-8')
else:
newpath_decoded = None
if not (specific_files is None or
(oldpath_decoded is not None and
osutils.is_inside_or_parent_of_any(
specific_files, oldpath_decoded)) or
(newpath_decoded is not None and
osutils.is_inside_or_parent_of_any(
specific_files, newpath_decoded))):
continue
if oldpath is not None and mapping.is_special_file(oldpath):
continue
if newpath is not None and mapping.is_special_file(newpath):
continue
if oldpath_decoded is None:
fileid = mapping.generate_file_id(newpath_decoded)
oldexe = None
oldkind = None
oldname = None
oldparent = None
oldversioned = False
else:
oldversioned = True
if oldmode:
oldexe = mode_is_executable(oldmode)
oldkind = mode_kind(oldmode)
else:
oldexe = False
oldkind = None
if oldpath_decoded == u'':
oldparent = None
oldname = u''
else:
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
oldparent = mapping.generate_file_id(oldparentpath)
fileid = mapping.generate_file_id(oldpath_decoded)
if newpath_decoded is None:
newexe = None
newkind = None
newname = None
newparent = None
newversioned = False
else:
newversioned = (newpath_decoded not in target_extras)
if newmode:
newexe = mode_is_executable(newmode)
newkind = mode_kind(newmode)
else:
newexe = False
newkind = None
if newpath_decoded == u'':
newparent = None
newname = u''
else:
newparentpath, newname = osutils.split(newpath_decoded)
newparent = mapping.generate_file_id(newparentpath)
if (not include_unchanged and
oldkind == 'directory' and newkind == 'directory' and
oldpath_decoded == newpath_decoded):
continue
yield _mod_tree.TreeChange(
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
(oldversioned, newversioned),
(oldparent, newparent), (oldname, newname),
(oldkind, newkind), (oldexe, newexe))
class InterGitTrees(_mod_tree.InterTree):
"""InterTree that works between two git trees."""
_matching_from_tree_format = None
_matching_to_tree_format = None
_test_mutable_trees_to_test_trees = None
@classmethod
def is_compatible(cls, source, target):
return (isinstance(source, GitRevisionTree) and
isinstance(target, GitRevisionTree))
def compare(self, want_unchanged=False, specific_files=None,
extra_trees=None, require_versioned=False, include_root=False,
want_unversioned=False):
with self.lock_read():
changes, target_extras = self._iter_git_changes(
want_unchanged=want_unchanged,
require_versioned=require_versioned,
specific_files=specific_files,
extra_trees=extra_trees,
want_unversioned=want_unversioned)
return tree_delta_from_git_changes(
changes, (self.source.mapping, self.target.mapping),
specific_files=specific_files,
include_root=include_root, target_extras=target_extras)
def iter_changes(self, include_unchanged=False, specific_files=None,
pb=None, extra_trees=[], require_versioned=True,
want_unversioned=False):
with self.lock_read():
changes, target_extras = self._iter_git_changes(
want_unchanged=include_unchanged,
require_versioned=require_versioned,
specific_files=specific_files,
extra_trees=extra_trees,
want_unversioned=want_unversioned)
return changes_from_git_changes(
changes, self.target.mapping,
specific_files=specific_files,
include_unchanged=include_unchanged,
target_extras=target_extras)
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
require_versioned=False, extra_trees=None,
want_unversioned=False):
raise NotImplementedError(self._iter_git_changes)
class InterGitRevisionTrees(InterGitTrees):
"""InterTree that works between two git revision trees."""
_matching_from_tree_format = None
_matching_to_tree_format = None
_test_mutable_trees_to_test_trees = None
@classmethod
def is_compatible(cls, source, target):
return (isinstance(source, GitRevisionTree) and
isinstance(target, GitRevisionTree))
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
require_versioned=True, extra_trees=None,
want_unversioned=False):
trees = [self.source]
if extra_trees is not None:
trees.extend(extra_trees)
if specific_files is not None:
specific_files = self.target.find_related_paths_across_trees(
specific_files, trees,
require_versioned=require_versioned)
if (self.source._repository._git.object_store !=
self.target._repository._git.object_store):
store = OverlayObjectStore(
[self.source._repository._git.object_store,
self.target._repository._git.object_store])
else:
store = self.source._repository._git.object_store
return store.tree_changes(
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
include_trees=True, change_type_same=True), set()
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
class MutableGitIndexTree(mutabletree.MutableTree):
def __init__(self):
self._lock_mode = None
self._lock_count = 0
self._versioned_dirs = None
self._index_dirty = False
self._submodules = None
def is_versioned(self, path):
with self.lock_read():
path = path.rstrip('/').encode('utf-8')
(index, subpath) = self._lookup_index(path)
return (subpath in index or self._has_dir(path))
def _has_dir(self, path):
if not isinstance(path, bytes):
raise TypeError(path)
if path == b"":
return True
if self._versioned_dirs is None:
self._load_dirs()
return path in self._versioned_dirs
def _load_dirs(self):
if self._lock_mode is None:
raise errors.ObjectNotLocked(self)
self._versioned_dirs = set()
for p, i in self._recurse_index_entries():
self._ensure_versioned_dir(posixpath.dirname(p))
def _ensure_versioned_dir(self, dirname):
if not isinstance(dirname, bytes):
raise TypeError(dirname)
if dirname in self._versioned_dirs:
return
if dirname != b"":
self._ensure_versioned_dir(posixpath.dirname(dirname))
self._versioned_dirs.add(dirname)
def path2id(self, path):
with self.lock_read():
path = path.rstrip('/')
if self.is_versioned(path.rstrip('/')):
return self.mapping.generate_file_id(
osutils.safe_unicode(path))
return None
def id2path(self, file_id, recurse='down'):
if file_id is None:
return ''
if type(file_id) is not bytes:
raise TypeError(file_id)
with self.lock_read():
try:
path = self.mapping.parse_file_id(file_id)
except ValueError:
raise errors.NoSuchId(self, file_id)
if self.is_versioned(path):
return path
raise errors.NoSuchId(self, file_id)
def _set_root_id(self, file_id):
raise errors.UnsupportedOperation(self._set_root_id, self)
def _add(self, files, ids, kinds):
for (path, file_id, kind) in zip(files, ids, kinds):
if file_id is not None:
raise workingtree.SettingFileIdUnsupported()
path, can_access = osutils.normalized_filename(path)
if not can_access:
raise errors.InvalidNormalization(path)
self._index_add_entry(path, kind)
def _read_submodule_head(self, path):
raise NotImplementedError(self._read_submodule_head)
def _submodule_info(self):
if self._submodules is None:
try:
with self.get_file('.gitmodules') as f:
config = GitConfigFile.from_file(f)
self._submodules = {
path: (url, section)
for path, url, section in parse_submodules(config)}
except errors.NoSuchFile:
self._submodules = {}
return self._submodules
def _lookup_index(self, encoded_path):
if not isinstance(encoded_path, bytes):
raise TypeError(encoded_path)
# Common case:
if encoded_path in self.index:
return self.index, encoded_path
# TODO(jelmer): Perhaps have a cache with paths under which some
# submodules exist?
index = self.index
remaining_path = encoded_path
while True:
parts = remaining_path.split(b'/')
for i in range(1, len(parts)):
basepath = b'/'.join(parts[:i])
try:
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
flags) = index[basepath]
except KeyError:
continue
else:
if S_ISGITLINK(mode):
index = self._get_submodule_index(basepath)
remaining_path = b'/'.join(parts[i:])
break
else:
return index, remaining_path
else:
return index, remaining_path
return index, remaining_path
def _index_del_entry(self, index, path):
del index[path]
# TODO(jelmer): Keep track of dirty per index
self._index_dirty = True
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
if kind == "directory":
# Git indexes don't contain directories
return
if kind == "file":
blob = Blob()
try:
file, stat_val = self.get_file_with_stat(path)
except (errors.NoSuchFile, IOError):
# TODO: Rather than come up with something here, use the old
# index
file = BytesIO()
stat_val = os.stat_result(
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
with file:
blob.set_raw_string(file.read())
# Add object to the repository if it didn't exist yet
if blob.id not in self.store:
self.store.add_object(blob)
hexsha = blob.id
elif kind == "symlink":
blob = Blob()
try:
stat_val = self._lstat(path)
except EnvironmentError:
# TODO: Rather than come up with something here, use the
# old index
stat_val = os.stat_result(
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
blob.set_raw_string(
self.get_symlink_target(path).encode("utf-8"))
# Add object to the repository if it didn't exist yet
if blob.id not in self.store:
self.store.add_object(blob)
hexsha = blob.id
elif kind == "tree-reference":
if reference_revision is not None:
hexsha = self.branch.lookup_bzr_revision_id(
reference_revision)[0]
else:
hexsha = self._read_submodule_head(path)
if hexsha is None:
raise errors.NoCommits(path)
try:
stat_val = self._lstat(path)
except EnvironmentError:
stat_val = os.stat_result(
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
else:
raise AssertionError("unknown kind '%s'" % kind)
# Add an entry to the index or update the existing entry
ensure_normalized_path(path)
encoded_path = path.encode("utf-8")
if b'\r' in encoded_path or b'\n' in encoded_path:
# TODO(jelmer): Why do we need to do this?
trace.mutter('ignoring path with invalid newline in it: %r', path)
return
(index, index_path) = self._lookup_index(encoded_path)
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
self._index_dirty = True
if self._versioned_dirs is not None:
self._ensure_versioned_dir(index_path)
def _recurse_index_entries(self, index=None, basepath=b"",
recurse_nested=False):
# Iterate over all index entries
with self.lock_read():
if index is None:
index = self.index
for path, value in index.items():
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
flags) = value
if S_ISGITLINK(mode) and recurse_nested:
subindex = self._get_submodule_index(path)
for entry in self._recurse_index_entries(
index=subindex, basepath=path,
recurse_nested=recurse_nested):
yield entry
else:
yield (posixpath.join(basepath, path), value)
def iter_entries_by_dir(self, specific_files=None,
recurse_nested=False):
with self.lock_read():
if specific_files is not None:
specific_files = set(specific_files)
else:
specific_files = None
root_ie = self._get_dir_ie(u"", None)
ret = {}
if specific_files is None or u"" in specific_files:
ret[(u"", u"")] = root_ie
dir_ids = {u"": root_ie.file_id}
for path, value in self._recurse_index_entries(
recurse_nested=recurse_nested):
if self.mapping.is_special_file(path):
continue
path = path.decode("utf-8")
if specific_files is not None and path not in specific_files:
continue
(parent, name) = posixpath.split(path)
try:
file_ie = self._get_file_ie(name, path, value, None)
except errors.NoSuchFile:
continue
if specific_files is None:
for (dir_path, dir_ie) in self._add_missing_parent_ids(
parent, dir_ids):
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
file_ie.parent_id = self.path2id(parent)
ret[(posixpath.dirname(path), path)] = file_ie
# Special casing for directories
if specific_files:
for path in specific_files:
key = (posixpath.dirname(path), path)
if key not in ret and self.is_versioned(path):
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
def iter_references(self):
if self.supports_tree_reference():
# TODO(jelmer): Implement a more efficient version of this
for path, entry in self.iter_entries_by_dir():
if entry.kind == 'tree-reference':
yield path
def _get_dir_ie(self, path, parent_id):
file_id = self.path2id(path)
return GitTreeDirectory(file_id,
posixpath.basename(path).strip("/"), parent_id)
def _get_file_ie(self, name, path, value, parent_id):
if not isinstance(name, str):
raise TypeError(name)
if not isinstance(path, str):
raise TypeError(path)
if not isinstance(value, tuple) or len(value) != 10:
raise TypeError(value)
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
file_id = self.path2id(path)
if not isinstance(file_id, bytes):
raise TypeError(file_id)
kind = mode_kind(mode)
ie = entry_factory[kind](file_id, name, parent_id)
if kind == 'symlink':
ie.symlink_target = self.get_symlink_target(path)
elif kind == 'tree-reference':
ie.reference_revision = self.get_reference_revision(path)
else:
try:
data = self.get_file_text(path)
except errors.NoSuchFile:
data = None
except IOError as e:
if e.errno != errno.ENOENT:
raise
data = None
if data is None:
data = self.branch.repository._git.object_store[sha].data
ie.text_sha1 = osutils.sha_string(data)
ie.text_size = len(data)
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
return ie
def _add_missing_parent_ids(self, path, dir_ids):
if path in dir_ids:
return []
parent = posixpath.dirname(path).strip("/")
ret = self._add_missing_parent_ids(parent, dir_ids)
parent_id = dir_ids[parent]
ie = self._get_dir_ie(path, parent_id)
dir_ids[path] = ie.file_id
ret.append((path, ie))
return ret
def _comparison_data(self, entry, path):
if entry is None:
return None, False, None
return entry.kind, entry.executable, None
def _unversion_path(self, path):
if self._lock_mode is None:
raise errors.ObjectNotLocked(self)
encoded_path = path.encode("utf-8")
count = 0
(index, subpath) = self._lookup_index(encoded_path)
try:
self._index_del_entry(index, encoded_path)
except KeyError:
# A directory, perhaps?
# TODO(jelmer): Deletes that involve submodules?
for p in list(index):
if p.startswith(subpath + b"/"):
count += 1
self._index_del_entry(index, p)
else:
count = 1
self._versioned_dirs = None
return count
def unversion(self, paths):
with self.lock_tree_write():
for path in paths:
if self._unversion_path(path) == 0:
raise errors.NoSuchFile(path)
self._versioned_dirs = None
self.flush()
def flush(self):
pass
def update_basis_by_delta(self, revid, delta):
# TODO(jelmer): This shouldn't be called, it's inventory specific.
for (old_path, new_path, file_id, ie) in delta:
if old_path is not None:
(index, old_subpath) = self._lookup_index(
old_path.encode('utf-8'))
if old_subpath in index:
self._index_del_entry(index, old_subpath)
self._versioned_dirs = None
if new_path is not None and ie.kind != 'directory':
self._index_add_entry(new_path, ie.kind)
self.flush()
self._set_merges_from_parent_ids([])
def move(self, from_paths, to_dir=None, after=None):
rename_tuples = []
with self.lock_tree_write():
to_abs = self.abspath(to_dir)
if not os.path.isdir(to_abs):
raise errors.BzrMoveFailedError('', to_dir,
errors.NotADirectory(to_abs))
for from_rel in from_paths:
from_tail = os.path.split(from_rel)[-1]
to_rel = os.path.join(to_dir, from_tail)
self.rename_one(from_rel, to_rel, after=after)
rename_tuples.append((from_rel, to_rel))
self.flush()
return rename_tuples
def rename_one(self, from_rel, to_rel, after=None):
from_path = from_rel.encode("utf-8")
to_rel, can_access = osutils.normalized_filename(to_rel)
if not can_access:
raise errors.InvalidNormalization(to_rel)
to_path = to_rel.encode("utf-8")
with self.lock_tree_write():
if not after:
# Perhaps it's already moved?
after = (
not self.has_filename(from_rel) and
self.has_filename(to_rel) and
not self.is_versioned(to_rel))
if after:
if not self.has_filename(to_rel):
raise errors.BzrMoveFailedError(
from_rel, to_rel, errors.NoSuchFile(to_rel))
if self.basis_tree().is_versioned(to_rel):
raise errors.BzrMoveFailedError(
from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
kind = self.kind(to_rel)
else:
try:
to_kind = self.kind(to_rel)
except errors.NoSuchFile:
exc_type = errors.BzrRenameFailedError
to_kind = None
else:
exc_type = errors.BzrMoveFailedError
if self.is_versioned(to_rel):
raise exc_type(from_rel, to_rel,
errors.AlreadyVersionedError(to_rel))
if not self.has_filename(from_rel):
raise errors.BzrMoveFailedError(
from_rel, to_rel, errors.NoSuchFile(from_rel))
kind = self.kind(from_rel)
if not self.is_versioned(from_rel) and kind != 'directory':
raise exc_type(from_rel, to_rel,
errors.NotVersionedError(from_rel))
if self.has_filename(to_rel):
raise errors.RenameFailedFilesExist(
from_rel, to_rel, errors.FileExists(to_rel))
kind = self.kind(from_rel)
if not after and kind != 'directory':
(index, from_subpath) = self._lookup_index(from_path)
if from_subpath not in index:
# It's not a file
raise errors.BzrMoveFailedError(
from_rel, to_rel,
errors.NotVersionedError(path=from_rel))
if not after:
try:
self._rename_one(from_rel, to_rel)
except OSError as e:
if e.errno == errno.ENOENT:
raise errors.BzrMoveFailedError(
from_rel, to_rel, errors.NoSuchFile(to_rel))
raise
if kind != 'directory':
(index, from_index_path) = self._lookup_index(from_path)
try:
self._index_del_entry(index, from_path)
except KeyError:
pass
self._index_add_entry(to_rel, kind)
else:
todo = [(p, i) for (p, i) in self._recurse_index_entries()
if p.startswith(from_path + b'/')]
for child_path, child_value in todo:
(child_to_index, child_to_index_path) = self._lookup_index(
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
child_to_index[child_to_index_path] = child_value
# TODO(jelmer): Mark individual index as dirty
self._index_dirty = True
(child_from_index, child_from_index_path) = self._lookup_index(
child_path)
self._index_del_entry(
child_from_index, child_from_index_path)
self._versioned_dirs = None
self.flush()
def find_related_paths_across_trees(self, paths, trees=[],
require_versioned=True):
if paths is None:
return None
if require_versioned:
trees = [self] + (trees if trees is not None else [])
unversioned = set()
for p in paths:
for t in trees:
if t.is_versioned(p):
break
else:
unversioned.add(p)
if unversioned:
raise errors.PathsNotVersionedError(unversioned)
return filter(self.is_versioned, paths)
def path_content_summary(self, path):
"""See Tree.path_content_summary."""
try:
stat_result = self._lstat(path)
except OSError as e:
if getattr(e, 'errno', None) == errno.ENOENT:
# no file.
return ('missing', None, None, None)
# propagate other errors
raise
kind = mode_kind(stat_result.st_mode)
if kind == 'file':
return self._file_content_summary(path, stat_result)
elif kind == 'directory':
# perhaps it looks like a plain directory, but it's really a
# reference.
if self._directory_is_tree_reference(path):
kind = 'tree-reference'
return kind, None, None, None
elif kind == 'symlink':
target = osutils.readlink(self.abspath(path))
return ('symlink', None, None, target)
else:
return (kind, None, None, None)
def stored_kind(self, relpath):
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
if index is None:
return kind
try:
mode = index[index_path].mode
except KeyError:
return kind
else:
if S_ISGITLINK(mode):
return 'tree-reference'
return 'directory'
def kind(self, relpath):
kind = osutils.file_kind(self.abspath(relpath))
if kind == 'directory':
if self._directory_is_tree_reference(relpath):
return 'tree-reference'
return 'directory'
else:
return kind
def _live_entry(self, relpath):
raise NotImplementedError(self._live_entry)
def get_transform(self, pb=None):
from ..transform import TreeTransform
return TreeTransform(self, pb=pb)
class InterIndexGitTree(InterGitTrees):
"""InterTree that works between a Git revision tree and an index."""
def __init__(self, source, target):
super(InterIndexGitTree, self).__init__(source, target)
self._index = target.index
@classmethod
def is_compatible(cls, source, target):
return (isinstance(source, GitRevisionTree) and
isinstance(target, MutableGitIndexTree))
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
require_versioned=False, extra_trees=None,
want_unversioned=False):
trees = [self.source]
if extra_trees is not None:
trees.extend(extra_trees)
if specific_files is not None:
specific_files = self.target.find_related_paths_across_trees(
specific_files, trees,
require_versioned=require_versioned)
# TODO(jelmer): Restrict to specific_files, for performance reasons.
with self.lock_read():
return changes_between_git_tree_and_working_copy(
self.source.store, self.source.tree,
self.target, want_unchanged=want_unchanged,
want_unversioned=want_unversioned)
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
want_unchanged=False,
want_unversioned=False):
"""Determine the changes between a git tree and a working tree with index.
"""
extras = set()
blobs = {}
# Report dirified directories to commit_tree first, so that they can be
# replaced with non-empty directories if they have contents.
dirified = []
trust_executable = target._supports_executable()
for path, index_entry in target._recurse_index_entries():
try:
live_entry = target._live_entry(path)
except EnvironmentError as e:
if e.errno == errno.ENOENT:
# Entry was removed; keep it listed, but mark it as gone.
blobs[path] = (ZERO_SHA, 0)
else:
raise
else:
if live_entry is None:
# Entry was turned into a directory.
# Maybe it's just a submodule that's not checked out?
if S_ISGITLINK(index_entry.mode):
blobs[path] = (index_entry.sha, index_entry.mode)
else:
dirified.append((path, Tree().id, stat.S_IFDIR))
store.add_object(Tree())
else:
mode = live_entry.mode
if not trust_executable:
if mode_is_executable(index_entry.mode):
mode |= 0o111
else:
mode &= ~0o111
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
if want_unversioned:
for e in target.extras():
st = target._lstat(e)
try:
np, accessible = osutils.normalized_filename(e)
except UnicodeDecodeError:
raise errors.BadFilenameEncoding(
e, osutils._fs_enc)
if stat.S_ISDIR(st.st_mode):
blob = Tree()
else:
blob = blob_from_path_and_stat(
target.abspath(e).encode(osutils._fs_enc), st)
store.add_object(blob)
np = np.encode('utf-8')
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
extras.add(np)
to_tree_sha = commit_tree(
store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
return store.tree_changes(
from_tree_sha, to_tree_sha, include_trees=True,
want_unchanged=want_unchanged, change_type_same=True), extras
|