~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

merge merge tweaks from aaron, which includes latest .dev

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
26
21
 
27
22
# XXX: mbp: I'm not totally convinced that we should handle conflicts
28
23
# as part of changeset application, rather than only in the merge
47
42
        newdict[value] = key
48
43
    return newdict
49
44
 
 
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)
105
94
 
106
 
 
107
95
def dir_create(filename, conflict_handler, reverse):
108
96
    """Creates the directory, or deletes it if reverse is true.  Intended to be
109
97
    used with ReplaceContents.
129
117
        try:
130
118
            os.rmdir(filename)
131
119
        except OSError, e:
132
 
            if e.errno != errno.ENOTEMPTY:
 
120
            if e.errno != 39:
133
121
                raise
134
122
            if conflict_handler.rmdir_non_empty(filename) == "skip":
135
123
                return
136
124
            os.rmdir(filename)
137
125
 
 
126
                
 
127
            
138
128
 
139
129
class SymlinkCreate(object):
140
130
    """Creates or deletes a symlink (for use with ReplaceContents)"""
146
136
        """
147
137
        self.target = contents
148
138
 
149
 
    def __repr__(self):
150
 
        return "SymlinkCreate(%s)" % self.target
151
 
 
152
139
    def __call__(self, filename, conflict_handler, reverse):
153
140
        """Creates or destroys the symlink.
154
141
 
236
223
 
237
224
                    
238
225
 
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
226
def reversed(sequence):
306
227
    max = len(sequence) - 1
307
228
    for i in range(len(sequence)):
374
295
            if mode is not None:
375
296
                os.chmod(filename, mode)
376
297
 
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
298
class ApplySequence(object):
384
299
    def __init__(self, changes=None):
385
300
        self.changes = []
416
331
        self.base = base
417
332
        self.other = other
418
333
 
419
 
    def is_creation(self):
420
 
        return False
421
 
 
422
 
    def is_deletion(self):
423
 
        return False
424
 
 
425
334
    def __eq__(self, other):
426
335
        if not isinstance(other, Diff3Merge):
427
336
            return False
431
340
    def __ne__(self, other):
432
341
        return not (self == other)
433
342
 
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
343
    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)
 
344
        new_file = filename+".new"
 
345
        base_file = self.base.readonly_path(self.file_id)
 
346
        other_file = self.other.readonly_path(self.file_id)
 
347
        if not reverse:
 
348
            base = base_file
 
349
            other = other_file
 
350
        else:
 
351
            base = other_file
 
352
            other = base_file
 
353
        status = patch.diff3(new_file, filename, base, other)
 
354
        if status == 0:
 
355
            os.chmod(new_file, os.stat(filename).st_mode)
 
356
            os.rename(new_file, filename)
 
357
            return
 
358
        else:
 
359
            assert(status == 1)
 
360
            def get_lines(filename):
 
361
                my_file = file(base, "rb")
 
362
                lines = my_file.readlines()
 
363
                my_file.close()
 
364
            base_lines = get_lines(base)
 
365
            other_lines = get_lines(other)
 
366
            conflict_handler.merge_conflict(new_file, filename, base_lines, 
 
367
                                            other_lines)
472
368
 
473
369
 
474
370
def CreateDir():
507
403
    """
508
404
    return ReplaceContents(FileCreate(contents), None)
509
405
 
510
 
def ReplaceFileContents(old_tree, new_tree, file_id):
 
406
def ReplaceFileContents(old_contents, new_contents):
511
407
    """Convenience fucntion to replace the contents of a file.
512
408
    
513
409
    :param old_contents: The contents of the file to replace 
517
413
    :return: A ReplaceContents that will replace the contents of a file a file 
518
414
    :rtype: `ReplaceContents`
519
415
    """
520
 
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
521
 
                           TreeFileCreate(new_tree, file_id))
 
416
    return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
522
417
 
523
418
def CreateSymlink(target):
524
419
    """Convenience fucntion to create a symlink.
687
582
        :param reverse: if true, the changeset is being applied in reverse
688
583
        :rtype: bool
689
584
        """
690
 
        return self.is_creation(not reverse)
 
585
        return ((self.new_parent is None and not reverse) or 
 
586
                (self.parent is None and reverse))
691
587
 
692
588
    def is_creation(self, reverse):
693
589
        """Return true if applying the entry would create a file/directory.
695
591
        :param reverse: if true, the changeset is being applied in reverse
696
592
        :rtype: bool
697
593
        """
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()
 
594
        return ((self.parent is None and not reverse) or 
 
595
                (self.new_parent is None and reverse))
