~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-07-11 03:42:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050711034253-412281abeb9f56ad
doc

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 bzrlib.trace import mutter
21
 
from bzrlib.osutils import rename
22
 
import bzrlib
23
 
 
24
 
# XXX: mbp: I'm not totally convinced that we should handle conflicts
25
 
# as part of changeset application, rather than only in the merge
26
 
# operation.
27
 
 
28
 
"""Represent and apply a changeset
29
 
 
30
 
Conflicts in applying a changeset are represented as exceptions.
31
 
"""
32
 
 
 
20
"""
 
21
Represent and apply a changeset
 
22
"""
33
23
__docformat__ = "restructuredtext"
34
24
 
35
25
NULL_ID = "!NULL"
44
34
        newdict[value] = key
45
35
    return newdict
46
36
 
47
 
       
 
37
 
 
38
class PatchApply(object):
 
39
    """Patch application as a kind of content change"""
 
40
    def __init__(self, contents):
 
41
        """Constructor.
 
42
 
 
43
        :param contents: The text of the patch to apply
 
44
        :type contents: str"""
 
45
        self.contents = contents
 
46
 
 
47
    def __eq__(self, other):
 
48
        if not isinstance(other, PatchApply):
 
49
            return False
 
50
        elif self.contents != other.contents:
 
51
            return False
 
52
        else:
 
53
            return True
 
54
 
 
55
    def __ne__(self, other):
 
56
        return not (self == other)
 
57
 
 
58
    def apply(self, filename, conflict_handler, reverse=False):
 
59
        """Applies the patch to the specified file.
 
60
 
 
61
        :param filename: the file to apply the patch to
 
62
        :type filename: str
 
63
        :param reverse: If true, apply the patch in reverse
 
64
        :type reverse: bool
 
65
        """
 
66
        input_name = filename+".orig"
 
67
        try:
 
68
            os.rename(filename, input_name)
 
69
        except OSError, e:
 
70
            if e.errno != errno.ENOENT:
 
71
                raise
 
72
            if conflict_handler.patch_target_missing(filename, self.contents)\
 
73
                == "skip":
 
74
                return
 
75
            os.rename(filename, input_name)
 
76
            
 
77
 
 
78
        status = patch.patch(self.contents, input_name, filename, 
 
79
                                    reverse)
 
80
        os.chmod(filename, os.stat(input_name).st_mode)
 
81
        if status == 0:
 
82
            os.unlink(input_name)
 
83
        elif status == 1:
 
84
            conflict_handler.failed_hunks(filename)
 
85
 
 
86
        
48
87
class ChangeUnixPermissions(object):
49
88
    """This is two-way change, suitable for file modification, creation,
50
89
    deletion"""
93
132
    def __ne__(self, other):
94
133
        return not (self == other)
95
134
 
