~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-10 07:46:15 UTC
  • mfrom: (5844 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5845.
  • Revision ID: jelmer@samba.org-20110510074615-eptod049ndjxc4i7
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from stat import S_ISREG, S_IEXEC
20
20
import time
21
21
 
22
 
import bzrlib
23
22
from bzrlib import (
24
23
    errors,
25
24
    lazy_import,
26
25
    registry,
 
26
    trace,
27
27
    tree,
28
28
    )
29
29
lazy_import.lazy_import(globals(), """
38
38
    multiparent,
39
39
    osutils,
40
40
    revision as _mod_revision,
41
 
    trace,
42
41
    ui,
43
42
    urlutils,
44
43
    )
48
47
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
49
48
                           UnableCreateSymlink)
50
49
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
51
 
from bzrlib.inventory import InventoryEntry
52
50
from bzrlib.osutils import (
53
51
    delete_any,
54
52
    file_kind,
64
62
    deprecated_in,
65
63
    deprecated_method,
66
64
    )
67
 
from bzrlib.trace import warning
68
65
 
69
66
 
70
67
ROOT_PARENT = "root-parent"
105
102
        self._new_parent = {}
106
103
        # mapping of trans_id with new contents -> new file_kind
107
104
        self._new_contents = {}
 
105
        # mapping of trans_id => (sha1 of content, stat_value)
 
106
        self._observed_sha1s = {}
108
107
        # Set of trans_ids whose contents will be removed
109
108
        self._removed_contents = set()
110
109
        # Mapping of trans_id -> new execute-bit value
629
628
            if kind is None:
630
629
                conflicts.append(('versioning no contents', trans_id))
631
630
                continue
632
 
            if not InventoryEntry.versionable_kind(kind):
 
631
            if not inventory.InventoryEntry.versionable_kind(kind):
633
632
                conflicts.append(('versioning bad kind', trans_id, kind))
634
633
        return conflicts
635
634
 
754
753
        return trans_id
755
754
 
756
755
    def new_file(self, name, parent_id, contents, file_id=None,
757
 
                 executable=None):
 
756
                 executable=None, sha1=None):
758
757
        """Convenience method to create files.
759
758
 
760
759
        name is the name of the file to create.
767
766
        trans_id = self._new_entry(name, parent_id, file_id)
768
767
        # TODO: rather than scheduling a set_executable call,
769
768
        # have create_file create the file with the right mode.
770
 
        self.create_file(contents, trans_id)
 
769
        self.create_file(contents, trans_id, sha1=sha1)
771
770
        if executable is not None:
772
771
            self.set_executability(executable, trans_id)
773
772
        return trans_id
1249
1248
            descendants.update(self._limbo_descendants(descendant))
1250
1249
        return descendants
1251
1250
 
1252
 
    def create_file(self, contents, trans_id, mode_id=None):
 
1251
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1253
1252
        """Schedule creation of a new file.
1254
1253
 
1255
 
        See also new_file.
1256
 
 
1257
 
        Contents is an iterator of strings, all of which will be written
1258
 
        to the target destination.
1259
 
 
1260
 
        New file takes the permissions of any existing file with that id,
1261
 
        unless mode_id is specified.
 
1254
        :seealso: new_file.
 
1255
 
 
1256
        :param contents: an iterator of strings, all of which will be written
 
1257
            to the target destination.
 
1258
        :param trans_id: TreeTransform handle
 
1259
        :param mode_id: If not None, force the mode of the target file to match
 
1260
            the mode of the object referenced by mode_id.
 
1261
            Otherwise, we will try to preserve mode bits of an existing file.
 
1262
        :param sha1: If the sha1 of this content is already known, pass it in.
 
1263
            We can use it to prevent future sha1 computations.
1262
1264
        """
1263
1265
        name = self._limbo_name(trans_id)
1264
1266
        f = open(name, 'wb')
1271
1273
                f.close()
1272
1274
                os.unlink(name)
1273
1275
                raise
1274
 
 
1275
1276
            f.writelines(contents)
1276
1277
        finally:
1277
1278
            f.close()
1278
1279
        self._set_mtime(name)
1279
1280
        self._set_mode(trans_id, mode_id, S_ISREG)
 
1281
        # It is unfortunate we have to use lstat instead of fstat, but we just
 
1282
        # used utime and chmod on the file, so we need the accurate final
 
1283
        # details.
 
1284
        if sha1 is not None:
 
1285
            self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1280
1286
 
1281
1287
    def _read_file_chunks(self, trans_id):
1282
1288
        cur_file = open(self._limbo_name(trans_id), 'rb')
1341
1347
    def cancel_creation(self, trans_id):
1342
1348
        """Cancel the creation of new file contents."""
1343
1349
        del self._new_contents[trans_id]
 
1350
        if trans_id in self._observed_sha1s:
 
1351
            del self._observed_sha1s[trans_id]
1344
1352
        children = self._limbo_children.get(trans_id)
1345
1353
        # if this is a limbo directory with children, move them before removing
1346
1354
        # the directory
1362
1370
        if orphan_policy is None:
1363
1371
            orphan_policy = default_policy
1364
1372
        if orphan_policy not in orphaning_registry:
1365
 
            trace.warning('%s (from %s) is not a known policy, defaulting to %s'
1366
 
                          % (orphan_policy, conf_var_name, default_policy))
 
1373
            trace.warning('%s (from %s) is not a known policy, defaulting '
 
1374
                'to %s' % (orphan_policy, conf_var_name, default_policy))
1367
1375
            orphan_policy = default_policy
1368
1376
        handle_orphan = orphaning_registry.get(orphan_policy)
1369
1377
        handle_orphan(self, trans_id, parent_id)
1702
1710
        finally:
1703
1711
            child_pb.finished()
1704
1712
        self._tree.apply_inventory_delta(inventory_delta)
 
1713
        self._apply_observed_sha1s()
1705
1714
        self._done = True
1706
1715
        self.finalize()
1707
1716
        return _TransformResults(modified_paths, self.rename_count)
1827
1836
                            raise
1828
1837
                    else:
1829
1838
                        self.rename_count += 1
 
1839
                    # TODO: if trans_id in self._observed_sha1s, we should
 
1840
                    #       re-stat the final target, since ctime will be
 
1841
                    #       updated by the change.
1830
1842
                if (trans_id in self._new_contents or
1831
1843
                    self.path_changed(trans_id)):
1832
1844
                    if trans_id in self._new_contents:
1833
1845
                        modified_paths.append(full_path)
1834
1846
                if trans_id in self._new_executability:
1835
1847
                    self._set_executability(path, trans_id)
 
1848
                if trans_id in self._observed_sha1s:
 
1849
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
 
1850
                    st = osutils.lstat(full_path)
 
1851
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1836
1852
        finally:
1837
1853
            child_pb.finished()
1838
1854
        self._new_contents.clear()
1839
1855
        return modified_paths
1840
1856
 
 
1857
    def _apply_observed_sha1s(self):
 
1858
        """After we have finished renaming everything, update observed sha1s
 
1859
 
 
1860
        This has to be done after self._tree.apply_inventory_delta, otherwise
 
1861
        it doesn't know anything about the files we are updating. Also, we want
 
1862
        to do this as late as possible, so that most entries end up cached.
 
1863
        """
 
1864
        # TODO: this doesn't update the stat information for directories. So
 
1865
        #       the first 'bzr status' will still need to rewrite
 
1866
        #       .bzr/checkout/dirstate. However, we at least don't need to
 
1867
        #       re-read all of the files.
 
1868
        # TODO: If the operation took a while, we could do a time.sleep(3) here
 
1869
        #       to allow the clock to tick over and ensure we won't have any
 
1870
        #       problems. (we could observe start time, and finish time, and if
 
1871
        #       it is less than eg 10% overhead, add a sleep call.)
 
1872
        paths = FinalPaths(self)
 
1873
        for trans_id, observed in self._observed_sha1s.iteritems():
 
1874
            path = paths.get_path(trans_id)
 
1875
            # We could get the file_id, but dirstate prefers to use the path
 
1876
            # anyway, and it is 'cheaper' to determine.
 
1877
            # file_id = self._new_id[trans_id]
 
1878
            self._tree._observed_sha1(None, path, observed)
 
1879
 
1841
1880
 
1842
1881
class TransformPreview(DiskTreeTransform):
1843
1882
    """A TreeTransform for generating preview trees.
1859
1898
        path = self._tree_id_paths.get(trans_id)
1860
1899
        if path is None:
1861
1900
            return None
1862
 
        file_id = self._tree.path2id(path)
1863
 
        try:
1864
 
            return self._tree.kind(file_id)
1865
 
        except errors.NoSuchFile:
1866
 
            return None
 
1901
        kind = self._tree.path_content_summary(path)[0]
 
1902
        if kind == 'missing':
 
1903
            kind = None
 
1904
        return kind
1867
1905
 
1868
1906
    def _set_mode(self, trans_id, mode_id, typefunc):
1869
1907
        """Set the mode of new file contents.
1893
1931
        raise NotImplementedError(self.new_orphan)
1894
1932
 
1895
1933
 
1896
 
class _PreviewTree(tree.Tree):
 
1934
class _PreviewTree(tree.InventoryTree):
1897
1935
    """Partial implementation of Tree to support show_diff_trees"""
1898
1936
 
1899
1937
    def __init__(self, transform):
1928
1966
                yield self._get_repository().revision_tree(revision_id)
1929
1967
 
1930
1968
    def _get_file_revision(self, file_id, vf, tree_revision):
1931
 
        parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
 
1969
        parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
1932
1970
                       self._iter_parent_trees()]
1933
1971
        vf.add_lines((file_id, tree_revision), parent_keys,
1934
1972
                     self.get_file_lines(file_id))
1938
1976
            vf.fallback_versionedfiles.append(base_vf)
1939
1977
        return tree_revision
1940
1978
 
1941
 
    def _stat_limbo_file(self, file_id):
1942
 
        trans_id = self._transform.trans_id_file_id(file_id)
 
1979
    def _stat_limbo_file(self, file_id=None, trans_id=None):
 
1980
        if trans_id is None:
 
1981
            trans_id = self._transform.trans_id_file_id(file_id)
1943
1982
        name = self._transform._limbo_name(trans_id)
1944
1983
        return os.lstat(name)
1945
1984
 
2160
2199
 
2161
2200
    def get_file_size(self, file_id):
2162
2201
        """See Tree.get_file_size"""
 
2202
        trans_id = self._transform.trans_id_file_id(file_id)
 
2203
        kind = self._transform.final_kind(trans_id)
 
2204
        if kind != 'file':
 
2205
            return None
 
2206
        if trans_id in self._transform._new_contents:
 
2207
            return self._stat_limbo_file(trans_id=trans_id).st_size
2163
2208
        if self.kind(file_id) == 'file':
2164
2209
            return self._transform._tree.get_file_size(file_id)
2165
2210
        else:
2193
2238
            except errors.NoSuchId:
2194
2239
                return False
2195
2240
 
 
2241
    def has_filename(self, path):
 
2242
        trans_id = self._path2trans_id(path)
 
2243
        if trans_id in self._transform._new_contents:
 
2244
            return True
 
2245
        elif trans_id in self._transform._removed_contents:
 
2246
            return False
 
2247
        else:
 
2248
            return self._transform._tree.has_filename(path)
 
2249
 
2196
2250
    def path_content_summary(self, path):
2197
2251
        trans_id = self._path2trans_id(path)
2198
2252
        tt = self._transform
2502
2556
                    executable = tree.is_executable(file_id, tree_path)
2503
2557
                    if executable:
2504
2558
                        tt.set_executability(executable, trans_id)
2505
 
                    trans_data = (trans_id, tree_path)
 
2559
                    trans_data = (trans_id, tree_path, entry.text_sha1)
2506
2560
                    deferred_contents.append((file_id, trans_data))
2507
2561
                else:
2508
2562
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2524
2578
            precomputed_delta = None
2525
2579
        conflicts = cook_conflicts(raw_conflicts, tt)
2526
2580
        for conflict in conflicts:
2527
 
            warning(conflict)
 
2581
            trace.warning(conflict)
2528
2582
        try:
2529
2583
            wt.add_conflicts(conflicts)
2530
2584
        except errors.UnsupportedOperation:
2553
2607
        unchanged = dict(unchanged)
2554
2608
        new_desired_files = []
2555
2609
        count = 0
2556
 
        for file_id, (trans_id, tree_path) in desired_files:
 
2610
        for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2557
2611
            accelerator_path = unchanged.get(file_id)
2558
2612
            if accelerator_path is None:
2559
 
                new_desired_files.append((file_id, (trans_id, tree_path)))
 
2613
                new_desired_files.append((file_id,
 
2614
                    (trans_id, tree_path, text_sha1)))
2560
2615
                continue
2561
2616
            pb.update('Adding file contents', count + offset, total)
2562
2617
            if hardlink:
2569
2624
                    contents = filtered_output_bytes(contents, filters,
2570
2625
                        ContentFilterContext(tree_path, tree))
2571
2626
                try:
2572
 
                    tt.create_file(contents, trans_id)
 
2627
                    tt.create_file(contents, trans_id, sha1=text_sha1)
2573
2628
                finally:
2574
2629
                    try:
2575
2630
                        contents.close()
2578
2633
                        pass
2579
2634
            count += 1
2580
2635
        offset += count
2581
 
    for count, ((trans_id, tree_path), contents) in enumerate(
 
2636
    for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2582
2637
            tree.iter_files_bytes(new_desired_files)):
2583
2638
        if wt.supports_content_filtering():
2584
2639
            filters = wt._content_filter_stack(tree_path)
2585
2640
            contents = filtered_output_bytes(contents, filters,
2586
2641
                ContentFilterContext(tree_path, tree))
2587
 
        tt.create_file(contents, trans_id)
 
2642
        tt.create_file(contents, trans_id, sha1=text_sha1)
2588
2643
        pb.update('Adding file contents', count + offset, total)
2589
2644
 
2590
2645
 
2765
2820
                unversioned_filter=working_tree.is_ignored)
2766
2821
            delta.report_changes(tt.iter_changes(), change_reporter)
2767
2822
        for conflict in conflicts:
2768
 
            warning(conflict)
 
2823
            trace.warning(conflict)
2769
2824
        pp.next_phase()
2770
2825
        tt.apply()
2771
2826
        working_tree.set_merge_modified(merge_modified)
2999
3054
                        file_id = tt.final_file_id(trans_id)
3000
3055
                        if file_id is None:
3001
3056
                            file_id = tt.inactive_file_id(trans_id)
3002
 
                        entry = path_tree.inventory[file_id]
 
3057
                        _, entry = path_tree.iter_entries_by_dir(
 
3058
                            [file_id]).next()
3003
3059
                        # special-case the other tree root (move its
3004
3060
                        # children to current root)
3005
3061
                        if entry.parent_id is None: