~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Robert Collins
  • Date: 2005-10-17 21:20:18 UTC
  • mfrom: (1461)
  • mto: This revision was merged to the branch mainline in revision 1462.
  • Revision ID: robertc@robertcollins.net-20051017212018-5e2a78c67f36a026
merge from integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import errno
18
18
import patch
19
19
import stat
 
20
from tempfile import mkdtemp
 
21
from shutil import rmtree
20
22
from bzrlib.trace import mutter
21
 
from bzrlib.osutils import rename
 
23
from bzrlib.osutils import rename, sha_file
22
24
import bzrlib
 
25
from itertools import izip
23
26
 
24
27
# XXX: mbp: I'm not totally convinced that we should handle conflicts
25
28
# as part of changeset application, rather than only in the merge
143
146
        """
144
147
        self.target = contents
145
148
 
 
149
    def __repr__(self):
 
150
        return "SymlinkCreate(%s)" % self.target
 
151
 
146
152
    def __call__(self, filename, conflict_handler, reverse):
147
153
        """Creates or destroys the symlink.
148
154
 
230
236
 
231
237
                    
232
238
 
 
239
class TreeFileCreate(object):
 
240
    """Create or delete a file (for use with ReplaceContents)"""
 
241
    def __init__(self, tree, file_id):
 
242
        """Constructor
 
243
 
 
244
        :param contents: The contents of the file to write
 
245
        :type contents: str
 
246
        """
 
247
        self.tree = tree
 
248
        self.file_id = file_id
 
249
 
 
250
    def __repr__(self):
 
251
        return "TreeFileCreate(%s)" % self.file_id
 
252
 
 
253
    def __eq__(self, other):
 
254
        if not isinstance(other, TreeFileCreate):
 
255
            return False
 
256
        return self.tree.get_file_sha1(self.file_id) == \
 
257
            other.tree.get_file_sha1(other.file_id)
 
258
 
 
259
    def __ne__(self, other):
 
260
        return not (self == other)
 
261
 
 
262
    def write_file(self, filename):
 
263
        outfile = file(filename, "wb")
 
264
        for line in self.tree.get_file(self.file_id):
 
265
            outfile.write(line)
 
266
 
 
267
    def same_text(self, filename):
 
268
        in_file = file(filename, "rb")
 
269
        return sha_file(in_file) == self.tree.get_file_sha1(self.file_id)
 
270
 
 
271
    def __call__(self, filename, conflict_handler, reverse):
 
272
        """Create or delete a file
 
273
 
 
274
        :param filename: The name of the file to create
 
275
        :type filename: str
 
276
        :param reverse: Delete the file instead of creating it
 
277
        :type reverse: bool
 
278
        """
 
279
        if not reverse:
 
280
            try:
 
281
                self.write_file(filename)
 
282
            except IOError, e:
 
283
                if e.errno == errno.ENOENT:
 
284
                    if conflict_handler.missing_parent(filename)=="continue":
 
285
                        self.write_file(filename)
 
286
                else:
 
287
                    raise
 
288
 
 
289
        else:
 
290
            try:
 
291
                if not self.same_text(filename):
 
292
                    direction = conflict_handler.wrong_old_contents(filename,
 
293
                        self.tree.get_file(self.file_id).read())
 
294
                    if  direction != "continue":
 
295
                        return
 
296
                os.unlink(filename)
 
297
            except IOError, e:
 
298
                if e.errno != errno.ENOENT:
 
299
                    raise
 
300
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
301
                    return
 
302
 
 
303
                    
 
304
 
233
305
def reversed(sequence):
234
306
    max = len(sequence) - 1
235
307
    for i in range(len(sequence)):
302
374
            if mode is not None:
303
375
                os.chmod(filename, mode)
304
376
 
 
377
    def is_creation(self):
 
378
        return self.new_contents is not None and self.old_contents is None
 
379
 
 
380
    def is_deletion(self):
 
381
        return self.old_contents is not None and self.new_contents is None
 
382
 
305
383
class ApplySequence(object):
306
384
    def __init__(self, changes=None):
307
385
        self.changes = []
338
416
        self.base = base
339
417
        self.other = other
340
418
 
 
419
    def is_creation(self):
 
420
        return False
 
421
 
 
422
    def is_deletion(self):
 
423
        return False
 
424
 
341
425
    def __eq__(self, other):
342
426
        if not isinstance(other, Diff3Merge):
343
427
            return False
347
431
    def __ne__(self, other):
348
432
        return not (self == other)
349
433
 
 
434
    def dump_file(self, temp_dir, name, tree):
 
435
        out_path = os.path.join(temp_dir, name)
 
436
        out_file = file(out_path, "wb")
 
437
        in_file = tree.get_file(self.file_id)
 
438
        for line in in_file:
 
439
            out_file.write(line)
 
