~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-06-22 06:37:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050622063743-e395f04c4db8977f
- move old blackbox code from testbzr into bzrlib.selftest.blackbox

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
20
"""
22
21
Represent and apply a changeset
23
22
"""
25
24
 
26
25
NULL_ID = "!NULL"
27
26
 
28
 
class OldFailedTreeOp(Exception):
29
 
    def __init__(self):
30
 
        Exception.__init__(self, "bzr-tree-change contains files from a"
31
 
                           " previous failed merge operation.")
 
27
 
32
28
def invert_dict(dict):
33
29
    newdict = {}
34
30
    for (key,value) in dict.iteritems():
36
32
    return newdict
37
33
 
38
34
 
39
 
       
 
35
class PatchApply(object):
 
36
    """Patch application as a kind of content change"""
 
37
    def __init__(self, contents):
 
38
        """Constructor.
 
39
 
 
40
        :param contents: The text of the patch to apply
 
41
        :type contents: str"""
 
42
        self.contents = contents
 
43
 
 
44
    def __eq__(self, other):
 
45
        if not isinstance(other, PatchApply):
 
46
            return False
 
47
        elif self.contents != other.contents:
 
48
            return False
 
49
        else:
 
50
            return True
 
51
 
 
52
    def __ne__(self, other):
 
53
        return not (self == other)
 
54
 
 
55
    def apply(self, filename, conflict_handler, reverse=False):
 
56
        """Applies the patch to the specified file.
 
57
 
 
58
        :param filename: the file to apply the patch to
 
59
        :type filename: str
 
60
        :param reverse: If true, apply the patch in reverse
 
61
        :type reverse: bool
 
62
        """
 
63
        input_name = filename+".orig"
 
64
        try:
 
65
            os.rename(filename, input_name)
 
66
        except OSError, e:
 
67
            if e.errno != errno.ENOENT:
 
68
                raise
 
69
            if conflict_handler.patch_target_missing(filename, self.contents)\
 
70
                == "skip":
 
71
                return
 
72
            os.rename(filename, input_name)
 
73
            
 
74
 
 
75
        status = patch.patch(self.contents, input_name, filename, 
 
76
                                    reverse)
 
77
        os.chmod(filename, os.stat(input_name).st_mode)
 
78
        if status == 0:
 
79
            os.unlink(input_name)
 
80
        elif status == 1:
 
81
            conflict_handler.failed_hunks(filename)
 
82
 
 
83
        
40
84
class ChangeUnixPermissions(object):
41
85
    """This is two-way change, suitable for file modification, creation,
42
86
    deletion"""
646
690
        :type reverse: bool
647
691
        :rtype: str
648
692
        """
649
 
        mutter("Finding new path for %s" % self.summarize_name(changeset))
650
693
        if reverse:
651
694
            parent = self.parent
652
695
            to_dir = self.dir
671
714
        if from_dir == to_dir:
672
715
            dir = os.path.dirname(id_map[self.id])
673
716
        else:
674
 
            mutter("path, new_path: %r %r" % (self.path, self.new_path))
675
717
            parent_entry = changeset.entries[parent]
676
718
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
677
719
        if from_name == to_name:
784
826
    my_sort(target_entries, shortest_to_longest)
785
827
    return (source_entries, target_entries)
786
828
 
787
 
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
788
 
                          conflict_handler, reverse):
 
829
def rename_to_temp_delete(source_entries, inventory, dir, conflict_handler,
 
830
                          reverse):
789
831
    """Delete and rename entries as appropriate.  Entries are renamed to temp
790
 
    names.  A map of id -> temp name (or None, for deletions) is returned.
 
832
    names.  A map of id -> temp name is returned.
791
833
 
792
834
    :param source_entries: The entries to rename and delete
793
835
    :type source_entries: List of `ChangesetEntry`
800
842
    :return: a mapping of id to temporary name
801
843
    :rtype: Dictionary