96
 
 
97
135
def dir_create(filename, conflict_handler, reverse):
98
136
    """Creates the directory, or deletes it if reverse is true.  Intended to be
99
137
    used with ReplaceContents.
119
157
        try:
120
158
            os.rmdir(filename)
121
159
        except OSError, e:
122
 
            if e.errno != errno.ENOTEMPTY:
 
160
            if e.errno != 39:
123
161
                raise
124
162
            if conflict_handler.rmdir_non_empty(filename) == "skip":
125
163
                return
126
164
            os.rmdir(filename)
127
165
 
 
166
                
 
167
            
128
168
 
129
169
class SymlinkCreate(object):
130
170
    """Creates or deletes a symlink (for use with ReplaceContents)"""
326
366
 
327
367
 
328
368
class Diff3Merge(object):
329
 
    def __init__(self, file_id, base, other):
330
 
        self.file_id = file_id
331
 
        self.base = base
332
 
        self.other = other
 
369
    def __init__(self, base_file, other_file):
 
370
        self.base_file = base_file
 
371
        self.other_file = other_file
333
372
 
334
373
    def __eq__(self, other):
335
374
        if not isinstance(other, Diff3Merge):
336
375
            return False
337
 
        return (self.base == other.base and 
338
 
                self.other == other.other and self.file_id == other.file_id)
 
376
        return (self.base_file == other.base_file and 
 
377
                self.other_file == other.other_file)
339
378
 
340
379
    def __ne__(self, other):
341
380
        return not (self == other)
342
381
 
343
382
    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)
 
383
        new_file = filename+".new" 
347
384
        if not reverse:
348
 
            base = base_file
349
 
            other = other_file
 
385
            base = self.base_file
 
386
            other = self.other_file
350
387
        else:
351
 
            base = other_file
352
 
            other = base_file
 
388
            base = self.other_file
 
389
            other = self.base_file
353
390
        status = patch.diff3(new_file, filename, base, other)
354
391
        if status == 0:
355
392
            os.chmod(new_file, os.stat(filename).st_mode)
356
 
            rename(new_file, filename)
 
393
            os.rename(new_file, filename)
357
394
            return
358
395
        else:
359
396
            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)
 
397
            conflict_handler.merge_conflict(new_file, filename, base, other)
368
398
 
369
399
 
370
400
def CreateDir():
626
656
                return None
627
657
            return self.path
628
658
 
629
 
    def summarize_name(self, reverse=False):
 
659
    def summarize_name(self, changeset, reverse=False):
630
660
        """Produce a one-line summary of the filename.  Indicates renames as
631
661
        old => new, indicates creation as None => new, indicates deletion as
632
662
        old => None.
663
693
        :type reverse: bool
664
694
        :rtype: str
665
695
        """
666
 
        mutter("Finding new path for %s" % self.summarize_name())
667
696
        if reverse:
668
697
            parent = self.parent
669
698
            to_dir = self.dir
688
717
        if from_dir == to_dir:
689
718
            dir = os.path.dirname(id_map[self.id])
690
719
        else:
691
 
            mutter("path, new_path: %r %r" % (self.path, self.new_path))
692
720
            parent_entry = changeset.entries[parent]
693
721
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
694
722
        if from_name == to_name:
831
859
            if src_path is not None:
832
860
                src_path = os.path.join(dir, src_path)
833
861
                try:
834
 
                    rename(src_path, to_name)
 
862
                    os.rename(src_path, to_name)
835
863
                    temp_name[entry.id] = to_name
836
864
                except OSError, e:
837
865
                    if e.errno != errno.ENOENT:
863
891
            continue
864
892
        new_path = os.path.join(dir, new_tree_path)
865
893
        old_path = changed_inventory.get(entry.id)
866
 
        if bzrlib.osutils.lexists(new_path):
 
894
        if os.path.exists(new_path):
867
895
            if conflict_handler.target_exists(entry, new_path, old_path) == \
868
896
                "skip":
869
897
                continue
874
902
            if old_path is None:
875
903
                continue
876
904
            try:
877
 
                rename(old_path, new_path)
 
905
                os.rename(old_path, new_path)
878
906
                changed_inventory[entry.id] = new_tree_path
879
907
            except OSError, e:
880
908
                raise Exception ("%s is missing" % new_path)
997
1025
 
998
1026
 
999
1027
class ExceptionConflictHandler(object):
1000
 
    """Default handler for merge exceptions.
1001
 
 
1002
 
    This throws an error on any kind of conflict.  Conflict handlers can
1003
 
    descend from this class if they have a better way to handle some or
1004
 
    all types of conflict.
