~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Robert Collins
  • Date: 2005-09-26 08:56:15 UTC
  • mto: (1092.3.4)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: robertc@robertcollins.net-20050926085615-99b8fb35f41b541d
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import errno
18
18
import patch
19
19
import stat
20
 
from tempfile import mkdtemp
21
 
from shutil import rmtree
22
20
from bzrlib.trace import mutter
23
 
from bzrlib.osutils import rename, sha_file
24
 
import bzrlib
25
 
from itertools import izip
 
21
from bzrlib.osutils import rename
26
22
 
27
23
# XXX: mbp: I'm not totally convinced that we should handle conflicts
28
24
# as part of changeset application, rather than only in the merge
48
44
    return newdict
49
45
 
50
46
       
51
 
class ChangeExecFlag(object):
 
47
class ChangeUnixPermissions(object):
52
48
    """This is two-way change, suitable for file modification, creation,
53
49
    deletion"""
54
 
    def __init__(self, old_exec_flag, new_exec_flag):
55
 
        self.old_exec_flag = old_exec_flag
56
 
        self.new_exec_flag = new_exec_flag
 
50
    def __init__(self, old_mode, new_mode):
 
51
        self.old_mode = old_mode
 
52
        self.new_mode = new_mode
57
53
 
58
54
    def apply(self, filename, conflict_handler, reverse=False):
59
55
        if not reverse:
60
 
            from_exec_flag = self.old_exec_flag
61
 
            to_exec_flag = self.new_exec_flag
 
56
            from_mode = self.old_mode
 
57
            to_mode = self.new_mode
62
58
        else:
63
 
            from_exec_flag = self.new_exec_flag
64
 
            to_exec_flag = self.old_exec_flag
 
59
            from_mode = self.new_mode
 
60
            to_mode = self.old_mode
65
61
        try:
66
 
            current_exec_flag = bool(os.stat(filename).st_mode & 0111)
 
62
            current_mode = os.stat(filename).st_mode &0777
67
63
        except OSError, e:
68
64
            if e.errno == errno.ENOENT:
69
 
                if conflict_handler.missing_for_exec_flag(filename) == "skip":
 
65
                if conflict_handler.missing_for_chmod(filename) == "skip":
70
66
                    return
71
67
                else:
72
 
                    current_exec_flag = from_exec_flag
 
68
                    current_mode = from_mode
73
69
 
74
 
        if from_exec_flag is not None and current_exec_flag != from_exec_flag:
75
 
            if conflict_handler.wrong_old_exec_flag(filename,
76
 
                        from_exec_flag, current_exec_flag) != "continue":
 
70
        if from_mode is not None and current_mode != from_mode:
 
71
            if conflict_handler.wrong_old_perms(filename, from_mode, 
 
72
                                                current_mode) != "continue":
77
73
                return
78
74
 
79
 
        if to_exec_flag is not None:
80
 
            current_mode = os.stat(filename).st_mode
81
 
            if to_exec_flag:
82
 
                umask = os.umask(0)
83
 
                os.umask(umask)
84
 
                to_mode = current_mode | (0100 & ~umask)
85
 
                # Enable x-bit for others only if they can read it.
86
 
                if current_mode & 0004:
87
 
                    to_mode |= 0001 & ~umask
88
 
                if current_mode & 0040:
89
 
                    to_mode |= 0010 & ~umask
90
 
            else:
91
 
                to_mode = current_mode & ~0111
 
75
        if to_mode is not None:
92
76
            try:
93
77
                os.chmod(filename, to_mode)
94
78
            except IOError, e:
95
79
                if e.errno == errno.ENOENT:
96
 
                    conflict_handler.missing_for_exec_flag(filename)
 
80
                    conflict_handler.missing_for_chmod(filename)
97
81
 
98
82
    def __eq__(self, other):
99
 
        return (isinstance(other, ChangeExecFlag) and
100
 
                self.old_exec_flag == other.old_exec_flag and
101
 
                self.new_exec_flag == other.new_exec_flag)
 