704
596
 
705
597
    def is_creation_or_deletion(self):
706
598
        """Return true if applying the entry would create or delete a 
708
600
 
709
601
        :rtype: bool
710
602
        """
711
 
        return self.is_creation(False) or self.is_deletion(False)
 
603
        return self.parent is None or self.new_parent is None
712
604
 
713
605
    def get_cset_path(self, mod=False):
714
606
        """Determine the path of the entry according to the changeset.
886
778
    :rtype: (List, List)
887
779
    """
888
780
    source_entries = [x for x in changeset.entries.itervalues() 
889
 
                      if x.needs_rename() or x.is_creation_or_deletion()]
 
781
                      if x.needs_rename()]
890
782
    # these are done from longest path to shortest, to avoid deleting a
891
783
    # parent before its children are deleted/renamed 
892
784
    def longest_to_shortest(entry):
933
825
            entry.apply(path, conflict_handler, reverse)
934
826
            temp_name[entry.id] = None
935
827
 
936
 
        elif entry.needs_rename():
 
828
        else:
937
829
            to_name = os.path.join(temp_dir, str(i))
938
830
            src_path = inventory.get(entry.id)
939
831
            if src_path is not None:
940
832
                src_path = os.path.join(dir, src_path)
941
833
                try:
942
 
                    rename(src_path, to_name)
 
834
                    os.rename(src_path, to_name)
943
835
                    temp_name[entry.id] = to_name
944
836
                except OSError, e:
945
837
                    if e.errno != errno.ENOENT:
946
838
                        raise
947
 
                    if conflict_handler.missing_for_rename(src_path, to_name) \
948
 
                        == "skip":
 
839
                    if conflict_handler.missing_for_rename(src_path) == "skip":
949
840
                        continue
950
841
 
951
842
    return temp_name
972
863
            continue
973
864
        new_path = os.path.join(dir, new_tree_path)
974
865
        old_path = changed_inventory.get(entry.id)
975
 
        if bzrlib.osutils.lexists(new_path):
 
866
        if os.path.exists(new_path):
976
867
            if conflict_handler.target_exists(entry, new_path, old_path) == \
977
868
                "skip":
978
869
                continue
979
870
        if entry.is_creation(reverse):
980
871
            entry.apply(new_path, conflict_handler, reverse)
981
872
            changed_inventory[entry.id] = new_tree_path
982
 
        elif entry.needs_rename():
 
873
        else:
983
874
            if old_path is None:
984
875
                continue
985
876
            try:
986
 
                rename(old_path, new_path)
 
877
                os.rename(old_path, new_path)
987
878
                changed_inventory[entry.id] = new_tree_path
988
879
            except OSError, e:
989
880
                raise Exception ("%s is missing" % new_path)
1024
915
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
1025
916
        self.this_path = this_path
1026
917
 
 
918
class MergePermissionConflict(Exception):
 
919
    def __init__(self, this_path, base_path, other_path):
 
920
        this_perms = os.stat(this_path).st_mode & 0755
 
921
        base_perms = os.stat(base_path).st_mode & 0755
 
922
        other_perms = os.stat(other_path).st_mode & 0755
 
923
        msg = """Conflicting permission for %s
 
924
this: %o
 
925
base: %o
 
926
other: %o
 
927
        """ % (this_path, this_perms, base_perms, other_perms)
 
928
        self.this_path = this_path
 
929
        self.base_path = base_path
 
930
        self.other_path = other_path
 
931
        Exception.__init__(self, msg)
 
932
 
1027
933
class WrongOldContents(Exception):
1028
934
    def __init__(self, filename):
1029
935
        msg = "Contents mismatch deleting %s" % filename
1030
936
        self.filename = filename
1031
937
        Exception.__init__(self, msg)
1032
938
 
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)
 
939
class WrongOldPermissions(Exception):
 
940
    def __init__(self, filename, old_perms, new_perms):
 
941
        msg = "Permission missmatch on %s:\n" \
 
942
        "Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
1037
943
        self.filename = filename
1038
944
        Exception.__init__(self, msg)
1039
945
 
1057
963
        Exception.__init__(self, msg)
1058
964
        self.filename = filename
1059
965
 
1060
 
class MissingForSetExec(Exception):
 
966
class MissingPermsFile(Exception):
1061
967
    def __init__(self, filename):
1062
968
        msg = "Attempt to change permissions on  %s, which does not exist" %\
1063
969
            filename
1072
978
 
1073
979
 
1074
980
class MissingForRename(Exception):
1075
 
    def __init__(self, filename, to_path):