440
        return out_path
 
441
 
350
442
    def apply(self, filename, conflict_handler, reverse=False):
351
 
        new_file = filename+".new"
352
 
        base_file = self.base.readonly_path(self.file_id)
353
 
        other_file = self.other.readonly_path(self.file_id)
354
 
        if not reverse:
355
 
            base = base_file
356
 
            other = other_file
357
 
        else:
358
 
            base = other_file
359
 
            other = base_file
360
 
        status = patch.diff3(new_file, filename, base, other)
361
 
        if status == 0:
362
 
            os.chmod(new_file, os.stat(filename).st_mode)
363
 
            rename(new_file, filename)
364
 
            return
365
 
        else:
366
 
            assert(status == 1)
367
 
            def get_lines(filename):
368
 
                my_file = file(filename, "rb")
369
 
                lines = my_file.readlines()
370
 
                my_file.close()
371
 
                return lines
372
 
            base_lines = get_lines(base)
373
 
            other_lines = get_lines(other)
374
 
            conflict_handler.merge_conflict(new_file, filename, base_lines, 
375
 
                                            other_lines)
 
443
        temp_dir = mkdtemp(prefix="bzr-")
 
444
        try:
 
445
            new_file = filename+".new"
 
446
            base_file = self.dump_file(temp_dir, "base", self.base)
 
447
            other_file = self.dump_file(temp_dir, "other", self.other)
 
448
            if not reverse:
 
449
                base = base_file
 
450
                other = other_file
 
451
            else:
 
452
                base = other_file
 
453
                other = base_file
 
454
            status = patch.diff3(new_file, filename, base, other)
 
455
            if status == 0:
 
456
                os.chmod(new_file, os.stat(filename).st_mode)
 
457
                rename(new_file, filename)
 
458
                return
 
459
            else:
 
460
                assert(status == 1)
 
461
                def get_lines(filename):
 
462
                    my_file = file(filename, "rb")
 
463
                    lines = my_file.readlines()
 
464
                    my_file.close()
 
465
                    return lines
 
466
                base_lines = get_lines(base)
 
467
                other_lines = get_lines(other)
 
468
                conflict_handler.merge_conflict(new_file, filename, base_lines, 
 
469
                                                other_lines)
 
470
        finally:
 
471
            rmtree(temp_dir)
376
472
 
377
473
 
378
474
def CreateDir():
411
507
    """
412
508
    return ReplaceContents(FileCreate(contents), None)
413
509
 
414
 
def ReplaceFileContents(old_contents, new_contents):
 
510
def ReplaceFileContents(old_tree, new_tree, file_id):
415
511
    """Convenience fucntion to replace the contents of a file.
416
512
    
417
513
    :param old_contents: The contents of the file to replace 
421
517
    :return: A ReplaceContents that will replace the contents of a file a file 
422
518
    :rtype: `ReplaceContents`
423
519
    """
424
 
    return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
 
520
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
 
521
                           TreeFileCreate(new_tree, file_id))
425
522
 
426
523
def CreateSymlink(target):
427
524
    """Convenience fucntion to create a symlink.
590
687
        :param reverse: if true, the changeset is being applied in reverse
591
688
        :rtype: bool
592
689
        """
593
 
        return ((self.new_parent is None and not reverse) or 
594
 
                (self.parent is None and reverse))
 
690
        return self.is_creation(not reverse)
595
691
 
596
692
    def is_creation(self, reverse):
597
693
        """Return true if applying the entry would create a file/directory.
599
695
        :param reverse: if true, the changeset is being applied in reverse
600
696
        :rtype: bool
601
697
        """
602
 
        return ((self.parent is None and not reverse) or 
603
 
                (self.new_parent is None and reverse))
 
698
        if self.contents_change is None:
 
699
            return False
 
700
        if reverse:
 
701
            return self.contents_change.is_deletion()
 
702
        else:
 
703
            return self.contents_change.is_creation()
604
704
 
605
705
    def is_creation_or_deletion(self):
606
706
        """Return true if applying the entry would create or delete a 
608
708
 
609
709
        :rtype: bool
610
710
        """
611
 
        return self.parent is None or self.new_parent is None
 
711
        return self.is_creation(False) or self.is_deletion(False)
612
712
 
613
713
    def get_cset_path(self, mod=False):
614
714
        """Determine the path of the entry according to the changeset.
786
886
    :rtype: (List, List)
