~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-07-20 17:31:25 UTC
  • Revision ID: mbp@sourcefrog.net-20050720173125-e5974e73bd5fc31e
- add back update-hashes command for testing/profiling

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#    You should have received a copy of the GNU General Public License
14
14
#    along with this program; if not, write to the Free Software
15
15
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Represent and apply a changeset.
18
 
 
19
 
Conflicts in applying a changeset are represented as exceptions.
20
 
 
21
 
This only handles the in-memory objects representing changesets, which are
22
 
primarily used by the merge code. 
23
 
"""
24
 
 
25
16
import os.path
26
17
import errno
 
18
import patch
27
19
import stat
28
 
from tempfile import mkdtemp
29
 
from shutil import rmtree
30
 
from itertools import izip
31
 
 
32
 
from bzrlib.trace import mutter, warning
33
 
from bzrlib.osutils import rename, sha_file
34
 
import bzrlib
35
 
from bzrlib.errors import BzrCheckError
36
 
 
 
20
from bzrlib.trace import mutter
 
21
"""
 
22
Represent and apply a changeset
 
23
"""
37
24
__docformat__ = "restructuredtext"
38
25
 
39
26
NULL_ID = "!NULL"
48
35
        newdict[value] = key
49
36
    return newdict
50
37
 
51
 
       
52
 
class ChangeExecFlag(object):
 
38
 
 
39
class PatchApply(object):
 
40
    """Patch application as a kind of content change"""
 
41
    def __init__(self, contents):
 
42
        """Constructor.
 
43
 
 
44
        :param contents: The text of the patch to apply
 
45
        :type contents: str"""
 
46
        self.contents = contents
 
47
 
 
48
    def __eq__(self, other):
 
49
        if not isinstance(other, PatchApply):
 
50
            return False
 
51
        elif self.contents != other.contents:
 
52
            return False
 
53
        else:
 
54
            return True
 
55
 
 
56
    def __ne__(self, other):
 
57
        return not (self == other)
 
58
 
 
59
    def apply(self, filename, conflict_handler, reverse=False):
 
60
        """Applies the patch to the specified file.
 
61
 
 
62
        :param filename: the file to apply the patch to
 
63
        :type filename: str
 
64
        :param reverse: If true, apply the patch in reverse
 
65
        :type reverse: bool
 
66
        """
 
67
        input_name = filename+".orig"
 
68
        try:
 
69
            os.rename(filename, input_name)
 
70
        except OSError, e:
 
71
            if e.errno != errno.ENOENT:
 
72
                raise
 
73
            if conflict_handler.patch_target_missing(filename, self.contents)\
 
74
                == "skip":
 
75
                return
 
76
            os.rename(filename, input_name)
 
77
            
 
78
 
 
79
        status = patch.patch(self.contents, input_name, filename, 
 
80
                                    reverse)
 
81
        os.chmod(filename, os.stat(input_name).st_mode)
 
82
        if status == 0:
 
83
            os.unlink(input_name)
 
84
        elif status == 1:
 
85
            conflict_handler.failed_hunks(filename)
 
86
 
 
87
        
 
88
class ChangeUnixPermissions(object):
53
89
    """This is two-way change, suitable for file modification, creation,
54
90
    deletion"""
55
 
    def __init__(self, old_exec_flag, new_exec_flag):
56
 
        self.old_exec_flag = old_exec_flag
57
 
        self.new_exec_flag = new_exec_flag
 
91
    def __init__(self, old_mode, new_mode):
 
92
        self.old_mode = old_mode
 
93
        self.new_mode = new_mode
58
94
 
59
95
    def apply(self, filename, conflict_handler, reverse=False):
60
96
        if not reverse:
61
 
            from_exec_flag = self.old_exec_flag
62
 
            to_exec_flag = self.new_exec_flag
 
97
            from_mode = self.old_mode
 
98
            to_mode = self.new_mode
63
99
        else:
64
 
            from_exec_flag = self.new_exec_flag
65
 
            to_exec_flag = self.old_exec_flag
 
100
            from_mode = self.new_mode
 
101
            to_mode = self.old_mode
66
102
        try:
67
 
            current_exec_flag = bool(os.stat(filename).st_mode & 0111)
 
103
            current_mode = os.stat(filename).st_mode &0777
68
104
        except OSError, e:
69
105
            if e.errno == errno.ENOENT:
70
 
                if conflict_handler.missing_for_exec_flag(filename) == "skip":
 
106
                if conflict_handler.missing_for_chmod(filename) == "skip":
71
107
                    return
72
108
                else:
73
 
                    current_exec_flag = from_exec_flag
 
109
                    current_mode = from_mode
74
110
 
75
 
        if from_exec_flag is not None and current_exec_flag != from_exec_flag:
76
 
            if conflict_handler.wrong_old_exec_flag(filename,
77
 
                        from_exec_flag, current_exec_flag) != "continue":
 
111
        if from_mode is not None and current_mode != from_mode:
 
112
            if conflict_handler.wrong_old_perms(filename, from_mode, 
 
113
                                                current_mode) != "continue":
78
114
                return
79
115
 
80
 
        if to_exec_flag is not None:
81
 
            current_mode = os.stat(filename).st_mode
82
 
            if to_exec_flag:
83
 
                umask = os.umask(0)
84
 
                os.umask(umask)
85
 
                to_mode = current_mode | (0100 & ~umask)
86
 
                # Enable x-bit for others only if they can read it.
87
 
                if current_mode & 0004:
88
 
                    to_mode |= 0001 & ~umask
89
 
                if current_mode & 0040:
90
 
                    to_mode |= 0010 & ~umask
91
 
            else:
92
 
                to_mode = current_mode & ~0111
 
116
        if to_mode is not None:
93
117
            try:
94
118
                os.chmod(filename, to_mode)
95
119
            except IOError, e:
96
120
                if e.errno == errno.ENOENT:
97
 
                    conflict_handler.missing_for_exec_flag(filename)
 
121
                    conflict_handler.missing_for_chmod(filename)
98
122
 
99
123
    def __eq__(self, other):
100
 
        return (isinstance(other, ChangeExecFlag) and
101
 
                self.old_exec_flag == other.old_exec_flag and
102
 
                self.new_exec_flag == other.new_exec_flag)
 
124
        if not isinstance(other, ChangeUnixPermissions):
 
125
            return False
 
126
        elif self.old_mode != other.old_mode:
 
127
            return False
 
128
        elif self.new_mode != other.new_mode:
 
129
            return False
 
130
        else:
 
131
            return True
103
132
 
104
133
    def __ne__(self, other):
105
134
        return not (self == other)
106
135
 
107
 
 
108
136
def dir_create(filename, conflict_handler, reverse):
109
137
    """Creates the directory, or deletes it if reverse is true.  Intended to be
