/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Robert Collins
  • Date: 2008-01-06 20:04:22 UTC
  • mto: (3221.11.1 StackableBranch)
  • mto: This revision was merged to the branch mainline in revision 3226.
  • Revision ID: robertc@robertcollins.net-20080106200422-x8yz6cxotlzltvwp
The bzrdir format registry now accepts an ``alias`` keyword to
register_metadir, used to indicate that a format name is an alias for
some other format and thus should not be reported when describing the
format. (Robert Collins)
-------------- This line and the fmllowing will be ignored --------------

modified:
  NEWS
  bzrlib/bzrdir.py
  bzrlib/info.py
  bzrlib/tests/test_bzrdir.py
  bzrlib/tests/test_info.py

=== modified file 'NEWS'
--- a/NEWS      2008-01-02 22:30:46 +0000
+++ b/NEWS      2008-01-06 20:04:15 +0000
@@ -135,6 +135,11 @@
     * Patience Diff now supports arbitrary python objects, as long as they
       support ``hash()``. (John Arbash Meinel)
 
+    * The bzrdir format registry now accepts an ``alias`` keyword to
+      register_metadir, used to indicate that a format name is an alias for
+      some other format and thus should not be reported when describing the
+      format. (Robert Collins)
+
   API BREAKS:
 
   TESTING:

=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py  2008-01-02 22:30:46 +0000
+++ b/bzrlib/bzrdir.py  2008-01-06 19:41:29 +0000
@@ -2447,12 +2447,22 @@
     e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
     """
 
+    def __init__(self):
+        """Create a BzrDirFormatRegistry."""
+        self._aliases = set()
+        super(BzrDirFormatRegistry, self).__init__()
+
+    def aliases(self):
+        """Return a set of the format names which are aliases."""
+        return frozenset(self._aliases)
+
     def register_metadir(self, key,
              repository_format, help, native=True, deprecated=False,
              branch_format=None,
              tree_format=None,
              hidden=False,
-             experimental=False):
+             experimental=False,
+             alias=False):
         """Register a metadir subformat.
 
         These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
@@ -2491,10 +2501,10 @@
                 bd.repository_format = _load(repository_format)
             return bd
         self.register(key, helper, help, native, deprecated, hidden,
-            experimental)
+            experimental, alias)
 
     def register(self, key, factory, help, native=True, deprecated=False,
-                 hidden=False, experimental=False):
+                 hidden=False, experimental=False, alias=False):
         """Register a BzrDirFormat factory.
         
         The factory must be a callable that takes one parameter: the key.
@@ -2505,11 +2515,15 @@
         """
         registry.Registry.register(self, key, factory, help,
             BzrDirFormatInfo(native, deprecated, hidden, experimental))