83
        if not isinstance(other, ChangeUnixPermissions):
 
84
            return False
 
85
        elif self.old_mode != other.old_mode:
 
86
            return False
 
87
        elif self.new_mode != other.new_mode:
 
88
            return False
 
89
        else:
 
90
            return True
102
91
 
103
92
    def __ne__(self, other):
104
93
        return not (self == other)
146
135
        """
147
136
        self.target = contents
148
137
 
149
 
    def __repr__(self):
150
 
        return "SymlinkCreate(%s)" % self.target
151
 
 
152
138
    def __call__(self, filename, conflict_handler, reverse):
153
139
        """Creates or destroys the symlink.
154
140
 
236
222
 
237
223
                    
238
224
 
239
 
class TreeFileCreate(object):
240
 
    """Create or delete a file (for use with ReplaceContents)"""
241
 
    def __init__(self, tree, file_id):
242
 
        """Constructor
243
 
 
244
 
        :param contents: The contents of the file to write
245
 
        :type contents: str
246
 
        """
247
 
        self.tree = tree
248
 
        self.file_id = file_id
249
 
 
250
 
    def __repr__(self):
251
 
        return "TreeFileCreate(%s)" % self.file_id
252
 
 
253
 
    def __eq__(self, other):
254
 
        if not isinstance(other, TreeFileCreate):
255
 
            return False
256
 
        return self.tree.get_file_sha1(self.file_id) == \
257
 
            other.tree.get_file_sha1(other.file_id)
258
 
 
259
 
    def __ne__(self, other):
260
 
        return not (self == other)
261
 
 
262
 
    def write_file(self, filename):
263
 
        outfile = file(filename, "wb")
264
 
        for line in self.tree.get_file(self.file_id):
265
 
            outfile.write(line)
266
 
 
267
 
    def same_text(self, filename):
268
 
        in_file = file(filename, "rb")
269
 
        return sha_file(in_file) == self.tree.get_file_sha1(self.file_id)
270
 
 
271
 
    def __call__(self, filename, conflict_handler, reverse):
272
 
        """Create or delete a file
273
 
 
274
 
        :param filename: The name of the file to create
275
 
        :type filename: str
276
 
        :param reverse: Delete the file instead of creating it
277
 
        :type reverse: bool
278
 
        """
279
 
        if not reverse:
280
 
            try:
281
 
                self.write_file(filename)
282
 
            except IOError, e:
283
 
                if e.errno == errno.ENOENT:
284
 
                    if conflict_handler.missing_parent(filename)=="continue":
285
 
                        self.write_file(filename)
286
 
                else:
287
 
                    raise
288
 
 
289
 
        else:
290
 
            try:
291
 
                if not self.same_text(filename):
292
 
                    direction = conflict_handler.wrong_old_contents(filename,
293
 
                        self.tree.get_file(self.file_id).read())
294
 
                    if  direction != "continue":
295
 
                        return
296
 
                os.unlink(filename)
297
 
            except IOError, e:
298
 
                if e.errno != errno.ENOENT:
299
 
                    raise
300
 
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
301
 
                    return
302
 
 
303
 
                    
304
 
 
305
225
def reversed(sequence):
306
226
    max = len(sequence) - 1
307
227
    for i in range(len(sequence)):
374
294
            if mode is not None:
375
295
                os.chmod(filename, mode)
376
296
 
377
 
    def is_creation(self):
378
 
        return self.new_contents is not None and self.old_contents is None
379
 
 
380
 
    def is_deletion(self):
381
 
        return self.old_contents is not None and self.new_contents is None
382
 
 
383
297
class ApplySequence(object):
384
298
    def __init__(self, changes=None):
385
299
        self.changes = []
416
330
        self.base = base
417
331
        self.other = other
418
332
 
419
 
    def is_creation(self):
420
 
        return False
