~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

add gettext to all the builtin commands outf usage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
56
56
from itertools import izip
57
57
import operator
58
58
import os
59
 
import sys
60
59
 
61
60
from bzrlib.lazy_import import lazy_import
62
61
lazy_import(globals(), """
 
62
import gzip
 
63
 
63
64
from bzrlib import (
64
 
    annotate,
65
65
    debug,
66
66
    diff,
67
67
    graph as _mod_graph,
68
68
    index as _mod_index,
69
 
    lru_cache,
70
69
    pack,
71
70
    patiencediff,
72
 
    progress,
73
71
    static_tuple,
74
72
    trace,
75
73
    tsort,
76
74
    tuned_gzip,
77
75
    ui,
78
76
    )
 
77
 
 
78
from bzrlib.repofmt import pack_repo
79
79
""")
80
80
from bzrlib import (
 
81
    annotate,
81
82
    errors,
82
83
    osutils,
83
84
    )
84
85
from bzrlib.errors import (
85
 
    FileExists,
86
86
    NoSuchFile,
87
 
    KnitError,
88
87
    InvalidRevisionId,
89
88
    KnitCorrupt,
90
89
    KnitHeaderError,
91
90
    RevisionNotPresent,
92
 
    RevisionAlreadyPresent,
93
91
    SHA1KnitCorrupt,
94
92
    )
95
93
from bzrlib.osutils import (
96
94
    contains_whitespace,
97
 
    contains_linebreaks,
98
95
    sha_string,
99
96
    sha_strings,
100
97
    split_lines,
101
98
    )
102
99
from bzrlib.versionedfile import (
 
100
    _KeyRefs,
103
101
    AbsentContentFactory,
104
102
    adapter_registry,
105
103
    ConstantMapper,
106
104
    ContentFactory,
107
 
    ChunkedContentFactory,
108
105
    sort_groupcompress,
109
 
    VersionedFile,
110
 
    VersionedFiles,
 
106
    VersionedFilesWithFallbacks,
111
107
    )
112
108
 
113
109
 
412
408
class KnitContent(object):
413
409
    """Content of a knit version to which deltas can be applied.
414
410
 
415
 
    This is always stored in memory as a list of lines with \n at the end,
 
411
    This is always stored in memory as a list of lines with \\n at the end,
416
412
    plus a flag saying if the final ending is really there or not, because that
417
413
    corresponds to the on-disk knit representation.
418
414
    """
805
801
        writer.begin()
806
802
        index = _KnitGraphIndex(graph_index, lambda:True, parents=parents,
807
803
            deltas=delta, add_callback=graph_index.add_nodes)
808
 
        access = _DirectPackAccess({})
 
804
        access = pack_repo._DirectPackAccess({})
809
805
        access.set_writer(writer, graph_index, (transport, 'newpack'))
810
806
        result = KnitVersionedFiles(index, access,
811
807
            max_delta_chain=max_delta_chain)
849
845
                in all_build_index_memos.itervalues()])
850
846
 
851
847
 
852
 
class KnitVersionedFiles(VersionedFiles):
 
848
class KnitVersionedFiles(VersionedFilesWithFallbacks):
853
849
    """Storage for many versioned files using knit compression.
854
850
 
855
851
    Backend storage is managed by indices and data objects.
882
878
            self._factory = KnitAnnotateFactory()
883
879
        else:
884
880
            self._factory = KnitPlainFactory()
885
 
        self._fallback_vfs = []
 
881
        self._immediate_fallback_vfs = []
886
882
        self._reload_func = reload_func
887
883
 
888
884
    def __repr__(self):
891
887
            self._index,
892
888
            self._access)
893
889
 
 
890
    def without_fallbacks(self):
 
891
        """Return a clone of this object without any fallbacks configured."""
 
892
        return KnitVersionedFiles(self._index, self._access,
 
893
            self._max_delta_chain, self._factory.annotated,
 
894
            self._reload_func)
 
895
 
894
896
    def add_fallback_versioned_files(self, a_versioned_files):
895
897
        """Add a source of texts for texts not present in this knit.
