~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
10
8
from bzrlib.osutils import rename
11
9
 
12
10
class Shelf(object):
62
60
    def display(self, patch=None):
63
61
        if patch is None:
64
62
            path = self.last_patch()
 
63
            if path is None:
 
64
                raise CommandError("No patches on shelf.")
65
65
        else:
66
66
            path = self.__path_from_user(patch)
67
67
        sys.stdout.write(open(path).read())
134
134
            return None
135
135
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
136
136
 
137
 
    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):
138
139
        self._check_upgrade()
139
140
 
 
141
        if no_color is False:
 
142
            color = None
 
143
        else:
 
144
            color = False
140
145
        if patch_name is None:
141
146
            patch_path = self.last_patch()
142
147
        else:
150
155
            to_unshelve = patches
151
156
            to_remain = []
152
157
        else:
153
 
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
 
158
            hs = UnshelveHunkSelector(patches, color)
 
159
            to_unshelve, to_remain = hs.select()
154
160
 
155
161
        if len(to_unshelve) == 0:
156
162
            raise CommandError('Nothing to unshelve')
189
195
                f.write(str(patch))
190
196
            f.close()
191
197
 
192
 
    def shelve(self, patch_source, all=False, message=None):
 
198
    def shelve(self, patch_source, all=False, message=None, no_color=False):
193
199
        self._check_upgrade()
 
200
        if no_color is False:
 
201
            color = None
 
202
        else:
 
203
            color = False
194
204
 
195
205
        patches = patch_source.readpatches()
196
206
 
197
207
        if all:
198
208
            to_shelve = patches
199
209
        else:
200
 
            to_shelve = ShelveHunkSelector(patches).select()[0]
 
210
            to_shelve = ShelveHunkSelector(patches, color).select()[0]
201
211
 
202
212
        if len(to_shelve) == 0:
203
213
            raise CommandError('Nothing to shelve')
234
244
                    "working tree!")
235
245
 
236
246
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
237
 
        args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
238
 
 
239
 
        if sys.platform == "win32":
240
 
            args.append('--binary')
241
 
 
242
 
        if reverse:
243
 
            args.append('-R')
244
 
        if dry_run:
245
 
            args.append('--dry-run')
246
 
            stdout = stderr = subprocess.PIPE
247
 
        else:
248
 
            stdout = stderr = None
249
 
 
250
 
        process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
251
 
                        stderr=stderr)
252
 
        for patch in patches:
253
 
            process.stdin.write(str(patch))
254
 
 
255
 
        process.communicate()
256
 
 
257
 
        result = process.wait()
258
 
        if result != 0:
259
 
            raise PatchFailed()
260
 
 
261
 
        return result
 
247
        run_patch(self.base, patches, strip, reverse, dry_run)
262
248
 
263
249
    def _check_upgrade(self):
264
250
        if len(self._list_old_shelves()) > 0: