~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-08-17 08:32:56 UTC
  • Revision ID: mbp@sourcefrog.net-20050817083255-894936d5aebf7a95
- merge merge improvements from aaron

  revisions to merge are now specified by the -r parameter; the 
  /@ syntax is no longer needed

  abentley@panoramicfeedback.com-20050811205739-dc1988c004f9503e

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    return newdict
44
44
 
45
45
 
46
 
class PatchApply(object):
47
 
    """Patch application as a kind of content change"""
48
 
    def __init__(self, contents):
49
 
        """Constructor.
50
 
 
51
 
        :param contents: The text of the patch to apply
52
 
        :type contents: str"""
53
 
        self.contents = contents
54
 
 
55
 
    def __eq__(self, other):
56
 
        if not isinstance(other, PatchApply):
57
 
            return False
58
 
        elif self.contents != other.contents:
59
 
            return False
60
 
        else:
61
 
            return True
62
 
 
63
 
    def __ne__(self, other):
64
 
        return not (self == other)
65
 
 
66
 
    def apply(self, filename, conflict_handler, reverse=False):
67
 
        """Applies the patch to the specified file.
68
 
 
69
 
        :param filename: the file to apply the patch to
70
 
        :type filename: str
71
 
        :param reverse: If true, apply the patch in reverse
72
 
        :type reverse: bool
73
 
        """
74
 
        input_name = filename+".orig"
75
 
        try:
76
 
            os.rename(filename, input_name)
77
 
        except OSError, e:
78
 
            if e.errno != errno.ENOENT:
79
 
                raise
80
 
            if conflict_handler.patch_target_missing(filename, self.contents)\
81
 
                == "skip":
82
 
                return
83
 
            os.rename(filename, input_name)
84
 
            
85
 
 
86
 
        status = patch.patch(self.contents, input_name, filename, 
87
 
                                    reverse)
88
 
        os.chmod(filename, os.stat(input_name).st_mode)
89
 
        if status == 0:
90
 
            os.unlink(input_name)
91
 
        elif status == 1:
92
 
            conflict_handler.failed_hunks(filename)
93
 
 
94
 
        
 
46
       
95
47
class ChangeUnixPermissions(object):
96
48
    """This is two-way change, suitable for file modification, creation,
97
49
    deletion"""
374
326
 
375
327
 
376
328
class Diff3Merge(object):
377
 
    def __init__(self, base_file, other_file):
378
 
        self.base_file = base_file
379
 
        self.other_file = other_file
 
329
    def __init__(self, file_id, base, other):
 
330
        self.file_id = file_id
 
331
        self.base = base
 
332
        self.other = other
380
333
 
381
334
    def __eq__(self, other):
382
335
        if not isinstance(other, Diff3Merge):
383
336
            return False
384
 
        return (self.base_file == other.base_file and 
385
 
                self.other_file == other.other_file)
 
337
        return (self.base == other.base and 
 
338
                self.other == other.other and self.file_id == other.file_id)
386
339
 
387
340
    def __ne__(self, other):
388
341
        return not (self == other)
389
342
 
390
343
    def apply(self, filename, conflict_handler, reverse=False):
391
 
        new_file = filename+".new" 
 
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)
392
347
        if not reverse:
393
 
            base = self.base_file
394
 
            other = self.other_file
 
348
            base = base_file
 
349
            other = other_file
395
350
        else:
396
 
            base = self.other_file
397
 
            other = self.base_file
 
351
            base = other_file
 
352
            other = base_file
398
353
        status = patch.diff3(new_file, filename, base, other)
399
354
        if status == 0:
400
355
            os.chmod(new_file, os.stat(filename).st_mode)
402
357
            return
403
358
        else:
404
359
            assert(status == 1)
405
 
            conflict_handler.merge_conflict(new_file, filename, base, other)
 
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)
406
368
 
407
369
 
408
370
def CreateDir():
1060
1022
    def rename_conflict(self, id, this_name, base_name, other_name):
1061
1023
        raise RenameConflict(id, this_name, base_name, other_name)
1062
1024
 
1063
 
    def move_conflict(self, id, inventory):
1064
 
        this_dir = inventory.this.get_dir(id)
1065
 
        base_dir = inventory.base.get_dir(id)
1066
 
        other_dir = inventory.other.get_dir(id)
 