802
844
    """
 
845
    temp_dir = os.path.join(dir, "temp")
803
846
    temp_name = {}
804
847
    for i in range(len(source_entries)):
805
848
        entry = source_entries[i]
806
849
        if entry.is_deletion(reverse):
807
850
            path = os.path.join(dir, inventory[entry.id])
808
851
            entry.apply(path, conflict_handler, reverse)
809
 
            temp_name[entry.id] = None
810
852
 
811
853
        else:
812
 
            to_name = os.path.join(temp_dir, str(i))
 
854
            to_name = temp_dir+"/"+str(i)
813
855
            src_path = inventory.get(entry.id)
814
856
            if src_path is not None:
815
857
                src_path = os.path.join(dir, src_path)
825
867
    return temp_name
826
868
 
827
869
 
828
 
def rename_to_new_create(changed_inventory, target_entries, inventory, 
829
 
                         changeset, dir, conflict_handler, reverse):
 
870
def rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
 
871
                         conflict_handler, reverse):
830
872
    """Rename entries with temp names to their final names, create new files.
831
873
 
832
 
    :param changed_inventory: A mapping of id to temporary name
833
 
    :type changed_inventory: Dictionary
 
874
    :param temp_name: A mapping of id to temporary name
 
875
    :type temp_name: Dictionary
834
876
    :param target_entries: The entries to apply changes to
835
877
    :type target_entries: List of `ChangesetEntry`
836
878
    :param changeset: The changeset to apply
841
883
    :type reverse: bool
842
884
    """
843
885
    for entry in target_entries:
844
 
        new_tree_path = entry.get_new_path(inventory, changeset, reverse)
845
 
        if new_tree_path is None:
 
886
        new_path = entry.get_new_path(inventory, changeset, reverse)
 
887
        if new_path is None:
846
888
            continue
847
 
        new_path = os.path.join(dir, new_tree_path)
848
 
        old_path = changed_inventory.get(entry.id)
 
889
        new_path = os.path.join(dir, new_path)
 
890
        old_path = temp_name.get(entry.id)
849
891
        if os.path.exists(new_path):
850
892
            if conflict_handler.target_exists(entry, new_path, old_path) == \
851
893
                "skip":
852
894
                continue
853
895
        if entry.is_creation(reverse):
854
896
            entry.apply(new_path, conflict_handler, reverse)
855
 
            changed_inventory[entry.id] = new_tree_path
856
897
        else:
857
898
            if old_path is None:
858
899
                continue
859
900
            try:
860
901
                os.rename(old_path, new_path)
861
 
                changed_inventory[entry.id] = new_tree_path
862
902
            except OSError, e:
863
903
                raise Exception ("%s is missing" % new_path)
864
904
 
966
1006
        Exception.__init__(self, msg)
967
1007
        self.filename = filename
968
1008
 
969
 
class NewContentsConflict(Exception):
970
 
    def __init__(self, filename):
971
 
        msg = "Conflicting contents for new file %s" % (filename)
972
 
        Exception.__init__(self, msg)
973
 
 
974
 
 
975
 
class MissingForMerge(Exception):
976
 
    def __init__(self, filename):
977
 
        msg = "The file %s was modified, but does not exist in this tree"\
978
 
            % (filename)
979
 
        Exception.__init__(self, msg)
980
 
 
981
 
 
982
1009
class ExceptionConflictHandler(object):
983
1010
    def __init__(self, dir):
984
1011
        self.dir = dir
1039
1066
    def missing_for_rename(self, filename):
1040
1067
        raise MissingForRename(filename)
1041
1068
 
1042
 
    def missing_for_merge(self, file_id, inventory):
1043
 
        raise MissingForMerge(inventory.other.get_path(file_id))
1044
 
 
1045
 
    def new_contents_conflict(self, filename, other_contents):
1046
 
        raise NewContentsConflict(filename)
1047
 
 
1048
1069
    def finalize():
1049
1070
        pass
1050
1071
 
1065
1086
    """
1066
1087
    if conflict_handler is None:
1067
1088
        conflict_handler = ExceptionConflictHandler(dir)
1068
 
    temp_dir = os.path.join(dir, "bzr-tree-change")
1069
 
    try:
1070
 
        os.mkdir(temp_dir)
