3
from patches import parse_patches
8
from bzrlib.commands import Command
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)
32
root = run_bzr('root')[0].strip()
33
shelf = os.path.join(root, '.bzr-shelf')
35
if not os.path.exists(shelf):
36
raise Exception("No shelf found in '%s'" % shelf)
38
patch = open(shelf, 'r').read()
40
print >>sys.stderr, "Reapplying shelved patches"
41
pipe = os.popen('patch -d %s -s -p0' % root, 'w')
45
if pipe.close() is not None:
46
raise Exception("Failed running patch!")
49
print 'Diff status is now:'
50
os.system('bzr diff | diffstat')
54
class QuitException(Exception):
58
patches = parse_patches(run_bzr('diff'))
60
patches = HunkSelector(patches).select()
65
print >>sys.stderr, 'Nothing to shelve'
68
root = run_bzr('root')[0].strip()
69
shelf = os.path.join(root, '.bzr-shelf')
70
print >>sys.stderr, "Saving shelved patches to", shelf
71
shelf = open(shelf, 'a')
74
shelf.write(str(patch))
77
os.fsync(shelf.fileno())
80
print >>sys.stderr, "Reverting shelved patches"
81
pipe = os.popen('patch -d %s -sR -p0' % root, 'w')
83
pipe.write(str(patch))
86
if pipe.close() is not None:
87
raise Exception("Failed running patch!")
89
print 'Diff status is now:'
90
os.system('bzr diff | diffstat')
97
pipe = os.popen('bzr %s' % string.join(args, ' '), 'r')
98
lines = pipe.readlines()
99
if pipe.close() is not None:
100
raise Exception("Failed running bzr")
103
class cmd_shelve(Command):
104
"""Temporarily remove some changes from the current tree.
105
Use 'unshelve' to restore these changes.
111
class cmd_unshelve(Command):
112
"""Restore previously-shelved changes to the current tree.
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)