~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Martin Packman
  • Date: 2012-01-05 10:37:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6427.
  • Revision ID: martin.packman@canonical.com-20120105103758-wzftnmsip5iv9n2g
Revert addition of get_message_encoding function

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from bzrlib import (
31
31
    annotate,
32
32
    bencode,
33
 
    bzrdir,
 
33
    controldir,
34
34
    commit,
35
35
    conflicts,
36
36
    delta,
37
 
    errors,
38
37
    inventory,
39
38
    multiparent,
40
39
    osutils,
42
41
    ui,
43
42
    urlutils,
44
43
    )
 
44
from bzrlib.i18n import gettext
45
45
""")
46
 
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
 
46
from bzrlib.errors import (DuplicateKey, MalformedTransform,
47
47
                           ReusingTransform, CantMoveRoot,
48
48
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
49
49
                           UnableCreateSymlink)
231
231
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
232
232
                     ROOT_PARENT]
233
233
        if len(new_roots) < 1:
234
 
            if self.final_kind(self.root) is None:
235
 
                self.cancel_deletion(self.root)
236
 
            if self.final_file_id(self.root) is None:
237
 
                self.version_file(self.tree_file_id(self.root),
238
 
                                     self.root)
239
234
            return
240
235
        if len(new_roots) != 1:
241
236
            raise ValueError('A tree cannot have two roots!')
243
238
            self._new_root = new_roots[0]
244
239
            return
245
240
        old_new_root = new_roots[0]
246
 
        # TODO: What to do if a old_new_root is present, but self._new_root is
247
 
        #       not listed as being removed? This code explicitly unversions
248
 
        #       the old root and versions it with the new file_id. Though that
249
 
        #       seems like an incomplete delta
250
 
 
251
241
        # unversion the new root's directory.
252
 
        file_id = self.final_file_id(old_new_root)
 
242
        if self.final_kind(self._new_root) is None:
 
243
            file_id = self.final_file_id(old_new_root)
 
244
        else:
 
245
            file_id = self.final_file_id(self._new_root)
253
246
        if old_new_root in self._new_id:
254
247
            self.cancel_versioning(old_new_root)
255
248
        else:
568
561
        for trans_id in self._removed_id:
569
562
            file_id = self.tree_file_id(trans_id)
570
563
            if file_id is not None:
571
 
                # XXX: This seems like something that should go via a different
572
 
                #      indirection.
573
 
                if self._tree.inventory[file_id].kind == 'directory':
 
564
                if self._tree.stored_kind(file_id) == 'directory':
574
565
                    parents.append(trans_id)
575
566
            elif self.tree_kind(trans_id) == 'directory':
576
567
                parents.append(trans_id)
785
776
                    to_mode |= 0010 & ~umask
786
777
            else:
787
778
                to_mode = current_mode & ~0111
788
 
            os.chmod(abspath, to_mode)
 
779
            osutils.chmod_if_possible(abspath, to_mode)
789
780
 
790
781
    def _new_entry(self, name, parent_id, file_id):
791
782
        """Helper function to create a new filesystem entry."""
1564
1555
        try:
1565
1556
            limbodir = urlutils.local_path_from_url(
1566
1557
                tree._transport.abspath('limbo'))
1567
 
            try:
1568
 
                os.mkdir(limbodir)
1569
 
            except OSError, e:
1570
 
                if e.errno == errno.EEXIST:
1571
 
                    raise ExistingLimbo(limbodir)
 
1558
            osutils.ensure_empty_directory_exists(
 
1559
                limbodir,
 
1560
                errors.ExistingLimbo)
1572
1561
            deletiondir = urlutils.local_path_from_url(
1573
1562
                tree._transport.abspath('pending-deletion'))
1574
 
            try:
1575
 
                os.mkdir(deletiondir)
1576
 
            except OSError, e:
1577
 
                if e.errno == errno.EEXIST:
1578
 
                    raise errors.ExistingPendingDeletion(deletiondir)
 
1563
            osutils.ensure_empty_directory_exists(
 
1564
                deletiondir,
 
1565
                errors.ExistingPendingDeletion)
1579
1566
        except:
1580
1567
            tree.unlock()
1581
1568
            raise
1644
1631
            else:
1645
1632
                raise
1646
1633
        if typefunc(mode):
1647
 
            os.chmod(self._limbo_name(trans_id), mode)
 
1634
            osutils.chmod_if_possible(self._limbo_name(trans_id), mode)
1648
1635
 
1649
1636
    def iter_tree_children(self, parent_id):
1650
1637
        """Iterate through the entry's tree children, if any"""