1025
    def move_conflict(self, id, this_dir, base_dir, other_dir):
1067
1026
        raise MoveConflict(id, this_dir, base_dir, other_dir)
1068
1027
 
1069
 
    def merge_conflict(self, new_file, this_path, base_path, other_path):
 
1028
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1070
1029
        os.unlink(new_file)
1071
1030
        raise MergeConflict(this_path)
1072
1031
 
1100
1059
    def missing_for_rename(self, filename):
1101
1060
        raise MissingForRename(filename)
1102
1061
 
1103
 
    def missing_for_merge(self, file_id, inventory):
1104
 
        raise MissingForMerge(inventory.other.get_path(file_id))
 
1062
    def missing_for_merge(self, file_id, other_path):
 
1063
        raise MissingForMerge(other_path)
1105
1064
 
1106
1065
    def new_contents_conflict(self, filename, other_contents):
1107
1066
        raise NewContentsConflict(filename)
1328
1287
        self.full_path = full_path
1329
1288
        self.stat_result = stat_result
1330
1289
 
1331
 
def generate_changeset(tree_a, tree_b, inventory_a=None, inventory_b=None):
1332
 
    return ChangesetGenerator(tree_a, tree_b, inventory_a, inventory_b)()
 
1290
def generate_changeset(tree_a, tree_b, interesting_ids=None):
 
1291
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1333
1292
 
1334
1293
class ChangesetGenerator(object):
1335
 
    def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
 
1294
    def __init__(self, tree_a, tree_b, interesting_ids=None):
1336
1295
        object.__init__(self)
1337
1296
        self.tree_a = tree_a
1338
1297
        self.tree_b = tree_b
1339
 
        if inventory_a is not None:
1340
 
            self.inventory_a = inventory_a
1341
 
        else:
1342
 
            self.inventory_a = tree_a.inventory()
1343
 
        if inventory_b is not None:
1344
 
            self.inventory_b = inventory_b
1345
 
        else:
1346
 
            self.inventory_b = tree_b.inventory()
1347
 
        self.r_inventory_a = self.reverse_inventory(self.inventory_a)
1348
 
        self.r_inventory_b = self.reverse_inventory(self.inventory_b)
 
1298
        self._interesting_ids = interesting_ids
1349
1299
 
1350
 
    def reverse_inventory(self, inventory):
1351
 
        r_inventory = {}
1352
 
        for entry in inventory.itervalues():
1353
 
            if entry.id is None:
1354
 
                continue
1355
 
            r_inventory[entry.id] = entry
1356
 
        return r_inventory
 
1300
    def iter_both_tree_ids(self):
 
1301
        for file_id in self.tree_a:
 
1302
            yield file_id
 
1303
        for file_id in self.tree_b:
 
1304
            if file_id not in self.tree_a:
 
1305
                yield file_id
1357
1306
 
1358
1307
    def __call__(self):
1359
1308
        cset = Changeset()
1360
 
        for entry in self.inventory_a.itervalues():
1361
 
            if entry.id is None:
1362
 
                continue
1363
 
            cs_entry = self.make_entry(entry.id)
 
1309
        for file_id in self.iter_both_tree_ids():
 
1310
            cs_entry = self.make_entry(file_id)
1364
1311
            if cs_entry is not None and not cs_entry.is_boring():
1365
1312
                cset.add_entry(cs_entry)
1366
1313
 
1367
 
        for entry in self.inventory_b.itervalues():
1368
 
            if entry.id is None:
1369
 
                continue
1370
 
            if not self.r_inventory_a.has_key(entry.id):
1371
 
                cs_entry = self.make_entry(entry.id)
1372
 
                if cs_entry is not None and not cs_entry.is_boring():
1373
 
                    cset.add_entry(cs_entry)
1374
1314
        for entry in list(cset.entries.itervalues()):
1375
1315
            if entry.parent != entry.new_parent:
1376
1316
                if not cset.entries.has_key(entry.parent) and\
1384
1324
                    cset.add_entry(parent_entry)
1385
1325
        return cset
1386
1326
 
1387
 
    def get_entry_parent(self, entry, inventory):
1388
 
        if entry is None:
1389
 
            return None
1390
 
        if entry.path == "./.":
1391
 
            return NULL_ID