421
 
 
422
 
    def is_deletion(self):
423
 
        return False
424
 
 
425
333
    def __eq__(self, other):
426
334
        if not isinstance(other, Diff3Merge):
427
335
            return False
431
339
    def __ne__(self, other):
432
340
        return not (self == other)
433
341
 
434
 
    def dump_file(self, temp_dir, name, tree):
435
 
        out_path = os.path.join(temp_dir, name)
436
 
        out_file = file(out_path, "wb")
437
 
        in_file = tree.get_file(self.file_id)
438
 
        for line in in_file:
439
 
            out_file.write(line)
440
 
        return out_path
441
 
 
442
342
    def apply(self, filename, conflict_handler, reverse=False):
443
 
        temp_dir = mkdtemp(prefix="bzr-")
444
 
        try:
445
 
            new_file = filename+".new"
446
 
            base_file = self.dump_file(temp_dir, "base", self.base)
447
 
            other_file = self.dump_file(temp_dir, "other", self.other)
448
 
            if not reverse:
449
 
                base = base_file
450
 
                other = other_file
451
 
            else:
452
 
                base = other_file
453
 
                other = base_file
454
 
            status = patch.diff3(new_file, filename, base, other)
455
 
            if status == 0:
456
 
                os.chmod(new_file, os.stat(filename).st_mode)
457
 
                rename(new_file, filename)
458
 
                return
459
 
            else:
460
 
                assert(status == 1)
461
 
                def get_lines(filename):
462
 
                    my_file = file(filename, "rb")
463
 
                    lines = my_file.readlines()
464
 
                    my_file.close()
465
 
                    return lines
466
 
                base_lines = get_lines(base)
467
 
                other_lines = get_lines(other)
468
 
                conflict_handler.merge_conflict(new_file, filename, base_lines, 
469
 
                                                other_lines)
470
 
        finally:
471
 
            rmtree(temp_dir)
 
343
        new_file = filename+".new"
 
344
        base_file = self.base.readonly_path(self.file_id)
 
345
        other_file = self.other.readonly_path(self.file_id)
 
346
        if not reverse:
 
347
            base = base_file
 
348
            other = other_file
 
349
        else:
 
350
            base = other_file
 
351
            other = base_file
 
352
        status = patch.diff3(new_file, filename, base, other)
 
353
        if status == 0:
 
354
            os.chmod(new_file, os.stat(filename).st_mode)
 
355
            rename(new_file, filename)
 
356
            return
 
357
        else:
 
358
            assert(status == 1)
 
359
            def get_lines(filename):
 
360
                my_file = file(base, "rb")
 
361
                lines = my_file.readlines()
 
362
                my_file.close()
 
363
            base_lines = get_lines(base)
 
364
            other_lines = get_lines(other)
 
365
            conflict_handler.merge_conflict(new_file, filename, base_lines, 
 
366
                                            other_lines)
472
367
 
473
368
 
474
369
def CreateDir():
507
402
    """
508
403
    return ReplaceContents(FileCreate(contents), None)
509
404
 
510
 
def ReplaceFileContents(old_tree, new_tree, file_id):
 
405
def ReplaceFileContents(old_contents, new_contents):
511
406
    """Convenience fucntion to replace the contents of a file.
512
407
    
513
408
    :param old_contents: The contents of the file to replace 
517
412
    :return: A ReplaceContents that will replace the contents of a file a file 
518
413
    :rtype: `ReplaceContents`
519
414
    """
520
 
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
521
 
                           TreeFileCreate(new_tree, file_id))
 
415
    return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
522
416
 
523
417
def CreateSymlink(target):
524
418
    """Convenience fucntion to create a symlink.
687
581
        :param reverse: if true, the changeset is being applied in reverse
688
582
        :rtype: bool
689
583
        """
690
 
        return self.is_creation(not reverse)
 
584
        return ((self.new_parent is None and not reverse) or 
 
585
                (self.parent is None and reverse))
691
586
 
692
587
    def is_creation(self, reverse):
693
588
        """Return true if applying the entry would create a file/directory.