1076
 
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
 
981
    def __init__(self, filename):
 
982
        msg = "Attempt to move missing path %s" % (filename)
1077
983
        Exception.__init__(self, msg)
1078
984
        self.filename = filename
1079
985
 
1082
988
        msg = "Conflicting contents for new file %s" % (filename)
1083
989
        Exception.__init__(self, msg)
1084
990
 
1085
 
class ThreewayContentsConflict(Exception):
1086
 
    def __init__(self, filename):
1087
 
        msg = "Conflicting contents for file %s" % (filename)
1088
 
        Exception.__init__(self, msg)
1089
 
 
1090
991
 
1091
992
class MissingForMerge(Exception):
1092
993
    def __init__(self, filename):
1125
1026
        os.unlink(new_file)
1126
1027
        raise MergeConflict(this_path)
1127
1028
 
 
1029
    def permission_conflict(self, this_path, base_path, other_path):
 
1030
        raise MergePermissionConflict(this_path, base_path, other_path)
 
1031
 
1128
1032
    def wrong_old_contents(self, filename, expected_contents):
1129
1033
        raise WrongOldContents(filename)
1130
1034
 
1131
1035
    def rem_contents_conflict(self, filename, this_contents, base_contents):
1132
1036
        raise RemoveContentsConflict(filename)
1133
1037
 
1134
 
    def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1135
 
        raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
 
1038
    def wrong_old_perms(self, filename, old_perms, new_perms):
 
1039
        raise WrongOldPermissions(filename, old_perms, new_perms)
1136
1040
 
1137
1041
    def rmdir_non_empty(self, filename):
1138
1042
        raise DeletingNonEmptyDirectory(filename)
1143
1047
    def patch_target_missing(self, filename, contents):
1144
1048
        raise PatchTargetMissing(filename)
1145
1049
 
1146
 
    def missing_for_exec_flag(self, filename):
1147
 
        raise MissingForExecFlag(filename)
 
1050
    def missing_for_chmod(self, filename):
 
1051
        raise MissingPermsFile(filename)
1148
1052
 
1149
1053
    def missing_for_rm(self, filename, change):
1150
1054
        raise MissingForRm(filename)
1151
1055
 
1152
 
    def missing_for_rename(self, filename, to_path):
1153
 
        raise MissingForRename(filename, to_path)
 
1056
    def missing_for_rename(self, filename):
 
1057
        raise MissingForRename(filename)
1154
1058
 
1155
1059
    def missing_for_merge(self, file_id, other_path):
1156
1060
        raise MissingForMerge(other_path)
1158
1062
    def new_contents_conflict(self, filename, other_contents):
1159
1063
        raise NewContentsConflict(filename)
1160
1064
 
1161
 
    def threeway_contents_conflict(self, filename, this_contents,
1162
 
                                   base_contents, other_contents):
1163
 
        raise ThreewayContentsConflict(filename)
1164
 
 
1165
1065
    def finalize(self):
1166
1066
        pass
1167
1067
 
1198
1098
    
1199
1099
    #apply changes that don't affect filenames
1200
1100
    for entry in changeset.entries.itervalues():
1201
 
        if not entry.is_creation_or_deletion() and not entry.is_boring():
 
1101
        if not entry.is_creation_or_deletion():
1202
1102
            path = os.path.join(dir, inventory[entry.id])
1203
1103
            entry.apply(path, conflict_handler, reverse)
1204
1104
 
1223
1123
    r_inventory = {}
1224
1124
    for entry in tree.source_inventory().itervalues():
1225
1125
        inventory[entry.id] = entry.path
1226
 
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
 
1126
    new_inventory = apply_changeset(cset, r_inventory, tree.root,
1227
1127
                                    reverse=reverse)
1228
1128
    new_entries, remove_entries = \
1229
1129
        get_inventory_change(inventory, new_inventory, cset, reverse)
1364
1264
        return new_meta
1365
1265
    elif new_meta is None:
1366
1266
        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)
 
1267
    elif isinstance(old_meta, ChangeUnixPermissions) and \
 
1268
        isinstance(new_meta, ChangeUnixPermissions):
 
1269
        return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1370
1270
    else:
1371
1271
        return ApplySequence(old_meta, new_meta)
1372
1272
 
1377
1277
            return False
1378
1278
    return True
1379
1279
 
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)
 
1280
class UnsuppportedFiletype(Exception):
 
1281
    def __init__(self, full_path, stat_result):
 
1282
        msg = "The file \"%s\" is not a supported filetype." % full_path
