~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/upgrade.py

MergeĀ fromĀ jam-integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
import sys
72
72
import shutil
73
73
 
74
 
from bzrlib.branch import Branch, find_branch
 
74
from bzrlib.branch import Branch
75
75
from bzrlib.branch import BZR_BRANCH_FORMAT_5, BZR_BRANCH_FORMAT_6
76
76
import bzrlib.hashcache as hashcache
77
77
from bzrlib.weave import Weave
81
81
from bzrlib.xml4 import serializer_v4
82
82
from bzrlib.xml5 import serializer_v5
83
83
from bzrlib.trace import mutter, note, warning
84
 
from bzrlib.osutils import sha_strings, sha_string
 
84
from bzrlib.osutils import sha_strings, sha_string, pathjoin, abspath
 
85
 
 
86
 
 
87
# TODO: jam 20060108 Create a new branch format, and as part of upgrade
 
88
#       make sure that ancestry.weave is deleted (it is never used, but
 
89
#       used to be created)
85
90
 
86
91
 
87
92
class Convert(object):
102
107
        self.pb = ui_factory.progress_bar()
103
108
        if self.old_format == 4:
104
109
            note('starting upgrade from format 4 to 5')
105
 
            self._convert_to_weaves()
 
110
            self.branch.lock_write()
 
111
            try:
 
112
                self._convert_to_weaves()
 
113
            finally:
 
114
                self.branch.unlock()
106
115
            self._open_branch()
107
116
        if self.old_format == 5:
108
117
            note('starting upgrade from format 5 to 6')
109
 
            self._convert_to_prefixed()
 
118
            self.branch.lock_write()
 
119
            try:
 
120
                self._convert_to_prefixed()
 
121
            finally:
 
122
                self.branch.unlock()
110
123
            self._open_branch()
111
 
        cache = hashcache.HashCache(os.path.abspath(self.base))
 
124
        cache = hashcache.HashCache(abspath(self.base))
112
125
        cache.clear()
113
126
        cache.write()
114
127
        note("finished")
118
131
        from bzrlib.store import hash_prefix
119
132
        for store_name in ["weaves", "revision-store"]:
120
133
            note("adding prefixes to %s" % store_name) 
121
 
            store_dir = os.path.join(self.base, ".bzr", store_name)
 
134
            store_dir = pathjoin(self.base, ".bzr", store_name)
122
135
            for filename in os.listdir(store_dir):
123
136
                if filename.endswith(".weave") or filename.endswith(".gz"):
124
137
                    file_id = os.path.splitext(filename)[0]
125
138
                else:
126
139
                    file_id = filename
127
 
                prefix_dir = os.path.join(store_dir, hash_prefix(file_id))
 
140
                prefix_dir = pathjoin(store_dir, hash_prefix(file_id))
128
141
                if not os.path.isdir(prefix_dir):
129
142
                    os.mkdir(prefix_dir)
130
 
                os.rename(os.path.join(store_dir, filename),
131
 
                          os.path.join(prefix_dir, filename))
 
143
                os.rename(pathjoin(store_dir, filename),
 
144
                          pathjoin(prefix_dir, filename))
132
145
        self._set_new_format(BZR_BRANCH_FORMAT_6)
133
146
 
134
147
 
139
152
        self.inv_weave = Weave('inventory')
140
153
        # holds in-memory weaves for all files
141
154
        self.text_weaves = {}
142
 
        os.remove(self.branch.controlfilename('branch-format'))
 
155
        os.remove(self.branch.control_files.controlfilename('branch-format'))
143
156
        self._convert_working_inv()
144
157
        rev_history = self.branch.revision_history()
145
158
        # to_read is a stack holding the revisions we still need to process;
178
191
                           self.branch._branch_format)
179
192
        return True
180
193
 
181
 
 
182
194
    def _set_new_format(self, format):
183
 
        self.branch.put_controlfile('branch-format', format)
184
 
 
 
195
        self.branch.control_files.put_utf8('branch-format', format)
185
196
 
186
197
    def _cleanup_spare_files(self):
187
198
        for n in 'merged-patches', 'pending-merged-patches':
188
 
            p = self.branch.controlfilename(n)
 
199
            p = self.branch.control_files.controlfilename(n)
189
200
            if not os.path.exists(p):
190
201
                continue
191
202
            ## assert os.path.getsize(p) == 0
193
204
        shutil.rmtree(self.base + '/.bzr/inventory-store')
194
205
        shutil.rmtree(self.base + '/.bzr/text-store')
195
206
 
196
 
 
197
207
    def _backup_control_dir(self):
198
208
        orig = self.base + '/.bzr'
199
209
        backup = orig + '.backup'
203
213
        note('if conversion fails, you can move this directory back to .bzr')
204
214
        note('if it succeeds, you can remove this directory if you wish')
205
215
 
206
 
 
207
216
    def _convert_working_inv(self):
208
217
        branch = self.branch
209
 
        inv = serializer_v4.read_inventory(branch.controlfile('inventory', 'rb'))
 
218
        inv = serializer_v4.read_inventory(branch.control_files.get('inventory'))
210
219
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
211
 
        branch.put_controlfile('inventory', new_inv_xml)
212
 
 
213
 
 
 
220
        branch.control_files.put('inventory', new_inv_xml)
214
221
 
215
222
    def _write_all_weaves(self):
216
223
        write_a_weave(self.inv_weave, self.base + '/.bzr/inventory.weave')
248
255
        self.pb.update('loading revision',
249
256
                       len(self.revisions),
250
257
                       len(self.known_revisions))
251
 
        if not self.branch.revision_store.has_id(rev_id):
 
258
        if not self.branch.repository.revision_store.has_id(rev_id):
252
259
            self.pb.clear()
253
260
            note('revision {%s} not present in branch; '
254
261
                 'will be converted as a ghost',
255
262
                 rev_id)
256
263
            self.absent_revisions.add(rev_id)
257
264
        else:
258
 
            rev_xml = self.branch.revision_store.get(rev_id).read()
 
265
            rev_xml = self.branch.repository.revision_store.get(rev_id).read()
259
266
            rev = serializer_v4.read_revision_from_string(rev_xml)
260
267
            for parent_id in rev.parent_ids:
261
268
                self.known_revisions.add(parent_id)
265
272
 
266
273
    def _load_old_inventory(self, rev_id):
267
274
        assert rev_id not in self.converted_revs
268
 
        old_inv_xml = self.branch.inventory_store.get(rev_id).read()
 
275
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
269
276
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
270
277
        rev = self.revisions[rev_id]
271
278
        if rev.inventory_sha1:
360
367
                return
361
368
        parent_indexes = map(w.lookup, previous_revisions)
362
369
        if ie.has_text():
363
 
            file_lines = self.branch.text_store.get(ie.text_id).readlines()
 
370
            text = self.branch.repository.text_store.get(ie.text_id)
 
371
            file_lines = text.readlines()
364
372
            assert sha_strings(file_lines) == ie.text_sha1
365
373
            assert sum(map(len, file_lines)) == ie.text_size
366
374
            w.add(rev_id, parent_indexes, file_lines, ie.text_sha1)