+        if alias:
+            self._aliases.add(key)
 
     def register_lazy(self, key, module_name, member_name, help, native=True,
-                      deprecated=False, hidden=False, experimental=False):
+        deprecated=False, hidden=False, experimental=False, alias=False):
         registry.Registry.register_lazy(self, key, module_name, member_name,
             help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
+        if alias:
+            self._aliases.add(key)
 
     def set_default(self, key):
         """Set the 'default' key to be a clone of the supplied key.
@@ -2518,6 +2532,7 @@
         """
         registry.Registry.register(self, 'default', self.get(key),
             self.get_help(key), info=self.get_info(key))
+        self._aliases.add('default')
 
     def set_default_repository(self, key):
         """Set the FormatRegistry default and Repository default.
@@ -2670,6 +2685,7 @@
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     hidden=False,
     )
+# The following two formats should always just be aliases.
 format_registry.register_metadir('development',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0',
     help='Current development format. Can convert data to and from pack-0.92 '
@@ -2681,6 +2697,7 @@
     branch_format='bzrlib.branch.BzrBranchFormat6',
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     experimental=True,
+    alias=True,
     )
 format_registry.register_metadir('development-subtree',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0Subtree',
@@ -2693,7 +2710,9 @@
     branch_format='bzrlib.branch.BzrBranchFormat6',
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     experimental=True,
+    alias=True,
     )
+# And the development formats which the will have aliased one of follow:
 format_registry.register_metadir('development0',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0',
     help='Trivial rename of pack-0.92 to provide a development format. '

=== modified file 'bzrlib/info.py'
--- a/bzrlib/info.py    2007-11-06 09:00:25 +0000
+++ b/bzrlib/info.py    2008-01-06 20:01:30 +0000
@@ -440,7 +440,9 @@
         tree.bzrdir.root_transport.base):
         branch = None
         repository = None
-    for key in bzrdir.format_registry.keys():
+    non_aliases = set(bzrdir.format_registry.keys())
+    non_aliases.difference_update(bzrdir.format_registry.aliases())
+    for key in non_aliases:
         format = bzrdir.format_registry.make_bzrdir(key)
         if isinstance(format, bzrdir.BzrDirMetaFormat1):
             if (tree and format.workingtree_format !=
@@ -457,11 +459,12 @@
         candidates.append(key)
     if len(candidates) == 0:
         return 'unnamed'
-    new_candidates = [c for c in candidates if c != 'default']
-    if len(new_candidates) > 0:
-        candidates = new_candidates
+    candidates.sort()
     new_candidates = [c for c in candidates if not
         bzrdir.format_registry.get_info(c).hidden]
     if len(new_candidates) > 0:
+        # If there are any non-hidden formats that match, only return those to
+        # avoid listing hidden formats except when only a hidden format will
+        # do.
         candidates = new_candidates
     return ' or '.join(candidates)

=== modified file 'bzrlib/tests/test_bzrdir.py'
--- a/bzrlib/tests/test_bzrdir.py       2007-12-21 20:32:22 +0000
+++ b/bzrlib/tests/test_bzrdir.py       2008-01-06 19:45:00 +0000
@@ -170,6 +170,16 @@
         finally:
             bzrdir.format_registry.set_default_repository(old_default)
 
+    def test_aliases(self):
+        a_registry = bzrdir.BzrDirFormatRegistry()
+        a_registry.register('weave', bzrdir.BzrDirFormat6,
+            'Pre-0.8 format.  Slower and does not support checkouts or shared'
+            ' repositories', deprecated=True)
+        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
+            'Pre-0.8 format.  Slower and does not support checkouts or shared'
+            ' repositories', deprecated=True, alias=True)
+        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
+    
 
 class SampleBranch(bzrlib.branch.Branch):
     """A dummy branch for guess what, dummy use."""

=== modified file 'bzrlib/tests/test_info.py'
--- a/bzrlib/tests/test_info.py 2007-11-26 13:55:51 +0000
+++ b/bzrlib/tests/test_info.py 2008-01-06 20:02:10 +0000
@@ -126,16 +126,22 @@
 
     def test_describe_tree_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             self.assertTreeDescription(key)
 
     def test_describe_checkout_format(self):
         for key in bzrdir.format_registry.keys():
-            if key in ('default', 'weave', 'experimental'):
-                continue
-            if key.startswith('experimental-'):
-                # these are typically hidden or aliases for other formats
+            if key in bzrdir.format_registry.aliases():
+                # Aliases will not describe correctly in the UI because the
+                # real format is found.
+                continue
+            # legacy: weave does not support checkouts
+            if key == 'weave':
+                continue
+            if bzrdir.format_registry.get_info(key).experimental:
+                # We don't require that experimental formats support checkouts
+                # or describe correctly in the UI.
                 continue
             expected = None
             if key in ('dirstate', 'dirstate-tags', 'dirstate-with-subtree',
@@ -149,7 +155,7 @@
 
     def test_describe_branch_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             expected = None
             if key in ('dirstate', 'knit'):
@@ -158,7 +164,7 @@
 
     def test_describe_repo_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             expected = None
             if key in ('dirstate', 'knit', 'dirstate-tags'):

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
1
# Copyright (C) 2005 Canonical Ltd
4
 
 
 
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
9
 
 
 
7
#
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
11
# GNU General Public License for more details.
14
 
 
 
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 
 
16
#
19
17
# Author: Martin Pool <mbp@canonical.com>
20
18
 
21
19
 
26
24
There is one format marker followed by a blank line, followed by a
27
25
series of version headers, followed by the weave itself.
28
26
 
29
 
Each version marker has 'i' and the included previous versions, then
30
 
'1' and the SHA-1 of the text, if known.  The inclusions do not need
31
 
to list versions included by a parent.
 
27
Each version marker has
 
28
 
 
29
 'i'   parent version indexes
 
30
 '1'   SHA-1 of text
 
31
 'n'   name
 
32
 
 
33
The inclusions do not need to list versions included by a parent.
32
34
 
33
35
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
34
36
processing instructions.  Lines of text are prefixed by '.' if the
36
38
"""
37
39
 
38
40
# TODO: When extracting a single version it'd be enough to just pass
39
 
# an iterator returning the weave lines...
 
41
# an iterator returning the weave lines...  We don't really need to
 
42
# deserialize it into memory.
40
43
 
41
 
FORMAT_1 = '# bzr weave file v3\n'
 
44
FORMAT_1 = '# bzr weave file v5\n'
42
45
 
43
46
 
44
47
def write_weave(weave, f, format=None):
45
 
    if format == None or format == 1:
46
 
        return write_weave_v1(weave, f)
 
48
    if format is None or format == 1:
 
49
        return write_weave_v5(weave, f)
47
50
    else:
48
51
        raise ValueError("unknown weave format %r" % format)
49
52
 
50
53
 
51
 
def write_weave_v1(weave, f):
 
54
def write_weave_v5(weave, f):
52
55
    """Write weave to file f."""
53
 
    print >>f, FORMAT_1,
 
56
    f.write(FORMAT_1)
54
57
 
55
58
    for version, included in enumerate(weave._parents):
56
59
        if included:
57
60
            # mininc = weave.minimal_parents(version)
58
61
            mininc = included
59
 
            print >>f, 'i',
60
 
            for i in mininc:
61
 
                print >>f, i,
62
 
            print >>f
 
62
            f.write('i ')
 
63
            f.write(' '.join(str(i) for i in mininc))
 
64
            f.write('\n')
63
65
        else:
64
 
            print >>f, 'i'
65
 
        print >>f, '1', weave._sha1s[version]
66
 
        print >>f
 
66
            f.write('i\n')
 
67
        f.write('1 ' + weave._sha1s[version] + '\n')
 
68
        f.write('n ' + weave._names[version] + '\n')
 
69
        f.write('\n')
67
70
 
68
 
    print >>f, 'w'
 
71
    f.write('w\n')
69
72
 
70
73
    for l in weave._weave:
71
74
        if isinstance(l, tuple):
72
75
            assert l[0] in '{}[]'
73
 
            print >>f, '%s %d' % l
 
76
            if l[0] == '}':
 
77
                f.write('}\n')
 
78
            else:
 
79
                f.write('%s %d\n' % l)
74
80
        else: # text line
75
81
            if not l:
76
 
                print >>f, ', '
 
82
                f.write(', \n')
77
83
            elif l[-1] == '\n':
78
84
                assert l.find('\n', 0, -1) == -1
79
 
                print >>f, '.', l,
 
85
                f.write('. ' + l)
80
86
            else:
81
87
                assert l.find('\n') == -1
82
 
                print >>f, ',', l
 
88
                f.write(', ' + l + '\n')
83
89
 
84
 
    print >>f, 'W'
 
90
    f.write('W\n')
85
91
 
86
92
 
87
93
 
88
94
def read_weave(f):
89
 
    return read_weave_v1(f)
90
 
 
91
 
 
92
 
def read_weave_v1(f):
93
 
    from weave import Weave, WeaveFormatError
94
 
    w = Weave()
95
 
 
96
 
    wfe = WeaveFormatError
97
 
    l = f.readline()
 
95
    # FIXME: detect the weave type and dispatch
 
96
    from bzrlib.trace import mutter
 
97
    from weave import Weave
 
98
    w = Weave(getattr(f, 'name', None))
 
99
    _read_weave_v5(f, w)
 
100
    return w
 
101
 
 
102
 
 
103
def _read_weave_v5(f, w):
 
104
    """Private helper routine to read a weave format 5 file into memory.
 
105
    
 
106
    This is only to be used by read_weave and WeaveFile.__init__.
 
107
    """
 
108
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
 
109
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
 
110
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
 
111
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
 
112
    # replace readline call with iter over all lines ->
 
113
    # safe because we already suck on memory.
 
114
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
 
115
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
 
116
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
 
117
    # replaced startswith with slice lookups:
 
118
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
 
119
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
 
120
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
 
121
                  
 
122
    from weave import WeaveFormatError
 
123
 
 
124
    lines = iter(f.readlines())
 
125
    
 
126
    try:
 
127
        l = lines.next()
 
128
    except StopIteration:
 
129
        raise WeaveFormatError('invalid weave file: no header')
 
130
 
98
131
    if l != FORMAT_1:
99
132
        raise WeaveFormatError('invalid weave file header: %r' % l)
100
133
 
101
134
    ver = 0
 
135
    # read weave header.
102
136
    while True:
103
 
        l = f.readline()
 
137
        l = lines.next()
104
138
        if l[0] == 'i':
105
 
            ver += 1
106
 
 
107
139
            if len(l) > 2:
108
 
                w._parents.append(frozenset(map(int, l[2:].split(' '))))
 
140
                w._parents.append(map(int, l[2:].split(' ')))
109
141
            else:
110
 
                w._parents.append(frozenset())
 
142
                w._parents.append([])
111
143
 
112
 
            l = f.readline()[:-1]
113
 
            assert l.startswith('1 ')
 
144
            l = lines.next()[:-1]
 
145
            assert '1 ' == l[0:2]
114
146
            w._sha1s.append(l[2:])
115
147
                
116
 
            l = f.readline()
 
148
            l = lines.next()
 
149
            assert 'n ' == l[0:2]
 
150
            name = l[2:-1]
 
151
            assert name not in w._name_map
 
152
            w._names.append(name)
 
153
            w._name_map[name] = ver
 
154
                
 
155
            l = lines.next()
117
156
            assert l == '\n'
 
157
 
 
158
            ver += 1
118
159
        elif l == 'w\n':
119
160
            break
120
161
        else:
121
162
            raise WeaveFormatError('unexpected line %r' % l)
122
163
 
 
164
    # read weave body
123
165
    while True:
124
 
        l = f.readline()
 
166
        l = lines.next()
125
167
        if l == 'W\n':
126
168
            break
127
 
        elif l.startswith('. '):
 
169
        elif '. ' == l[0:2]:
128
170
            w._weave.append(l[2:])  # include newline
129
 
        elif l.startswith(', '):
 
171
        elif ', ' == l[0:2]:
130
172
            w._weave.append(l[2:-1])        # exclude newline
 
173
        elif l == '}\n':
 
174
            w._weave.append(('}', None))
131
175
        else:
132
 
            assert l[0] in '{}[]', l
 
176
            assert l[0] in '{[]', l
133
177
            assert l[1] == ' ', l
134
178
            w._weave.append((intern(l[0]), int(l[2:])))
135
179