1384
1283
        Exception.__init__(self, msg)
1385
1284
        self.full_path = full_path
1386
 
        self.kind = kind
 
1285
        self.stat_result = stat_result
1387
1286
 
1388
1287
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1389
1288
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1390
1289
 
1391
 
 
1392
1290
class ChangesetGenerator(object):
1393
1291
    def __init__(self, tree_a, tree_b, interesting_ids=None):
1394
1292
        object.__init__(self)
1430
1328
    def get_entry(self, file_id, tree):
1431
1329
        if not tree.has_or_had_id(file_id):
1432
1330
            return None
1433
 
        return tree.inventory[file_id]
 
1331
        return tree.tree.inventory[file_id]
1434
1332
 
1435
1333
    def get_entry_parent(self, entry):
1436
1334
        if entry is None:
1486
1384
 
1487
1385
        if cs_entry is None:
1488
1386
            return None
1489
 
 
1490
 
        cs_entry.metadata_change = self.make_exec_flag_change(id)
1491
 
 
1492
1387
        if id in self.tree_a and id in self.tree_b:
1493
1388
            a_sha1 = self.tree_a.get_file_sha1(id)
1494
1389
            b_sha1 = self.tree_b.get_file_sha1(id)
1495
1390
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1496
1391
                return cs_entry
1497
1392
 
1498
 
        cs_entry.contents_change = self.make_contents_change(id)
 
1393
        full_path_a = self.tree_a.readonly_path(id)
 
1394
        full_path_b = self.tree_b.readonly_path(id)
 
1395
        stat_a = self.lstat(full_path_a)
 
1396
        stat_b = self.lstat(full_path_b)
 
1397
        
 
1398
        cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
 
1399
        cs_entry.contents_change = self.make_contents_change(full_path_a,
 
1400
                                                             stat_a, 
 
1401
                                                             full_path_b, 
 
1402
                                                             stat_b)
1499
1403
        return cs_entry
1500
1404
 
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)
 
1405
    def make_mode_change(self, stat_a, stat_b):
 
1406
        mode_a = None
 
1407
        if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
 
1408
            mode_a = stat_a.st_mode & 0777
 
1409
        mode_b = None
 
1410
        if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
 
1411
            mode_b = stat_b.st_mode & 0777
 
1412
        if mode_a == mode_b:
 
1413
            return None
 
1414
        return ChangeUnixPermissions(mode_a, mode_b)
 
1415
 
 
1416
    def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
 
1417
        if stat_a is None and stat_b is None:
 
1418
            return None
 
1419
        if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
 
1420
            stat.S_ISDIR(stat_b.st_mode):
 
1421
            return None
 
1422
        if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
 
1423
            stat.S_ISREG(stat_b.st_mode):
 
1424
            if stat_a.st_ino == stat_b.st_ino and \
 
1425
                stat_a.st_dev == stat_b.st_dev:
 
1426
                return None
 
1427
 
 
1428
        a_contents = self.get_contents(stat_a, full_path_a)
 
1429
        b_contents = self.get_contents(stat_b, full_path_b)
1516
1430
        if a_contents == b_contents:
1517
1431
            return None
1518
1432
        return ReplaceContents(a_contents, b_contents)
1519
1433
 
 
1434
    def get_contents(self, stat_result, full_path):
 
1435
        if stat_result is None:
 
1436
            return None
 
1437
        elif stat.S_ISREG(stat_result.st_mode):
 
1438
            return FileCreate(file(full_path, "rb").read())
 
1439
        elif stat.S_ISDIR(stat_result.st_mode):
 
1440
            return dir_create
 
1441
        elif stat.S_ISLNK(stat_result.st_mode):
 
1442
            return SymlinkCreate(os.readlink(full_path))
 
1443
        else:
 
1444
            raise UnsupportedFiletype(full_path, stat_result)
1520
1445
 
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))
 
1446
    def lstat(self, full_path):
 
1447
        stat_result = None
 
1448
        if full_path is not None:
 
1449
            try:
 
1450
                stat_result = os.lstat(full_path)
 
1451
            except OSError, e:
 
1452
                if e.errno != errno.ENOENT:
 
1453
                    raise
 
1454
        return stat_result
1534
1455
 
1535
1456
 
1536
1457
def full_path(entry, tree):
1537
 
    return os.path.join(tree.basedir, entry.path)
 
1458
    return os.path.join(tree.root, entry.path)
1538
1459
 
1539
1460
def new_delete_entry(entry, tree, inventory, delete):
1540
1461
    if entry.path == "":