110
138
    used with ReplaceContents.
130
158
        try:
131
159
            os.rmdir(filename)
132
160
        except OSError, e:
133
 
            if e.errno != errno.ENOTEMPTY:
 
161
            if e.errno != 39:
134
162
                raise
135
163
            if conflict_handler.rmdir_non_empty(filename) == "skip":
136
164
                return
137
165
            os.rmdir(filename)
138
166
 
 
167
                
 
168
            
139
169
 
140
170
class SymlinkCreate(object):
141
171
    """Creates or deletes a symlink (for use with ReplaceContents)"""
147
177
        """
148
178
        self.target = contents
149
179
 
150
 
    def __repr__(self):
151
 
        return "SymlinkCreate(%s)" % self.target
152
 
 
153
180
    def __call__(self, filename, conflict_handler, reverse):
154
181
        """Creates or destroys the symlink.
155
182
 
237
264
 
238
265
                    
239
266
 
240
 
class TreeFileCreate(object):
241
 
    """Create or delete a file (for use with ReplaceContents)"""
242
 
    def __init__(self, tree, file_id):
243
 
        """Constructor
244
 
 
245
 
        :param contents: The contents of the file to write
246
 
        :type contents: str
247
 
        """
248
 
        self.tree = tree
249
 
        self.file_id = file_id
250
 
 
251
 
    def __repr__(self):
252
 
        return "TreeFileCreate(%s)" % self.file_id
253
 
 
254
 
    def __eq__(self, other):
255
 
        if not isinstance(other, TreeFileCreate):
256
 
            return False
257
 
        return self.tree.get_file_sha1(self.file_id) == \
258
 
            other.tree.get_file_sha1(other.file_id)
259
 
 
260
 
    def __ne__(self, other):
261
 
        return not (self == other)
262
 
 
263
 
    def write_file(self, filename):
264
 
        outfile = file(filename, "wb")
265
 
        for line in self.tree.get_file(self.file_id):
266
 
            outfile.write(line)
267
 
 
268
 
    def same_text(self, filename):
269
 
        in_file = file(filename, "rb")
270
 
        return sha_file(in_file) == self.tree.get_file_sha1(self.file_id)
271
 
 
272
 
    def __call__(self, filename, conflict_handler, reverse):
273
 
        """Create or delete a file
274
 
 
275
 
        :param filename: The name of the file to create
276
 
        :type filename: str
277
 
        :param reverse: Delete the file instead of creating it
278
 
        :type reverse: bool
279
 
        """
280
 
        if not reverse:
281
 
            try:
282
 
                self.write_file(filename)
283
 
            except IOError, e:
284
 
                if e.errno == errno.ENOENT:
285
 
                    if conflict_handler.missing_parent(filename)=="continue":
286
 
                        self.write_file(filename)
287
 
                else:
288
 
                    raise
289
 
 
290
 
        else:
291
 
            try:
292
 
                if not self.same_text(filename):
293
 
                    direction = conflict_handler.wrong_old_contents(filename,
294
 
                        self.tree.get_file(self.file_id).read())
295
 
                    if  direction != "continue":
296
 
                        return
297
 
                os.unlink(filename)
298
 
            except IOError, e:
299
 
                if e.errno != errno.ENOENT:
300
 
                    raise
301
 
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
302
 
                    return
303
 
 
304
 
                    
305
 
 
306
267
def reversed(sequence):
307
268
    max = len(sequence) - 1
308
269
    for i in range(len(sequence)):
375
336
            if mode is not None:
376
337
                os.chmod(filename, mode)
377
338
 
378
 
    def is_creation(self):
379
 
        return self.new_contents is not None and self.old_contents is None
380
 
 
381
 
    def is_deletion(self):
382
 
        return self.old_contents is not None and self.new_contents is None
383
 
 
384
339
class ApplySequence(object):
385
340
    def __init__(self, changes=None):
386
341
        self.changes = []
412
367
 
413
368
 
414
369
class Diff3Merge(object):
415
 
    history_based = False
416
 
    def __init__(self, file_id, base, other):
417
 
        self.file_id = file_id
418
 
        self.base = base
419
 
        self.other = other
420
 
 
421
 
    def is_creation(self):