695
590
        :param reverse: if true, the changeset is being applied in reverse
696
591
        :rtype: bool
697
592
        """
698
 
        if self.contents_change is None:
699
 
            return False
700
 
        if reverse:
701
 
            return self.contents_change.is_deletion()
702
 
        else:
703
 
            return self.contents_change.is_creation()
 
593
        return ((self.parent is None and not reverse) or 
 
594
                (self.new_parent is None and reverse))
704
595
 
705
596
    def is_creation_or_deletion(self):
706
597
        """Return true if applying the entry would create or delete a 
708
599
 
709
600
        :rtype: bool
710
601
        """
711
 
        return self.is_creation(False) or self.is_deletion(False)
 
602
        return self.parent is None or self.new_parent is None
712
603
 
713
604
    def get_cset_path(self, mod=False):
714
605
        """Determine the path of the entry according to the changeset.
886
777
    :rtype: (List, List)
887
778
    """
888
779
    source_entries = [x for x in changeset.entries.itervalues() 
889
 
                      if x.needs_rename() or x.is_creation_or_deletion()]
 
780
                      if x.needs_rename()]
890
781
    # these are done from longest path to shortest, to avoid deleting a
891
782
    # parent before its children are deleted/renamed 
892
783
    def longest_to_shortest(entry):
933
824
            entry.apply(path, conflict_handler, reverse)
934
825
            temp_name[entry.id] = None
935
826
 
936
 
        elif entry.needs_rename():
 
827
        else:
937
828
            to_name = os.path.join(temp_dir, str(i))
938
829
            src_path = inventory.get(entry.id)
939
830
            if src_path is not None:
944
835
                except OSError, e:
945
836
                    if e.errno != errno.ENOENT:
946
837
                        raise
947
 
                    if conflict_handler.missing_for_rename(src_path, to_name) \
948
 
                        == "skip":
 
838
                    if conflict_handler.missing_for_rename(src_path) == "skip":
949
839
                        continue
950
840
 
951
841
    return temp_name
972
862
            continue
973
863
        new_path = os.path.join(dir, new_tree_path)
974
864
        old_path = changed_inventory.get(entry.id)
975
 
        if bzrlib.osutils.lexists(new_path):
 
865
        if os.path.exists(new_path):
976
866
            if conflict_handler.target_exists(entry, new_path, old_path) == \
977
867
                "skip":
978
868
                continue
979
869
        if entry.is_creation(reverse):
980
870
            entry.apply(new_path, conflict_handler, reverse)
981
871
            changed_inventory[entry.id] = new_tree_path
982
 
        elif entry.needs_rename():
 
872
        else:
983
873
            if old_path is None:
984
874
                continue
985
875
            try:
1024
914
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
1025
915
        self.this_path = this_path
1026
916
 
 
917
class MergePermissionConflict(Exception):
 
918
    def __init__(self, this_path, base_path, other_path):
 
919
        this_perms = os.stat(this_path).st_mode & 0755
 
920
        base_perms = os.stat(base_path).st_mode & 0755
 
921
        other_perms = os.stat(other_path).st_mode & 0755
 
922
        msg = """Conflicting permission for %s
 
923
this: %o
 
924
base: %o
 
925
other: %o
 
