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
21
from bzrlib.osutils import rename
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
28
"""Represent and apply a changeset
30
Conflicts in applying a changeset are represented as exceptions.
33
__docformat__ = "restructuredtext"
37
class OldFailedTreeOp(Exception):
39
Exception.__init__(self, "bzr-tree-change contains files from a"
40
" previous failed merge operation.")
41
def invert_dict(dict):
43
for (key,value) in dict.iteritems():
48
class ChangeUnixPermissions(object):
49
"""This is two-way change, suitable for file modification, creation,
51
def __init__(self, old_mode, new_mode):
52
self.old_mode = old_mode
53
self.new_mode = new_mode
55
def apply(self, filename, conflict_handler, reverse=False):
57
from_mode = self.old_mode
58
to_mode = self.new_mode
60
from_mode = self.new_mode
61
to_mode = self.old_mode
63
current_mode = os.stat(filename).st_mode &0777
65
if e.errno == errno.ENOENT:
66
if conflict_handler.missing_for_chmod(filename) == "skip":
69
current_mode = from_mode
71
if from_mode is not None and current_mode != from_mode:
72
if conflict_handler.wrong_old_perms(filename, from_mode,
73
current_mode) != "continue":
76
if to_mode is not None:
78
os.chmod(filename, to_mode)
80
if e.errno == errno.ENOENT:
81
conflict_handler.missing_for_chmod(filename)
83
def __eq__(self, other):
84
if not isinstance(other, ChangeUnixPermissions):
86
elif self.old_mode != other.old_mode:
88
elif self.new_mode != other.new_mode:
93
def __ne__(self, other):
94
return not (self == other)
97
def dir_create(filename, conflict_handler, reverse):
98
"""Creates the directory, or deletes it if reverse is true. Intended to be
99
used with ReplaceContents.
101
:param filename: The name of the directory to create
103
:param reverse: If true, delete the directory, instead
110
if e.errno != errno.EEXIST:
112
if conflict_handler.dir_exists(filename) == "continue":
115
if e.errno == errno.ENOENT:
116
if conflict_handler.missing_parent(filename)=="continue":
117
file(filename, "wb").write(self.contents)
122
if e.errno != errno.ENOTEMPTY:
124
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
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
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 bzrlib.osutils.lexists(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
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 missing_parent(self, pathname):
1007
parent = os.path.dirname(pathname)
1008
raise Exception("Parent directory missing for %s" % pathname)
1010
def dir_exists(self, pathname):
1011
raise Exception("Directory already exists for %s" % pathname)
1013
def failed_hunks(self, pathname):
1014
raise Exception("Failed to apply some hunks for %s" % pathname)
1016
def target_exists(self, entry, target, old_path):
1017
raise TargetExists(entry, target)
1019
def rename_conflict(self, id, this_name, base_name, other_name):
1020
raise RenameConflict(id, this_name, base_name, other_name)
1022
def move_conflict(self, id, this_dir, base_dir, other_dir):
1023
raise MoveConflict(id, this_dir, base_dir, other_dir)
1025
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1027
raise MergeConflict(this_path)
1029
def permission_conflict(self, this_path, base_path, other_path):
1030
raise MergePermissionConflict(this_path, base_path, other_path)
1032
def wrong_old_contents(self, filename, expected_contents):
1033
raise WrongOldContents(filename)
1035
def rem_contents_conflict(self, filename, this_contents, base_contents):
1036
raise RemoveContentsConflict(filename)
1038
def wrong_old_perms(self, filename, old_perms, new_perms):
1039
raise WrongOldPermissions(filename, old_perms, new_perms)
1041
def rmdir_non_empty(self, filename):
1042
raise DeletingNonEmptyDirectory(filename)
1044
def link_name_exists(self, filename):
1045
raise TargetExists(filename)
1047
def patch_target_missing(self, filename, contents):
1048
raise PatchTargetMissing(filename)
1050
def missing_for_chmod(self, filename):
1051
raise MissingPermsFile(filename)
1053
def missing_for_rm(self, filename, change):
1054
raise MissingForRm(filename)
1056
def missing_for_rename(self, filename):
1057
raise MissingForRename(filename)
1059
def missing_for_merge(self, file_id, other_path):
1060
raise MissingForMerge(other_path)
1062
def new_contents_conflict(self, filename, other_contents):
1063
raise NewContentsConflict(filename)
1068
def apply_changeset(changeset, inventory, dir, conflict_handler=None,
1070
"""Apply a changeset to a directory.
1072
:param changeset: The changes to perform
1073
:type changeset: `Changeset`
1074
:param inventory: The mapping of id to filename for the directory
1075
:type inventory: Dictionary
1076
:param dir: The path of the directory to apply the changes to
1078
:param reverse: If true, apply the changes in reverse
1080
:return: The mapping of the changed entries
1083
if conflict_handler is None:
1084
conflict_handler = ExceptionConflictHandler()
1085
temp_dir = os.path.join(dir, "bzr-tree-change")
1
# Copyright (C) 2006 by Canonical Ltd
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
17
from bzrlib.bundle.serializer import read_bundle
18
import bzrlib.errors as errors
19
import bzrlib.urlutils
20
import bzrlib.transport
23
def read_bundle_from_url(url):
24
"""Read a bundle from a given URL.
26
:return: A BundleReader, may raise NotABundle if the target
27
is not a proper bundle.
29
url, filename = bzrlib.urlutils.split(url, exclude_trailing_slash=False)
31
# A path to a directory was passed in
32
# definitely not a bundle
33
raise errors.NotABundle('A directory cannot be a bundle')
35
# All of this must be in the try/except
36
# Some transports cannot detect that we are trying to read a
37
# directory until we actually issue read() on the handle.
1089
if e.errno == errno.EEXIST:
1093
if e.errno == errno.ENOTEMPTY:
1094
raise OldFailedTreeOp()
1099
#apply changes that don't affect filenames
1100
for entry in changeset.entries.itervalues():
1101
if not entry.is_creation_or_deletion():
1102
path = os.path.join(dir, inventory[entry.id])
1103
entry.apply(path, conflict_handler, reverse)
1105
# Apply renames in stages, to minimize conflicts:
1106
# Only files whose name or parent change are interesting, because their
1107
# target name may exist in the source tree. If a directory's name changes,
1108
# that doesn't make its children interesting.
1109
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1112
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1113
temp_dir, conflict_handler,
1116
rename_to_new_create(changed_inventory, target_entries, inventory,
1117
changeset, dir, conflict_handler, reverse)
1119
return changed_inventory
1122
def apply_changeset_tree(cset, tree, reverse=False):
1124
for entry in tree.source_inventory().itervalues():
1125
inventory[entry.id] = entry.path
1126
new_inventory = apply_changeset(cset, r_inventory, tree.root,
1128
new_entries, remove_entries = \
1129
get_inventory_change(inventory, new_inventory, cset, reverse)
1130
tree.update_source_inventory(new_entries, remove_entries)
1133
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1136
for entry in cset.entries.itervalues():
1137
if entry.needs_rename():
1138
new_path = entry.get_new_path(inventory, cset)
1139
if new_path is None:
1140
remove_entries.append(entry.id)
1142
new_entries[new_path] = entry.id
1143
return new_entries, remove_entries
1146
def print_changeset(cset):
1147
"""Print all non-boring changeset entries
1149
:param cset: The changeset to print
1150
:type cset: `Changeset`
1152
for entry in cset.entries.itervalues():
1153
if entry.is_boring():
1156
print entry.summarize_name(cset)
1158
class CompositionFailure(Exception):
1159
def __init__(self, old_entry, new_entry, problem):
1160
msg = "Unable to conpose entries.\n %s" % problem
1161
Exception.__init__(self, msg)
1163
class IDMismatch(CompositionFailure):
1164
def __init__(self, old_entry, new_entry):
1165
problem = "Attempt to compose entries with different ids: %s and %s" %\
1166
(old_entry.id, new_entry.id)
1167
CompositionFailure.__init__(self, old_entry, new_entry, problem)
1169
def compose_changesets(old_cset, new_cset):
1170
"""Combine two changesets into one. This works well for exact patching.
1171
Otherwise, not so well.
1173
:param old_cset: The first changeset that would be applied
1174
:type old_cset: `Changeset`
1175
:param new_cset: The second changeset that would be applied
1176
:type new_cset: `Changeset`
1177
:return: A changeset that combines the changes in both changesets
1180
composed = Changeset()
1181
for old_entry in old_cset.entries.itervalues():
1182
new_entry = new_cset.entries.get(old_entry.id)
1183
if new_entry is None:
1184
composed.add_entry(old_entry)
1186
composed_entry = compose_entries(old_entry, new_entry)
1187
if composed_entry.parent is not None or\
1188
composed_entry.new_parent is not None:
1189
composed.add_entry(composed_entry)
1190
for new_entry in new_cset.entries.itervalues():
1191
if not old_cset.entries.has_key(new_entry.id):
1192
composed.add_entry(new_entry)
1195
def compose_entries(old_entry, new_entry):
1196
"""Combine two entries into one.
1198
:param old_entry: The first entry that would be applied
1199
:type old_entry: ChangesetEntry
1200
:param old_entry: The second entry that would be applied
1201
:type old_entry: ChangesetEntry
1202
:return: A changeset entry combining both entries
1203
:rtype: `ChangesetEntry`
1205
if old_entry.id != new_entry.id:
1206
raise IDMismatch(old_entry, new_entry)
1207
output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
1209
if (old_entry.parent != old_entry.new_parent or
1210
new_entry.parent != new_entry.new_parent):
1211
output.new_parent = new_entry.new_parent
1213
if (old_entry.path != old_entry.new_path or
1214
new_entry.path != new_entry.new_path):
1215
output.new_path = new_entry.new_path
1217
output.contents_change = compose_contents(old_entry, new_entry)
1218
output.metadata_change = compose_metadata(old_entry, new_entry)
1221
def compose_contents(old_entry, new_entry):
1222
"""Combine the contents of two changeset entries. Entries are combined
1223
intelligently where possible, but the fallback behavior returns an
1226
:param old_entry: The first entry that would be applied
1227
:type old_entry: `ChangesetEntry`
1228
:param new_entry: The second entry that would be applied
1229
:type new_entry: `ChangesetEntry`
1230
:return: A combined contents change
1231
:rtype: anything supporting the apply(reverse=False) method
1233
old_contents = old_entry.contents_change
1234
new_contents = new_entry.contents_change
1235
if old_entry.contents_change is None:
1236
return new_entry.contents_change
1237
elif new_entry.contents_change is None:
1238
return old_entry.contents_change
1239
elif isinstance(old_contents, ReplaceContents) and \
1240
isinstance(new_contents, ReplaceContents):
1241
if old_contents.old_contents == new_contents.new_contents:
1244
return ReplaceContents(old_contents.old_contents,
1245
new_contents.new_contents)
1246
elif isinstance(old_contents, ApplySequence):
1247
output = ApplySequence(old_contents.changes)
1248
if isinstance(new_contents, ApplySequence):
1249
output.changes.extend(new_contents.changes)
1251
output.changes.append(new_contents)
1253
elif isinstance(new_contents, ApplySequence):
1254
output = ApplySequence((old_contents.changes,))
1255
output.extend(new_contents.changes)
1258
return ApplySequence((old_contents, new_contents))
1260
def compose_metadata(old_entry, new_entry):
1261
old_meta = old_entry.metadata_change
1262
new_meta = new_entry.metadata_change
1263
if old_meta is None:
1265
elif new_meta is None:
1267
elif isinstance(old_meta, ChangeUnixPermissions) and \
1268
isinstance(new_meta, ChangeUnixPermissions):
1269
return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1271
return ApplySequence(old_meta, new_meta)
1274
def changeset_is_null(changeset):
1275
for entry in changeset.entries.itervalues():
1276
if not entry.is_boring():
1280
class UnsuppportedFiletype(Exception):
1281
def __init__(self, full_path, stat_result):
1282
msg = "The file \"%s\" is not a supported filetype." % full_path
1283
Exception.__init__(self, msg)
1284
self.full_path = full_path
1285
self.stat_result = stat_result
1287
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1288
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1290
class ChangesetGenerator(object):
1291
def __init__(self, tree_a, tree_b, interesting_ids=None):
1292
object.__init__(self)
1293
self.tree_a = tree_a
1294
self.tree_b = tree_b
1295
self._interesting_ids = interesting_ids
1297
def iter_both_tree_ids(self):
1298
for file_id in self.tree_a:
1300
for file_id in self.tree_b:
1301
if file_id not in self.tree_a:
1306
for file_id in self.iter_both_tree_ids():
1307
cs_entry = self.make_entry(file_id)
1308
if cs_entry is not None and not cs_entry.is_boring():
1309
cset.add_entry(cs_entry)
1311
for entry in list(cset.entries.itervalues()):
1312
if entry.parent != entry.new_parent:
1313
if not cset.entries.has_key(entry.parent) and\
1314
entry.parent != NULL_ID and entry.parent is not None:
1315
parent_entry = self.make_boring_entry(entry.parent)
1316
cset.add_entry(parent_entry)
1317
if not cset.entries.has_key(entry.new_parent) and\
1318
entry.new_parent != NULL_ID and \
1319
entry.new_parent is not None:
1320
parent_entry = self.make_boring_entry(entry.new_parent)
1321
cset.add_entry(parent_entry)
1324
def iter_inventory(self, tree):
1325
for file_id in tree:
1326
yield self.get_entry(file_id, tree)
1328
def get_entry(self, file_id, tree):
1329
if not tree.has_or_had_id(file_id):
1331
return tree.tree.inventory[file_id]
1333
def get_entry_parent(self, entry):
1336
return entry.parent_id
1338
def get_path(self, file_id, tree):
1339
if not tree.has_or_had_id(file_id):
1341
path = tree.id2path(file_id)
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)
1350
if only_interesting and not self.is_interesting(entry_a, entry_b):
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)
1357
new_path = self.get_path(file_id, self.tree_b)
1359
cs_entry.new_path = new_path
1360
cs_entry.new_parent = new_parent
1363
def is_interesting(self, entry_a, entry_b):
1364
if self._interesting_ids is None:
1366
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
1372
return file_id in self._interesting_ids
1374
def make_boring_entry(self, id):
1375
cs_entry = self.make_basic_entry(id, only_interesting=False)
1376
if cs_entry.is_creation_or_deletion():
1377
return self.make_entry(id, only_interesting=False)
1382
def make_entry(self, id, only_interesting=True):
1383
cs_entry = self.make_basic_entry(id, only_interesting)
1385
if cs_entry is None:
1388
full_path_a = self.tree_a.readonly_path(id)
1389
full_path_b = self.tree_b.readonly_path(id)
1390
stat_a = self.lstat(full_path_a)
1391
stat_b = self.lstat(full_path_b)
1393
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
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:
1401
cs_entry.contents_change = self.make_contents_change(full_path_a,
1407
def make_mode_change(self, stat_a, stat_b):
1409
if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1410
mode_a = stat_a.st_mode & 0777
1412
if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1413
mode_b = stat_b.st_mode & 0777
1414
if mode_a == mode_b:
1416
return ChangeUnixPermissions(mode_a, mode_b)
1418
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1419
if stat_a is None and stat_b is None:
1421
if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
1422
stat.S_ISDIR(stat_b.st_mode):
1424
if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
1425
stat.S_ISREG(stat_b.st_mode):
1426
if stat_a.st_ino == stat_b.st_ino and \
1427
stat_a.st_dev == stat_b.st_dev:
1430
a_contents = self.get_contents(stat_a, full_path_a)
1431
b_contents = self.get_contents(stat_b, full_path_b)
1432
if a_contents == b_contents:
1434
return ReplaceContents(a_contents, b_contents)
1436
def get_contents(self, stat_result, full_path):
1437
if stat_result is None:
1439
elif stat.S_ISREG(stat_result.st_mode):
1440
return FileCreate(file(full_path, "rb").read())
1441
elif stat.S_ISDIR(stat_result.st_mode):
1443
elif stat.S_ISLNK(stat_result.st_mode):
1444
return SymlinkCreate(os.readlink(full_path))
1446
raise UnsupportedFiletype(full_path, stat_result)
1448
def lstat(self, full_path):
1450
if full_path is not None:
1452
stat_result = os.lstat(full_path)
1454
if e.errno != errno.ENOENT:
1459
def full_path(entry, tree):
1460
return os.path.join(tree.root, entry.path)
1462
def new_delete_entry(entry, tree, inventory, delete):
1463
if entry.path == "":
1466
parent = inventory[dirname(entry.path)].id
1467
cs_entry = ChangesetEntry(parent, entry.path)
1469
cs_entry.new_path = None
1470
cs_entry.new_parent = None
1472
cs_entry.path = None
1473
cs_entry.parent = None
1474
full_path = full_path(entry, tree)
1475
status = os.lstat(full_path)
1476
if stat.S_ISDIR(file_stat.st_mode):
1482
# XXX: Can't we unify this with the regular inventory object
1483
class Inventory(object):
1484
def __init__(self, inventory):
1485
self.inventory = inventory
1486
self.rinventory = None
1488
def get_rinventory(self):
1489
if self.rinventory is None:
1490
self.rinventory = invert_dict(self.inventory)
1491
return self.rinventory
1493
def get_path(self, id):
1494
return self.inventory.get(id)
1496
def get_name(self, id):
1497
path = self.get_path(id)
1501
return os.path.basename(path)
1503
def get_dir(self, id):
1504
path = self.get_path(id)
1509
return os.path.dirname(path)
1511
def get_parent(self, id):
1512
if self.get_path(id) is None:
1514
directory = self.get_dir(id)
1515
if directory == '.':
1517
if directory is None:
1519
return self.get_rinventory().get(directory)
39
t = bzrlib.transport.get_transport(url)
42
except (errors.TransportError, errors.PathError), e:
43
raise errors.NotABundle(str(e))