422
 
        return False
423
 
 
424
 
    def is_deletion(self):
425
 
        return False
 
370
    def __init__(self, base_file, other_file):
 
371
        self.base_file = base_file
 
372
        self.other_file = other_file
426
373
 
427
374
    def __eq__(self, other):
428
375
        if not isinstance(other, Diff3Merge):
429
376
            return False
430
 
        return (self.base == other.base and 
431
 
                self.other == other.other and self.file_id == other.file_id)
 
377
        return (self.base_file == other.base_file and 
 
378
                self.other_file == other.other_file)
432
379
 
433
380
    def __ne__(self, other):
434
381
        return not (self == other)
435
382
 
436
 
    def dump_file(self, temp_dir, name, tree):
437
 
        out_path = os.path.join(temp_dir, name)
438
 
        out_file = file(out_path, "wb")
439
 
        in_file = tree.get_file(self.file_id)
440
 
        for line in in_file:
441
 
            out_file.write(line)
442
 
        return out_path
443
 
 
444
383
    def apply(self, filename, conflict_handler, reverse=False):
445
 
        import bzrlib.patch
446
 
        temp_dir = mkdtemp(prefix="bzr-")
447
 
        try:
448
 
            new_file = filename+".new"
449
 
            base_file = self.dump_file(temp_dir, "base", self.base)
450
 
            other_file = self.dump_file(temp_dir, "other", self.other)
451
 
            if not reverse:
452
 
                base = base_file
453
 
                other = other_file
454
 
            else:
455
 
                base = other_file
456
 
                other = base_file
457
 
            status = bzrlib.patch.diff3(new_file, filename, base, other)
458
 
            if status == 0:
459
 
                os.chmod(new_file, os.stat(filename).st_mode)
460
 
                rename(new_file, filename)
461
 
                return
462
 
            else:
463
 
                assert(status == 1)
464
 
                def get_lines(filename):
465
 
                    my_file = file(filename, "rb")
466
 
                    lines = my_file.readlines()
467
 
                    my_file.close()
468
 
                    return lines
469
 
                base_lines = get_lines(base)
470
 
                other_lines = get_lines(other)
471
 
                conflict_handler.merge_conflict(new_file, filename, base_lines, 
472
 
                                                other_lines)
473
 
        finally:
474
 
            rmtree(temp_dir)
 
384
        new_file = filename+".new" 
 
385
        if not reverse:
 
386
            base = self.base_file
 
387
            other = self.other_file
 
388
        else:
 
389
            base = self.other_file
 
390
            other = self.base_file
 
391
        status = patch.diff3(new_file, filename, base, other)
 
392
        if status == 0:
 
393
            os.chmod(new_file, os.stat(filename).st_mode)
 
394
            os.rename(new_file, filename)
 
395
            return
 
396
        else:
 
397
            assert(status == 1)
 
398
            conflict_handler.merge_conflict(new_file, filename, base, other)
475
399
 
476
400
 
477
401
def CreateDir():
510
434
    """
511
435
    return ReplaceContents(FileCreate(contents), None)
512
436
 
513
 
def ReplaceFileContents(old_tree, new_tree, file_id):
 
437
def ReplaceFileContents(old_contents, new_contents):
514
438
    """Convenience fucntion to replace the contents of a file.
515
439
    
516
440
    :param old_contents: The contents of the file to replace 
520
444
    :return: A ReplaceContents that will replace the contents of a file a file 
521
445
    :rtype: `ReplaceContents`
522
446
    """
523
 
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
524
 
                           TreeFileCreate(new_tree, file_id))
 
447
    return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
525
448
 
526
449
def CreateSymlink(target):
527
450
    """Convenience fucntion to create a symlink.
633
556
        if self.id  == self.parent:
634
557
            raise ParentIDIsSelf(self)
635
558
 
636
 
    def __repr__(self):
 
559
    def __str__(self):
637
560
        return "ChangesetEntry(%s)" % self.id
638
561
 
639
 
    __str__ = __repr__
640
 
 
641
562
    def __get_dir(self):
642
563
        if self.path is None:
643
564
            return None
692
613
        :param reverse: if true, the changeset is being applied in reverse
693
614
        :rtype: bool
694
615
        """
695
 
        return self.is_creation(not reverse)
 
616
        return ((self.new_parent is None and not reverse) or 
 
617
                (self.parent is None and reverse))
696
618
 
697
619
    def is_creation(self, reverse):
698
620
        """Return true if applying the entry would create a file/directory.
700
622
        :param reverse: if true, the changeset is being applied in reverse
701
623
        :rtype: bool
702
624
        """
703
 
        if self.contents_change is None:
704
 
            return False
705
 
        if reverse:
706
 
            return self.contents_change.is_deletion()
707
 
        else:
708
 
            return self.contents_change.is_creation()
 
625
        return ((self.parent is None and not reverse) or 
 
626
                (self.new_parent is None and reverse))
709
627
 
710
628
    def is_creation_or_deletion(self):
711
629
        """Return true if applying the entry would create or delete a 
713
631
 
714
632
        :rtype: bool
715
633
        """
716
 
        return self.is_creation(False) or self.is_deletion(False)
 
634
        return self.parent is None or self.new_parent is None
717
635
 
718
636
    def get_cset_path(self, mod=False):
719
637
        """Determine the path of the entry according to the changeset.
739
657
                return None
740
658
            return self.path
741
659
 
742
 
    def summarize_name(self, reverse=False):
 
