~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2007-05-18 09:20:28 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20070518092028-6oii053t2hkevi9k
Report new bug

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
3
import subprocess
6
4
from datetime import datetime
7
 
from errors import CommandError, PatchFailed
 
5
from errors import CommandError, PatchFailed, PatchInvokeError
8
6
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
9
7
from patchsource import PatchSource, 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:
81
80
    def __path_from_user(self, patch_id):
82
81
        try:
83
82
            patch_index = int(patch_id)
84
 
        except TypeError:
 
83
        except (TypeError, ValueError):
85
84
            raise CommandError("Invalid patch name '%s'" % patch_id)
86
85
 
87
86
        path = self.__path(patch_index)
133
132
            return None
134
133
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
135
134
 
136
 
    def unshelve(self, patch_source, all=False, force=False):
 
135
    def unshelve(self, patch_source, patch_name=None, all=False, force=False,
 
136
                 no_color=False):
137
137
        self._check_upgrade()
138
138
 
139
 
        patch_path = self.last_patch()
 
139
        if no_color is False:
 
140
            color = None
 
141
        else:
 
142
            color = False
 
143
        if patch_name is None:
 
144
            patch_path = self.last_patch()
 
145
        else:
 
146
            patch_path = self.__path_from_user(patch_name)
140
147
 
141
148
        if patch_path is None:
142
149
            raise CommandError("No patch found on shelf %s" % self.name)
146
153
            to_unshelve = patches
147
154
            to_remain = []
148
155
        else:
149
 
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
 
156
            hs = UnshelveHunkSelector(patches, color)
 
157
            to_unshelve, to_remain = hs.select()
150
158
 
151
159
        if len(to_unshelve) == 0:
152
160
            raise CommandError('Nothing to unshelve')
177
185
                    "longer applies cleanly to the working tree!")
178
186
 
179
187
        # Backup the shelved patch
180
 
        os.rename(patch_path, '%s~' % patch_path)
 
188
        rename(patch_path, '%s~' % patch_path)
181
189
 
182
190
        if len(to_remain) > 0:
183
191
            f = open(patch_path, 'w')
185
193
                f.write(str(patch))
186
194
            f.close()
187
195
 
188
 
    def shelve(self, patch_source, all=False, message=None):
 
196
    def shelve(self, patch_source, all=False, message=None, no_color=False):
189
197
        self._check_upgrade()
 
198
        if no_color is False:
 
199
            color = None
 
200
        else:
 
201
            color = False
190
202
 
191
203
        patches = patch_source.readpatches()
192
204
 
193
205
        if all:
194
206
            to_shelve = patches
195
207
        else:
196
 
            to_shelve = ShelveHunkSelector(patches).select()[0]
 
208
            to_shelve = ShelveHunkSelector(patches, color).select()[0]
197
209
 
198
210
        if len(to_shelve) == 0:
199
211
            raise CommandError('Nothing to shelve')
231
243
 
232
244
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
233
245
        args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
 
246
 
 
247
        if sys.platform == "win32":
 
248
            args.append('--binary')
 
249
 
234
250
        if reverse:
235
251
            args.append('-R')
236
252
        if dry_run:
239
255
        else:
240
256
            stdout = stderr = None
241
257
 
242
 
        process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
243
 
                        stderr=stderr)
244
 
        for patch in patches:
245
 
            process.stdin.write(str(patch))
 
258
        try:
 
259
            process = subprocess.Popen(args, stdin=subprocess.PIPE,
 
260
                                       stdout=stdout, stderr=stderr)
 
261
            for patch in patches:
 
262
                process.stdin.write(str(patch))
 
263
 
 
264
        except IOError, e:
 
265
            raise PatchInvokeError(e, process.stderr.read())
246
266
 
247
267
        process.communicate()
248
268
 
307
327
            new_file.close()
308
328
            self.log('Copied %s to %s/%s\n' % (os.path.basename(patch),
309
329
                self.name, os.path.basename(new_path)))
310
 
            os.rename(patch, patch + '~')
 
330
            rename(patch, patch + '~')