1005
 
    """
 
1028
    def __init__(self, dir):
 
1029
        self.dir = dir
 
1030
    
1006
1031
    def missing_parent(self, pathname):
1007
1032
        parent = os.path.dirname(pathname)
1008
1033
        raise Exception("Parent directory missing for %s" % pathname)
1019
1044
    def rename_conflict(self, id, this_name, base_name, other_name):
1020
1045
        raise RenameConflict(id, this_name, base_name, other_name)
1021
1046
 
1022
 
    def move_conflict(self, id, this_dir, base_dir, other_dir):
 
1047
    def move_conflict(self, id, inventory):
 
1048
        this_dir = inventory.this.get_dir(id)
 
1049
        base_dir = inventory.base.get_dir(id)
 
1050
        other_dir = inventory.other.get_dir(id)
1023
1051
        raise MoveConflict(id, this_dir, base_dir, other_dir)
1024
1052
 
1025
 
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
 
1053
    def merge_conflict(self, new_file, this_path, base_path, other_path):
1026
1054
        os.unlink(new_file)
1027
1055
        raise MergeConflict(this_path)
1028
1056
 
1056
1084
    def missing_for_rename(self, filename):
1057
1085
        raise MissingForRename(filename)
1058
1086
 
1059
 
    def missing_for_merge(self, file_id, other_path):
1060
 
        raise MissingForMerge(other_path)
 
1087
    def missing_for_merge(self, file_id, inventory):
 
1088
        raise MissingForMerge(inventory.other.get_path(file_id))
1061
1089
 
1062
1090
    def new_contents_conflict(self, filename, other_contents):
1063
1091
        raise NewContentsConflict(filename)
1064
1092
 
1065
 
    def finalize(self):
 
1093
    def finalize():
1066
1094
        pass
1067
1095
 
1068
1096
def apply_changeset(changeset, inventory, dir, conflict_handler=None, 
1081
1109
    :rtype: Dictionary
1082
1110
    """
1083
1111
    if conflict_handler is None:
1084
 
        conflict_handler = ExceptionConflictHandler()
 
1112
        conflict_handler = ExceptionConflictHandler(dir)
1085
1113
    temp_dir = os.path.join(dir, "bzr-tree-change")
1086
1114
    try:
1087
1115
        os.mkdir(temp_dir)
1284
1312
        self.full_path = full_path
1285
1313
        self.stat_result = stat_result
1286
1314
 
1287
 
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1288
 
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
 
1315
def generate_changeset(tree_a, tree_b, inventory_a=None, inventory_b=None):
 
1316
    return ChangesetGenerator(tree_a, tree_b, inventory_a, inventory_b)()
1289
1317
 
1290
1318
class ChangesetGenerator(object):
1291
 
    def __init__(self, tree_a, tree_b, interesting_ids=None):
 
1319
    def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
1292
1320
        object.__init__(self)
1293
1321
        self.tree_a = tree_a
1294
1322
        self.tree_b = tree_b
1295
 
        self._interesting_ids = interesting_ids
 
1323
        if inventory_a is not None:
 
1324
            self.inventory_a = inventory_a
 
1325
        else:
 
1326
            self.inventory_a = tree_a.inventory()
 
1327
        if inventory_b is not None:
 
1328
            self.inventory_b = inventory_b
 
1329
        else:
 
1330
            self.inventory_b = tree_b.inventory()
 
1331
        self.r_inventory_a = self.reverse_inventory(self.inventory_a)
 
1332
        self.r_inventory_b = self.reverse_inventory(self.inventory_b)
1296
1333
 
1297
 
    def iter_both_tree_ids(self):
1298
 
        for file_id in self.tree_a:
1299
 
            yield file_id
1300
 
        for file_id in self.tree_b:
1301
 
            if file_id not in self.tree_a:
1302
 
                yield file_id
 
1334
    def reverse_inventory(self, inventory):
 
1335
        r_inventory = {}
 
1336
        for entry in inventory.itervalues():
 
1337
            if entry.id is None:
 
1338
                continue
 
1339
            r_inventory[entry.id] = entry
 
1340
        return r_inventory
1303
1341
 
1304
1342
    def __call__(self):
1305
1343
        cset = Changeset()
1306
 
        for file_id in self.iter_both_tree_ids():
