~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-08-02 03:23:09 UTC
  • mto: This revision was merged to the branch mainline in revision 425.
  • Revision ID: aaron.bentley@utoronto.ca-20060802032309-6ad0139e61304b19
Etienne Goyer: remove unused shebangs, update packaging

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