1
# Copyright (C) 2004 Aaron Bentley <aaron.bentley@utoronto.ca>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.trace import mutter
22
# XXX: mbp: I'm not totally convinced that we should handle conflicts
23
# as part of changeset application, rather than only in the merge
26
"""Represent and apply a changeset
28
Conflicts in applying a changeset are represented as exceptions.
31
__docformat__ = "restructuredtext"
35
class OldFailedTreeOp(Exception):
37
Exception.__init__(self, "bzr-tree-change contains files from a"
38
" previous failed merge operation.")
39
def invert_dict(dict):
41
for (key,value) in dict.iteritems():
47
class ChangeUnixPermissions(object):
48
"""This is two-way change, suitable for file modification, creation,
50
def __init__(self, old_mode, new_mode):
51
self.old_mode = old_mode
52
self.new_mode = new_mode
54
def apply(self, filename, conflict_handler, reverse=False):
56
from_mode = self.old_mode
57
to_mode = self.new_mode
59
from_mode = self.new_mode
60
to_mode = self.old_mode
62
current_mode = os.stat(filename).st_mode &0777
64
if e.errno == errno.ENOENT:
65
if conflict_handler.missing_for_chmod(filename) == "skip":
68
current_mode = from_mode
70
if from_mode is not None and current_mode != from_mode:
71
if conflict_handler.wrong_old_perms(filename, from_mode,
72
current_mode) != "continue":
75
if to_mode is not None:
77
os.chmod(filename, to_mode)
79
if e.errno == errno.ENOENT:
80
conflict_handler.missing_for_chmod(filename)
82
def __eq__(self, other):
83
if not isinstance(other, ChangeUnixPermissions):
85
elif self.old_mode != other.old_mode:
87
elif self.new_mode != other.new_mode:
92
def __ne__(self, other):
93
return not (self == other)
95
def dir_create(filename, conflict_handler, reverse):
96
"""Creates the directory, or deletes it if reverse is true. Intended to be
97
used with ReplaceContents.
99
:param filename: The name of the directory to create
101
:param reverse: If true, delete the directory, instead
108
if e.errno != errno.EEXIST:
110
if conflict_handler.dir_exists(filename) == "continue":
113
if e.errno == errno.ENOENT:
114
if conflict_handler.missing_parent(filename)=="continue":
115
file(filename, "wb").write(self.contents)
122
if conflict_handler.rmdir_non_empty(filename) == "skip":
129
class SymlinkCreate(object):
130
"""Creates or deletes a symlink (for use with ReplaceContents)"""
131
def __init__(self, contents):
134
:param contents: The filename of the target the symlink should point to
137
self.target = contents
139
def __call__(self, filename, conflict_handler, reverse):
140
"""Creates or destroys the symlink.
142
:param filename: The name of the symlink to create
146
assert(os.readlink(filename) == self.target)
150
os.symlink(self.target, filename)
152
if e.errno != errno.EEXIST:
154
if conflict_handler.link_name_exists(filename) == "continue":
155
os.symlink(self.target, filename)
157
def __eq__(self, other):
158
if not isinstance(other, SymlinkCreate):
160
elif self.target != other.target:
165
def __ne__(self, other):
166
return not (self == other)
168
class FileCreate(object):
169
"""Create or delete a file (for use with ReplaceContents)"""
170
def __init__(self, contents):
173
:param contents: The contents of the file to write
176
self.contents = contents
179
return "FileCreate(%i b)" % len(self.contents)
181
def __eq__(self, other):
182
if not isinstance(other, FileCreate):
184
elif self.contents != other.contents:
189
def __ne__(self, other):
190
return not (self == other)
192
def __call__(self, filename, conflict_handler, reverse):
193
"""Create or delete a file
195
:param filename: The name of the file to create
197
:param reverse: Delete the file instead of creating it
202
file(filename, "wb").write(self.contents)
204
if e.errno == errno.ENOENT:
205
if conflict_handler.missing_parent(filename)=="continue":
206
file(filename, "wb").write(self.contents)
212
if (file(filename, "rb").read() != self.contents):
213
direction = conflict_handler.wrong_old_contents(filename,
215
if direction != "continue":
219
if e.errno != errno.ENOENT:
221
if conflict_handler.missing_for_rm(filename, undo) == "skip":
226
def reversed(sequence):
227
max = len(sequence) - 1
228
for i in range(len(sequence)):
229
yield sequence[max - i]
231
class ReplaceContents(object):
232
"""A contents-replacement framework. It allows a file/directory/symlink to
233
be created, deleted, or replaced with another file/directory/symlink.
234
Arguments must be callable with (filename, reverse).
236
def __init__(self, old_contents, new_contents):
239
:param old_contents: The change to reverse apply (e.g. a deletion), \
241
:type old_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
243
:param new_contents: The second change to apply (e.g. a creation), \
245
:type new_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
248
self.old_contents=old_contents
249
self.new_contents=new_contents
252
return "ReplaceContents(%r -> %r)" % (self.old_contents,
255
def __eq__(self, other):
256
if not isinstance(other, ReplaceContents):
258
elif self.old_contents != other.old_contents:
260
elif self.new_contents != other.new_contents:
264
def __ne__(self, other):
265
return not (self == other)
267
def apply(self, filename, conflict_handler, reverse=False):
268
"""Applies the FileReplacement to the specified filename
270
:param filename: The name of the file to apply changes to
272
:param reverse: If true, apply the change in reverse
276
undo = self.old_contents
277
perform = self.new_contents
279
undo = self.new_contents
280
perform = self.old_contents
284
mode = os.lstat(filename).st_mode
285
if stat.S_ISLNK(mode):
288
if e.errno != errno.ENOENT:
290
if conflict_handler.missing_for_rm(filename, undo) == "skip":
292
undo(filename, conflict_handler, reverse=True)
293
if perform is not None:
294
perform(filename, conflict_handler, reverse=False)
296
os.chmod(filename, mode)
298
class ApplySequence(object):
299
def __init__(self, changes=None):
301
if changes is not None:
302
self.changes.extend(changes)
304
def __eq__(self, other):
305
if not isinstance(other, ApplySequence):
307
elif len(other.changes) != len(self.changes):
310
for i in range(len(self.changes)):
311
if self.changes[i] != other.changes[i]:
315
def __ne__(self, other):
316
return not (self == other)
319
def apply(self, filename, conflict_handler, reverse=False):
323
iter = reversed(self.changes)
325
change.apply(filename, conflict_handler, reverse)
328
class Diff3Merge(object):
329
def __init__(self, file_id, base, other):
330
self.file_id = file_id
334
def __eq__(self, other):
335
if not isinstance(other, Diff3Merge):
337
return (self.base == other.base and
338
self.other == other.other and self.file_id == other.file_id)
340
def __ne__(self, other):
341
return not (self == other)
343
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)
353
status = patch.diff3(new_file, filename, base, other)
355
os.chmod(new_file, os.stat(filename).st_mode)
356
os.rename(new_file, filename)
360
def get_lines(filename):
361
my_file = file(base, "rb")
362
lines = my_file.readlines()
364
base_lines = get_lines(base)
365
other_lines = get_lines(other)
366
conflict_handler.merge_conflict(new_file, filename, base_lines,
371
"""Convenience function to create a directory.
373
:return: A ReplaceContents that will create a directory
374
:rtype: `ReplaceContents`
376
return ReplaceContents(None, dir_create)
379
"""Convenience function to delete a directory.
381
:return: A ReplaceContents that will delete a directory
382
:rtype: `ReplaceContents`
384
return ReplaceContents(dir_create, None)
386
def CreateFile(contents):
387
"""Convenience fucntion to create a file.
389
:param contents: The contents of the file to create
391
:return: A ReplaceContents that will create a file
392
:rtype: `ReplaceContents`
394
return ReplaceContents(None, FileCreate(contents))
396
def DeleteFile(contents):
397
"""Convenience fucntion to delete a file.
399
:param contents: The contents of the file to delete
401
:return: A ReplaceContents that will delete a file
402
:rtype: `ReplaceContents`
404
return ReplaceContents(FileCreate(contents), None)
406
def ReplaceFileContents(old_contents, new_contents):
407
"""Convenience fucntion to replace the contents of a file.
409
:param old_contents: The contents of the file to replace
410
:type old_contents: str
411
:param new_contents: The contents to replace the file with
412
:type new_contents: str
413
:return: A ReplaceContents that will replace the contents of a file a file
414
:rtype: `ReplaceContents`
416
return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
418
def CreateSymlink(target):
419
"""Convenience fucntion to create a symlink.
421
:param target: The path the link should point to
423
:return: A ReplaceContents that will delete a file
424
:rtype: `ReplaceContents`
426
return ReplaceContents(None, SymlinkCreate(target))
428
def DeleteSymlink(target):
429
"""Convenience fucntion to delete a symlink.
431
:param target: The path the link should point to
433
:return: A ReplaceContents that will delete a file
434
:rtype: `ReplaceContents`
436
return ReplaceContents(SymlinkCreate(target), None)
438
def ChangeTarget(old_target, new_target):
439
"""Convenience fucntion to change the target of a symlink.
441
:param old_target: The current link target
442
:type old_target: str
443
:param new_target: The new link target to use
444
:type new_target: str
445
:return: A ReplaceContents that will delete a file
446
:rtype: `ReplaceContents`
448
return ReplaceContents(SymlinkCreate(old_target), SymlinkCreate(new_target))
451
class InvalidEntry(Exception):
452
"""Raise when a ChangesetEntry is invalid in some way"""
453
def __init__(self, entry, problem):
456
:param entry: The invalid ChangesetEntry
457
:type entry: `ChangesetEntry`
458
:param problem: The problem with the entry
461
msg = "Changeset entry for %s (%s) is invalid.\n%s" % (entry.id,
464
Exception.__init__(self, msg)
468
class SourceRootHasName(InvalidEntry):
469
"""This changeset entry has a name other than "", but its parent is !NULL"""
470
def __init__(self, entry, name):
473
:param entry: The invalid ChangesetEntry
474
:type entry: `ChangesetEntry`
475
:param name: The name of the entry
478
msg = 'Child of !NULL is named "%s", not "./.".' % name
479
InvalidEntry.__init__(self, entry, msg)
481
class NullIDAssigned(InvalidEntry):
482
"""The id !NULL was assigned to a real entry"""
483
def __init__(self, entry):
486
:param entry: The invalid ChangesetEntry
487
:type entry: `ChangesetEntry`
489
msg = '"!NULL" id assigned to a file "%s".' % entry.path
490
InvalidEntry.__init__(self, entry, msg)
492
class ParentIDIsSelf(InvalidEntry):
493
"""An entry is marked as its own parent"""
494
def __init__(self, entry):
497
:param entry: The invalid ChangesetEntry
498
:type entry: `ChangesetEntry`
500
msg = 'file %s has "%s" id for both self id and parent id.' % \
501
(entry.path, entry.id)
502
InvalidEntry.__init__(self, entry, msg)
504
class ChangesetEntry(object):
505
"""An entry the changeset"""
506
def __init__(self, id, parent, path):
507
"""Constructor. Sets parent and name assuming it was not
508
renamed/created/deleted.
509
:param id: The id associated with the entry
510
:param parent: The id of the parent of this entry (or !NULL if no
512
:param path: The file path relative to the tree root of this entry
518
self.new_parent = parent
519
self.contents_change = None
520
self.metadata_change = None
521
if parent == NULL_ID and path !='./.':
522
raise SourceRootHasName(self, path)
523
if self.id == NULL_ID:
524
raise NullIDAssigned(self)
525
if self.id == self.parent:
526
raise ParentIDIsSelf(self)
529
return "ChangesetEntry(%s)" % self.id
532
if self.path is None:
534
return os.path.dirname(self.path)
536
def __set_dir(self, dir):
537
self.path = os.path.join(dir, os.path.basename(self.path))
539
dir = property(__get_dir, __set_dir)
541
def __get_name(self):
542
if self.path is None:
544
return os.path.basename(self.path)
546
def __set_name(self, name):
547
self.path = os.path.join(os.path.dirname(self.path), name)
549
name = property(__get_name, __set_name)
551
def __get_new_dir(self):
552
if self.new_path is None:
554
return os.path.dirname(self.new_path)
556
def __set_new_dir(self, dir):
557
self.new_path = os.path.join(dir, os.path.basename(self.new_path))
559
new_dir = property(__get_new_dir, __set_new_dir)
561
def __get_new_name(self):
562
if self.new_path is None:
564
return os.path.basename(self.new_path)
566
def __set_new_name(self, name):
567
self.new_path = os.path.join(os.path.dirname(self.new_path), name)
569
new_name = property(__get_new_name, __set_new_name)
571
def needs_rename(self):
572
"""Determines whether the entry requires renaming.
577
return (self.parent != self.new_parent or self.name != self.new_name)
579
def is_deletion(self, reverse):
580
"""Return true if applying the entry would delete a file/directory.
582
:param reverse: if true, the changeset is being applied in reverse
585
return ((self.new_parent is None and not reverse) or
586
(self.parent is None and reverse))
588
def is_creation(self, reverse):
589
"""Return true if applying the entry would create a file/directory.
591
:param reverse: if true, the changeset is being applied in reverse
594
return ((self.parent is None and not reverse) or
595
(self.new_parent is None and reverse))
597
def is_creation_or_deletion(self):
598
"""Return true if applying the entry would create or delete a
603
return self.parent is None or self.new_parent is None
605
def get_cset_path(self, mod=False):
606
"""Determine the path of the entry according to the changeset.
608
:param changeset: The changeset to derive the path from
609
:type changeset: `Changeset`
610
:param mod: If true, generate the MOD path. Otherwise, generate the \
612
:return: the path of the entry, or None if it did not exist in the \
614
:rtype: str or NoneType
617
if self.new_parent == NULL_ID:
619
elif self.new_parent is None:
623
if self.parent == NULL_ID:
625
elif self.parent is None:
629
def summarize_name(self, reverse=False):
630
"""Produce a one-line summary of the filename. Indicates renames as
631
old => new, indicates creation as None => new, indicates deletion as
634
:param changeset: The changeset to get paths from
635
:type changeset: `Changeset`
636
:param reverse: If true, reverse the names in the output
640
orig_path = self.get_cset_path(False)
641
mod_path = self.get_cset_path(True)
642
if orig_path is not None:
643
orig_path = orig_path[2:]
644
if mod_path is not None:
645
mod_path = mod_path[2:]
646
if orig_path == mod_path:
650
return "%s => %s" % (orig_path, mod_path)
652
return "%s => %s" % (mod_path, orig_path)
655
def get_new_path(self, id_map, changeset, reverse=False):
656
"""Determine the full pathname to rename to
658
:param id_map: The map of ids to filenames for the tree
659
:type id_map: Dictionary
660
:param changeset: The changeset to get data from
661
:type changeset: `Changeset`
662
:param reverse: If true, we're applying the changeset in reverse
666
mutter("Finding new path for %s" % self.summarize_name())
670
from_dir = self.new_dir
672
from_name = self.new_name
674
parent = self.new_parent
675
to_dir = self.new_dir
677
to_name = self.new_name
678
from_name = self.name
683
if parent == NULL_ID or parent is None:
685
raise SourceRootHasName(self, to_name)
688
if from_dir == to_dir:
689
dir = os.path.dirname(id_map[self.id])
691
mutter("path, new_path: %r %r" % (self.path, self.new_path))
692
parent_entry = changeset.entries[parent]
693
dir = parent_entry.get_new_path(id_map, changeset, reverse)
694
if from_name == to_name:
695
name = os.path.basename(id_map[self.id])
698
assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
699
return os.path.join(dir, name)
702
"""Determines whether the entry does nothing
704
:return: True if the entry does no renames or content changes
707
if self.contents_change is not None:
709
elif self.metadata_change is not None:
711
elif self.parent != self.new_parent:
713
elif self.name != self.new_name:
718
def apply(self, filename, conflict_handler, reverse=False):
719
"""Applies the file content and/or metadata changes.
721
:param filename: the filename of the entry
723
:param reverse: If true, apply the changes in reverse
726
if self.is_deletion(reverse) and self.metadata_change is not None:
727
self.metadata_change.apply(filename, conflict_handler, reverse)
728
if self.contents_change is not None:
729
self.contents_change.apply(filename, conflict_handler, reverse)
730
if not self.is_deletion(reverse) and self.metadata_change is not None:
731
self.metadata_change.apply(filename, conflict_handler, reverse)
733
class IDPresent(Exception):
734
def __init__(self, id):
735
msg = "Cannot add entry because that id has already been used:\n%s" %\
737
Exception.__init__(self, msg)
740
class Changeset(object):
741
"""A set of changes to apply"""
745
def add_entry(self, entry):
746
"""Add an entry to the list of entries"""
747
if self.entries.has_key(entry.id):
748
raise IDPresent(entry.id)
749
self.entries[entry.id] = entry
751
def my_sort(sequence, key, reverse=False):
752
"""A sort function that supports supplying a key for comparison
754
:param sequence: The sequence to sort
755
:param key: A callable object that returns the values to be compared
756
:param reverse: If true, sort in reverse order
759
def cmp_by_key(entry_a, entry_b):
764
return cmp(key(entry_a), key(entry_b))
765
sequence.sort(cmp_by_key)
767
def get_rename_entries(changeset, inventory, reverse):
768
"""Return a list of entries that will be renamed. Entries are sorted from
769
longest to shortest source path and from shortest to longest target path.
771
:param changeset: The changeset to look in
772
:type changeset: `Changeset`
773
:param inventory: The source of current tree paths for the given ids
774
:type inventory: Dictionary
775
:param reverse: If true, the changeset is being applied in reverse
777
:return: source entries and target entries as a tuple
780
source_entries = [x for x in changeset.entries.itervalues()
782
# these are done from longest path to shortest, to avoid deleting a
783
# parent before its children are deleted/renamed
784
def longest_to_shortest(entry):
785
path = inventory.get(entry.id)
790
my_sort(source_entries, longest_to_shortest, reverse=True)
792
target_entries = source_entries[:]
793
# These are done from shortest to longest path, to avoid creating a
794
# child before its parent has been created/renamed
795
def shortest_to_longest(entry):
796
path = entry.get_new_path(inventory, changeset, reverse)
801
my_sort(target_entries, shortest_to_longest)
802
return (source_entries, target_entries)
804
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
805
conflict_handler, reverse):
806
"""Delete and rename entries as appropriate. Entries are renamed to temp
807
names. A map of id -> temp name (or None, for deletions) is returned.
809
:param source_entries: The entries to rename and delete
810
:type source_entries: List of `ChangesetEntry`
811
:param inventory: The map of id -> filename in the current tree
812
:type inventory: Dictionary
813
:param dir: The directory to apply changes to
815
:param reverse: Apply changes in reverse
817
:return: a mapping of id to temporary name
821
for i in range(len(source_entries)):
822
entry = source_entries[i]
823
if entry.is_deletion(reverse):
824
path = os.path.join(dir, inventory[entry.id])
825
entry.apply(path, conflict_handler, reverse)
826
temp_name[entry.id] = None
829
to_name = os.path.join(temp_dir, str(i))
830
src_path = inventory.get(entry.id)
831
if src_path is not None:
832
src_path = os.path.join(dir, src_path)
834
os.rename(src_path, to_name)
835
temp_name[entry.id] = to_name
837
if e.errno != errno.ENOENT:
839
if conflict_handler.missing_for_rename(src_path) == "skip":
845
def rename_to_new_create(changed_inventory, target_entries, inventory,
846
changeset, dir, conflict_handler, reverse):
847
"""Rename entries with temp names to their final names, create new files.
849
:param changed_inventory: A mapping of id to temporary name
850
:type changed_inventory: Dictionary
851
:param target_entries: The entries to apply changes to
852
:type target_entries: List of `ChangesetEntry`
853
:param changeset: The changeset to apply
854
:type changeset: `Changeset`
855
:param dir: The directory to apply changes to
857
:param reverse: If true, apply changes in reverse
860
for entry in target_entries:
861
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
862
if new_tree_path is None:
864
new_path = os.path.join(dir, new_tree_path)
865
old_path = changed_inventory.get(entry.id)
866
if os.path.exists(new_path):
867
if conflict_handler.target_exists(entry, new_path, old_path) == \
870
if entry.is_creation(reverse):
871
entry.apply(new_path, conflict_handler, reverse)
872
changed_inventory[entry.id] = new_tree_path
877
os.rename(old_path, new_path)
878
changed_inventory[entry.id] = new_tree_path
880
raise Exception ("%s is missing" % new_path)
882
class TargetExists(Exception):
883
def __init__(self, entry, target):
884
msg = "The path %s already exists" % target
885
Exception.__init__(self, msg)
889
class RenameConflict(Exception):
890
def __init__(self, id, this_name, base_name, other_name):
891
msg = """Trees all have different names for a file
895
id: %s""" % (this_name, base_name, other_name, id)
896
Exception.__init__(self, msg)
897
self.this_name = this_name
898
self.base_name = base_name
899
self_other_name = other_name
901
class MoveConflict(Exception):
902
def __init__(self, id, this_parent, base_parent, other_parent):
903
msg = """The file is in different directories in every tree
907
id: %s""" % (this_parent, base_parent, other_parent, id)
908
Exception.__init__(self, msg)
909
self.this_parent = this_parent
910
self.base_parent = base_parent
911
self_other_parent = other_parent
913
class MergeConflict(Exception):
914
def __init__(self, this_path):
915
Exception.__init__(self, "Conflict applying changes to %s" % this_path)
916
self.this_path = this_path
918
class MergePermissionConflict(Exception):
919
def __init__(self, this_path, base_path, other_path):
920
this_perms = os.stat(this_path).st_mode & 0755
921
base_perms = os.stat(base_path).st_mode & 0755
922
other_perms = os.stat(other_path).st_mode & 0755
923
msg = """Conflicting permission for %s
927
""" % (this_path, this_perms, base_perms, other_perms)
928
self.this_path = this_path
929
self.base_path = base_path
930
self.other_path = other_path
931
Exception.__init__(self, msg)
933
class WrongOldContents(Exception):
934
def __init__(self, filename):
935
msg = "Contents mismatch deleting %s" % filename
936
self.filename = filename
937
Exception.__init__(self, msg)
939
class WrongOldPermissions(Exception):
940
def __init__(self, filename, old_perms, new_perms):
941
msg = "Permission missmatch on %s:\n" \
942
"Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
943
self.filename = filename
944
Exception.__init__(self, msg)
946
class RemoveContentsConflict(Exception):
947
def __init__(self, filename):
948
msg = "Conflict deleting %s, which has different contents in BASE"\
949
" and THIS" % filename
950
self.filename = filename
951
Exception.__init__(self, msg)
953
class DeletingNonEmptyDirectory(Exception):
954
def __init__(self, filename):
955
msg = "Trying to remove dir %s while it still had files" % filename
956
self.filename = filename
957
Exception.__init__(self, msg)
960
class PatchTargetMissing(Exception):
961
def __init__(self, filename):
962
msg = "Attempt to patch %s, which does not exist" % filename
963
Exception.__init__(self, msg)
964
self.filename = filename
966
class MissingPermsFile(Exception):
967
def __init__(self, filename):
968
msg = "Attempt to change permissions on %s, which does not exist" %\
970
Exception.__init__(self, msg)
971
self.filename = filename
973
class MissingForRm(Exception):
974
def __init__(self, filename):
975
msg = "Attempt to remove missing path %s" % filename
976
Exception.__init__(self, msg)
977
self.filename = filename
980
class MissingForRename(Exception):
981
def __init__(self, filename):
982
msg = "Attempt to move missing path %s" % (filename)
983
Exception.__init__(self, msg)
984
self.filename = filename
986
class NewContentsConflict(Exception):
987
def __init__(self, filename):
988
msg = "Conflicting contents for new file %s" % (filename)
989
Exception.__init__(self, msg)
992
class MissingForMerge(Exception):
993
def __init__(self, filename):
994
msg = "The file %s was modified, but does not exist in this tree"\
996
Exception.__init__(self, msg)
999
class ExceptionConflictHandler(object):
1000
"""Default handler for merge exceptions.
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.
1006
def __init__(self, dir):
1009
def missing_parent(self, pathname):
1010
parent = os.path.dirname(pathname)
1011
raise Exception("Parent directory missing for %s" % pathname)
1013
def dir_exists(self, pathname):
1014
raise Exception("Directory already exists for %s" % pathname)
1016
def failed_hunks(self, pathname):
1017
raise Exception("Failed to apply some hunks for %s" % pathname)
1019
def target_exists(self, entry, target, old_path):
1020
raise TargetExists(entry, target)
1022
def rename_conflict(self, id, this_name, base_name, other_name):
1023
raise RenameConflict(id, this_name, base_name, other_name)
1025
def move_conflict(self, id, this_dir, base_dir, other_dir):
1026
raise MoveConflict(id, this_dir, base_dir, other_dir)
1028
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1030
raise MergeConflict(this_path)
1032
def permission_conflict(self, this_path, base_path, other_path):
1033
raise MergePermissionConflict(this_path, base_path, other_path)
1035
def wrong_old_contents(self, filename, expected_contents):
1036
raise WrongOldContents(filename)
1038
def rem_contents_conflict(self, filename, this_contents, base_contents):
1039
raise RemoveContentsConflict(filename)
1041
def wrong_old_perms(self, filename, old_perms, new_perms):
1042
raise WrongOldPermissions(filename, old_perms, new_perms)
1044
def rmdir_non_empty(self, filename):
1045
raise DeletingNonEmptyDirectory(filename)
1047
def link_name_exists(self, filename):
1048
raise TargetExists(filename)
1050
def patch_target_missing(self, filename, contents):
1051
raise PatchTargetMissing(filename)
1053
def missing_for_chmod(self, filename):
1054
raise MissingPermsFile(filename)
1056
def missing_for_rm(self, filename, change):
1057
raise MissingForRm(filename)
1059
def missing_for_rename(self, filename):
1060
raise MissingForRename(filename)
1062
def missing_for_merge(self, file_id, other_path):
1063
raise MissingForMerge(other_path)
1065
def new_contents_conflict(self, filename, other_contents):
1066
raise NewContentsConflict(filename)
1071
def apply_changeset(changeset, inventory, dir, conflict_handler=None,
1073
"""Apply a changeset to a directory.
1075
:param changeset: The changes to perform
1076
:type changeset: `Changeset`
1077
:param inventory: The mapping of id to filename for the directory
1078
:type inventory: Dictionary
1079
:param dir: The path of the directory to apply the changes to
1081
:param reverse: If true, apply the changes in reverse
1083
:return: The mapping of the changed entries
1086
if conflict_handler is None:
1087
conflict_handler = ExceptionConflictHandler(dir)
1088
temp_dir = os.path.join(dir, "bzr-tree-change")
1092
if e.errno == errno.EEXIST:
1096
if e.errno == errno.ENOTEMPTY:
1097
raise OldFailedTreeOp()
1102
#apply changes that don't affect filenames
1103
for entry in changeset.entries.itervalues():
1104
if not entry.is_creation_or_deletion():
1105
path = os.path.join(dir, inventory[entry.id])
1106
entry.apply(path, conflict_handler, reverse)
1108
# Apply renames in stages, to minimize conflicts:
1109
# Only files whose name or parent change are interesting, because their
1110
# target name may exist in the source tree. If a directory's name changes,
1111
# that doesn't make its children interesting.
1112
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1115
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1116
temp_dir, conflict_handler,
1119
rename_to_new_create(changed_inventory, target_entries, inventory,
1120
changeset, dir, conflict_handler, reverse)
1122
return changed_inventory
1125
def apply_changeset_tree(cset, tree, reverse=False):
1127
for entry in tree.source_inventory().itervalues():
1128
inventory[entry.id] = entry.path
1129
new_inventory = apply_changeset(cset, r_inventory, tree.root,
1131
new_entries, remove_entries = \
1132
get_inventory_change(inventory, new_inventory, cset, reverse)
1133
tree.update_source_inventory(new_entries, remove_entries)
1136
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1139
for entry in cset.entries.itervalues():
1140
if entry.needs_rename():
1141
new_path = entry.get_new_path(inventory, cset)
1142
if new_path is None:
1143
remove_entries.append(entry.id)
1145
new_entries[new_path] = entry.id
1146
return new_entries, remove_entries
1149
def print_changeset(cset):
1150
"""Print all non-boring changeset entries
1152
:param cset: The changeset to print
1153
:type cset: `Changeset`
1155
for entry in cset.entries.itervalues():
1156
if entry.is_boring():
1159
print entry.summarize_name(cset)
1161
class CompositionFailure(Exception):
1162
def __init__(self, old_entry, new_entry, problem):
1163
msg = "Unable to conpose entries.\n %s" % problem
1164
Exception.__init__(self, msg)
1166
class IDMismatch(CompositionFailure):
1167
def __init__(self, old_entry, new_entry):
1168
problem = "Attempt to compose entries with different ids: %s and %s" %\
1169
(old_entry.id, new_entry.id)
1170
CompositionFailure.__init__(self, old_entry, new_entry, problem)
1172
def compose_changesets(old_cset, new_cset):
1173
"""Combine two changesets into one. This works well for exact patching.
1174
Otherwise, not so well.
1176
:param old_cset: The first changeset that would be applied
1177
:type old_cset: `Changeset`
1178
:param new_cset: The second changeset that would be applied
1179
:type new_cset: `Changeset`
1180
:return: A changeset that combines the changes in both changesets
1183
composed = Changeset()
1184
for old_entry in old_cset.entries.itervalues():
1185
new_entry = new_cset.entries.get(old_entry.id)
1186
if new_entry is None:
1187
composed.add_entry(old_entry)
1189
composed_entry = compose_entries(old_entry, new_entry)
1190
if composed_entry.parent is not None or\
1191
composed_entry.new_parent is not None:
1192
composed.add_entry(composed_entry)
1193
for new_entry in new_cset.entries.itervalues():
1194
if not old_cset.entries.has_key(new_entry.id):
1195
composed.add_entry(new_entry)
1198
def compose_entries(old_entry, new_entry):
1199
"""Combine two entries into one.
1201
:param old_entry: The first entry that would be applied
1202
:type old_entry: ChangesetEntry
1203
:param old_entry: The second entry that would be applied
1204
:type old_entry: ChangesetEntry
1205
:return: A changeset entry combining both entries
1206
:rtype: `ChangesetEntry`
1208
if old_entry.id != new_entry.id:
1209
raise IDMismatch(old_entry, new_entry)
1210
output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
1212
if (old_entry.parent != old_entry.new_parent or
1213
new_entry.parent != new_entry.new_parent):
1214
output.new_parent = new_entry.new_parent
1216
if (old_entry.path != old_entry.new_path or
1217
new_entry.path != new_entry.new_path):
1218
output.new_path = new_entry.new_path
1220
output.contents_change = compose_contents(old_entry, new_entry)
1221
output.metadata_change = compose_metadata(old_entry, new_entry)
1224
def compose_contents(old_entry, new_entry):
1225
"""Combine the contents of two changeset entries. Entries are combined
1226
intelligently where possible, but the fallback behavior returns an
1229
:param old_entry: The first entry that would be applied
1230
:type old_entry: `ChangesetEntry`
1231
:param new_entry: The second entry that would be applied
1232
:type new_entry: `ChangesetEntry`
1233
:return: A combined contents change
1234
:rtype: anything supporting the apply(reverse=False) method
1236
old_contents = old_entry.contents_change
1237
new_contents = new_entry.contents_change
1238
if old_entry.contents_change is None:
1239
return new_entry.contents_change
1240
elif new_entry.contents_change is None:
1241
return old_entry.contents_change
1242
elif isinstance(old_contents, ReplaceContents) and \
1243
isinstance(new_contents, ReplaceContents):
1244
if old_contents.old_contents == new_contents.new_contents:
1247
return ReplaceContents(old_contents.old_contents,
1248
new_contents.new_contents)
1249
elif isinstance(old_contents, ApplySequence):
1250
output = ApplySequence(old_contents.changes)
1251
if isinstance(new_contents, ApplySequence):
1252
output.changes.extend(new_contents.changes)
1254
output.changes.append(new_contents)
1256
elif isinstance(new_contents, ApplySequence):
1257
output = ApplySequence((old_contents.changes,))
1258
output.extend(new_contents.changes)
1261
return ApplySequence((old_contents, new_contents))
1263
def compose_metadata(old_entry, new_entry):
1264
old_meta = old_entry.metadata_change
1265
new_meta = new_entry.metadata_change
1266
if old_meta is None:
1268
elif new_meta is None:
1270
elif isinstance(old_meta, ChangeUnixPermissions) and \
1271
isinstance(new_meta, ChangeUnixPermissions):
1272
return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1274
return ApplySequence(old_meta, new_meta)
1277
def changeset_is_null(changeset):
1278
for entry in changeset.entries.itervalues():
1279
if not entry.is_boring():
1283
class UnsuppportedFiletype(Exception):
1284
def __init__(self, full_path, stat_result):
1285
msg = "The file \"%s\" is not a supported filetype." % full_path
1286
Exception.__init__(self, msg)
1287
self.full_path = full_path
1288
self.stat_result = stat_result
1290
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1291
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1293
class ChangesetGenerator(object):
1294
def __init__(self, tree_a, tree_b, interesting_ids=None):
1295
object.__init__(self)
1296
self.tree_a = tree_a
1297
self.tree_b = tree_b
1298
self._interesting_ids = interesting_ids
1300
def iter_both_tree_ids(self):
1301
for file_id in self.tree_a:
1303
for file_id in self.tree_b:
1304
if file_id not in self.tree_a:
1309
for file_id in self.iter_both_tree_ids():
1310
cs_entry = self.make_entry(file_id)
1311
if cs_entry is not None and not cs_entry.is_boring():
1312
cset.add_entry(cs_entry)
1314
for entry in list(cset.entries.itervalues()):
1315
if entry.parent != entry.new_parent:
1316
if not cset.entries.has_key(entry.parent) and\
1317
entry.parent != NULL_ID and entry.parent is not None:
1318
parent_entry = self.make_boring_entry(entry.parent)
1319
cset.add_entry(parent_entry)
1320
if not cset.entries.has_key(entry.new_parent) and\
1321
entry.new_parent != NULL_ID and \
1322
entry.new_parent is not None:
1323
parent_entry = self.make_boring_entry(entry.new_parent)
1324
cset.add_entry(parent_entry)
1327
def iter_inventory(self, tree):
1328
for file_id in tree:
1329
yield self.get_entry(file_id, tree)
1331
def get_entry(self, file_id, tree):
1332
if not tree.has_or_had_id(file_id):
1334
return tree.tree.inventory[file_id]
1336
def get_entry_parent(self, entry):
1339
return entry.parent_id
1341
def get_path(self, file_id, tree):
1342
if not tree.has_or_had_id(file_id):
1344
path = tree.id2path(file_id)
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)
1353
if only_interesting and not self.is_interesting(entry_a, entry_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)
1360
new_path = self.get_path(file_id, self.tree_b)
1362
cs_entry.new_path = new_path
1363
cs_entry.new_parent = new_parent
1366
def is_interesting(self, entry_a, entry_b):
1367
if self._interesting_ids is None:
1369
if entry_a is not None:
1370
file_id = entry_a.file_id
1371
elif entry_b is not None:
1372
file_id = entry_b.file_id
1375
return file_id in self._interesting_ids
1377
def make_boring_entry(self, id):
1378
cs_entry = self.make_basic_entry(id, only_interesting=False)
1379
if cs_entry.is_creation_or_deletion():
1380
return self.make_entry(id, only_interesting=False)
1385
def make_entry(self, id, only_interesting=True):
1386
cs_entry = self.make_basic_entry(id, only_interesting)
1388
if cs_entry is None:
1390
if id in self.tree_a and id in self.tree_b:
1391
a_sha1 = self.tree_a.get_file_sha1(id)
1392
b_sha1 = self.tree_b.get_file_sha1(id)
1393
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1396
full_path_a = self.tree_a.readonly_path(id)
1397
full_path_b = self.tree_b.readonly_path(id)
1398
stat_a = self.lstat(full_path_a)
1399
stat_b = self.lstat(full_path_b)
1401
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1402
cs_entry.contents_change = self.make_contents_change(full_path_a,
1408
def make_mode_change(self, stat_a, stat_b):
1410
if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1411
mode_a = stat_a.st_mode & 0777
1413
if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1414
mode_b = stat_b.st_mode & 0777
1415
if mode_a == mode_b:
1417
return ChangeUnixPermissions(mode_a, mode_b)
1419
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1420
if stat_a is None and stat_b is None:
1422
if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
1423
stat.S_ISDIR(stat_b.st_mode):
1425
if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
1426
stat.S_ISREG(stat_b.st_mode):
1427
if stat_a.st_ino == stat_b.st_ino and \
1428
stat_a.st_dev == stat_b.st_dev:
1431
a_contents = self.get_contents(stat_a, full_path_a)
1432
b_contents = self.get_contents(stat_b, full_path_b)
1433
if a_contents == b_contents:
1435
return ReplaceContents(a_contents, b_contents)
1437
def get_contents(self, stat_result, full_path):
1438
if stat_result is None:
1440
elif stat.S_ISREG(stat_result.st_mode):
1441
return FileCreate(file(full_path, "rb").read())
1442
elif stat.S_ISDIR(stat_result.st_mode):
1444
elif stat.S_ISLNK(stat_result.st_mode):
1445
return SymlinkCreate(os.readlink(full_path))
1447
raise UnsupportedFiletype(full_path, stat_result)
1449
def lstat(self, full_path):
1451
if full_path is not None:
1453
stat_result = os.lstat(full_path)
1455
if e.errno != errno.ENOENT:
1460
def full_path(entry, tree):
1461
return os.path.join(tree.root, entry.path)
1463
def new_delete_entry(entry, tree, inventory, delete):
1464
if entry.path == "":
1467
parent = inventory[dirname(entry.path)].id
1468
cs_entry = ChangesetEntry(parent, entry.path)
1470
cs_entry.new_path = None
1471
cs_entry.new_parent = None
1473
cs_entry.path = None
1474
cs_entry.parent = None
1475
full_path = full_path(entry, tree)
1476
status = os.lstat(full_path)
1477
if stat.S_ISDIR(file_stat.st_mode):
1483
# XXX: Can't we unify this with the regular inventory object
1484
class Inventory(object):
1485
def __init__(self, inventory):
1486
self.inventory = inventory
1487
self.rinventory = None
1489
def get_rinventory(self):
1490
if self.rinventory is None:
1491
self.rinventory = invert_dict(self.inventory)
1492
return self.rinventory
1494
def get_path(self, id):
1495
return self.inventory.get(id)
1497
def get_name(self, id):
1498
path = self.get_path(id)
1502
return os.path.basename(path)
1504
def get_dir(self, id):
1505
path = self.get_path(id)
1510
return os.path.dirname(path)
1512
def get_parent(self, id):
1513
if self.get_path(id) is None:
1515
directory = self.get_dir(id)
1516
if directory == '.':
1518
if directory is None:
1520
return self.get_rinventory().get(directory)