~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-06-13 02:31:28 UTC
  • mfrom: (0.1.119 shelf)
  • Revision ID: aaron.bentley@utoronto.ca-20060613023128-20f2ff3a212a6ea2
Pull in latest shelf stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
1
3
import os
2
4
import sys
3
5
import subprocess
4
6
from datetime import datetime
5
 
from errors import CommandError, PatchFailed, PatchInvokeError
 
7
from errors import CommandError, PatchFailed
6
8
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
7
 
from patch import run_patch
8
9
from patchsource import PatchSource, FilePatchSource
9
 
from bzrlib.osutils import rename
10
10
 
11
11
class Shelf(object):
12
12
    MESSAGE_PREFIX = "# Shelved patch: "
56
56
 
57
57
    def delete(self, patch):
58
58
        path = self.__path_from_user(patch)
59
 
        rename(path, '%s~' % path)
 
59
        os.rename(path, '%s~' % path)
60
60
 
61
61
    def display(self, patch=None):
62
62
        if patch is None:
63
63
            path = self.last_patch()
64
 
            if path is None:
65
 
                raise CommandError("No patches on shelf.")
66
64
        else:
67
65
            path = self.__path_from_user(patch)
68
66
        sys.stdout.write(open(path).read())
83
81
    def __path_from_user(self, patch_id):
84
82
        try:
85
83
            patch_index = int(patch_id)
86
 
        except (TypeError, ValueError):
 
84
        except TypeError:
87
85
            raise CommandError("Invalid patch name '%s'" % patch_id)
88
86
 
89
87
        path = self.__path(patch_index)
135
133
            return None
136
134
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
137
135
 
138
 
    def unshelve(self, patch_source, patch_name=None, all=False, force=False,
139
 
                 no_color=False):
 
136
    def unshelve(self, patch_source, patch_name=None, all=False, force=False):
140
137
        self._check_upgrade()
141
138
 
142
 
        if no_color is False:
143
 
            color = None
144
 
        else:
145
 
            color = False
146
139
        if patch_name is None:
147
140
            patch_path = self.last_patch()
148
141
        else:
156
149
            to_unshelve = patches
157
150
            to_remain = []
158
151
        else:
159
 
            hs = UnshelveHunkSelector(patches, color)
160
 
            to_unshelve, to_remain = hs.select()
 
152
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
161
153
 
162
154
        if len(to_unshelve) == 0:
163
155
            raise CommandError('Nothing to unshelve')
188
180
                    "longer applies cleanly to the working tree!")
189
181
 
190
182
        # Backup the shelved patch
191
 
        rename(patch_path, '%s~' % patch_path)
 
183
        os.rename(patch_path, '%s~' % patch_path)
192
184
 
193
185
        if len(to_remain) > 0:
194
186
            f = open(patch_path, 'w')
196
188
                f.write(str(patch))
197
189
            f.close()
198
190
 
199
 
    def shelve(self, patch_source, all=False, message=None, no_color=False):
 
191
    def shelve(self, patch_source, all=False, message=None):
200
192
        self._check_upgrade()
201
 
        if no_color is False:
202
 
            color = None
203
 
        else:
204
 
            color = False
205
193
 
206
194
        patches = patch_source.readpatches()
207
195
 
208
196
        if all:
209
197
            to_shelve = patches
210
198
        else:
211
 
            to_shelve = ShelveHunkSelector(patches, color).select()[0]
 
199
            to_shelve = ShelveHunkSelector(patches).select()[0]
212
200
 
213
201
        if len(to_shelve) == 0:
214
202
            raise CommandError('Nothing to shelve')
245
233
                    "working tree!")
246
234
 
247
235
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
248
 
        run_patch(self.base, patches, strip, reverse, dry_run)
 
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
249
257
 
250
258
    def _check_upgrade(self):
251
259
        if len(self._list_old_shelves()) > 0:
302
310
            new_file.close()
303
311
            self.log('Copied %s to %s/%s\n' % (os.path.basename(patch),
304
312
                self.name, os.path.basename(new_path)))
305
 
            rename(patch, patch + '~')
 
313
            os.rename(patch, patch + '~')