660
    def summarize_name(self, changeset, reverse=False):
743
661
        """Produce a one-line summary of the filename.  Indicates renames as
744
662
        old => new, indicates creation as None => new, indicates deletion as
745
663
        old => None.
752
670
        """
753
671
        orig_path = self.get_cset_path(False)
754
672
        mod_path = self.get_cset_path(True)
755
 
        if orig_path and orig_path.startswith('./'):
 
673
        if orig_path is not None:
756
674
            orig_path = orig_path[2:]
757
 
        if mod_path and mod_path.startswith('./'):
 
675
        if mod_path is not None:
758
676
            mod_path = mod_path[2:]
759
677
        if orig_path == mod_path:
760
678
            return orig_path
776
694
        :type reverse: bool
777
695
        :rtype: str
778
696
        """
779
 
        mutter("Finding new path for %s", self.summarize_name())
 
697
        mutter("Finding new path for %s" % self.summarize_name(changeset))
780
698
        if reverse:
781
699
            parent = self.parent
782
700
            to_dir = self.dir
798
716
                raise SourceRootHasName(self, to_name)
799
717
            else:
800
718
                return '.'
801
 
        parent_entry = changeset.entries.get(parent)
802
 
        if parent_entry is None:
 
719
        if from_dir == to_dir:
803
720
            dir = os.path.dirname(id_map[self.id])
804
721
        else:
805
 
            mutter("path, new_path: %r %r", self.path, self.new_path)
 
722
            mutter("path, new_path: %r %r" % (self.path, self.new_path))
 
723
            parent_entry = changeset.entries[parent]
806
724
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
807
725
        if from_name == to_name:
808
726
            name = os.path.basename(id_map[self.id])
891
809
    :rtype: (List, List)
892
810
    """
893
811
    source_entries = [x for x in changeset.entries.itervalues() 
894
 
                      if x.needs_rename() or x.is_creation_or_deletion()]
 
812
                      if x.needs_rename()]
895
813
    # these are done from longest path to shortest, to avoid deleting a
896
814
    # parent before its children are deleted/renamed 
897
815
    def longest_to_shortest(entry):
938
856
            entry.apply(path, conflict_handler, reverse)
939
857
            temp_name[entry.id] = None
940
858
 
941
 
        elif entry.needs_rename():
942
 
            if entry.is_creation(reverse):
943
 
                continue
 
859
        else:
944
860
            to_name = os.path.join(temp_dir, str(i))
945
861
            src_path = inventory.get(entry.id)
946
862
            if src_path is not None:
947
863
                src_path = os.path.join(dir, src_path)
948
864
                try:
949
 
                    rename(src_path, to_name)
 
865
                    os.rename(src_path, to_name)
950
866
                    temp_name[entry.id] = to_name
951
867
                except OSError, e:
952
868
                    if e.errno != errno.ENOENT:
953
869
                        raise
954
 
                    if conflict_handler.missing_for_rename(src_path, to_name) \
955
 
                        == "skip":
 
870
                    if conflict_handler.missing_for_rename(src_path) == "skip":
956
871
                        continue
957
872
 
958
873
    return temp_name
979
894
            continue
980
895
        new_path = os.path.join(dir, new_tree_path)
981
896
        old_path = changed_inventory.get(entry.id)
982
 
        if bzrlib.osutils.lexists(new_path):
 
897
        if os.path.exists(new_path):
983
898
            if conflict_handler.target_exists(entry, new_path, old_path) == \
984
899
                "skip":
985
900
                continue
986
901
        if entry.is_creation(reverse):
987
902
            entry.apply(new_path, conflict_handler, reverse)
988
903
            changed_inventory[entry.id] = new_tree_path
989
 
        elif entry.needs_rename():
990
 
            if entry.is_deletion(reverse):
991
 
                continue
 
904
        else:
992
905
            if old_path is None:
993
906
                continue
994
907
            try:
995
 
                mutter('rename %s to final name %s', old_path, new_path)
996
 
                rename(old_path, new_path)
 
908
                os.rename(old_path, new_path)
997
909
                changed_inventory[entry.id] = new_tree_path
998
910
            except OSError, e:
999
 
                raise BzrCheckError('failed to rename %s to %s for changeset entry %s: %s'
1000
 
                        % (old_path, new_path, entry, e))
 
911
                raise Exception ("%s is missing" % new_path)
1001
912
 
1002
913
class TargetExists(Exception):
1003
914
    def __init__(self, entry, target):
1035
946
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
1036
947
        self.this_path = this_path
1037
948
 
 
949
class MergePermissionConflict(Exception):
 
950
    def __init__(self, this_path, base_path, other_path):
 
951
        this_perms = os.stat(this_path).st_mode & 0755
 
952
        base_perms = os.stat(base_path).st_mode & 0755
 
953
        other_perms = os.stat(other_path).st_mode & 0755
 
954
        msg = """Conflicting permission for %s
 
955
this: %o
 
956
base: %o
 
957
other: %o
 
958
        """ % (this_path, this_perms, base_perms, other_perms)
 
959
        self.this_path = this_path
 
960
        self.base_path = base_path
 
961
        self.other_path = other_path
 
962
        Exception.__init__(self, msg)
 
963
 
1038
964
class WrongOldContents(Exception):
1039
965
    def __init__(self, filename):
1040
966
        msg = "Contents mismatch deleting %s" % filename
1041
967
        self.filename = filename
1042
968
        Exception.__init__(self, msg)
1043
969
 
1044
 