1071
 
    except OSError, e:
1072
 
        if e.errno == errno.EEXIST:
1073
 
            try:
1074
 
                os.rmdir(temp_dir)
1075
 
            except OSError, e:
1076
 
                if e.errno == errno.ENOTEMPTY:
1077
 
                    raise OldFailedTreeOp()
1078
 
            os.mkdir(temp_dir)
1079
 
        else:
1080
 
            raise
 
1089
    temp_dir = dir+"/temp"
 
1090
    os.mkdir(temp_dir)
1081
1091
    
1082
1092
    #apply changes that don't affect filenames
1083
1093
    for entry in changeset.entries.itervalues():
1092
1102
    (source_entries, target_entries) = get_rename_entries(changeset, inventory,
1093
1103
                                                          reverse)
1094
1104
 
1095
 
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1096
 
                                              temp_dir, conflict_handler,
1097
 
                                              reverse)
 
1105
    temp_name = rename_to_temp_delete(source_entries, inventory, dir,
 
1106
                                      conflict_handler, reverse)
1098
1107
 
1099
 
    rename_to_new_create(changed_inventory, target_entries, inventory,
1100
 
                         changeset, dir, conflict_handler, reverse)
 
1108
    rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
 
1109
                         conflict_handler, reverse)
1101
1110
    os.rmdir(temp_dir)
1102
 
    return changed_inventory
 
1111
    r_inventory = invert_dict(inventory)
 
1112
    new_entries, removed_entries = get_inventory_change(inventory,
 
1113
    r_inventory, changeset, reverse)
 
1114
    new_inventory = {}
 
1115
    for path, file_id in new_entries.iteritems():
 
1116
        new_inventory[file_id] = path
 
1117
    for file_id in removed_entries:
 
1118
        new_inventory[file_id] = None
 
1119
    return new_inventory
1103
1120
 
1104
1121
 
1105
1122
def apply_changeset_tree(cset, tree, reverse=False):
1116
1133
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1117
1134
    new_entries = {}
1118
1135
    remove_entries = []
 
1136
    r_inventory = invert_dict(inventory)
 
1137
    r_new_inventory = invert_dict(new_inventory)
1119
1138
    for entry in cset.entries.itervalues():
1120
1139
        if entry.needs_rename():
1121
 
            new_path = entry.get_new_path(inventory, cset)
1122
 
            if new_path is None:
1123
 
                remove_entries.append(entry.id)
 
1140
            old_path = r_inventory.get(entry.id)
 
1141
            if old_path is not None:
 
1142
                remove_entries.append(old_path)
1124
1143
            else:
1125
 
                new_entries[new_path] = entry.id
 
1144
                new_path = entry.get_new_path(inventory, cset)
 
1145
                if new_path is not None:
 
1146
                    new_entries[new_path] = entry.id
1126
1147
    return new_entries, remove_entries
1127
1148
 
1128
1149
 
1334
1355
        parent = inventory[dirname]
1335
1356
        return parent.id
1336
1357
 
1337
 
    def get_path(self, entry, tree):
 
1358
    def get_paths(self, entry, tree):
1338
1359
        if entry is None:
1339
1360
            return (None, None)
 
1361
        full_path = tree.readonly_path(entry.id)
1340
1362
        if entry.path == ".":
1341
 
            return ""
1342
 
        return entry.path
 
1363
            return ("", full_path)
 
1364
        return (entry.path, full_path)
1343
1365
 
1344
1366
    def make_basic_entry(self, id, only_interesting):
1345
1367
        entry_a = self.r_inventory_a.get(id)
1346
1368
        entry_b = self.r_inventory_b.get(id)
1347
1369
        if only_interesting and not self.is_interesting(entry_a, entry_b):
1348
 
            return None
 
1370
            return (None, None, None)
1349
1371
        parent = self.get_entry_parent(entry_a, self.inventory_a)
1350
 
        path = self.get_path(entry_a, self.tree_a)
 
1372
        (path, full_path_a) = self.get_paths(entry_a, self.tree_a)
