~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2013-08-20 03:02:43 UTC
  • Revision ID: aaron@aaronbentley.com-20130820030243-r8v1xfbcnd8f10p4
Fix zap command for 2.6/7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
1
import os
4
2
import sys
5
 
import subprocess
6
3
from datetime import datetime
7
4
from errors import CommandError, PatchFailed
8
5
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
9
 
from patchsource import PatchSource, FilePatchSource
 
6
from patch import run_patch
 
7
from patchsource import FilePatchSource
 
8
from bzrlib.osutils import rename
10
9
 
11
10
class Shelf(object):
12
11
    MESSAGE_PREFIX = "# Shelved patch: "
56
55
 
57
56
    def delete(self, patch):
58
57
        path = self.__path_from_user(patch)
59
 
        os.rename(path, '%s~' % path)
 
58
        rename(path, '%s~' % path)
60
59
 
61
60
    def display(self, patch=None):
62
61
        if patch is None:
63
62
            path = self.last_patch()
 
63
            if path is None:
 
64
                raise CommandError("No patches on shelf.")
64
65
        else:
65
66
            path = self.__path_from_user(patch)
66
67
        sys.stdout.write(open(path).read())
133
134
            return None
134
135
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
135
136
 
136
 
    def unshelve(self, patch_source, patch_name=None, all=False, force=False):
 
137
    def unshelve(self, patch_source, patch_name=None, all=False, force=False,
 
138
                 no_color=False):
137
139
        self._check_upgrade()
138
140
 
 
141
        if no_color is False:
 
142
            color = None
 
143
        else:
 
144
            color = False
139
145
        if patch_name is None:
140
146
            patch_path = self.last_patch()
141
147
        else:
149
155
            to_unshelve = patches
150
156
            to_remain = []
151
157
        else:
152
 
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
 
158
            hs = UnshelveHunkSelector(patches, color)
 
159
            to_unshelve, to_remain = hs.select()
153
160
 
154
161
        if len(to_unshelve) == 0:
155
162
            raise CommandError('Nothing to unshelve')
180
187
                    "longer applies cleanly to the working tree!")
181
188
 
182
189
        # Backup the shelved patch
183
 
        os.rename(patch_path, '%s~' % patch_path)
 
190
        rename(patch_path, '%s~' % patch_path)
184
191
 
185
192
        if len(to_remain) > 0:
186
193
            f = open(patch_path, 'w')
188
195
                f.write(str(patch))
189
196
            f.close()
190
197
 
191
 
    def shelve(self, patch_source, all=False, message=None):
 
198
    def shelve(self, patch_source, all=False, message=None, no_color=False):
192
199
        self._check_upgrade()
 
200
        if no_color is False:
 
201
            color = None
 
202
        else:
 
203
            color = False
193
204
 
194
205
        patches = patch_source.readpatches()
195
206
 
196
207
        if all:
197
208
            to_shelve = patches
198
209
        else:
199
 
            to_shelve = ShelveHunkSelector(patches).select()[0]
 
210
            to_shelve = ShelveHunkSelector(patches, color).select()[0]
200
211
 
201
212
        if len(to_shelve) == 0:
202
213
            raise CommandError('Nothing to shelve')
233
244
                    "working tree!")
234
245
 
235
246
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
236
 
        args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
237
 
        if reverse:
238
 
            args.append('-R')
239
 
        if dry_run:
240
 
            args.append('--dry-run')
241
 
            stdout = stderr = subprocess.PIPE
242
 
        else:
243
 
            stdout = stderr = None
244
 
 
245
 
        process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
246
 
                        stderr=stderr)
247
 
        for patch in patches:
248
 
            process.stdin.write(str(patch))
249
 
 
250
 
        process.communicate()
251
 
 
252
 
        result = process.wait()
253
 
        if result != 0:
254
 
            raise PatchFailed()
255
 
 
256
 
        return result
 
247
        run_patch(self.base, patches, strip, reverse, dry_run)
257
248
 
258
249
    def _check_upgrade(self):
259
250
        if len(self._list_old_shelves()) > 0:
310
301
            new_file.close()
311
302
            self.log('Copied %s to %s/%s\n' % (os.path.basename(patch),
312
303
                self.name, os.path.basename(new_path)))
313
 
            os.rename(patch, patch + '~')
 
304
            rename(patch, patch + '~')