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
17
"""Represent and apply a changeset.
19
Conflicts in applying a changeset are represented as exceptions.
21
This only handles the in-memory objects representing changesets, which are
22
primarily used by the merge code.
28
from shutil import rmtree
29
from itertools import izip
31
from bzrlib.trace import mutter, warning
32
from bzrlib.osutils import rename, sha_file, pathjoin, mkdtemp
34
from bzrlib.errors import BzrCheckError
36
__docformat__ = "restructuredtext"
42
class OldFailedTreeOp(Exception):
44
Exception.__init__(self, "bzr-tree-change contains files from a"
45
" previous failed merge operation.")
48
def invert_dict(dict):
50
for (key,value) in dict.iteritems():
55
class ChangeExecFlag(object):
56
"""This is two-way change, suitable for file modification, creation,
58
def __init__(self, old_exec_flag, new_exec_flag):
59
self.old_exec_flag = old_exec_flag
60
self.new_exec_flag = new_exec_flag
62
def apply(self, filename, conflict_handler):
63
from_exec_flag = self.old_exec_flag
64
to_exec_flag = self.new_exec_flag
66
current_exec_flag = bool(os.stat(filename).st_mode & 0111)
68
if e.errno == errno.ENOENT:
69
if conflict_handler.missing_for_exec_flag(filename) == "skip":
72
current_exec_flag = from_exec_flag
74
if from_exec_flag is not None and current_exec_flag != from_exec_flag:
75
if conflict_handler.wrong_old_exec_flag(filename,
76
from_exec_flag, current_exec_flag) != "continue":
79
if to_exec_flag is not None:
80
current_mode = os.stat(filename).st_mode
84
to_mode = current_mode | (0100 & ~umask)
85
# Enable x-bit for others only if they can read it.
86
if current_mode & 0004:
87
to_mode |= 0001 & ~umask
88
if current_mode & 0040:
89
to_mode |= 0010 & ~umask
91
to_mode = current_mode & ~0111
93
os.chmod(filename, to_mode)
95
if e.errno == errno.ENOENT:
96
conflict_handler.missing_for_exec_flag(filename)
98
def __eq__(self, other):
99
return (isinstance(other, ChangeExecFlag) and
100
self.old_exec_flag == other.old_exec_flag and
101
self.new_exec_flag == other.new_exec_flag)
103
def __ne__(self, other):
104
return not (self == other)
107
def dir_create(filename, conflict_handler, reverse=False):
108
"""Creates the directory, or deletes it if reverse is true. Intended to be
109
used with ReplaceContents.
111
:param filename: The name of the directory to create
113
:param reverse: If true, delete the directory, instead
120
if e.errno != errno.EEXIST:
122
if conflict_handler.dir_exists(filename) == "continue":
125
if e.errno == errno.ENOENT:
126
if conflict_handler.missing_parent(filename)=="continue":
127
file(filename, "wb").write(self.contents)
132
if e.errno != errno.ENOTEMPTY:
134
if conflict_handler.rmdir_non_empty(filename) == "skip":
139
class SymlinkCreate(object):
140
"""Creates or deletes a symlink (for use with ReplaceContents)"""
141
def __init__(self, contents):
144
:param contents: The filename of the target the symlink should point to
147
self.target = contents
150
return "SymlinkCreate(%s)" % self.target
152
def __call__(self, filename, conflict_handler, reverse=False):
153
"""Creates or destroys the symlink.
155
:param filename: The name of the symlink to create
159
assert(os.readlink(filename) == self.target)
163
os.symlink(self.target, filename)
165
if e.errno != errno.EEXIST:
167
if conflict_handler.link_name_exists(filename) == "continue":
168
os.symlink(self.target, filename)
170
def __eq__(self, other):
171
if not isinstance(other, SymlinkCreate):
173
elif self.target != other.target:
178
def __ne__(self, other):
179
return not (self == other)
182
class FileCreate(object):
183
"""Create or delete a file (for use with ReplaceContents)"""
184
def __init__(self, contents):
187
:param contents: The contents of the file to write
190
self.contents = contents
193
return "FileCreate(%i b)" % len(self.contents)
195
def __eq__(self, other):
196
if not isinstance(other, FileCreate):
198
elif self.contents != other.contents:
203
def __ne__(self, other):
204
return not (self == other)
206
def __call__(self, filename, conflict_handler, reverse=False):
207
"""Create or delete a file
209
:param filename: The name of the file to create
211
:param reverse: Delete the file instead of creating it
216
file(filename, "wb").write(self.contents)
218
if e.errno == errno.ENOENT:
219
if conflict_handler.missing_parent(filename)=="continue":
220
file(filename, "wb").write(self.contents)
226
if (file(filename, "rb").read() != self.contents):
227
direction = conflict_handler.wrong_old_contents(filename,
229
if direction != "continue":
233
if e.errno != errno.ENOENT:
235
if conflict_handler.missing_for_rm(filename, undo) == "skip":
239
class TreeFileCreate(object):
240
"""Create or delete a file (for use with ReplaceContents)"""
241
def __init__(self, tree, file_id):
244
:param contents: The contents of the file to write
248
self.file_id = file_id
251
return "TreeFileCreate(%s)" % self.file_id
253
def __eq__(self, other):
254
if not isinstance(other, TreeFileCreate):
256
return self.tree.get_file_sha1(self.file_id) == \
257
other.tree.get_file_sha1(other.file_id)
259
def __ne__(self, other):
260
return not (self == other)
262
def write_file(self, filename):
263
outfile = file(filename, "wb")
264
for line in self.tree.get_file(self.file_id):
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)
271
def __call__(self, filename, conflict_handler, reverse=False):
272
"""Create or delete a file
274
:param filename: The name of the file to create
276
:param reverse: Delete the file instead of creating it
281
self.write_file(filename)
283
if e.errno == errno.ENOENT:
284
if conflict_handler.missing_parent(filename)=="continue":
285
self.write_file(filename)
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":
298
if e.errno != errno.ENOENT:
300
if conflict_handler.missing_for_rm(filename, undo) == "skip":
304
class ReplaceContents(object):
305
"""A contents-replacement framework. It allows a file/directory/symlink to
306
be created, deleted, or replaced with another file/directory/symlink.
307
Arguments must be callable with (filename, reverse).
309
def __init__(self, old_contents, new_contents):
312
:param old_contents: The change to reverse apply (e.g. a deletion), \
314
:type old_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
316
:param new_contents: The second change to apply (e.g. a creation), \
318
:type new_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
321
self.old_contents=old_contents
322
self.new_contents=new_contents
325
return "ReplaceContents(%r -> %r)" % (self.old_contents,
328
def __eq__(self, other):
329
if not isinstance(other, ReplaceContents):
331
elif self.old_contents != other.old_contents:
333
elif self.new_contents != other.new_contents:
337
def __ne__(self, other):
338
return not (self == other)
340
def apply(self, filename, conflict_handler):
341
"""Applies the FileReplacement to the specified filename
343
:param filename: The name of the file to apply changes to
346
undo = self.old_contents
347
perform = self.new_contents
351
mode = os.lstat(filename).st_mode
352
if stat.S_ISLNK(mode):
355
if e.errno != errno.ENOENT:
357
if conflict_handler.missing_for_rm(filename, undo) == "skip":
359
undo(filename, conflict_handler, reverse=True)
360
if perform is not None:
361
perform(filename, conflict_handler)
363
os.chmod(filename, mode)
365
def is_creation(self):
366
return self.new_contents is not None and self.old_contents is None
368
def is_deletion(self):
369
return self.old_contents is not None and self.new_contents is None
372
class Diff3Merge(object):
373
history_based = False
374
def __init__(self, file_id, base, other):
375
self.file_id = file_id
379
def is_creation(self):
382
def is_deletion(self):
385
def __eq__(self, other):
386
if not isinstance(other, Diff3Merge):
388
return (self.base == other.base and
389
self.other == other.other and self.file_id == other.file_id)
391
def __ne__(self, other):
392
return not (self == other)
394
def dump_file(self, temp_dir, name, tree):
395
out_path = pathjoin(temp_dir, name)
396
out_file = file(out_path, "wb")
397
in_file = tree.get_file(self.file_id)
402
def apply(self, filename, conflict_handler):
404
temp_dir = mkdtemp(prefix="bzr-", dir=os.path.dirname(filename))
406
new_file = os.path.join(temp_dir, filename)
407
base_file = self.dump_file(temp_dir, "base", self.base)
408
other_file = self.dump_file(temp_dir, "other", self.other)
411
status = bzrlib.patch.diff3(new_file, filename, base, other)
413
os.chmod(new_file, os.stat(filename).st_mode)
414
rename(new_file, filename)
418
def get_lines(filename):
419
my_file = file(filename, "rb")
420
lines = my_file.readlines()
423
base_lines = get_lines(base)
424
other_lines = get_lines(other)
425
conflict_handler.merge_conflict(new_file, filename, base_lines,
432
"""Convenience function to create a directory.
434
:return: A ReplaceContents that will create a directory
435
:rtype: `ReplaceContents`
437
return ReplaceContents(None, dir_create)
441
"""Convenience function to delete a directory.
443
:return: A ReplaceContents that will delete a directory
444
:rtype: `ReplaceContents`
446
return ReplaceContents(dir_create, None)
449
def CreateFile(contents):
450
"""Convenience fucntion to create a file.
452
:param contents: The contents of the file to create
454
:return: A ReplaceContents that will create a file
455
:rtype: `ReplaceContents`
457
return ReplaceContents(None, FileCreate(contents))
460
def DeleteFile(contents):
461
"""Convenience fucntion to delete a file.
463
:param contents: The contents of the file to delete
465
:return: A ReplaceContents that will delete a file
466
:rtype: `ReplaceContents`
468
return ReplaceContents(FileCreate(contents), None)
471
def ReplaceFileContents(old_tree, new_tree, file_id):
472
"""Convenience fucntion to replace the contents of a file.
474
:param old_contents: The contents of the file to replace
475
:type old_contents: str
476
:param new_contents: The contents to replace the file with
477
:type new_contents: str
478
:return: A ReplaceContents that will replace the contents of a file a file
479
:rtype: `ReplaceContents`
481
return ReplaceContents(TreeFileCreate(old_tree, file_id),
482
TreeFileCreate(new_tree, file_id))
485
def CreateSymlink(target):
486
"""Convenience fucntion to create a symlink.
488
:param target: The path the link should point to
490
:return: A ReplaceContents that will delete a file
491
:rtype: `ReplaceContents`
493
return ReplaceContents(None, SymlinkCreate(target))
496
def DeleteSymlink(target):
497
"""Convenience fucntion to delete a symlink.
499
:param target: The path the link should point to
501
:return: A ReplaceContents that will delete a file
502
:rtype: `ReplaceContents`
504
return ReplaceContents(SymlinkCreate(target), None)
507
def ChangeTarget(old_target, new_target):
508
"""Convenience fucntion to change the target of a symlink.
510
:param old_target: The current link target
511
:type old_target: str
512
:param new_target: The new link target to use
513
:type new_target: str
514
:return: A ReplaceContents that will delete a file
515
:rtype: `ReplaceContents`
517
return ReplaceContents(SymlinkCreate(old_target), SymlinkCreate(new_target))
520
class InvalidEntry(Exception):
521
"""Raise when a ChangesetEntry is invalid in some way"""
522
def __init__(self, entry, problem):
525
:param entry: The invalid ChangesetEntry
526
:type entry: `ChangesetEntry`
527
:param problem: The problem with the entry
530
msg = "Changeset entry for %s (%s) is invalid.\n%s" % (entry.id,
533
Exception.__init__(self, msg)
537
class SourceRootHasName(InvalidEntry):
538
"""This changeset entry has a name other than "", but its parent is !NULL"""
539
def __init__(self, entry, name):
542
:param entry: The invalid ChangesetEntry
543
:type entry: `ChangesetEntry`
544
:param name: The name of the entry
547
msg = 'Child of !NULL is named "%s", not "./.".' % name
548
InvalidEntry.__init__(self, entry, msg)
551
class NullIDAssigned(InvalidEntry):
552
"""The id !NULL was assigned to a real entry"""
553
def __init__(self, entry):
556
:param entry: The invalid ChangesetEntry
557
:type entry: `ChangesetEntry`
559
msg = '"!NULL" id assigned to a file "%s".' % entry.path
560
InvalidEntry.__init__(self, entry, msg)
563
class ParentIDIsSelf(InvalidEntry):
564
"""An entry is marked as its own parent"""
565
def __init__(self, entry):
568
:param entry: The invalid ChangesetEntry
569
:type entry: `ChangesetEntry`
571
msg = 'file %s has "%s" id for both self id and parent id.' % \
572
(entry.path, entry.id)
573
InvalidEntry.__init__(self, entry, msg)
576
class ChangesetEntry(object):
577
"""An entry the changeset"""
578
def __init__(self, id, parent, path):
579
"""Constructor. Sets parent and name assuming it was not
580
renamed/created/deleted.
581
:param id: The id associated with the entry
582
:param parent: The id of the parent of this entry (or !NULL if no
584
:param path: The file path relative to the tree root of this entry
590
self.new_parent = parent
591
self.contents_change = None
592
self.metadata_change = None
593
if parent == NULL_ID and path !='./.':
594
raise SourceRootHasName(self, path)
595
if self.id == NULL_ID:
596
raise NullIDAssigned(self)
597
if self.id == self.parent:
598
raise ParentIDIsSelf(self)
601
return "ChangesetEntry(%s)" % self.id
606
if self.path is None:
608
return os.path.dirname(self.path)
610
def __set_dir(self, dir):
611
self.path = pathjoin(dir, os.path.basename(self.path))
613
dir = property(__get_dir, __set_dir)
615
def __get_name(self):
616
if self.path is None:
618
return os.path.basename(self.path)
620
def __set_name(self, name):
621
self.path = pathjoin(os.path.dirname(self.path), name)
623
name = property(__get_name, __set_name)
625
def __get_new_dir(self):
626
if self.new_path is None:
628
return os.path.dirname(self.new_path)
630
def __set_new_dir(self, dir):
631
self.new_path = pathjoin(dir, os.path.basename(self.new_path))
633
new_dir = property(__get_new_dir, __set_new_dir)
635
def __get_new_name(self):
636
if self.new_path is None:
638
return os.path.basename(self.new_path)
640
def __set_new_name(self, name):
641
self.new_path = pathjoin(os.path.dirname(self.new_path), name)
643
new_name = property(__get_new_name, __set_new_name)
645
def needs_rename(self):
646
"""Determines whether the entry requires renaming.
650
if None in (self.parent, self.new_parent, self.name, self.new_name):
652
return (self.parent != self.new_parent or self.name != self.new_name)
654
def is_deletion(self, reverse=False):
655
"""Return true if applying the entry would delete a file/directory.
657
:param reverse: if true, the changeset is being applied in reverse
660
return self.is_creation(not reverse)
662
def is_creation(self, reverse=False):
663
"""Return true if applying the entry would create a file/directory.
665
:param reverse: if true, the changeset is being applied in reverse
668
if self.contents_change is None:
671
return self.contents_change.is_deletion()
673
return self.contents_change.is_creation()
675
def is_creation_or_deletion(self):
676
"""Return true if applying the entry would create or delete a
681
return self.is_creation() or self.is_deletion()
683
def get_cset_path(self, mod=False):
684
"""Determine the path of the entry according to the changeset.
686
:param changeset: The changeset to derive the path from
687
:type changeset: `Changeset`
688
:param mod: If true, generate the MOD path. Otherwise, generate the \
690
:return: the path of the entry, or None if it did not exist in the \
692
:rtype: str or NoneType
695
if self.new_parent == NULL_ID:
697
elif self.new_parent is None:
701
if self.parent == NULL_ID:
703
elif self.parent is None:
707
def summarize_name(self):
708
"""Produce a one-line summary of the filename. Indicates renames as
709
old => new, indicates creation as None => new, indicates deletion as
714
orig_path = self.get_cset_path(False)
715
mod_path = self.get_cset_path(True)
716
if orig_path and orig_path.startswith('./'):
717
orig_path = orig_path[2:]
718
if mod_path and mod_path.startswith('./'):
719
mod_path = mod_path[2:]
720
if orig_path == mod_path:
723
return "%s => %s" % (orig_path, mod_path)
725
def get_new_path(self, id_map, changeset):
726
"""Determine the full pathname to rename to
728
:param id_map: The map of ids to filenames for the tree
729
:type id_map: Dictionary
730
:param changeset: The changeset to get data from
731
:type changeset: `Changeset`
734
mutter("Finding new path for %s", self.summarize_name())
735
parent = self.new_parent
736
to_dir = self.new_dir
738
to_name = self.new_name
739
from_name = self.name
744
if parent == NULL_ID or parent is None:
746
raise SourceRootHasName(self, to_name)
749
parent_entry = changeset.entries.get(parent)
750
if parent_entry is None:
751
dir = os.path.dirname(id_map[self.id])
753
mutter("path, new_path: %r %r", self.path, self.new_path)
754
dir = parent_entry.get_new_path(id_map, changeset)
755
if from_name == to_name:
756
name = os.path.basename(id_map[self.id])
759
assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
760
return pathjoin(dir, name)
763
"""Determines whether the entry does nothing
765
:return: True if the entry does no renames or content changes
768
if self.contents_change is not None:
770
elif self.metadata_change is not None:
772
elif self.parent != self.new_parent:
774
elif self.name != self.new_name:
779
def apply(self, filename, conflict_handler):
780
"""Applies the file content and/or metadata changes.
782
:param filename: the filename of the entry
785
if self.is_deletion() and self.metadata_change is not None:
786
self.metadata_change.apply(filename, conflict_handler)
787
if self.contents_change is not None:
788
self.contents_change.apply(filename, conflict_handler)
789
if not self.is_deletion() and self.metadata_change is not None:
790
self.metadata_change.apply(filename, conflict_handler)
793
class IDPresent(Exception):
794
def __init__(self, id):
795
msg = "Cannot add entry because that id has already been used:\n%s" %\
797
Exception.__init__(self, msg)
801
class Changeset(object):
802
"""A set of changes to apply"""
806
def add_entry(self, entry):
807
"""Add an entry to the list of entries"""
808
if self.entries.has_key(entry.id):
809
raise IDPresent(entry.id)
810
self.entries[entry.id] = entry
813
def get_rename_entries(changeset, inventory):
814
"""Return a list of entries that will be renamed. Entries are sorted from
815
longest to shortest source path and from shortest to longest target path.
817
:param changeset: The changeset to look in
818
:type changeset: `Changeset`
819
:param inventory: The source of current tree paths for the given ids
820
:type inventory: Dictionary
821
:return: source entries and target entries as a tuple
824
source_entries = [x for x in changeset.entries.itervalues()
825
if x.needs_rename() or x.is_creation_or_deletion()]
826
# these are done from longest path to shortest, to avoid deleting a
827
# parent before its children are deleted/renamed
828
def longest_to_shortest(entry):
829
path = inventory.get(entry.id)
834
source_entries.sort(None, longest_to_shortest, True)
836
target_entries = source_entries[:]
837
# These are done from shortest to longest path, to avoid creating a
838
# child before its parent has been created/renamed
839
def shortest_to_longest(entry):
840
path = entry.get_new_path(inventory, changeset)
845
target_entries.sort(None, shortest_to_longest)
846
return (source_entries, target_entries)
849
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
851
"""Delete and rename entries as appropriate. Entries are renamed to temp
852
names. A map of id -> temp name (or None, for deletions) is returned.
854
:param source_entries: The entries to rename and delete
855
:type source_entries: List of `ChangesetEntry`
856
:param inventory: The map of id -> filename in the current tree
857
:type inventory: Dictionary
858
:param dir: The directory to apply changes to
860
:return: a mapping of id to temporary name
864
for i in range(len(source_entries)):
865
entry = source_entries[i]
866
if entry.is_deletion():
867
path = pathjoin(dir, inventory[entry.id])
868
entry.apply(path, conflict_handler)
869
temp_name[entry.id] = None
871
elif entry.needs_rename():
872
if entry.is_creation():
874
to_name = pathjoin(temp_dir, str(i))
875
src_path = inventory.get(entry.id)
876
if src_path is not None:
877
src_path = pathjoin(dir, src_path)
879
rename(src_path, to_name)
880
temp_name[entry.id] = to_name
882
if e.errno != errno.ENOENT:
884
if conflict_handler.missing_for_rename(src_path, to_name) \
891
def rename_to_new_create(changed_inventory, target_entries, inventory,
892
changeset, dir, conflict_handler):
893
"""Rename entries with temp names to their final names, create new files.
895
:param changed_inventory: A mapping of id to temporary name
896
:type changed_inventory: Dictionary
897
:param target_entries: The entries to apply changes to
898
:type target_entries: List of `ChangesetEntry`
899
:param changeset: The changeset to apply
900
:type changeset: `Changeset`
901
:param dir: The directory to apply changes to
904
for entry in target_entries:
905
new_tree_path = entry.get_new_path(inventory, changeset)
906
if new_tree_path is None:
908
new_path = pathjoin(dir, new_tree_path)
909
old_path = changed_inventory.get(entry.id)
910
if bzrlib.osutils.lexists(new_path):
911
if conflict_handler.target_exists(entry, new_path, old_path) == \
914
if entry.is_creation():
915
entry.apply(new_path, conflict_handler)
916
changed_inventory[entry.id] = new_tree_path
917
elif entry.needs_rename():
918
if entry.is_deletion():
923
mutter('rename %s to final name %s', old_path, new_path)
924
rename(old_path, new_path)
925
changed_inventory[entry.id] = new_tree_path
927
raise BzrCheckError('failed to rename %s to %s for changeset entry %s: %s'
928
% (old_path, new_path, entry, e))
931
class TargetExists(Exception):
932
def __init__(self, entry, target):
933
msg = "The path %s already exists" % target
934
Exception.__init__(self, msg)
939
class RenameConflict(Exception):
940
def __init__(self, id, this_name, base_name, other_name):
941
msg = """Trees all have different names for a file
945
id: %s""" % (this_name, base_name, other_name, id)
946
Exception.__init__(self, msg)
947
self.this_name = this_name
948
self.base_name = base_name
949
self_other_name = other_name
952
class MoveConflict(Exception):
953
def __init__(self, id, this_parent, base_parent, other_parent):
954
msg = """The file is in different directories in every tree
958
id: %s""" % (this_parent, base_parent, other_parent, id)
959
Exception.__init__(self, msg)
960
self.this_parent = this_parent
961
self.base_parent = base_parent
962
self_other_parent = other_parent
965
class MergeConflict(Exception):
966
def __init__(self, this_path):
967
Exception.__init__(self, "Conflict applying changes to %s" % this_path)
968
self.this_path = this_path
971
class WrongOldContents(Exception):
972
def __init__(self, filename):
973
msg = "Contents mismatch deleting %s" % filename
974
self.filename = filename
975
Exception.__init__(self, msg)
978
class WrongOldExecFlag(Exception):
979
def __init__(self, filename, old_exec_flag, new_exec_flag):
980
msg = "Executable flag missmatch on %s:\n" \
981
"Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
982
self.filename = filename
983
Exception.__init__(self, msg)
986
class RemoveContentsConflict(Exception):
987
def __init__(self, filename):
988
msg = "Conflict deleting %s, which has different contents in BASE"\
989
" and THIS" % filename
990
self.filename = filename
991
Exception.__init__(self, msg)
994
class DeletingNonEmptyDirectory(Exception):
995
def __init__(self, filename):
996
msg = "Trying to remove dir %s while it still had files" % filename
997
self.filename = filename
998
Exception.__init__(self, msg)
1001
class PatchTargetMissing(Exception):
1002
def __init__(self, filename):
1003
msg = "Attempt to patch %s, which does not exist" % filename
1004
Exception.__init__(self, msg)
1005
self.filename = filename
1008
class MissingForSetExec(Exception):
1009
def __init__(self, filename):
1010
msg = "Attempt to change permissions on %s, which does not exist" %\
1012
Exception.__init__(self, msg)
1013
self.filename = filename
1016
class MissingForRm(Exception):
1017
def __init__(self, filename):
1018
msg = "Attempt to remove missing path %s" % filename
1019
Exception.__init__(self, msg)
1020
self.filename = filename
1023
class MissingForRename(Exception):
1024
def __init__(self, filename, to_path):
1025
msg = "Attempt to move missing path %s to %s" % (filename, to_path)
1026
Exception.__init__(self, msg)
1027
self.filename = filename
1030
class NewContentsConflict(Exception):
1031
def __init__(self, filename):
1032
msg = "Conflicting contents for new file %s" % (filename)
1033
Exception.__init__(self, msg)
1036
class WeaveMergeConflict(Exception):
1037
def __init__(self, filename):
1038
msg = "Conflicting contents for file %s" % (filename)
1039
Exception.__init__(self, msg)
1042
class ThreewayContentsConflict(Exception):
1043
def __init__(self, filename):
1044
msg = "Conflicting contents for file %s" % (filename)
1045
Exception.__init__(self, msg)
1048
class MissingForMerge(Exception):
1049
def __init__(self, filename):
1050
msg = "The file %s was modified, but does not exist in this tree"\
1052
Exception.__init__(self, msg)
1055
class ExceptionConflictHandler(object):
1056
"""Default handler for merge exceptions.
1058
This throws an error on any kind of conflict. Conflict handlers can
1059
descend from this class if they have a better way to handle some or
1060
all types of conflict.
1062
def missing_parent(self, pathname):
1063
parent = os.path.dirname(pathname)
1064
raise Exception("Parent directory missing for %s" % pathname)
1066
def dir_exists(self, pathname):
1067
raise Exception("Directory already exists for %s" % pathname)
1069
def failed_hunks(self, pathname):
1070
raise Exception("Failed to apply some hunks for %s" % pathname)
1072
def target_exists(self, entry, target, old_path):
1073
raise TargetExists(entry, target)
1075
def rename_conflict(self, id, this_name, base_name, other_name):
1076
raise RenameConflict(id, this_name, base_name, other_name)
1078
def move_conflict(self, id, this_dir, base_dir, other_dir):
1079
raise MoveConflict(id, this_dir, base_dir, other_dir)
1081
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1083
raise MergeConflict(this_path)
1085
def wrong_old_contents(self, filename, expected_contents):
1086
raise WrongOldContents(filename)
1088
def rem_contents_conflict(self, filename, this_contents, base_contents):
1089
raise RemoveContentsConflict(filename)
1091
def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1092
raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
1094
def rmdir_non_empty(self, filename):
1095
raise DeletingNonEmptyDirectory(filename)
1097
def link_name_exists(self, filename):
1098
raise TargetExists(filename)
1100
def patch_target_missing(self, filename, contents):
1101
raise PatchTargetMissing(filename)
1103
def missing_for_exec_flag(self, filename):
1104
raise MissingForExecFlag(filename)
1106
def missing_for_rm(self, filename, change):
1107
raise MissingForRm(filename)
1109
def missing_for_rename(self, filename, to_path):
1110
raise MissingForRename(filename, to_path)
1112
def missing_for_merge(self, file_id, other_path):
1113
raise MissingForMerge(other_path)
1115
def new_contents_conflict(self, filename, other_contents):
1116
raise NewContentsConflict(filename)
1118
def weave_merge_conflict(self, filename, weave, other_i, out_file):
1119
raise WeaveMergeConflict(filename)
1121
def threeway_contents_conflict(self, filename, this_contents,
1122
base_contents, other_contents):
1123
raise ThreewayContentsConflict(filename)
1129
def apply_changeset(changeset, inventory, dir, conflict_handler=None):
1130
"""Apply a changeset to a directory.
1132
:param changeset: The changes to perform
1133
:type changeset: `Changeset`
1134
:param inventory: The mapping of id to filename for the directory
1135
:type inventory: Dictionary
1136
:param dir: The path of the directory to apply the changes to
1138
:return: The mapping of the changed entries
1141
if conflict_handler is None:
1142
conflict_handler = ExceptionConflictHandler()
1143
temp_dir = pathjoin(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.
1147
if e.errno == errno.EEXIST:
1151
if e.errno == errno.ENOTEMPTY:
1152
raise OldFailedTreeOp()
1157
#apply changes that don't affect filenames
1158
for entry in changeset.entries.itervalues():
1159
if not entry.is_creation_or_deletion() and not entry.is_boring():
1160
if entry.id not in inventory:
1161
warning("entry {%s} no longer present, can't be updated",
1164
path = pathjoin(dir, inventory[entry.id])
1165
entry.apply(path, conflict_handler)
1167
# Apply renames in stages, to minimize conflicts:
1168
# Only files whose name or parent change are interesting, because their
1169
# target name may exist in the source tree. If a directory's name changes,
1170
# that doesn't make its children interesting.
1171
(source_entries, target_entries) = get_rename_entries(changeset, inventory)
1173
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1174
temp_dir, conflict_handler)
1176
rename_to_new_create(changed_inventory, target_entries, inventory,
1177
changeset, dir, conflict_handler)
1179
return changed_inventory
1182
def print_changeset(cset):
1183
"""Print all non-boring changeset entries
1185
:param cset: The changeset to print
1186
:type cset: `Changeset`
1188
for entry in cset.entries.itervalues():
1189
if entry.is_boring():
1192
print entry.summarize_name(cset)
1195
class UnsupportedFiletype(Exception):
1196
def __init__(self, kind, full_path):
1197
msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1199
Exception.__init__(self, msg)
1200
self.full_path = full_path
1204
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1205
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1208
class ChangesetGenerator(object):
1209
def __init__(self, tree_a, tree_b, interesting_ids=None):
1210
object.__init__(self)
1211
self.tree_a = tree_a
1212
self.tree_b = tree_b
1213
self._interesting_ids = interesting_ids
1215
def iter_both_tree_ids(self):
1216
for file_id in self.tree_a:
1218
for file_id in self.tree_b:
1219
if file_id not in self.tree_a:
1224
for file_id in self.iter_both_tree_ids():
1225
cs_entry = self.make_entry(file_id)
1226
if cs_entry is not None and not cs_entry.is_boring():
1227
cset.add_entry(cs_entry)
1229
for entry in list(cset.entries.itervalues()):
1230
if entry.parent != entry.new_parent:
1231
if not cset.entries.has_key(entry.parent) and\
1232
entry.parent != NULL_ID and entry.parent is not None:
1233
parent_entry = self.make_boring_entry(entry.parent)
1234
cset.add_entry(parent_entry)
1235
if not cset.entries.has_key(entry.new_parent) and\
1236
entry.new_parent != NULL_ID and \
1237
entry.new_parent is not None:
1238
parent_entry = self.make_boring_entry(entry.new_parent)
1239
cset.add_entry(parent_entry)
1242
def iter_inventory(self, tree):
1243
for file_id in tree:
1244
yield self.get_entry(file_id, tree)
1246
def get_entry(self, file_id, tree):
1247
if not tree.has_or_had_id(file_id):
1249
return tree.inventory[file_id]
1251
def get_entry_parent(self, entry):
1254
return entry.parent_id
1256
def get_path(self, file_id, tree):
1257
if not tree.has_or_had_id(file_id):
1259
path = tree.id2path(file_id)
1265
def make_basic_entry(self, file_id, only_interesting):
1266
entry_a = self.get_entry(file_id, self.tree_a)
1267
entry_b = self.get_entry(file_id, self.tree_b)
1268
if only_interesting and not self.is_interesting(entry_a, entry_b):
1270
parent = self.get_entry_parent(entry_a)
1271
path = self.get_path(file_id, self.tree_a)
1272
cs_entry = ChangesetEntry(file_id, parent, path)
1273
new_parent = self.get_entry_parent(entry_b)
1275
new_path = self.get_path(file_id, self.tree_b)
1277
cs_entry.new_path = new_path
1278
cs_entry.new_parent = new_parent
1281
def is_interesting(self, entry_a, entry_b):
1282
if self._interesting_ids is None:
1284
if entry_a is not None:
1285
file_id = entry_a.file_id
1286
elif entry_b is not None:
1287
file_id = entry_b.file_id
1290
return file_id in self._interesting_ids
1292
def make_boring_entry(self, id):
1293
cs_entry = self.make_basic_entry(id, only_interesting=False)
1294
if cs_entry.is_creation_or_deletion():
1295
return self.make_entry(id, only_interesting=False)
1299
def make_entry(self, id, only_interesting=True):
1300
cs_entry = self.make_basic_entry(id, only_interesting)
1302
if cs_entry is None:
1305
cs_entry.metadata_change = self.make_exec_flag_change(id)
1307
if id in self.tree_a and id in self.tree_b:
1308
a_sha1 = self.tree_a.get_file_sha1(id)
1309
b_sha1 = self.tree_b.get_file_sha1(id)
1310
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1313
cs_entry.contents_change = self.make_contents_change(id)
1316
def make_exec_flag_change(self, file_id):
1317
exec_flag_a = exec_flag_b = None
1318
if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1319
exec_flag_a = self.tree_a.is_executable(file_id)
1321
if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1322
exec_flag_b = self.tree_b.is_executable(file_id)
1324
if exec_flag_a == exec_flag_b:
1326
return ChangeExecFlag(exec_flag_a, exec_flag_b)
1328
def make_contents_change(self, file_id):
1329
a_contents = get_contents(self.tree_a, file_id)
1330
b_contents = get_contents(self.tree_b, file_id)
1331
if a_contents == b_contents:
1333
return ReplaceContents(a_contents, b_contents)
1336
def get_contents(tree, file_id):
1337
"""Return the appropriate contents to create a copy of file_id from tree"""
1338
if file_id not in tree:
1340
kind = tree.kind(file_id)
1342
return TreeFileCreate(tree, file_id)
1343
elif kind in ("directory", "root_directory"):
1345
elif kind == "symlink":
1346
return SymlinkCreate(tree.get_symlink_target(file_id))
1348
raise UnsupportedFiletype(kind, tree.id2path(file_id))
1351
def full_path(entry, tree):
1352
return pathjoin(tree.basedir, entry.path)
1355
def new_delete_entry(entry, tree, inventory, delete):
1356
if entry.path == "":
1359
parent = inventory[dirname(entry.path)].id
1360
cs_entry = ChangesetEntry(parent, entry.path)
1362
cs_entry.new_path = None
1363
cs_entry.new_parent = None
1365
cs_entry.path = None
1366
cs_entry.parent = None
1367
full_path = full_path(entry, tree)
1368
status = os.lstat(full_path)
1369
if stat.S_ISDIR(file_stat.st_mode):
1373
# XXX: Can't we unify this with the regular inventory object
1374
class Inventory(object):
1375
def __init__(self, inventory):
1376
self.inventory = inventory
1377
self.rinventory = None
1379
def get_rinventory(self):
1380
if self.rinventory is None:
1381
self.rinventory = invert_dict(self.inventory)
1382
return self.rinventory
1384
def get_path(self, id):
1385
return self.inventory.get(id)
1387
def get_name(self, id):
1388
path = self.get_path(id)
1392
return os.path.basename(path)
1394
def get_dir(self, id):
1395
path = self.get_path(id)
1400
return os.path.dirname(path)
1402
def get_parent(self, id):
1403
if self.get_path(id) is None:
1405
directory = self.get_dir(id)
1406
if directory == '.':
1408
if directory is None:
1410
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))
46
# Abstraction leakage, SFTPTransport.get('directory')
47
# doesn't always fail at get() time. Sometimes it fails
48
# during read. And that raises a generic IOError with
49
# just the string 'Failure'
50
# StubSFTPServer does fail during get() (because of prefetch)
51
# so it has an opportunity to translate the error.
52
raise errors.NotABundle(str(e))