~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Jelmer Vernooij
  • Date: 2011-08-29 17:00:45 UTC
  • mto: This revision was merged to the branch mainline in revision 778.
  • Revision ID: jelmer@samba.org-20110829170045-j1yns6q2e6jb236i
Remove unused imports and fix two imports.

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