1351
1373
        cs_entry = ChangesetEntry(id, parent, path)
1352
1374
        new_parent = self.get_entry_parent(entry_b, self.inventory_b)
1353
1375
 
1354
1376
 
1355
 
        new_path = self.get_path(entry_b, self.tree_b)
 
1377
        (new_path, full_path_b) = self.get_paths(entry_b, self.tree_b)
1356
1378
 
1357
1379
        cs_entry.new_path = new_path
1358
1380
        cs_entry.new_parent = new_parent
1359
 
        return cs_entry
 
1381
        return (cs_entry, full_path_a, full_path_b)
1360
1382
 
1361
1383
    def is_interesting(self, entry_a, entry_b):
1362
1384
        if entry_a is not None:
1368
1390
        return False
1369
1391
 
1370
1392
    def make_boring_entry(self, id):
1371
 
        cs_entry = self.make_basic_entry(id, only_interesting=False)
 
1393
        (cs_entry, full_path_a, full_path_b) = \
 
1394
            self.make_basic_entry(id, only_interesting=False)
1372
1395
        if cs_entry.is_creation_or_deletion():
1373
1396
            return self.make_entry(id, only_interesting=False)
1374
1397
        else:
1376
1399
        
1377
1400
 
1378
1401
    def make_entry(self, id, only_interesting=True):
1379
 
        cs_entry = self.make_basic_entry(id, only_interesting)
 
1402
        (cs_entry, full_path_a, full_path_b) = \
 
1403
            self.make_basic_entry(id, only_interesting)
1380
1404
 
1381
1405
        if cs_entry is None:
1382
1406
            return None
1383
 
        if id in self.tree_a and id in self.tree_b:
1384
 
            a_sha1 = self.tree_a.get_file_sha1(id)
1385
 
            b_sha1 = self.tree_b.get_file_sha1(id)
1386
 
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1387
 
                return cs_entry
1388
 
 
1389
 
        full_path_a = self.tree_a.readonly_path(id)
1390
 
        full_path_b = self.tree_b.readonly_path(id)
 
1407
       
1391
1408
        stat_a = self.lstat(full_path_a)
1392
1409
        stat_b = self.lstat(full_path_b)
1393
1410
        if stat_b is None:
1423
1440
            if stat_a.st_ino == stat_b.st_ino and \
1424
1441
                stat_a.st_dev == stat_b.st_dev:
1425
1442
                return None
 
1443
            if file(full_path_a, "rb").read() == \
 
1444
                file(full_path_b, "rb").read():
 
1445
                return None
 
1446
 
 
1447
            patch_contents = patch.diff(full_path_a, 
 
1448
                                        file(full_path_b, "rb").read())
 
1449
            if patch_contents is None:
 
1450
                return None
 
1451
            return PatchApply(patch_contents)
1426
1452
 
1427
1453
        a_contents = self.get_contents(stat_a, full_path_a)
1428
1454
        b_contents = self.get_contents(stat_b, full_path_b)
1476
1502
 
1477
1503
 
1478
1504
        
1479
 
# XXX: Can't we unify this with the regular inventory object
 
1505
    
1480
1506
class Inventory(object):
1481
1507
    def __init__(self, inventory):
1482
1508
        self.inventory = inventory
1491
1517
        return self.inventory.get(id)
1492
1518
 
1493
1519
    def get_name(self, id):
1494
 
        path = self.get_path(id)
1495
 
        if path is None:
1496
 
            return None
1497
 
        else:
1498
 
            return os.path.basename(path)
 
1520
        return os.path.basename(self.get_path(id))
1499
1521
 
1500
1522
    def get_dir(self, id):
1501
1523
        path = self.get_path(id)
1502
1524
        if path == "":
1503
1525
            return None
1504
 
        if path is None:
1505
 
            return None
1506
1526
        return os.path.dirname(path)
1507
1527
 
1508
1528
    def get_parent(self, id):
1509
 
        if self.get_path(id) is None:
1510
 
            return None
1511
1529
        directory = self.get_dir(id)
1512
1530
        if directory == '.':
1513
1531
            directory = './.'