3
# Copyright (C) 2005 Michael Ellerman <michael@ellerman.id.au>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3
from patches import parse_patches
23
8
from bzrlib.commands import Command
26
if len(args) == 2 and args[1] == '--bzr-usage':
29
elif len(args) == 2 and args[1] == '--bzr-help':
30
print 'Shelve a patch, you can get it back later with unshelve.'
35
raise Exception("Don't understand args %s" % args)
11
name = os.path.basename(args.pop(0))
13
if name not in ['shelve', 'unshelve']:
14
raise Exception("Unknown command name '%s'" % name)
17
if args[0] == '--bzr-usage':
20
elif args[0] == '--bzr-help':
21
print 'Shelve a patch, you can get it back later with unshelve.'
24
raise Exception("Don't understand args %s" % args)
40
32
root = run_bzr('root')[0].strip()
103
100
raise Exception("Failed running bzr")
106
def prompt_user(patch_list):
108
for patch in patch_list:
109
total += len(patch.hunks)
116
for patch in patch_list:
117
if patch.oldname != patch.newname:
118
name = "%s -> %s" % (patch.oldname, patch.newname)
122
hunks = patch.hunks[:]
127
out.write('Shelve this change? (%d of %d) [yn] ' % (i, total))
128
line = inp.readline().strip().lower()
129
if line == 'y' or line == 'yes':
131
elif line == 'n' or line == 'no':
132
patch.hunks.remove(hunk)
137
if len(patch.hunks) > 0:
138
to_shelve.append(patch)
142
103
class cmd_shelve(Command):
143
104
"""Temporarily remove some changes from the current tree.
144
105
Use 'unshelve' to restore these changes.
155
116
return unshelve()
120
def __init__(self, char, action, help, default=False):
123
self.default = default
127
Option('n', 'shelve', 'shelve this change for the moment.',
129
Option('y', 'keep', 'keep this change in your tree.'),
130
Option('d', 'done', 'done, skip to the end.'),
131
Option('i', 'invert', 'invert the current selection of all hunks.'),
132
Option('s', 'status', 'show status of hunks.'),
133
Option('q', 'quit', 'quit')
137
Option('y', 'continue', 'proceed to shelve selected changes.',
139
Option('r', 'restart', 'restart the hunk selection loop.'),
140
Option('s', 'status', 'show status of hunks.'),
141
Option('i', 'invert', 'invert the current selection of all hunks.'),
142
Option('q', 'quit', 'quit')
145
def __init__(self, patches):
146
self.patches = patches
148
for patch in patches:
149
for hunk in patch.hunks:
150
# everything's shelved by default
152
self.total_hunks += 1
154
def __get_option(self, char):
155
for opt in self.standard_options:
158
raise Exception('Option "%s" not found!' % char)
160
def __select_loop(self):
162
for patch in self.patches:
165
while i < len(patch.hunks):
166
hunk = patch.hunks[i]
168
print patch.get_header(), hunk
172
prompt = 'Keep this change? (%d of %d)' \
173
% (j, self.total_hunks)
176
self.__get_option('n').default = True
177
self.__get_option('y').default = False
179
self.__get_option('n').default = False
180
self.__get_option('y').default = True
182
action = self.__ask_user(prompt, self.standard_options)
185
hunk.selected = False
186
elif action == 'shelve':
188
elif action == 'done':
190
elif action == 'invert':
191
self.__invert_selection()
194
elif action == 'status':
197
elif action == 'quit':
204
if self.total_hunks == 0:
209
if not self.__select_loop():
214
prompt = "Shelve these changes, or restart?"
215
action = self.__ask_user(prompt, self.end_options)
217
if action == 'continue':
220
elif action == 'quit':
222
elif action == 'status':
224
elif action == 'invert':
225
self.__invert_selection()
226
elif action == 'restart':
230
for patch in self.patches:
232
for hunk in patch.hunks:
238
for patch in self.patches:
245
def __invert_selection(self):
246
for patch in self.patches:
247
for hunk in patch.hunks:
248
if hunk.__dict__.has_key('selected'):
249
hunk.selected = not hunk.selected
253
def __show_status(self):
255
for patch in self.patches:
256
print ' %s' % patch.oldname
259
for hunk in patch.hunks:
265
print ' %d hunks to be shelved' % shelve
266
print ' %d hunks to be kept' % keep
270
fd = sys.stdin.fileno()
271
settings = termios.tcgetattr(fd)
274
ch = sys.stdin.read(1)
276
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
279
def __ask_user(self, prompt, options):
281
sys.stdout.write(prompt)
282
sys.stdout.write(' [')
286
sys.stdout.write(opt.char)
287
sys.stdout.write('?] (%s): ' % default.char)
289
response = self.__getchar()
291
# default, which we see as newline, is 'n'
292
if response in ['\n', '\r', '\r\n']:
293
response = default.char
295
print response # because echo is off
298
if opt.char == response:
302
print ' %s - %s' % (opt.char, opt.help)