~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-06-14 18:09:53 UTC
  • mto: This revision was merged to the branch mainline in revision 395.
  • Revision ID: abentley@panoramicfeedback.com-20060614180953-4671478534fd8823
Update NEWS

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
 
5
import subprocess
3
6
from datetime import datetime
4
7
from errors import CommandError, PatchFailed
5
8
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
6
 
from patch import run_patch
7
 
from patchsource import FilePatchSource
8
 
from bzrlib.osutils import rename
 
9
from patchsource import PatchSource, FilePatchSource
9
10
 
10
11
class Shelf(object):
11
12
    MESSAGE_PREFIX = "# Shelved patch: "
55
56
 
56
57
    def delete(self, patch):
57
58
        path = self.__path_from_user(patch)
58
 
        rename(path, '%s~' % path)
 
59
        os.rename(path, '%s~' % path)
59
60
 
60
 
    def display(self, patch=None):
61
 
        if patch is None:
62
 
            path = self.last_patch()
63
 
            if path is None:
64
 
                raise CommandError("No patches on shelf.")
65
 
        else:
66
 
            path = self.__path_from_user(patch)
 
61
    def display(self, patch):
 
62
        path = self.__path_from_user(patch)
67
63
        sys.stdout.write(open(path).read())
68
64
 
69
65
    def list(self):
82
78
    def __path_from_user(self, patch_id):
83
79
        try:
84
80
            patch_index = int(patch_id)
85
 
        except (TypeError, ValueError):
 
81
        except TypeError:
86
82
            raise CommandError("Invalid patch name '%s'" % patch_id)
87
83
 
88
84
        path = self.__path(patch_index)
134
130
            return None
135
131
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
136
132
 
137
 
    def unshelve(self, patch_source, patch_name=None, all=False, force=False,
138
 
                 no_color=False):
 
133
    def unshelve(self, patch_source, all=False, force=False):
139
134
        self._check_upgrade()
140
135
 
141
 
        if no_color is False:
142
 
            color = None
143
 
        else:
144
 
            color = False
 
136
        patch_name = self.last_patch()
 
137
 
145
138
        if patch_name is None:
146
 
            patch_path = self.last_patch()
147
 
        else:
148
 
            patch_path = self.__path_from_user(patch_name)
149
 
 
150
 
        if patch_path is None:
151
139
            raise CommandError("No patch found on shelf %s" % self.name)
152
140
 
153
 
        patches = FilePatchSource(patch_path).readpatches()
 
141
        patches = FilePatchSource(patch_name).readpatches()
154
142
        if all:
155
143
            to_unshelve = patches
156
144
            to_remain = []
157
145
        else:
158
 
            hs = UnshelveHunkSelector(patches, color)
159
 
            to_unshelve, to_remain = hs.select()
 
146
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
160
147
 
161
148
        if len(to_unshelve) == 0:
162
149
            raise CommandError('Nothing to unshelve')
163
150
 
164
 
        message = self.get_patch_message(patch_path)
 
151
        message = self.get_patch_message(patch_name)
165
152
        if message is None:
166
153
            message = "No message saved with patch."
167
154
        self.log('Unshelving from %s/%s: "%s"\n' % \
168
 
                (self.name, os.path.basename(patch_path), message))
 
155
                (self.name, os.path.basename(patch_name), message))
169
156
 
170
157
        try:
171
158
            self._run_patch(to_unshelve, dry_run=True)
187
174
                    "longer applies cleanly to the working tree!")
188
175
 
189
176
        # Backup the shelved patch
190
 
        rename(patch_path, '%s~' % patch_path)
 
177
        os.rename(patch_name, '%s~' % patch_name)
191
178
 
192
179
        if len(to_remain) > 0:
193
 
            f = open(patch_path, 'w')
 
180
            f = open(patch_name, 'w')
194
181
            for patch in to_remain:
195
182
                f.write(str(patch))
196
183
            f.close()
197
184
 
198
 
    def shelve(self, patch_source, all=False, message=None, no_color=False):
 
185
    def shelve(self, patch_source, all=False, message=None):
199
186
        self._check_upgrade()
200
 
        if no_color is False:
201
 
            color = None
202
 
        else:
203
 
            color = False
204
187
 
205
188
        patches = patch_source.readpatches()
206
189
 
207
190
        if all:
208
191
            to_shelve = patches
209
192
        else:
210
 
            to_shelve = ShelveHunkSelector(patches, color).select()[0]
 
193
            to_shelve = ShelveHunkSelector(patches).select()[0]
211
194
 
212
195
        if len(to_shelve) == 0:
213
196
            raise CommandError('Nothing to shelve')
216
199
            timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
217
200
            message = "Changes shelved on %s" % timestamp
218
201
 
219
 
        patch_path = self.next_patch()
 
202
        patch_name = self.next_patch()
220
203
        self.log('Shelving to %s/%s: "%s"\n' % \
221
 
                (self.name, os.path.basename(patch_path), message))
 
204
                (self.name, os.path.basename(patch_name), message))
222
205
 
223
 
        f = open(patch_path, 'a')
 
206
        f = open(patch_name, 'a')
224
207
 
225
208
        assert '\n' not in message
226
209
        f.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
244
227
                    "working tree!")
245
228
 
246
229
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
247
 
        run_patch(self.base, patches, strip, reverse, dry_run)
 
230
        args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
 
231
        if reverse:
 
232
            args.append('-R')
 
233
        if dry_run:
 
234
            args.append('--dry-run')
 
235
            stdout = stderr = subprocess.PIPE
 
236
        else:
 
237
            stdout = stderr = None
 
238
 
 
239
        process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
 
240
                        stderr=stderr)
 
241
        for patch in patches:
 
242
            process.stdin.write(str(patch))
 
243
 
 
244
        process.communicate()
 
245
 
 
246
        result = process.wait()
 
247
        if result != 0:
 
248
            raise PatchFailed()
 
249
 
 
250
        return result
248
251
 
249
252
    def _check_upgrade(self):
250
253
        if len(self._list_old_shelves()) > 0:
301
304
            new_file.close()
302
305
            self.log('Copied %s to %s/%s\n' % (os.path.basename(patch),
303
306
                self.name, os.path.basename(new_path)))
304
 
            rename(patch, patch + '~')
 
307
            os.rename(patch, patch + '~')