98
98
if pipe.close() is not None:
99
99
raise Exception("Failed running bzr")
105
def __init__(self, char, action, help, default=False):
108
self.default = default
112
Option('n', 'shelve', 'shelve this change for the moment.',
114
Option('y', 'keep', 'keep this change in your tree.'),
115
Option('d', 'done', 'done, skip to the end.'),
116
Option('i', 'invert', 'invert the current selection of all hunks.'),
117
Option('s', 'status', 'show status of hunks.'),
118
Option('q', 'quit', 'quit')
122
Option('y', 'continue', 'proceed to shelve selected changes.',
124
Option('r', 'restart', 'restart the hunk selection loop.'),
125
Option('s', 'status', 'show status of hunks.'),
126
Option('i', 'invert', 'invert the current selection of all hunks.'),
127
Option('q', 'quit', 'quit')
130
def __init__(self, patches):
131
self.patches = patches
133
for patch in patches:
134
for hunk in patch.hunks:
135
# everything's shelved by default
137
self.total_hunks += 1
139
def __get_option(self, char):
140
for opt in self.standard_options:
143
raise Exception('Option "%s" not found!' % char)
145
def __select_loop(self):
147
for patch in self.patches:
150
while i < len(patch.hunks):
151
hunk = patch.hunks[i]
153
print patch.get_header(), hunk
157
prompt = 'Keep this change? (%d of %d)' \
158
% (j, self.total_hunks)
161
self.__get_option('n').default = True
162
self.__get_option('y').default = False
164
self.__get_option('n').default = False
165
self.__get_option('y').default = True
167
action = self.__ask_user(prompt, self.standard_options)
170
hunk.selected = False
171
elif action == 'shelve':
173
elif action == 'done':
175
elif action == 'invert':
176
self.__invert_selection()
179
elif action == 'status':
182
elif action == 'quit':
189
if self.total_hunks == 0:
194
if not self.__select_loop():
199
prompt = "Shelve these changes, or restart?"
200
action = self.__ask_user(prompt, self.end_options)
202
if action == 'continue':
205
elif action == 'quit':
207
elif action == 'status':
209
elif action == 'invert':
210
self.__invert_selection()
211
elif action == 'restart':
215
for patch in self.patches:
217
for hunk in patch.hunks:
223
for patch in self.patches:
230
def __invert_selection(self):
231
for patch in self.patches:
232
for hunk in patch.hunks:
233
if hunk.__dict__.has_key('selected'):
234
hunk.selected = not hunk.selected
238
def __show_status(self):
240
for patch in self.patches:
241
print ' %s' % patch.oldname
244
for hunk in patch.hunks:
250
print ' %d hunks to be shelved' % shelve
251
print ' %d hunks to be kept' % keep
255
fd = sys.stdin.fileno()
256
settings = termios.tcgetattr(fd)
259
ch = sys.stdin.read(1)
261
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
264
def __ask_user(self, prompt, options):
266
sys.stdout.write(prompt)
267
sys.stdout.write(' [')
271
sys.stdout.write(opt.char)
272
sys.stdout.write('?] (%s): ' % default.char)
274
response = self.__getchar()
276
# default, which we see as newline, is 'n'
277
if response in ['\n', '\r', '\r\n']:
278
response = default.char
280
print response # because echo is off
283
if opt.char == response:
287
print ' %s - %s' % (opt.char, opt.help)