1733
1720
        child_pb = ui.ui_factory.nested_progress_bar()
1734
1721
        try:
1735
1722
            if precomputed_delta is None:
1736
 
                child_pb.update('Apply phase', 0, 2)
 
1723
                child_pb.update(gettext('Apply phase'), 0, 2)
1737
1724
                inventory_delta = self._generate_inventory_delta()
1738
1725
                offset = 1
1739
1726
            else:
1744
1731
            else:
1745
1732
                mover = _mover
1746
1733
            try:
1747
 
                child_pb.update('Apply phase', 0 + offset, 2 + offset)
 
1734
                child_pb.update(gettext('Apply phase'), 0 + offset, 2 + offset)
1748
1735
                self._apply_removals(mover)
1749
 
                child_pb.update('Apply phase', 1 + offset, 2 + offset)
 
1736
                child_pb.update(gettext('Apply phase'), 1 + offset, 2 + offset)
1750
1737
                modified_paths = self._apply_insertions(mover)
1751
1738
            except:
1752
1739
                mover.rollback()
1755
1742
                mover.apply_deletions()
1756
1743
        finally:
1757
1744
            child_pb.finished()
 
1745
        if self.final_file_id(self.root) is None:
 
1746
            inventory_delta = [e for e in inventory_delta if e[0] != '']
1758
1747
        self._tree.apply_inventory_delta(inventory_delta)
1759
1748
        self._apply_observed_sha1s()
1760
1749
        self._done = True
1770
1759
        try:
1771
1760
            for num, trans_id in enumerate(self._removed_id):
1772
1761
                if (num % 10) == 0:
1773
 
                    child_pb.update('removing file', num, total_entries)
 
1762
                    child_pb.update(gettext('removing file'), num, total_entries)
1774
1763
                if trans_id == self._new_root:
1775
1764
                    file_id = self._tree.get_root_id()
1776
1765
                else:
1788
1777
            final_kinds = {}
1789
1778
            for num, (path, trans_id) in enumerate(new_paths):
1790
1779
                if (num % 10) == 0:
1791
 
                    child_pb.update('adding file',
 
1780
                    child_pb.update(gettext('adding file'),
1792
1781
                                    num + len(self._removed_id), total_entries)
1793
1782
                file_id = new_path_file_ids[trans_id]
1794
1783
                if file_id is None:
1838
1827
                # do not attempt to move root into a subdirectory of itself.
1839
1828
                if path == '':
1840
1829
                    continue
1841
 
                child_pb.update('removing file', num, len(tree_paths))
 
1830
                child_pb.update(gettext('removing file'), num, len(tree_paths))
1842
1831
                full_path = self._tree.abspath(path)
1843
1832
                if trans_id in self._removed_contents:
1844
1833
                    delete_path = os.path.join(self._deletiondir, trans_id)
1873
1862
        try:
1874
1863
            for num, (path, trans_id) in enumerate(new_paths):
1875
1864
                if (num % 10) == 0:
1876
 
                    child_pb.update('adding file', num, len(new_paths))
 
1865
                    child_pb.update(gettext('adding file'), num, len(new_paths))
1877
1866
                full_path = self._tree.abspath(path)
1878
1867
                if trans_id in self._needs_rename:
1879
1868
                    try:
2263
2252
        else:
2264
2253
            return None
2265
2254
 
 
2255
    def get_file_verifier(self, file_id, path=None, stat_value=None):
 
2256
        trans_id = self._transform.trans_id_file_id(file_id)
 
2257
        kind = self._transform._new_contents.get(trans_id)
 
2258
        if kind is None:
 
2259
            return self._transform._tree.get_file_verifier(file_id)
 
2260
        if kind == 'file':
 
2261
            fileobj = self.get_file(file_id)
 
2262
            try:
 
2263
                return ("SHA1", sha_file(fileobj))
 
2264
            finally:
 
2265
                fileobj.close()
 
2266
 
2266
2267
    def get_file_sha1(self, file_id, path=None, stat_value=None):
2267
2268
        trans_id = self._transform.trans_id_file_id(file_id)
2268
2269
        kind = self._transform._new_contents.get(trans_id)
2539
2540
    file_trans_id = {}
2540
2541
    top_pb = ui.ui_factory.nested_progress_bar()
2541
2542
    pp = ProgressPhase("Build phase", 2, top_pb)
2542
 
    if tree.inventory.root is not None:
 
2543
    if tree.get_root_id() is not None:
2543
2544
        # This is kind of a hack: we should be altering the root
2544
2545
        # as part of the regular tree shape diff logic.
2545
2546
        # The conditional test here is to avoid doing an
2560
2561
        try:
2561
2562
            deferred_contents = []
2562
2563
            num = 0
2563
 
            total = len(tree.inventory)
 
2564
            total = len(tree.all_file_ids())
2564
2565
            if delta_from_tree:
2565
2566
                precomputed_delta = []
2566
2567
            else:
2575
2576
                for dir, files in wt.walkdirs():
2576
2577
                    existing_files.update(f[0] for f in files)
2577
2578
            for num, (tree_path, entry) in \
2578
 
                enumerate(tree.inventory.iter_entries_by_dir()):
2579
 
                pb.update("Building tree", num - len(deferred_contents), total)
 
2579
                enumerate(tree.iter_entries_by_dir()):
 
2580
                pb.update(gettext("Building tree"), num - len(deferred_contents), total)
2580
2581
                if entry.parent_id is None:
2581
2582
                    continue
2582
2583
                reparent = False
2588
2589
                    kind = file_kind(target_path)
2589
2590
                    if kind == "directory":
2590
2591
                        try:
2591
 
                            bzrdir.BzrDir.open(target_path)
 
2592
                            controldir.ControlDir.open(target_path)
2592
2593
                        except errors.NotBranchError:
2593
2594
                            pass
2594
2595
                        else:
2666
2667
                new_desired_files.append((file_id,
2667
2668
                    (trans_id, tree_path, text_sha1)))
2668
2669
                continue
2669
 
            pb.update('Adding file contents', count + offset, total)
 
2670
            pb.update(gettext('Adding file contents'), count + offset, total)
2670
2671
            if hardlink:
2671
2672
                tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2672
2673
                                   trans_id)
