~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: abentley
  • Date: 2005-10-16 23:56:25 UTC
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1460.
  • Revision ID: abentley@lappy-20051016235625-f2e4f5d4ff1e889c
Cleanups for pull

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