1307
 
            cs_entry = self.make_entry(file_id)
 
1344
        for entry in self.inventory_a.itervalues():
 
1345
            if entry.id is None:
 
1346
                continue
 
1347
            cs_entry = self.make_entry(entry.id)
1308
1348
            if cs_entry is not None and not cs_entry.is_boring():
1309
1349
                cset.add_entry(cs_entry)
1310
1350
 
 
1351
        for entry in self.inventory_b.itervalues():
 
1352
            if entry.id is None:
 
1353
                continue
 
1354
            if not self.r_inventory_a.has_key(entry.id):
 
1355
                cs_entry = self.make_entry(entry.id)
 
1356
                if cs_entry is not None and not cs_entry.is_boring():
 
1357
                    cset.add_entry(cs_entry)
1311
1358
        for entry in list(cset.entries.itervalues()):
1312
1359
            if entry.parent != entry.new_parent:
1313
1360
                if not cset.entries.has_key(entry.parent) and\
1321
1368
                    cset.add_entry(parent_entry)
1322
1369
        return cset
1323
1370
 
1324
 
    def iter_inventory(self, tree):
1325
 
        for file_id in tree:
1326
 
            yield self.get_entry(file_id, tree)
1327
 
 
1328
 
    def get_entry(self, file_id, tree):
1329
 
        if not tree.has_or_had_id(file_id):
1330
 
            return None
1331
 
        return tree.tree.inventory[file_id]
1332
 
 
1333
 
    def get_entry_parent(self, entry):
1334
 
        if entry is None:
1335
 
            return None
1336
 
        return entry.parent_id
1337
 
 
1338
 
    def get_path(self, file_id, tree):
1339
 
        if not tree.has_or_had_id(file_id):
1340
 
            return None
1341
 
        path = tree.id2path(file_id)
1342
 
        if path == '':
1343
 
            return './.'
1344
 
        else:
1345
 
            return path
1346
 
 
1347
 
    def make_basic_entry(self, file_id, only_interesting):
1348
 
        entry_a = self.get_entry(file_id, self.tree_a)
1349
 
        entry_b = self.get_entry(file_id, self.tree_b)
 
1371
    def get_entry_parent(self, entry, inventory):
 
1372
        if entry is None:
 
1373
            return None
 
1374
        if entry.path == "./.":
 
1375
            return NULL_ID
 
1376
        dirname = os.path.dirname(entry.path)
 
1377
        if dirname == ".":
 
1378
            dirname = "./."
 
1379
        parent = inventory[dirname]
 
1380
        return parent.id
 
1381
 
 
1382
    def get_paths(self, entry, tree):
 
1383
        if entry is None:
 
1384
            return (None, None)
 
1385
        full_path = tree.readonly_path(entry.id)
 
1386
        if entry.path == ".":
 
1387
            return ("", full_path)
 
1388
        return (entry.path, full_path)
 
1389
 
 
1390
    def make_basic_entry(self, id, only_interesting):
 
1391
        entry_a = self.r_inventory_a.get(id)
 
1392
        entry_b = self.r_inventory_b.get(id)
1350
1393
        if only_interesting and not self.is_interesting(entry_a, entry_b):
1351
 
            return None
1352
 
        parent = self.get_entry_parent(entry_a)
1353
 
        path = self.get_path(file_id, self.tree_a)
1354
 
        cs_entry = ChangesetEntry(file_id, parent, path)
1355
 
        new_parent = self.get_entry_parent(entry_b)
1356
 
 
1357
 
        new_path = self.get_path(file_id, self.tree_b)
 
1394
            return (None, None, None)
 
1395
        parent = self.get_entry_parent(entry_a, self.inventory_a)
 
1396
        (path, full_path_a) = self.get_paths(entry_a, self.tree_a)
 
1397
        cs_entry = ChangesetEntry(id, parent, path)
 
