~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2006-03-12 13:55:53 UTC
  • mto: (325.1.2 bzrtools) (0.3.1 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060312135553-151b108c9d215d85
Add support for detecting and upgrading from old format shelves.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import os
4
4
import sys
5
5
import subprocess
 
6
from datetime import datetime
6
7
from errors import CommandError
7
8
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
8
9
from patchsource import PatchSource, FilePatchSource
130
131
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
131
132
 
132
133
    def unshelve(self, patch_source, all_hunks=False):
 
134
        self._check_upgrade()
 
135
 
133
136
        patch_name = self.last_patch()
134
137
 
135
138
        if patch_name is None:
168
171
            f.close()
169
172
 
170
173
    def shelve(self, patch_source, all_hunks=False, message=None):
171
 
        from datetime import datetime
 
174
        self._check_upgrade()
172
175
 
173
176
        hunks = patch_source.readhunks()
174
177
 
219
222
            raise CommandError("Failed applying patches!")
220
223
 
221
224
        return result
 
225
 
 
226
    def _check_upgrade(self):
 
227
        if len(self._list_old_shelves()) > 0:
 
228
            raise CommandError("Old format shelves found, either upgrade " \
 
229
                    "or remove them!")
 
230
 
 
231
    def _list_old_shelves(self):
 
232
        import glob
 
233
        stem = os.path.join(self.base, '.bzr-shelf')
 
234
 
 
235
        patches = glob.glob(stem)
 
236
        patches.extend(glob.glob(stem + '-*[!~]'))
 
237
 
 
238
        if len(patches) == 0:
 
239
            return []
 
240
 
 
241
        def patch_index(name):
 
242
            if name == stem:
 
243
                return 0
 
244
            return int(name[len(stem) + 1:])
 
245
 
 
246
        # patches might not be sorted in the right order
 
247
        patch_ids = []
 
248
        for patch in patches:
 
249
            if patch == stem:
 
250
                patch_ids.append(0)
 
251
            else:
 
252
                patch_ids.append(int(patch[len(stem) + 1:]))
 
253
 
 
254
        patch_ids.sort()
 
255
 
 
256
        patches = []
 
257
        for id in patch_ids:
 
258
            if id == 0:
 
259
                patches.append(stem)
 
260
            else:
 
261
                patches.append('%s-%s' % (stem, id))
 
262
 
 
263
        return patches
 
264
 
 
265
    def upgrade(self):
 
266
        patches = self._list_old_shelves()
 
267
 
 
268
        if len(patches) == 0:
 
269
            self.log('No old-style shelves found to upgrade.\n')
 
270
            return
 
271
 
 
272
        for patch in patches:
 
273
            old_file = open(patch, 'r')
 
274
            new_path = self.next_patch()
 
275
            new_file = open(new_path, 'w')
 
276
            new_file.write(old_file.read())
 
277
            old_file.close()
 
278
            new_file.close()
 
279
            self.log('Copied %s to %s/%s\n' % (os.path.basename(patch),
 
280
                self.name, os.path.basename(new_path)))
 
281
            os.rename(patch, patch + '~')