class WrongOldExecFlag(Exception):
1045
 
    def __init__(self, filename, old_exec_flag, new_exec_flag):
1046
 
        msg = "Executable flag missmatch on %s:\n" \
1047
 
        "Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
 
970
class WrongOldPermissions(Exception):
 
971
    def __init__(self, filename, old_perms, new_perms):
 
972
        msg = "Permission missmatch on %s:\n" \
 
973
        "Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
1048
974
        self.filename = filename
1049
975
        Exception.__init__(self, msg)
1050
976
 
1068
994
        Exception.__init__(self, msg)
1069
995
        self.filename = filename
1070
996
 
1071
 
class MissingForSetExec(Exception):
 
997
class MissingPermsFile(Exception):
1072
998
    def __init__(self, filename):
1073
999
        msg = "Attempt to change permissions on  %s, which does not exist" %\
1074
1000
            filename
1083
1009
 
1084
1010
 
1085
1011
class MissingForRename(Exception):
1086
 
    def __init__(self, filename, to_path):
1087
 
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
 
1012
    def __init__(self, filename):
 
1013
        msg = "Attempt to move missing path %s" % (filename)
1088
1014
        Exception.__init__(self, msg)
1089
1015
        self.filename = filename
1090
1016
 
1093
1019
        msg = "Conflicting contents for new file %s" % (filename)
1094
1020
        Exception.__init__(self, msg)
1095
1021
 
1096
 
class WeaveMergeConflict(Exception):
1097
 
    def __init__(self, filename):
1098
 
        msg = "Conflicting contents for file %s" % (filename)
1099
 
        Exception.__init__(self, msg)
1100
 
 
1101
 
class ThreewayContentsConflict(Exception):
1102
 
    def __init__(self, filename):
1103
 
        msg = "Conflicting contents for file %s" % (filename)
1104
 
        Exception.__init__(self, msg)
1105
 
 
1106
1022
 
1107
1023
class MissingForMerge(Exception):
1108
1024
    def __init__(self, filename):
1112
1028
 
1113
1029
 
1114
1030
class ExceptionConflictHandler(object):
1115
 
    """Default handler for merge exceptions.
1116
 
 
1117
 
    This throws an error on any kind of conflict.  Conflict handlers can
1118
 
    descend from this class if they have a better way to handle some or
1119
 
    all types of conflict.
1120
 
    """
 
1031
    def __init__(self, dir):
 
1032
        self.dir = dir
 
1033
    
1121
1034
    def missing_parent(self, pathname):
1122
1035
        parent = os.path.dirname(pathname)
1123
1036
        raise Exception("Parent directory missing for %s" % pathname)
1134
1047
    def rename_conflict(self, id, this_name, base_name, other_name):
1135
1048
        raise RenameConflict(id, this_name, base_name, other_name)
1136
1049
 
1137
 
    def move_conflict(self, id, this_dir, base_dir, other_dir):
 
1050
    def move_conflict(self, id, inventory):
 
1051
        this_dir = inventory.this.get_dir(id)
 
1052
        base_dir = inventory.base.get_dir(id)
 
1053
        other_dir = inventory.other.get_dir(id)
1138
1054
        raise MoveConflict(id, this_dir, base_dir, other_dir)
1139
1055
 
1140
 
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
 
1056
    def merge_conflict(self, new_file, this_path, base_path, other_path):
1141
1057
        os.unlink(new_file)
1142
1058
        raise MergeConflict(this_path)
1143
1059
 
 
1060
    def permission_conflict(self, this_path, base_path, other_path):
 
1061
        raise MergePermissionConflict(this_path, base_path, other_path)
 
1062
 
1144
1063
    def wrong_old_contents(self, filename, expected_contents):
1145
1064
        raise WrongOldContents(filename)
1146
1065
 
1147
1066
    def rem_contents_conflict(self, filename, this_contents, base_contents):
1148
1067
        raise RemoveContentsConflict(filename)
1149
1068
 
1150
 
    def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1151
 
        raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
 
1069
    def wrong_old_perms(self, filename, old_perms, new_perms):
 
1070
        raise WrongOldPermissions(filename, old_perms, new_perms)
1152
1071
 
1153
1072
    def rmdir_non_empty(self, filename):
1154
1073
        raise DeletingNonEmptyDirectory(filename)
1159
1078
    def patch_target_missing(self, filename, contents):
1160
1079
        raise PatchTargetMissing(filename)
1161
1080
 
1162
 
    def missing_for_exec_flag(self, filename):
1163
 
        raise MissingForExecFlag(filename)
 
1081
    def missing_for_chmod(self, filename):
 
1082
        raise MissingPermsFile(filename)
1164
1083
 
1165
1084
    def missing_for_rm(self, filename, change):
1166
1085
        raise MissingForRm(filename)
1167
1086
 
1168
 
    def missing_for_rename(self, filename, to_path):
1169
 
        raise MissingForRename(filename, to_path)
 
1087
    def missing_for_rename(self, filename):
 
1088
        raise MissingForRename(filename)
1170
1089
 
1171
 
    def missing_for_merge(self, file_id, other_path):
1172
 
        raise MissingForMerge(other_path)
 
1090
    def missing_for_merge(self, file_id, inventory):
 
1091
        raise MissingForMerge(inventory.other.get_path(file_id))
1173
1092
 
1174
1093
    def new_contents_conflict(self, filename, other_contents):
1175
1094
        raise NewContentsConflict(filename)
1176
1095
 
