1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#!/usr/bin/python
from userinteractor import UserInteractor, UserOption
import copy
class HunkSelector:
strings = {}
def __init__(self, patches):
self.standard_options = [
UserOption('y', self._selected, self.strings['select_desc'],
default=True),
UserOption('n', self._unselected, self.strings['unselect_desc']),
UserOption('d', UserInteractor.FINISH, 'done, skip to the end.'),
UserOption('i', self._invert,
'invert the current selection status of all hunks.'),
UserOption('s', self._status,
'show selection status of all hunks.'),
UserOption('q', UserInteractor.QUIT, 'quit')
]
self.end_options = [
UserOption('y', UserInteractor.FINISH, self.strings['finish_desc'],
default=True),
UserOption('r', UserInteractor.RESTART,
'restart the hunk selection loop.'),
UserOption('s', self._status,
'show selection status of all hunks.'),
UserOption('i', self._invert,
'invert the current selection status of all hunks.'),
UserOption('q', UserInteractor.QUIT, 'quit')
]
self.patches = patches
self.total_hunks = 0
self.interactor = UserInteractor()
self.interactor.set_item_callback(self._hunk_callback)
self.interactor.set_start_callback(self._start_callback)
self.interactor.set_end_callback(self._end_callback)
for patch in patches:
for hunk in patch.hunks:
# everything's selected by default
hunk.selected = True
self.total_hunks += 1
# we need a back pointer in the callbacks
hunk.patch = patch
self.interactor.add_item(hunk)
# Called at the start of the main loop
def _start_callback(self):
self.last_printed = -1
self.interactor.set_prompt(self.strings['prompt'])
self.interactor.set_options(self.standard_options)
# Called at the end of the item loop, return False to indicate that the
# interaction isn't finished and the confirmation prompt should be displayed
def _end_callback(self):
self._status()
self.interactor.set_prompt(self.strings['end_prompt'])
self.interactor.set_options(self.end_options)
return False
# Called once for each hunk
def _hunk_callback(self, hunk, count):
if self.last_printed != count:
print hunk.patch.get_header(), hunk
self.last_printed = count
if hunk.selected:
self.interactor.get_option('y').default = True
self.interactor.get_option('n').default = False
else:
self.interactor.get_option('y').default = False
self.interactor.get_option('n').default = True
# The user chooses to (un)shelve a hunk
def _selected(self, hunk):
hunk.selected = True
return True
# The user chooses to keep a hunk
def _unselected(self, hunk):
hunk.selected = False
return True
# The user chooses to invert the selection
def _invert(self, hunk):
for patch in self.patches:
for hunk in patch.hunks:
if hunk.__dict__.has_key('selected'):
hunk.selected = not hunk.selected
else:
hunk.selected = True
self._status()
return False
# The user wants to see the status
def _status(self, hunk=None):
print '\nStatus:'
for patch in self.patches:
print ' %s' % patch.oldname
selected = 0
unselected = 0
for hunk in patch.hunks:
if hunk.selected:
selected += 1
else:
unselected += 1
print ' ', self.strings['status_selected'] % selected
print ' ', self.strings['status_unselected'] % unselected
print
# Tell the interactor we're not done with this item
return False
def select(self):
if self.total_hunks == 0 or not self.interactor.interact():
# False from interact means they chose to quit
return ([], [])
# Go through each patch and collect all selected/unselected hunks
for patch in self.patches:
patch.selected = []
patch.unselected = []
for hunk in patch.hunks:
if hunk.selected:
patch.selected.append(hunk)
else:
patch.unselected.append(hunk)
# Now build two lists, one of selected patches the other unselected
selected_patches = []
unselected_patches = []
for patch in self.patches:
if len(patch.selected):
tmp = copy.copy(patch)
tmp.hunks = tmp.selected
del tmp.selected
del tmp.unselected
selected_patches.append(tmp)
if len(patch.unselected):
tmp = copy.copy(patch)
tmp.hunks = tmp.unselected
del tmp.selected
del tmp.unselected
unselected_patches.append(tmp)
return (selected_patches, unselected_patches)
class ShelveHunkSelector(HunkSelector):
def __init__(self, patches):
self.strings = {}
self.strings['status_selected'] = '%d hunks to be shelved'
self.strings['status_unselected'] = '%d hunks to be kept'
self.strings['select_desc'] = 'shelve this change.'
self.strings['unselect_desc'] = 'keep this change in your tree.'
self.strings['finish_desc'] = 'shelve selected changes.'
self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
self.strings['end_prompt'] = 'Shelve these changes?'
HunkSelector.__init__(self, patches)
class UnshelveHunkSelector(HunkSelector):
def __init__(self, patches):
self.strings = {}
self.strings['status_selected'] = '%d hunks to be unshelved'
self.strings['status_unselected'] = '%d hunks left on shelf'
self.strings['select_desc'] = 'unshelve this change.'
self.strings['unselect_desc'] = 'leave this change on the shelf.'
self.strings['finish_desc'] = 'unshelve selected changes.'
self.strings['prompt'] = 'Unshelve this change? ' \
'(%(count)d of %(total)d)'
self.strings['end_prompt'] = 'Unshelve these changes?'
HunkSelector.__init__(self, patches)
|