~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Jelmer Vernooij
  • Date: 2009-10-27 21:54:26 UTC
  • mfrom: (4771 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4833.
  • Revision ID: jelmer@samba.org-20091027215426-72164bkd4mq9dsd4
merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
import os
18
18
import errno
19
19
from stat import S_ISREG, S_IEXEC
20
 
import time
21
20
 
22
21
from bzrlib.lazy_import import lazy_import
23
22
lazy_import(globals(), """
162
161
 
163
162
    def adjust_path(self, name, parent, trans_id):
164
163
        """Change the path that is assigned to a transaction id."""
165
 
        if parent is None:
166
 
            raise ValueError("Parent trans-id may not be None")
167
164
        if trans_id == self._new_root:
168
165
            raise CantMoveRoot
169
166
        self._new_name[trans_id] = name
170
167
        self._new_parent[trans_id] = parent
 
168
        if parent == ROOT_PARENT:
 
169
            if self._new_root is not None:
 
170
                raise ValueError("Cannot have multiple roots.")
 
171
            self._new_root = trans_id
171
172
 
172
173
    def adjust_root_path(self, name, parent):
173
174
        """Emulate moving the root by moving all children, instead.
201
202
        self.version_file(old_root_file_id, old_root)
202
203
        self.unversion_file(self._new_root)
203
204
 
204
 
    def fixup_new_roots(self):
205
 
        """Reinterpret requests to change the root directory
206
 
 
207
 
        Instead of creating a root directory, or moving an existing directory,
208
 
        all the attributes and children of the new root are applied to the
209
 
        existing root directory.
210
 
 
211
 
        This means that the old root trans-id becomes obsolete, so it is
212
 
        recommended only to invoke this after the root trans-id has become
213
 
        irrelevant.
214
 
        """
215
 
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
216
 
                     ROOT_PARENT]
217
 
        if len(new_roots) < 1:
218
 
            return
219
 
        if len(new_roots) != 1:
220
 
            raise ValueError('A tree cannot have two roots!')
221
 
        if self._new_root is None:
222
 
            self._new_root = new_roots[0]
223
 
            return
224
 
        old_new_root = new_roots[0]
225
 
        # TODO: What to do if a old_new_root is present, but self._new_root is
226
 
        #       not listed as being removed? This code explicitly unversions
227
 
        #       the old root and versions it with the new file_id. Though that
228
 
        #       seems like an incomplete delta
229
 
 
230
 
        # unversion the new root's directory.
231
 
        file_id = self.final_file_id(old_new_root)
232
 
        if old_new_root in self._new_id:
233
 
            self.cancel_versioning(old_new_root)
234
 
        else:
235
 
            self.unversion_file(old_new_root)
236
 
        # if, at this stage, root still has an old file_id, zap it so we can
237
 
        # stick a new one in.
238
 
        if (self.tree_file_id(self._new_root) is not None and
239
 
            self._new_root not in self._removed_id):
240
 
            self.unversion_file(self._new_root)
241
 
        self.version_file(file_id, self._new_root)
242
 
 
243
 
        # Now move children of new root into old root directory.
244
 
        # Ensure all children are registered with the transaction, but don't
245
 
        # use directly-- some tree children have new parents
246
 
        list(self.iter_tree_children(old_new_root))
247
 
        # Move all children of new root into old root directory.
248
 
        for child in self.by_parent().get(old_new_root, []):
249
 
            self.adjust_path(self.final_name(child), self._new_root, child)
250
 
 
251
 
        # Ensure old_new_root has no directory.
252
 
        if old_new_root in self._new_contents:
253
 
            self.cancel_creation(old_new_root)
254
 
        else:
255
 
            self.delete_contents(old_new_root)
256
 
 
257
 
        # prevent deletion of root directory.
258
 
        if self._new_root in self._removed_contents:
259
 
            self.cancel_deletion(self._new_root)
260
 
 
261
 
        # destroy path info for old_new_root.
262
 
        del self._new_parent[old_new_root]
263
 
        del self._new_name[old_new_root]
264
 
 
265
205
    def trans_id_tree_file_id(self, inventory_id):
266
206
        """Determine the transaction id of a working tree file.
267
207
 
313
253
 
314
254
    def delete_contents(self, trans_id):
315
255
        """Schedule the contents of a path entry for deletion"""
316
 
        # Ensure that the object exists in the WorkingTree, this will raise an
317
 
        # exception if there is a problem
318
256
        self.tree_kind(trans_id)
319
257
        self._removed_contents.add(trans_id)
320
258
 
1085
1023
        self._limbo_children_names = {}
1086
1024
        # List of transform ids that need to be renamed from limbo into place
1087
1025
        self._needs_rename = set()
1088
 
        self._creation_mtime = None
1089
1026
 
1090
1027
    def finalize(self):
1091
1028
        """Release the working tree lock, if held, clean up limbo dir.
1138
1075
        if (trans_id in self._limbo_files and
1139
1076
            trans_id not in self._needs_rename):
1140
1077
            self._rename_in_limbo([trans_id])
1141
 
            if previous_parent != parent:
1142
 
                self._limbo_children[previous_parent].remove(trans_id)
1143
 
            if previous_parent != parent or previous_name != name:
1144
 
                del self._limbo_children_names[previous_parent][previous_name]
 
1078
            self._limbo_children[previous_parent].remove(trans_id)
 
1079
            del self._limbo_children_names[previous_parent][previous_name]
1145
1080
 
1146
1081
    def _rename_in_limbo(self, trans_ids):
1147
1082
        """Fix limbo names so that the right final path is produced.
1198
1133
            f.writelines(contents)
1199
1134
        finally:
1200
1135
            f.close()
1201
 
        self._set_mtime(name)
1202
1136
        self._set_mode(trans_id, mode_id, S_ISREG)
1203
1137
 
1204
1138
    def _read_file_chunks(self, trans_id):
1211
1145
    def _read_symlink_target(self, trans_id):
1212
1146
        return os.readlink(self._limbo_name(trans_id))
1213
1147
 
1214
 
    def _set_mtime(self, path):
1215
 
        """All files that are created get the same mtime.
1216
 
 
1217
 
        This time is set by the first object to be created.
1218
 
        """
1219
 
        if self._creation_mtime is None:
1220
 
            self._creation_mtime = time.time()
1221
 
        os.utime(path, (self._creation_mtime, self._creation_mtime))
1222
 
 
1223
1148
    def create_hardlink(self, path, trans_id):
1224
1149
        """Schedule creation of a hard link"""
1225
1150
        name = self._limbo_name(trans_id)
1628
1553
                child_pb.update('removing file', num, len(tree_paths))
1629
1554
                full_path = self._tree.abspath(path)
1630
1555
                if trans_id in self._removed_contents:
1631
 
                    delete_path = os.path.join(self._deletiondir, trans_id)
1632
 
                    mover.pre_delete(full_path, delete_path)
1633
 
                elif (trans_id in self._new_name
1634
 
                      or trans_id in self._new_parent):
 
1556
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
 
1557
                                     trans_id))
 
1558
                elif trans_id in self._new_name or trans_id in \
 
1559
                    self._new_parent:
1635
1560
                    try:
1636
1561
                        mover.rename(full_path, self._limbo_name(trans_id))
1637
1562
                    except OSError, e:
2048
1973
                statval = os.lstat(limbo_name)
2049
1974
                size = statval.st_size
2050
1975
                if not supports_executable():
2051
 
                    executable = False
 
1976
                    executable = None
2052
1977
                else:
2053
1978
                    executable = statval.st_mode & S_IEXEC
2054
1979
            else:
2056
1981
                executable = None
2057
1982
            if kind == 'symlink':
2058
1983
                link_or_sha1 = os.readlink(limbo_name).decode(osutils._fs_enc)
2059
 
        executable = tt._new_executability.get(trans_id, executable)
 
1984
        if supports_executable():
 
1985
            executable = tt._new_executability.get(trans_id, executable)
2060
1986
        return kind, size, executable, link_or_sha1
2061
1987
 
2062
1988
    def iter_changes(self, from_tree, include_unchanged=False,
2375
2301
        new_desired_files = desired_files
2376
2302
    else:
2377
2303
        iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
2378
 
        unchanged = [(f, p[1]) for (f, p, c, v, d, n, k, e)
2379
 
                     in iter if not (c or e[0] != e[1])]
2380
 
        if accelerator_tree.supports_content_filtering():
2381
 
            unchanged = [(f, p) for (f, p) in unchanged
2382
 
                         if not accelerator_tree.iter_search_rules([p]).next()]
2383
 
        unchanged = dict(unchanged)
 
2304
        unchanged = dict((f, p[1]) for (f, p, c, v, d, n, k, e)
 
2305
                         in iter if not (c or e[0] != e[1]))
2384
2306
        new_desired_files = []
2385
2307
        count = 0
2386
2308
        for file_id, (trans_id, tree_path) in desired_files:
2509
2431
        tt.create_directory(trans_id)
2510
2432
 
2511
2433
 
2512
 
def create_from_tree(tt, trans_id, tree, file_id, bytes=None,
2513
 
    filter_tree_path=None):
2514
 
    """Create new file contents according to tree contents.
2515
 
    
2516
 
    :param filter_tree_path: the tree path to use to lookup
2517
 
      content filters to apply to the bytes output in the working tree.
2518
 
      This only applies if the working tree supports content filtering.
2519
 
    """
 
2434
def create_from_tree(tt, trans_id, tree, file_id, bytes=None):
 
2435
    """Create new file contents according to tree contents."""
2520
2436
    kind = tree.kind(file_id)
2521
2437
    if kind == 'directory':
2522
2438
        tt.create_directory(trans_id)
2527
2443
                bytes = tree_file.readlines()
2528
2444
            finally:
2529
2445
                tree_file.close()
2530
 
        wt = tt._tree
2531
 
        if wt.supports_content_filtering() and filter_tree_path is not None:
2532
 
            filters = wt._content_filter_stack(filter_tree_path)
2533
 
            bytes = filtered_output_bytes(bytes, filters,
2534
 
                ContentFilterContext(filter_tree_path, tree))
2535
2446
        tt.create_file(bytes, trans_id)
2536
2447
    elif kind == "symlink":
2537
2448
        tt.create_symlink(tree.get_symlink_target(file_id), trans_id)
2725
2636
                    parent_trans = ROOT_PARENT
2726
2637
                else:
2727
2638
                    parent_trans = tt.trans_id_file_id(parent[1])
2728
 
                if parent[0] is None and versioned[0]:
2729
 
                    tt.adjust_root_path(name[1], parent_trans)
2730
 
                else:
2731
 
                    tt.adjust_path(name[1], parent_trans, trans_id)
 
2639
                tt.adjust_path(name[1], parent_trans, trans_id)
2732
2640
            if executable[0] != executable[1] and kind[1] == "file":
2733
2641
                tt.set_executability(executable[1], trans_id)
2734
 
        if working_tree.supports_content_filtering():
2735
 
            for index, ((trans_id, mode_id), bytes) in enumerate(
2736
 
                target_tree.iter_files_bytes(deferred_files)):
2737
 
                file_id = deferred_files[index][0]
2738
 
                # We're reverting a tree to the target tree so using the
2739
 
                # target tree to find the file path seems the best choice
2740
 
                # here IMO - Ian C 27/Oct/2009
2741
 
                filter_tree_path = target_tree.id2path(file_id)
2742
 
                filters = working_tree._content_filter_stack(filter_tree_path)
2743
 
                bytes = filtered_output_bytes(bytes, filters,
2744
 
                    ContentFilterContext(filter_tree_path, working_tree))
2745
 
                tt.create_file(bytes, trans_id, mode_id)
2746
 
        else:
2747
 
            for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
2748
 
                deferred_files):
2749
 
                tt.create_file(bytes, trans_id, mode_id)
2750
 
        tt.fixup_new_roots()
 
2642
        for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
 
2643
            deferred_files):
 
2644
            tt.create_file(bytes, trans_id, mode_id)
2751
2645
    finally:
2752
2646
        if basis_tree is not None:
2753
2647
            basis_tree.unlock()