~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2008-11-05 00:11:09 UTC
  • mto: This revision was merged to the branch mainline in revision 678.
  • Revision ID: aaron@aaronbentley.com-20081105001109-yt2dp0h5h3ssb7xt
Restore runtime ignore for .shelf

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