896
898
 
897
899
        :param a_versioned_files: A VersionedFiles object.
898
900
        """
899
 
        self._fallback_vfs.append(a_versioned_files)
 
901
        self._immediate_fallback_vfs.append(a_versioned_files)
900
902
 
901
903
    def add_lines(self, key, parents, lines, parent_texts=None,
902
904
        left_matching_blocks=None, nostore_sha=None, random_id=False,
1069
1071
                    raise errors.KnitCorrupt(self,
1070
1072
                        "Missing basis parent %s for %s" % (
1071
1073
                        compression_parent, key))
1072
 
        for fallback_vfs in self._fallback_vfs:
 
1074
        for fallback_vfs in self._immediate_fallback_vfs:
1073
1075
            fallback_vfs.check()
1074
1076
 
1075
1077
    def _check_add(self, key, lines, random_id, check_content):
1153
1155
 
1154
1156
        A dict of key to (record_details, index_memo, next, parents) is
1155
1157
        returned.
1156
 
        method is the way referenced data should be applied.
1157
 
        index_memo is the handle to pass to the data access to actually get the
1158
 
            data
1159
 
        next is the build-parent of the version, or None for fulltexts.
1160
 
        parents is the version_ids of the parents of this version
1161
 
 
1162
 
        :param allow_missing: If True do not raise an error on a missing component,
1163
 
            just ignore it.
 
1158
 
 
1159
        * method is the way referenced data should be applied.
 
1160
        * index_memo is the handle to pass to the data access to actually get
 
1161
          the data
 
1162
        * next is the build-parent of the version, or None for fulltexts.
 
1163
        * parents is the version_ids of the parents of this version
 
1164
 
 
1165
        :param allow_missing: If True do not raise an error on a missing
 
1166
            component, just ignore it.
1164
1167
        """
1165
1168
        component_data = {}
1166
1169
        pending_components = keys
1192
1195
        generator = _VFContentMapGenerator(self, [key])
1193
1196
        return generator._get_content(key)
1194
1197
 
1195
 
    def get_known_graph_ancestry(self, keys):
1196
 
        """Get a KnownGraph instance with the ancestry of keys."""
1197
 
        parent_map, missing_keys = self._index.find_ancestry(keys)
1198
 
        for fallback in self._fallback_vfs:
1199
 
            if not missing_keys:
1200
 
                break
1201
 
            (f_parent_map, f_missing_keys) = fallback._index.find_ancestry(
1202
 
                                                missing_keys)
1203
 
            parent_map.update(f_parent_map)
1204
 
            missing_keys = f_missing_keys
1205
 
        kg = _mod_graph.KnownGraph(parent_map)
1206
 
        return kg
1207
 
 
1208
1198
    def get_parent_map(self, keys):
1209
1199
        """Get a map of the graph parents of keys.
1210
1200
 
1225
1215
            and so on.
1226
1216
        """
1227
1217
        result = {}
1228
 
        sources = [self._index] + self._fallback_vfs
 
1218
        sources = [self._index] + self._immediate_fallback_vfs
1229
1219
        source_results = []
1230
1220
        missing = set(keys)
1231
1221
        for source in sources:
1241
1231
        """Produce a dictionary of knit records.
1242
1232
 
1243
1233
        :return: {key:(record, record_details, digest, next)}
1244
 
            record
1245
 
                data returned from read_records (a KnitContentobject)
1246
 
            record_details
1247
 
                opaque information to pass to parse_record
1248
 
            digest
1249
 
                SHA1 digest of the full text after all steps are done
1250
 
            next
1251
 
                build-parent of the version, i.e. the leftmost ancestor.
 
1234
 
 
1235
            * record: data returned from read_records (a KnitContentobject)
 
1236
            * record_details: opaque information to pass to parse_record
 
1237
            * digest: SHA1 digest of the full text after all steps are done
 
1238
            * next: build-parent of the version, i.e. the leftmost ancestor.
1252
1239
                Will be None if the record is not a delta.
 
1240
 
1253
1241
        :param keys: The keys to build a map for
1254
1242
        :param allow_missing: If some records are missing, rather than
1255
1243
            error, just return the data that could be generated.
1525
1513
                        yield KnitContentFactory(key, global_map[key],
1526
1514
                            record_details, None, raw_data, self._factory.annotated, None)
1527
1515
                else:
1528
 
                    vf = self._fallback_vfs[parent_maps.index(source) - 1]
 
1516
                    vf = self._immediate_fallback_vfs[parent_maps.index(source) - 1]
1529
1517
                    for record in vf.get_record_stream(keys, ordering,
1530
1518
                        include_delta_closure):
1531
1519
                        yield record
1541
1529
            # record entry 2 is the 'digest'.
1542
1530
            result[key] = details[2]
1543
1531
        missing.difference_update(set(result))
1544
 
        for source in self._fallback_vfs:
 
1532
        for source in self._immediate_fallback_vfs:
1545
1533
            if not missing:
1546
1534
                break
1547
1535
            new_result = source.get_sha1s(missing)
1618
1606
                raise RevisionNotPresent([record.key], self)
1619
1607
            elif ((record.storage_kind in knit_types)
1620
1608
                  and (compression_parent is None
1621
 
                       or not self._fallback_vfs
 
1609
                       or not self._immediate_fallback_vfs
1622
1610
                       or self._index.has_key(compression_parent)
1623
1611
                       or not self.has_key(compression_parent))):
1624
1612
                # we can insert the knit record literally if either it has no
1796
1784
        # vfs, and hope to find them there.  Note that if the keys are found
1797
1785
        # but had no changes or no content, the fallback may not return
1798
1786
        # anything.
1799
 
        if keys and not self._fallback_vfs:
 
1787
        if keys and not self._immediate_fallback_vfs:
1800
1788
            # XXX: strictly the second parameter is meant to be the file id
1801
1789
            # but it's not easily accessible here.
1802
1790
            raise RevisionNotPresent(keys, repr(self))
1803
 
        for source in self._fallback_vfs:
 
1791
        for source in self._immediate_fallback_vfs:
1804
1792
            if not keys:
1805
1793
                break
1806
1794
            source_keys = set()
1879
1867
        :return: the header and the decompressor stream.
1880
1868
                 as (stream, header_record)
1881
1869
        """
1882
 
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
 
1870
        df = gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
1883
1871
        try:
1884
1872
            # Current serialise
1885
1873
            rec = self._check_header(key, df.readline())
1894
1882
        # 4168 calls in 2880 217 internal
1895
1883
        # 4168 calls to _parse_record_header in 2121
1896
1884
        # 4168 calls to readlines in 330
1897
 
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
 
1885
        df = gzip.GzipFile(mode='rb', fileobj=StringIO(data))
1898
1886
        try:
1899
1887
            record_contents = df.readlines()
1900
1888
        except Exception, e:
1922
1910
        The result will be returned in whatever is the fastest to read.
1923
1911
        Not by the order requested. Also, multiple requests for the same
1924
1912
        record will only yield 1 response.
 
1913
 
1925
1914
        :param records: A list of (key, access_memo) entries
1926
1915
        :return: Yields (key, contents, digest) in the order
1927
1916
                 read, not the order requested
1985
1974
        :param key: The key of the record. Currently keys are always serialised
1986
1975
            using just the trailing component.
1987
1976
        :param dense_lines: The bytes of lines but in a denser form. For
1988
 
            instance, if lines is a list of 1000 bytestrings each ending in \n,
1989
 
            dense_lines may be a list with one line in it, containing all the
1990
 
            1000's lines and their \n's. Using dense_lines if it is already
1991
 
            known is a win because the string join to create bytes in this
1992
 
            function spends less time resizing the final string.
 
1977
            instance, if lines is a list of 1000 bytestrings each ending in
 
1978
            \\n, dense_lines may be a list with one line in it, containing all
 
1979
            the 1000's lines and their \\n's. Using dense_lines if it is
 
1980
            already known is a win because the string join to create bytes in
 
1981
            this function spends less time resizing the final string.
1993
1982
        :return: (len, a StringIO instance with the raw data ready to read.)