2693
2694
            contents = filtered_output_bytes(contents, filters,
2694
2695
                ContentFilterContext(tree_path, tree))
2695
2696
        tt.create_file(contents, trans_id, sha1=text_sha1)
2696
 
        pb.update('Adding file contents', count + offset, total)
 
2697
        pb.update(gettext('Adding file contents'), count + offset, total)
2697
2698
 
2698
2699
 
2699
2700
def _reparent_children(tt, old_parent, new_parent):
2831
2832
            return new_name
2832
2833
 
2833
2834
 
2834
 
def _entry_changes(file_id, entry, working_tree):
2835
 
    """Determine in which ways the inventory entry has changed.
2836
 
 
2837
 
    Returns booleans: has_contents, content_mod, meta_mod
2838
 
    has_contents means there are currently contents, but they differ
2839
 
    contents_mod means contents need to be modified
2840
 
    meta_mod means the metadata needs to be modified
2841
 
    """
2842
 
    cur_entry = working_tree.inventory[file_id]
2843
 
    try:
2844
 
        working_kind = working_tree.kind(file_id)
2845
 
        has_contents = True
2846
 
    except NoSuchFile:
2847
 
        has_contents = False
2848
 
        contents_mod = True
2849
 
        meta_mod = False
2850
 
    if has_contents is True:
2851
 
        if entry.kind != working_kind:
2852
 
            contents_mod, meta_mod = True, False
2853
 
        else:
2854
 
            cur_entry._read_tree_state(working_tree.id2path(file_id),
2855
 
                                       working_tree)
2856
 
            contents_mod, meta_mod = entry.detect_changes(cur_entry)
2857
 
            cur_entry._forget_tree_state()
2858
 
    return has_contents, contents_mod, meta_mod
2859
 
 
2860
 
 
2861
2835
def revert(working_tree, target_tree, filenames, backups=False,
2862
2836
           pb=None, change_reporter=None):
2863
2837
    """Revert a working tree's contents to those of a target tree."""
3040
3014
    pb = ui.ui_factory.nested_progress_bar()
3041
3015
    try:
3042
3016
        for n in range(10):
3043
 
            pb.update('Resolution pass', n+1, 10)
 
3017
            pb.update(gettext('Resolution pass'), n+1, 10)
3044
3018
            conflicts = tt.find_conflicts()
3045
3019
            if len(conflicts) == 0:
3046
3020
                return new_conflicts
3070
3044
                existing_file, new_file = conflict[2], conflict[1]
3071
3045
            else:
3072
3046
                existing_file, new_file = conflict[1], conflict[2]
3073
 
            new_name = tt.final_name(existing_file)+'.moved'
 
3047
            new_name = tt.final_name(existing_file) + '.moved'
3074
3048
            tt.adjust_path(new_name, final_parent, existing_file)
3075
3049
            new_conflicts.add((c_type, 'Moved existing file to',
3076
3050
                               existing_file, new_file))