787
887
    """
788
888
    source_entries = [x for x in changeset.entries.itervalues() 
789
 
                      if x.needs_rename()]
 
889
                      if x.needs_rename() or x.is_creation_or_deletion()]
790
890
    # these are done from longest path to shortest, to avoid deleting a
791
891
    # parent before its children are deleted/renamed 
792
892
    def longest_to_shortest(entry):
833
933
            entry.apply(path, conflict_handler, reverse)
834
934
            temp_name[entry.id] = None
835
935
 
836
 
        else:
 
936
        elif entry.needs_rename():
837
937
            to_name = os.path.join(temp_dir, str(i))
838
938
            src_path = inventory.get(entry.id)
839
939
            if src_path is not None:
844
944
                except OSError, e:
845
945
                    if e.errno != errno.ENOENT:
846
946
                        raise
847
 
                    if conflict_handler.missing_for_rename(src_path) == "skip":
 
947
                    if conflict_handler.missing_for_rename(src_path, to_name) \
 
948
                        == "skip":
848
949
                        continue
849
950
 
850
951
    return temp_name
878
979
        if entry.is_creation(reverse):
879
980
            entry.apply(new_path, conflict_handler, reverse)
880
981
            changed_inventory[entry.id] = new_tree_path
881
 
        else:
 
982
        elif entry.needs_rename():
882
983
            if old_path is None:
883
984
                continue
884
985
            try:
971
1072
 
972
1073
 
973
1074
class MissingForRename(Exception):
974
 
    def __init__(self, filename):
975
 
        msg = "Attempt to move missing path %s" % (filename)
 
1075
    def __init__(self, filename, to_path):
 
1076
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
976
1077
        Exception.__init__(self, msg)
977
1078
        self.filename = filename
978
1079
 
981
1082
        msg = "Conflicting contents for new file %s" % (filename)
982
1083
        Exception.__init__(self, msg)
983
1084
 
 
1085
class ThreewayContentsConflict(Exception):
 
1086
    def __init__(self, filename):
 
1087
        msg = "Conflicting contents for file %s" % (filename)
 
1088
        Exception.__init__(self, msg)
 
1089
 
984
1090
 
985
1091
class MissingForMerge(Exception):
986
1092
    def __init__(self, filename):
1043
1149
    def missing_for_rm(self, filename, change):
1044
1150
        raise MissingForRm(filename)
1045
1151
 
1046
 
    def missing_for_rename(self, filename):
1047
 
        raise MissingForRename(filename)
 
1152
    def missing_for_rename(self, filename, to_path):
 
1153
        raise MissingForRename(filename, to_path)
1048
1154
 
1049
1155
    def missing_for_merge(self, file_id, other_path):
1050
1156
        raise MissingForMerge(other_path)
1052
1158
    def new_contents_conflict(self, filename, other_contents):
1053
1159
        raise NewContentsConflict(filename)
1054
1160
 
 
1161
    def threeway_contents_conflict(self, filename, this_contents,
 
1162
                                   base_contents, other_contents):
 
1163
        raise ThreewayContentsConflict(filename)
 
1164
 
1055
1165
    def finalize(self):
1056
1166
        pass
1057
1167
 
1088
1198
    
1089
1199
    #apply changes that don't affect filenames
1090
1200
    for entry in changeset.entries.itervalues():
1091
 
        if not entry.is_creation_or_deletion():
 
1201
        if not entry.is_creation_or_deletion() and not entry.is_boring():
1092
1202
            path = os.path.join(dir, inventory[entry.id])
1093
1203
            entry.apply(path, conflict_handler, reverse)
1094
1204
 
1113
1223
    r_inventory = {}
1114
1224
    for entry in tree.source_inventory().itervalues():
1115
1225
        inventory[entry.id] = entry.path
1116
 
    new_inventory = apply_changeset(cset, r_inventory, tree.root,
 
1226
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1117
1227
                                    reverse=reverse)
1118
1228
    new_entries, remove_entries = \
1119
1229
        get_inventory_change(inventory, new_inventory, cset, reverse)
1267
1377
            return False
1268
1378
    return True
1269
1379
 
1270
 
class UnsuppportedFiletype(Exception):
1271
 
    def __init__(self, full_path, stat_result):
1272
 
        msg = "The file \"%s\" is not a supported filetype." % full_path
 
1380
class UnsupportedFiletype(Exception):
 
1381
    def __init__(self, kind, full_path):
 
1382
        msg = "The file \"%s\" is a %s, which is not a supported filetype." \
 
1383
            % (full_path, kind)
1273
1384
        Exception.__init__(self, msg)
1274
1385
        self.full_path = full_path
1275
 
        self.stat_result = stat_result
 
1386
        self.kind = kind
1276
1387
 
1277
1388
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1278
1389
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1319
1430
    def get_entry(self, file_id, tree):
1320
1431
        if not tree.has_or_had_id(file_id):
1321
1432
            return None
1322
 
        return tree.tree.inventory[file_id]
 
1433
        return tree.inventory[file_id]
1323
1434
 
1324
1435
    def get_entry_parent(self, entry):
1325
1436
        if entry is None:
1376
1487
        if cs_entry is None:
1377
1488
            return None
1378
1489
 
1379
 
        full_path_a = self.tree_a.readonly_path(id)
1380
 
        full_path_b = self.tree_b.readonly_path(id)
1381
 
        stat_a = self.lstat(full_path_a)
1382
 
        stat_b = self.lstat(full_path_b)
1383
 
 
1384
 
        cs_entry.metadata_change = self.make_exec_flag_change(stat_a, stat_b)
 
1490
        cs_entry.metadata_change = self.make_exec_flag_change(id)
1385
1491
 
1386
1492
        if id in self.tree_a and id in self.tree_b:
1387
1493
            a_sha1 = self.tree_a.get_file_sha1(id)
1389
1495
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1390
1496
                return cs_entry
1391
1497
 
1392
 
        cs_entry.contents_change = self.make_contents_change(full_path_a,
1393
 
                                                             stat_a, 
1394
 
                                                             full_path_b, 
1395
 
                                                             stat_b)
 
1498
        cs_entry.contents_change = self.make_contents_change(id)
1396
1499
        return cs_entry
1397
1500
 
1398
 
    def make_exec_flag_change(self, stat_a, stat_b):
 
1501
    def make_exec_flag_change(self, file_id):
1399
1502
        exec_flag_a = exec_flag_b = None
1400
 
        if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1401
 
            exec_flag_a = bool(stat_a.st_mode & 0111)
1402
 
        if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1403
 
            exec_flag_b = bool(stat_b.st_mode & 0111)
 
1503
        if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
 
1504
            exec_flag_a = self.tree_a.is_executable(file_id)
 
1505
 
 
1506
        if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
 
1507
            exec_flag_b = self.tree_b.is_executable(file_id)
 
1508
 
1404
1509
        if exec_flag_a == exec_flag_b:
1405
1510
            return None
1406
1511
        return ChangeExecFlag(exec_flag_a, exec_flag_b)
1407
1512
 
1408
 
    def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1409
 
        if stat_a is None and stat_b is None:
1410
 
            return None
1411
 
        if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
1412
 
            stat.S_ISDIR(stat_b.st_mode):
1413
 
            return None
1414
 
        if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
1415
 
            stat.S_ISREG(stat_b.st_mode):
1416
 
            if stat_a.st_ino == stat_b.st_ino and \
1417
 
                stat_a.st_dev == stat_b.st_dev:
1418
 
                return None
1419
 
 
1420
 
        a_contents = self.get_contents(stat_a, full_path_a)
1421
 
        b_contents = self.get_contents(stat_b, full_path_b)
 
1513
    def make_contents_change(self, file_id):
 
1514
        a_contents = get_contents(self.tree_a, file_id)
 
1515
        b_contents = get_contents(self.tree_b, file_id)
1422
1516
        if a_contents == b_contents:
1423
1517
            return None
1424
1518
        return ReplaceContents(a_contents, b_contents)
1425
1519
 
1426
 
    def get_contents(self, stat_result, full_path):
1427
 
        if stat_result is None:
1428
 
            return None
1429
 
        elif stat.S_ISREG(stat_result.st_mode):
1430
 
            return FileCreate(file(full_path, "rb").read())
1431
 
        elif stat.S_ISDIR(stat_result.st_mode):
1432
 
            return dir_create
1433
 
        elif stat.S_ISLNK(stat_result.st_mode):
1434
 
            return SymlinkCreate(os.readlink(full_path))
1435
 
        else:
1436
 
            raise UnsupportedFiletype(full_path, stat_result)
1437
1520
 
1438
 
    def lstat(self, full_path):
1439
 
        stat_result = None
1440
 
        if full_path is not None:
1441
 
            try:
1442
 
                stat_result = os.lstat(full_path)
1443
 
            except OSError, e:
1444
 
                if e.errno != errno.ENOENT:
1445
 
                    raise
1446
 
        return stat_result
 
1521
def get_contents(tree, file_id):
 
1522
    """Return the appropriate contents to create a copy of file_id from tree"""
 
1523
    if file_id not in tree:
 
1524
        return None
 
1525
    kind = tree.kind(file_id)
 
1526
    if kind == "file":
 
1527
        return TreeFileCreate(tree, file_id)
 
1528
    elif kind in ("directory", "root_directory"):
 
1529
        return dir_create
 
1530
    elif kind == "symlink":
 
1531
        return SymlinkCreate(tree.get_symlink_target(file_id))
 
1532
    else:
 
1533
        raise UnsupportedFiletype(kind, tree.id2path(file_id))
1447
1534
 
1448
1535
 
1449
1536
def full_path(entry, tree):
1450
 
    return os.path.join(tree.root, entry.path)
 
1537
    return os.path.join(tree.basedir, entry.path)
1451
1538
 
1452
1539
def new_delete_entry(entry, tree, inventory, delete):
1453
1540
    if entry.path == "":