1994
1983
        """
1995
1984
        chunks = ["version %s %d %s\n" % (key[-1], len(lines), digest)]
2015
2004
        """See VersionedFiles.keys."""
2016
2005
        if 'evil' in debug.debug_flags:
2017
2006
            trace.mutter_callsite(2, "keys scales with size of history")
2018
 
        sources = [self._index] + self._fallback_vfs
 
2007
        sources = [self._index] + self._immediate_fallback_vfs
2019
2008
        result = set()
2020
2009
        for source in sources:
2021
2010
            result.update(source.keys())
2061
2050
 
2062
2051
        missing_keys = set(nonlocal_keys)
2063
2052
        # Read from remote versioned file instances and provide to our caller.
2064
 
        for source in self.vf._fallback_vfs:
 
2053
        for source in self.vf._immediate_fallback_vfs:
2065
2054
            if not missing_keys:
2066
2055
                break
2067
2056
            # Loop over fallback repositories asking them for texts - ignore
2786
2775
        return key[:-1], key[-1]
2787
2776
 
2788
2777
 
2789
 
class _KeyRefs(object):
2790
 
 
2791
 
    def __init__(self, track_new_keys=False):
2792
 
        # dict mapping 'key' to 'set of keys referring to that key'
2793
 
        self.refs = {}
2794
 
        if track_new_keys:
2795
 
            # set remembering all new keys
2796
 
            self.new_keys = set()
2797
 
        else:
2798
 
            self.new_keys = None
2799
 
 
2800
 
    def clear(self):
2801
 
        if self.refs:
2802
 
            self.refs.clear()
2803
 
        if self.new_keys:
2804
 
            self.new_keys.clear()
2805
 
 
2806
 
    def add_references(self, key, refs):
2807
 
        # Record the new references
2808
 
        for referenced in refs:
2809
 
            try:
2810
 
                needed_by = self.refs[referenced]
2811
 
            except KeyError:
2812
 
                needed_by = self.refs[referenced] = set()
2813
 
            needed_by.add(key)
2814
 
        # Discard references satisfied by the new key
2815
 
        self.add_key(key)
2816
 
 
2817
 
    def get_new_keys(self):
2818
 
        return self.new_keys
2819
 
    
2820
 
    def get_unsatisfied_refs(self):
2821
 
        return self.refs.iterkeys()
2822
 
 
2823
 
    def _satisfy_refs_for_key(self, key):
2824
 
        try:
2825
 
            del self.refs[key]
2826
 
        except KeyError:
2827
 
            # No keys depended on this key.  That's ok.
2828
 
            pass
2829
 
 
2830
 
    def add_key(self, key):
2831
 
        # satisfy refs for key, and remember that we've seen this key.
2832
 
        self._satisfy_refs_for_key(key)
2833
 
        if self.new_keys is not None:
2834
 
            self.new_keys.add(key)
2835
 
 
2836
 
    def satisfy_refs_for_keys(self, keys):
2837
 
        for key in keys:
2838
 
            self._satisfy_refs_for_key(key)
2839
 
 
2840
 
    def get_referrers(self):
2841
 
        result = set()
2842
 
        for referrers in self.refs.itervalues():
2843
 
            result.update(referrers)
2844
 
        return result
2845
 
 
2846
 
 
2847
2778
class _KnitGraphIndex(object):
2848
2779
    """A KnitVersionedFiles index layered on GraphIndex."""
2849
2780
 
3520
3451
        return records, ann_keys
3521
3452
 
3522
3453
    def _get_needed_texts(self, key, pb=None):
3523
 
        # if True or len(self._vf._fallback_vfs) > 0:
3524
 
        if len(self._vf._fallback_vfs) > 0:
 
3454
        # if True or len(self._vf._immediate_fallback_vfs) > 0:
 
3455
        if len(self._vf._immediate_fallback_vfs) > 0:
3525
3456
            # If we have fallbacks, go to the generic path
3526
3457
            for v in annotate.Annotator._get_needed_texts(self, key, pb=pb):
3527
3458
                yield v