1398
        new_parent = self.get_entry_parent(entry_b, self.inventory_b)
 
1399
 
 
1400
 
 
1401
        (new_path, full_path_b) = self.get_paths(entry_b, self.tree_b)
1358
1402
 
1359
1403
        cs_entry.new_path = new_path
1360
1404
        cs_entry.new_parent = new_parent
1361
 
        return cs_entry
 
1405
        return (cs_entry, full_path_a, full_path_b)
1362
1406
 
1363
1407
    def is_interesting(self, entry_a, entry_b):
1364
 
        if self._interesting_ids is None:
1365
 
            return True
1366
1408
        if entry_a is not None:
1367
 
            file_id = entry_a.file_id
1368
 
        elif entry_b is not None:
1369
 
            file_id = entry_b.file_id
1370
 
        else:
1371
 
            return False
1372
 
        return file_id in self._interesting_ids
 
1409
            if entry_a.interesting:
 
1410
                return True
 
1411
        if entry_b is not None:
 
1412
            if entry_b.interesting:
 
1413
                return True
 
1414
        return False
1373
1415
 
1374
1416
    def make_boring_entry(self, id):
1375
 
        cs_entry = self.make_basic_entry(id, only_interesting=False)
 
1417
        (cs_entry, full_path_a, full_path_b) = \
 
1418
            self.make_basic_entry(id, only_interesting=False)
1376
1419
        if cs_entry.is_creation_or_deletion():
1377
1420
            return self.make_entry(id, only_interesting=False)
1378
1421
        else:
1380
1423
        
1381
1424
 
1382
1425
    def make_entry(self, id, only_interesting=True):
1383
 
        cs_entry = self.make_basic_entry(id, only_interesting)
 
1426
        (cs_entry, full_path_a, full_path_b) = \
 
1427
            self.make_basic_entry(id, only_interesting)
1384
1428
 
1385
1429
        if cs_entry is None:
1386
1430
            return None
1387
 
 
1388
 
        full_path_a = self.tree_a.readonly_path(id)
1389
 
        full_path_b = self.tree_b.readonly_path(id)
 
1431
       
1390
1432
        stat_a = self.lstat(full_path_a)
1391
1433
        stat_b = self.lstat(full_path_b)
1392
 
 
 
1434
        if stat_b is None:
 
1435
            cs_entry.new_parent = None
 
1436
            cs_entry.new_path = None
 
1437
        
1393
1438
        cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1394
 
 
1395
 
        if id in self.tree_a and id in self.tree_b:
1396
 
            a_sha1 = self.tree_a.get_file_sha1(id)
1397
 
            b_sha1 = self.tree_b.get_file_sha1(id)
1398
 
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1399
 
                return cs_entry
1400
 
 
1401
1439
        cs_entry.contents_change = self.make_contents_change(full_path_a,
1402
1440
                                                             stat_a, 
1403
1441
                                                             full_path_b, 
1426
1464
            if stat_a.st_ino == stat_b.st_ino and \
1427
1465
                stat_a.st_dev == stat_b.st_dev:
1428
1466
                return None
 
1467
            if file(full_path_a, "rb").read() == \
 
1468
                file(full_path_b, "rb").read():
 
1469
                return None
 
1470
 
 
1471
            patch_contents = patch.diff(full_path_a, 
 
1472
                                        file(full_path_b, "rb").read())
 
1473
            if patch_contents is None:
 
1474
                return None
 
1475
            return PatchApply(patch_contents)
1429
1476
 
1430
1477
        a_contents = self.get_contents(stat_a, full_path_a)
1431
1478
        b_contents = self.get_contents(stat_b, full_path_b)
1479
1526
 
1480
1527
 
1481
1528
        
1482
 
# XXX: Can't we unify this with the regular inventory object
 
1529
    
1483
1530
class Inventory(object):
1484
1531
    def __init__(self, inventory):
1485
1532
        self.inventory = inventory