1392
 
        dirname = os.path.dirname(entry.path)
1393
 
        if dirname == ".":
1394
 
            dirname = "./."
1395
 
        parent = inventory[dirname]
1396
 
        return parent.id
1397
 
 
1398
 
    def get_path(self, entry, tree):
1399
 
        if entry is None:
1400
 
            return (None, None)
1401
 
        if entry.path == ".":
1402
 
            return ""
1403
 
        return entry.path
1404
 
 
1405
 
    def make_basic_entry(self, id, only_interesting):
1406
 
        entry_a = self.r_inventory_a.get(id)
1407
 
        entry_b = self.r_inventory_b.get(id)
 
1327
    def iter_inventory(self, tree):
 
1328
        for file_id in tree:
 
1329
            yield self.get_entry(file_id, tree)
 
1330
 
 
1331
    def get_entry(self, file_id, tree):
 
1332
        if file_id not in tree:
 
1333
            return None
 
1334
        return tree.tree.inventory[file_id]
 
1335
 
 
1336
    def get_entry_parent(self, entry):
 
1337
        if entry is None:
 
1338
            return None
 
1339
        return entry.parent_id
 
1340
 
 
1341
    def get_path(self, file_id, tree):
 
1342
        if not tree.has_id(file_id):
 
1343
            return None
 
1344
        path = tree.id2path(file_id)
 
1345
        if path == '':
 
1346
            return './.'
 
1347
        else:
 
1348
            return path
 
1349
 
 
1350
    def make_basic_entry(self, file_id, only_interesting):
 
1351
        entry_a = self.get_entry(file_id, self.tree_a)
 
1352
        entry_b = self.get_entry(file_id, self.tree_b)
1408
1353
        if only_interesting and not self.is_interesting(entry_a, entry_b):
1409
1354
            return None
1410
 
        parent = self.get_entry_parent(entry_a, self.inventory_a)
1411
 
        path = self.get_path(entry_a, self.tree_a)
1412
 
        cs_entry = ChangesetEntry(id, parent, path)
1413
 
        new_parent = self.get_entry_parent(entry_b, self.inventory_b)
1414
 
 
1415
 
 
1416
 
        new_path = self.get_path(entry_b, self.tree_b)
 
1355
        parent = self.get_entry_parent(entry_a)
 
1356
        path = self.get_path(file_id, self.tree_a)
 
1357
        cs_entry = ChangesetEntry(file_id, parent, path)
 
1358
        new_parent = self.get_entry_parent(entry_b)
 
1359
 
 
1360
        new_path = self.get_path(file_id, self.tree_b)
1417
1361
 
1418
1362
        cs_entry.new_path = new_path
1419
1363
        cs_entry.new_parent = new_parent
1420
1364
        return cs_entry
1421
1365
 
1422
1366
    def is_interesting(self, entry_a, entry_b):
 
1367
        if self._interesting_ids is None:
 
1368
            return True
1423
1369
        if entry_a is not None:
1424
 
            if entry_a.interesting:
1425
 
                return True
1426
 
        if entry_b is not None:
1427
 
            if entry_b.interesting:
1428
 
                return True
1429
 
        return False
 
1370
            file_id = entry_a.file_id
 
1371
        elif entry_b is not None:
 
1372
            file_id = entry_b.file_id
 
1373
        else:
 
1374
            return False
 
1375
        return file_id in self._interesting_ids
1430
1376
 
1431
1377
    def make_boring_entry(self, id):
1432
1378
        cs_entry = self.make_basic_entry(id, only_interesting=False)
1484
1430
            if stat_a.st_ino == stat_b.st_ino and \
1485
1431
                stat_a.st_dev == stat_b.st_dev:
1486
1432
                return None
1487
 
            if file(full_path_a, "rb").read() == \
1488
 
                file(full_path_b, "rb").read():
1489
 
                return None
1490
 
 
1491
 
            patch_contents = patch.diff(full_path_a, 
1492
 
                                        file(full_path_b, "rb").read())
1493
 
            if patch_contents is None:
1494
 
                return None
1495
 
            return PatchApply(patch_contents)
1496
1433
 
1497
1434
        a_contents = self.get_contents(stat_a, full_path_a)
1498
1435
        b_contents = self.get_contents(stat_b, full_path_b)