1177
 
    def weave_merge_conflict(self, filename, weave, other_i, out_file):
1178
 
        raise WeaveMergeConflict(filename)
1179
 
 
1180
 
    def threeway_contents_conflict(self, filename, this_contents,
1181
 
                                   base_contents, other_contents):
1182
 
        raise ThreewayContentsConflict(filename)
1183
 
 
1184
 
    def finalize(self):
 
1096
    def finalize():
1185
1097
        pass
1186
1098
 
1187
1099
def apply_changeset(changeset, inventory, dir, conflict_handler=None, 
1200
1112
    :rtype: Dictionary
1201
1113
    """
1202
1114
    if conflict_handler is None:
1203
 
        conflict_handler = ExceptionConflictHandler()
 
1115
        conflict_handler = ExceptionConflictHandler(dir)
1204
1116
    temp_dir = os.path.join(dir, "bzr-tree-change")
1205
1117
    try:
1206
1118
        os.mkdir(temp_dir)
1217
1129
    
1218
1130
    #apply changes that don't affect filenames
1219
1131
    for entry in changeset.entries.itervalues():
1220
 
        if not entry.is_creation_or_deletion() and not entry.is_boring():
1221
 
            if entry.id not in inventory:
1222
 
                warning("entry {%s} no longer present, can't be updated",
1223
 
                        entry.id)
1224
 
                continue
 
1132
        if not entry.is_creation_or_deletion():
1225
1133
            path = os.path.join(dir, inventory[entry.id])
1226
1134
            entry.apply(path, conflict_handler, reverse)
1227
1135
 
1246
1154
    r_inventory = {}
1247
1155
    for entry in tree.source_inventory().itervalues():
1248
1156
        inventory[entry.id] = entry.path
1249
 
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
 
1157
    new_inventory = apply_changeset(cset, r_inventory, tree.root,
1250
1158
                                    reverse=reverse)
1251
1159
    new_entries, remove_entries = \
1252
1160
        get_inventory_change(inventory, new_inventory, cset, reverse)
1387
1295
        return new_meta
1388
1296
    elif new_meta is None:
1389
1297
        return old_meta
1390
 
    elif (isinstance(old_meta, ChangeExecFlag) and
1391
 
          isinstance(new_meta, ChangeExecFlag)):
1392
 
        return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
 
1298
    elif isinstance(old_meta, ChangeUnixPermissions) and \
 
1299
        isinstance(new_meta, ChangeUnixPermissions):
 
1300
        return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1393
1301
    else:
1394
1302
        return ApplySequence(old_meta, new_meta)
1395
1303
 
1400
1308
            return False
1401
1309
    return True
1402
1310
 
1403
 
class UnsupportedFiletype(Exception):
1404
 
    def __init__(self, kind, full_path):
1405
 
        msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1406
 
            % (full_path, kind)
 
1311
class UnsuppportedFiletype(Exception):
 
1312
    def __init__(self, full_path, stat_result):
 
1313
        msg = "The file \"%s\" is not a supported filetype." % full_path
1407
1314
        Exception.__init__(self, msg)
1408
1315
        self.full_path = full_path
1409
 
        self.kind = kind
1410
 
 
1411
 
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1412
 
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1413
 
 
 
1316
        self.stat_result = stat_result
 
1317
 
 
1318
def generate_changeset(tree_a, tree_b, inventory_a=None, inventory_b=None):
 
1319
    return ChangesetGenerator(tree_a, tree_b, inventory_a, inventory_b)()
1414
1320
 
1415
1321
class ChangesetGenerator(object):
1416
 
    def __init__(self, tree_a, tree_b, interesting_ids=None):
 
1322
    def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
1417
1323
        object.__init__(self)
1418
1324
        self.tree_a = tree_a
1419
1325
        self.tree_b = tree_b
1420
 
        self._interesting_ids = interesting_ids
 
1326
        if inventory_a is not None:
 
1327
            self.inventory_a = inventory_a
 
1328
        else:
 
1329
            self.inventory_a = tree_a.inventory()
 
1330
        if inventory_b is not None:
 
1331
            self.inventory_b = inventory_b
 
1332
        else:
 
1333
            self.inventory_b = tree_b.inventory()
 
1334
        self.r_inventory_a = self.reverse_inventory(self.inventory_a)
 
1335
        self.r_inventory_b = self.reverse_inventory(self.inventory_b)
1421
1336
 
1422
 
    def iter_both_tree_ids(self):
1423
 
        for file_id in self.tree_a:
1424
 
            yield file_id
1425
 
        for file_id in self.tree_b:
1426
 
            if file_id not in self.tree_a:
1427
 
                yield file_id
 
1337
    def reverse_inventory(self, inventory):
 
1338
        r_inventory = {}
 
1339
        for entry in inventory.itervalues():
 
1340
            if entry.id is None:
 
1341
                continue
 
1342
            r_inventory[entry.id] = entry
 
1343
        return r_inventory
1428
1344
 
1429
1345
    def __call__(self):
1430
1346
        cset = Changeset()
1431
 
        for file_id in self.iter_both_tree_ids():
1432
 
            cs_entry = self.make_entry(file_id)
 
1347
        for entry in self.inventory_a.itervalues():
 
1348
            if entry.id is None:
 
1349
                continue
 
1350
            cs_entry = self.make_entry(entry.id)
1433
1351
            if cs_entry is not None and not cs_entry.is_boring():
1434
1352
                cset.add_entry(cs_entry)
1435
1353
 
 
1354
        for entry in self.inventory_b.itervalues():
 
1355
            if entry.id is None:
 
1356
                continue
 
1357
            if not self.r_inventory_a.has_key(entry.id):
 
1358
                cs_entry = self.make_entry(entry.id)
 
1359
                if cs_entry is not None and not cs_entry.is_boring():
 
1360
                    cset.add_entry(cs_entry)
1436
1361
        for entry in list(cset.entries.itervalues()):
1437
1362
            if entry.parent != entry.new_parent:
1438
1363
                if not cset.entries.has_key(entry.parent) and\
1446
1371
                    cset.add_entry(parent_entry)
1447
1372
        return cset
1448
1373
 
1449
 
    def iter_inventory(self, tree):
1450
 
        for file_id in tree:
1451
 
            yield self.get_entry(file_id, tree)
1452
 
 
1453
 
    def get_entry(self, file_id, tree):
1454
 
        if not tree.has_or_had_id(file_id):
1455
 
            return None
1456
 
        return tree.inventory[file_id]
1457
 
 
1458
 
    def get_entry_parent(self, entry):
1459
 
        if entry is None:
1460
 
            return None
1461
 
        return entry.parent_id
1462
 
 
1463
 
    def get_path(self, file_id, tree):
1464
 
        if not tree.has_or_had_id(file_id):
1465
 
            return None
1466
 
        path = tree.id2path(file_id)
1467
 
        if path == '':
1468
 
            return './.'
1469
 
        else:
1470
 
            return path
1471
 
 
1472
 
    def make_basic_entry(self, file_id, only_interesting):
1473
 
        entry_a = self.get_entry(file_id, self.tree_a)
1474
 
        entry_b = self.get_entry(file_id, self.tree_b)
 
1374
    def get_entry_parent(self, entry, inventory):
 
1375
        if entry is None:
 
1376
            return None
 
1377
        if entry.path == "./.":
 
1378
            return NULL_ID
 
1379
        dirname = os.path.dirname(entry.path)
 
1380
        if dirname == ".":
 
1381
            dirname = "./."
 
1382
        parent = inventory[dirname]
 
1383
        return parent.id
 
1384
 
 
1385
    def get_paths(self, entry, tree):
 
1386
        if entry is None:
 
1387
            return (None, None)
 
1388
        full_path = tree.readonly_path(entry.id)
 
1389
        if entry.path == ".":
 
1390
            return ("", full_path)
 
1391
        return (entry.path, full_path)
 
1392
 
 
1393
    def make_basic_entry(self, id, only_interesting):
 
1394
        entry_a = self.r_inventory_a.get(id)
 
1395
        entry_b = self.r_inventory_b.get(id)
1475
1396
        if only_interesting and not self.is_interesting(entry_a, entry_b):
1476
 
            return None
1477
 
        parent = self.get_entry_parent(entry_a)
1478
 
        path = self.get_path(file_id, self.tree_a)
1479
 
        cs_entry = ChangesetEntry(file_id, parent, path)
1480
 
        new_parent = self.get_entry_parent(entry_b)
1481
 
 
1482
 
        new_path = self.get_path(file_id, self.tree_b)
 
1397
            return (None, None, None)
 
1398
        parent = self.get_entry_parent(entry_a, self.inventory_a)
 
1399
        (path, full_path_a) = self.get_paths(entry_a, self.tree_a)
 
1400
        cs_entry = ChangesetEntry(id, parent, path)
 
1401
        new_parent = self.get_entry_parent(entry_b, self.inventory_b)
 
1402
 
 
1403
 
 
1404
        (new_path, full_path_b) = self.get_paths(entry_b, self.tree_b)
1483
1405
 
1484
1406
        cs_entry.new_path = new_path
1485
1407
        cs_entry.new_parent = new_parent
1486
 
        return cs_entry
 
1408
        return (cs_entry, full_path_a, full_path_b)
1487
1409
 
1488
1410
    def is_interesting(self, entry_a, entry_b):
1489
 
        if self._interesting_ids is None:
1490
 
            return True
1491
1411
        if entry_a is not None:
1492
 
            file_id = entry_a.file_id
1493
 
        elif entry_b is not None:
1494
 
            file_id = entry_b.file_id
1495
 
        else:
1496
 
            return False
1497
 
        return file_id in self._interesting_ids
 
1412
            if entry_a.interesting:
 
1413
                return True
 
1414
        if entry_b is not None:
 
1415
            if entry_b.interesting:
 
1416
                return True
 
1417
        return False
1498
1418
 
1499
1419
    def make_boring_entry(self, id):
1500
 
        cs_entry = self.make_basic_entry(id, only_interesting=False)
 
1420
        (cs_entry, full_path_a, full_path_b) = \
 
1421
            self.make_basic_entry(id, only_interesting=False)
1501
1422
        if cs_entry.is_creation_or_deletion():
1502
1423
            return self.make_entry(id, only_interesting=False)
1503
1424
        else:
1505
1426
        
1506
1427
 
1507
1428
    def make_entry(self, id, only_interesting=True):
1508
 
        cs_entry = self.make_basic_entry(id, only_interesting)
 
1429
        (cs_entry, full_path_a, full_path_b) = \
 
1430
            self.make_basic_entry(id, only_interesting)
1509
1431
 
1510
1432
        if cs_entry is None:
1511
1433
            return None
1512
 
 
1513
 
        cs_entry.metadata_change = self.make_exec_flag_change(id)
1514
 
 
1515
 
        if id in self.tree_a and id in self.tree_b:
1516
 
            a_sha1 = self.tree_a.get_file_sha1(id)
1517
 
            b_sha1 = self.tree_b.get_file_sha1(id)
1518
 
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1519
 
                return cs_entry
1520
 
 
1521
 
        cs_entry.contents_change = self.make_contents_change(id)
 
1434
       
 
1435
        stat_a = self.lstat(full_path_a)
 
1436
        stat_b = self.lstat(full_path_b)
 
1437
        if stat_b is None:
 
1438
            cs_entry.new_parent = None
 
1439
            cs_entry.new_path = None
 
1440
        
 
1441
        cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
 
1442
        cs_entry.contents_change = self.make_contents_change(full_path_a,
 
1443
                                                             stat_a, 
 
1444
                                                             full_path_b, 
 
1445
                                                             stat_b)
1522
1446
        return cs_entry
1523
1447
 
1524
 
    def make_exec_flag_change(self, file_id):
1525
 
        exec_flag_a = exec_flag_b = None
1526
 
        if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1527
 
            exec_flag_a = self.tree_a.is_executable(file_id)
1528
 
 
1529
 
        if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1530
 
            exec_flag_b = self.tree_b.is_executable(file_id)
1531
 
 
1532
 
        if exec_flag_a == exec_flag_b:
1533
 
            return None
1534
 
        return ChangeExecFlag(exec_flag_a, exec_flag_b)
1535
 
 
1536
 
    def make_contents_change(self, file_id):
1537
 
        a_contents = get_contents(self.tree_a, file_id)
1538
 
        b_contents = get_contents(self.tree_b, file_id)
 
1448
    def make_mode_change(self, stat_a, stat_b):
 
1449
        mode_a = None
 
1450
        if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
 
1451
            mode_a = stat_a.st_mode & 0777
 
1452
        mode_b = None
 
1453
        if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
 
1454
            mode_b = stat_b.st_mode & 0777
 
1455
        if mode_a == mode_b:
 
1456
            return None
 
1457
        return ChangeUnixPermissions(mode_a, mode_b)
 
1458
 
 
1459
    def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
 
1460
        if stat_a is None and stat_b is None:
 
1461
            return None
 
1462
        if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
 
1463
            stat.S_ISDIR(stat_b.st_mode):
 
1464
            return None
 
1465
        if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
 
1466
            stat.S_ISREG(stat_b.st_mode):
 
1467
            if stat_a.st_ino == stat_b.st_ino and \
 
1468
                stat_a.st_dev == stat_b.st_dev:
 
1469
                return None
 
1470
            if file(full_path_a, "rb").read() == \
 
1471
                file(full_path_b, "rb").read():
 
1472
                return None
 
1473
 
 
1474
            patch_contents = patch.diff(full_path_a, 
 
1475
                                        file(full_path_b, "rb").read())
 
1476
            if patch_contents is None:
 
1477
                return None
 
1478
            return PatchApply(patch_contents)
 
1479
 
 
1480
        a_contents = self.get_contents(stat_a, full_path_a)
 
1481
        b_contents = self.get_contents(stat_b, full_path_b)
1539
1482
        if a_contents == b_contents:
1540
1483
            return None
1541
1484
        return ReplaceContents(a_contents, b_contents)
1542
1485
 
 
1486
    def get_contents(self, stat_result, full_path):
 
1487
        if stat_result is None:
 
1488
            return None
 
1489
        elif stat.S_ISREG(stat_result.st_mode):
 
1490
            return FileCreate(file(full_path, "rb").read())
 
1491
        elif stat.S_ISDIR(stat_result.st_mode):
 
1492
            return dir_create
 
1493
        elif stat.S_ISLNK(stat_result.st_mode):
 
1494
            return SymlinkCreate(os.readlink(full_path))
 
1495
        else:
 
1496
            raise UnsupportedFiletype(full_path, stat_result)
1543
1497
 
1544
 
def get_contents(tree, file_id):
1545
 
    """Return the appropriate contents to create a copy of file_id from tree"""
1546
 
    if file_id not in tree:
1547
 
        return None
1548
 
    kind = tree.kind(file_id)
1549
 
    if kind == "file":
1550
 
        return TreeFileCreate(tree, file_id)
1551
 
    elif kind in ("directory", "root_directory"):
1552
 
        return dir_create
1553
 
    elif kind == "symlink":
1554
 
        return SymlinkCreate(tree.get_symlink_target(file_id))
1555
 
    else:
1556
 
        raise UnsupportedFiletype(kind, tree.id2path(file_id))
 
1498
    def lstat(self, full_path):
 
1499
        stat_result = None
 
1500
        if full_path is not None:
 
1501
            try:
 
1502
                stat_result = os.lstat(full_path)
 
1503
            except OSError, e:
 
1504
                if e.errno != errno.ENOENT:
 
1505
                    raise
 
1506
        return stat_result
1557
1507
 
1558
1508
 
1559
1509
def full_path(entry, tree):
1560
 
    return os.path.join(tree.basedir, entry.path)
 
1510
    return os.path.join(tree.root, entry.path)
1561
1511
 
1562
1512
def new_delete_entry(entry, tree, inventory, delete):
1563
1513
    if entry.path == "":
1579
1529
 
1580
1530
 
1581
1531
        
1582
 
# XXX: Can't we unify this with the regular inventory object
 
1532
    
1583
1533
class Inventory(object):
1584
1534
    def __init__(self, inventory):
1585
1535
        self.inventory = inventory