926
        """ % (this_path, this_perms, base_perms, other_perms)
 
927
        self.this_path = this_path
 
928
        self.base_path = base_path
 
929
        self.other_path = other_path
 
930
        Exception.__init__(self, msg)
 
931
 
1027
932
class WrongOldContents(Exception):
1028
933
    def __init__(self, filename):
1029
934
        msg = "Contents mismatch deleting %s" % filename
1030
935
        self.filename = filename
1031
936
        Exception.__init__(self, msg)
1032
937
 
1033
 
class WrongOldExecFlag(Exception):
1034
 
    def __init__(self, filename, old_exec_flag, new_exec_flag):
1035
 
        msg = "Executable flag missmatch on %s:\n" \
1036
 
        "Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
 
938
class WrongOldPermissions(Exception):
 
939
    def __init__(self, filename, old_perms, new_perms):
 
940
        msg = "Permission missmatch on %s:\n" \
 
941
        "Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
1037
942
        self.filename = filename
1038
943
        Exception.__init__(self, msg)
1039
944
 
1057
962
        Exception.__init__(self, msg)
1058
963
        self.filename = filename
1059
964
 
1060
 
class MissingForSetExec(Exception):
 
965
class MissingPermsFile(Exception):
1061
966
    def __init__(self, filename):
1062
967
        msg = "Attempt to change permissions on  %s, which does not exist" %\
1063
968
            filename
1072
977
 
1073
978
 
1074
979
class MissingForRename(Exception):
1075
 
    def __init__(self, filename, to_path):
1076
 
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
 
980
    def __init__(self, filename):
 
981
        msg = "Attempt to move missing path %s" % (filename)
1077
982
        Exception.__init__(self, msg)
1078
983
        self.filename = filename
1079
984
 
1082
987
        msg = "Conflicting contents for new file %s" % (filename)
1083
988
        Exception.__init__(self, msg)
1084
989
 
1085
 
class ThreewayContentsConflict(Exception):
1086
 
    def __init__(self, filename):
1087
 
        msg = "Conflicting contents for file %s" % (filename)
1088
 
        Exception.__init__(self, msg)
1089
 
 
1090
990
 
1091
991
class MissingForMerge(Exception):
1092
992
    def __init__(self, filename):
1125
1025
        os.unlink(new_file)
1126
1026
        raise MergeConflict(this_path)
1127
1027
 
 
1028
    def permission_conflict(self, this_path, base_path, other_path):
 
1029
        raise MergePermissionConflict(this_path, base_path, other_path)
 
1030
 
1128
1031
    def wrong_old_contents(self, filename, expected_contents):
1129
1032
        raise WrongOldContents(filename)
1130
1033
 
1131
1034
    def rem_contents_conflict(self, filename, this_contents, base_contents):
1132
1035
        raise RemoveContentsConflict(filename)
1133
1036
 
1134
 
    def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1135
 
        raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
 
1037
    def wrong_old_perms(self, filename, old_perms, new_perms):
 
1038
        raise WrongOldPermissions(filename, old_perms, new_perms)
1136
1039
 
1137
1040
    def rmdir_non_empty(self, filename):
1138
1041
        raise DeletingNonEmptyDirectory(filename)
1143
1046
    def patch_target_missing(self, filename, contents):
1144
1047
        raise PatchTargetMissing(filename)
1145
1048
 
1146
 
    def missing_for_exec_flag(self, filename):
1147
 
        raise MissingForExecFlag(filename)
 
1049
    def missing_for_chmod(self, filename):
 
1050
        raise MissingPermsFile(filename)
1148
1051
 
1149
1052
    def missing_for_rm(self, filename, change):
1150
1053
        raise MissingForRm(filename)
1151
1054
 
1152
 
    def missing_for_rename(self, filename, to_path):
1153
 
        raise MissingForRename(filename, to_path)
 
1055
    def missing_for_rename(self, filename):
 
1056
        raise MissingForRename(filename)
1154
1057
 
1155
1058
    def missing_for_merge(self, file_id, other_path):
1156
1059
        raise MissingForMerge(other_path)
1158
1061
    def new_contents_conflict(self, filename, other_contents):
1159
1062
        raise NewContentsConflict(filename)
1160
1063
 
1161
 
    def threeway_contents_conflict(self, filename, this_contents,
1162
 
                                   base_contents, other_contents):
1163
 
        raise ThreewayContentsConflict(filename)
1164
 
 
1165
1064
    def finalize(self):
1166
1065
        pass
1167
1066
 
1198
1097
    
1199
1098
    #apply changes that don't affect filenames
1200
1099
    for entry in changeset.entries.itervalues():
1201
 
        if not entry.is_creation_or_deletion() and not entry.is_boring():
 
1100
        if not entry.is_creation_or_deletion():
1202
1101
            path = os.path.join(dir, inventory[entry.id])
1203
1102
            entry.apply(path, conflict_handler, reverse)
1204
1103
 
1223
1122
    r_inventory = {}
1224
1123
    for entry in tree.source_inventory().itervalues():
1225
1124
        inventory[entry.id] = entry.path
1226
 
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
 
1125
    new_inventory = apply_changeset(cset, r_inventory, tree.root,
1227
1126
                                    reverse=reverse)
1228
1127
    new_entries, remove_entries = \
1229
1128
        get_inventory_change(inventory, new_inventory, cset, reverse)
1364
1263
        return new_meta
1365
1264
    elif new_meta is None:
1366
1265
        return old_meta
1367
 
    elif (isinstance(old_meta, ChangeExecFlag) and
1368
 
          isinstance(new_meta, ChangeExecFlag)):
1369
 
        return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
 
1266
    elif isinstance(old_meta, ChangeUnixPermissions) and \
 
1267
        isinstance(new_meta, ChangeUnixPermissions):
 
1268
        return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1370
1269
    else:
1371
1270
        return ApplySequence(old_meta, new_meta)
1372
1271
 
1377
1276
            return False
1378
1277
    return True
1379
1278
 
1380
 
class UnsupportedFiletype(Exception):
1381
 
    def __init__(self, kind, full_path):
1382
 
        msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1383
 
            % (full_path, kind)
 
1279
class UnsuppportedFiletype(Exception):
 
1280
    def __init__(self, full_path, stat_result):
 
1281
        msg = "The file \"%s\" is not a supported filetype." % full_path
1384
1282
        Exception.__init__(self, msg)
1385
1283
        self.full_path = full_path
1386
 
        self.kind = kind
 
1284
        self.stat_result = stat_result
1387
1285
 
1388
1286
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1389
1287
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1390
1288
 
1391
 
 
1392
1289
class ChangesetGenerator(object):
1393
1290
    def __init__(self, tree_a, tree_b, interesting_ids=None):
1394
1291
        object.__init__(self)
1430
1327
    def get_entry(self, file_id, tree):
1431
1328
        if not tree.has_or_had_id(file_id):
1432
1329
            return None
1433
 
        return tree.inventory[file_id]
 
1330
        return tree.tree.inventory[file_id]
1434
1331
 
1435
1332
    def get_entry_parent(self, entry):
1436
1333
        if entry is None:
1486
1383
 
1487
1384
        if cs_entry is None:
1488
1385
            return None
1489
 
 
1490
 
        cs_entry.metadata_change = self.make_exec_flag_change(id)
1491
 
 
1492
1386
        if id in self.tree_a and id in self.tree_b:
1493
1387
            a_sha1 = self.tree_a.get_file_sha1(id)
1494
1388
            b_sha1 = self.tree_b.get_file_sha1(id)
1495
1389
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1496
1390
                return cs_entry
1497
1391
 
1498
 
        cs_entry.contents_change = self.make_contents_change(id)
 
1392
        full_path_a = self.tree_a.readonly_path(id)
 
1393
        full_path_b = self.tree_b.readonly_path(id)
 
1394
        stat_a = self.lstat(full_path_a)
 
1395
        stat_b = self.lstat(full_path_b)
 
1396
        
 
1397
        cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
 
1398
        cs_entry.contents_change = self.make_contents_change(full_path_a,
 
1399
                                                             stat_a, 
 
1400
                                                             full_path_b, 
 
1401
                                                             stat_b)
1499
1402
        return cs_entry
1500
1403
 
1501
 
    def make_exec_flag_change(self, file_id):
1502
 
        exec_flag_a = exec_flag_b = None
1503
 
        if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1504
 
            exec_flag_a = self.tree_a.is_executable(file_id)
1505
 
 
1506
 
        if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1507
 
            exec_flag_b = self.tree_b.is_executable(file_id)
1508
 
 
1509
 
        if exec_flag_a == exec_flag_b:
1510
 
            return None
1511
 
        return ChangeExecFlag(exec_flag_a, exec_flag_b)
1512
 
 
1513
 
    def make_contents_change(self, file_id):
1514
 
        a_contents = get_contents(self.tree_a, file_id)
1515
 
        b_contents = get_contents(self.tree_b, file_id)
 
1404
    def make_mode_change(self, stat_a, stat_b):
 
1405
        mode_a = None
 
1406
        if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
 
1407
            mode_a = stat_a.st_mode & 0777
 
1408
        mode_b = None
 
1409
        if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
 
1410
            mode_b = stat_b.st_mode & 0777
 
1411
        if mode_a == mode_b:
 
1412
            return None
 
1413
        return ChangeUnixPermissions(mode_a, mode_b)
 
1414
 
 
1415
    def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
 
1416
        if stat_a is None and stat_b is None:
 
1417
            return None
 
1418
        if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
 
1419
            stat.S_ISDIR(stat_b.st_mode):
 
1420
            return None
 
1421
        if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
 
1422
            stat.S_ISREG(stat_b.st_mode):
 
1423
            if stat_a.st_ino == stat_b.st_ino and \
 
1424
                stat_a.st_dev == stat_b.st_dev:
 
1425
                return None
 
1426
 
 
1427
        a_contents = self.get_contents(stat_a, full_path_a)
 
1428
        b_contents = self.get_contents(stat_b, full_path_b)
1516
1429
        if a_contents == b_contents:
1517
1430
            return None
1518
1431
        return ReplaceContents(a_contents, b_contents)
1519
1432
 
 
1433
    def get_contents(self, stat_result, full_path):
 
1434
        if stat_result is None:
 
1435
            return None
 
1436
        elif stat.S_ISREG(stat_result.st_mode):
 
1437
            return FileCreate(file(full_path, "rb").read())
 
1438
        elif stat.S_ISDIR(stat_result.st_mode):
 
1439
            return dir_create
 
1440
        elif stat.S_ISLNK(stat_result.st_mode):
 
1441
            return SymlinkCreate(os.readlink(full_path))
 
1442
        else:
 
1443
            raise UnsupportedFiletype(full_path, stat_result)
1520
1444
 
1521
 
def get_contents(tree, file_id):
1522
 
    """Return the appropriate contents to create a copy of file_id from tree"""
1523
 
    if file_id not in tree:
1524
 
        return None
1525
 
    kind = tree.kind(file_id)
1526
 
    if kind == "file":
1527
 
        return TreeFileCreate(tree, file_id)
1528
 
    elif kind in ("directory", "root_directory"):
1529
 
        return dir_create
1530
 
    elif kind == "symlink":
1531
 
        return SymlinkCreate(tree.get_symlink_target(file_id))
1532
 
    else:
1533
 
        raise UnsupportedFiletype(kind, tree.id2path(file_id))
 
1445
    def lstat(self, full_path):
 
1446
        stat_result = None
 
1447
        if full_path is not None:
 
1448
            try:
 
1449
                stat_result = os.lstat(full_path)
 
1450
            except OSError, e:
 
1451
                if e.errno != errno.ENOENT:
 
1452
                    raise
 
1453
        return stat_result
1534
1454
 
1535
1455
 
1536
1456
def full_path(entry, tree):
1537
 
    return os.path.join(tree.basedir, entry.path)
 
1457
    return os.path.join(tree.root, entry.path)
1538
1458
 
1539
1459
def new_delete_entry(entry, tree, inventory, delete):
1540
1460
    if entry.path == "":