~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 07:12:26 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129071226-a04b3f827880025d
Unshelve --pick was broken, because we deleted the whole patch, even when only
part of it was unshelved. So now if we unshelve part of a patch, the patch is
replaced with a new patch that has just the unshelved parts. That's a long way
of saying it does what you'd expect.

Implementing this required changing HunkSelector to return both the selected,
and unselected hunks (ie. patches to shelve, and patches to keep).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import sys
 
1
#!/usr/bin/python
2
2
 
3
3
from userinteractor import UserInteractor, UserOption
4
 
from errors import NoColor
5
4
import copy
6
5
 
7
6
class HunkSelector:
8
7
    strings = {}
9
8
 
10
 
    def __init__(self, patches, color=None):
11
 
        if color is True or color is None:
12
 
            try:
13
 
                from colordiff import DiffWriter
14
 
                from terminal import has_ansi_colors
15
 
                if has_ansi_colors():
16
 
                    self.diff_stream = DiffWriter(sys.stdout,
17
 
                                                  check_style=False)
18
 
                else:
19
 
                    if color is True:
20
 
                        raise NoColor()
21
 
                    self.diff_stream = sys.stdout
22
 
            except ImportError:
23
 
                if color is True:
24
 
                    raise NoBzrtoolsColor()
25
 
                self.diff_stream = sys.stdout
26
 
        else:
27
 
            self.diff_stream = sys.stdout
28
 
 
 
9
    def __init__(self, patches):
29
10
        self.standard_options = [
30
11
            UserOption('y', self._selected, self.strings['select_desc'],
31
12
                default=True),
84
65
    # Called once for each hunk
85
66
    def _hunk_callback(self, hunk, count):
86
67
        if self.last_printed != count:
87
 
            self.diff_stream.write(str(hunk.patch.get_header()))
88
 
            self.diff_stream.write(str(hunk))
 
68
            print hunk.patch.get_header(), hunk
89
69
            self.last_printed = count
90
70
 
91
71
        if hunk.selected:
173
153
        return (selected_patches, unselected_patches)
174
154
 
175
155
class ShelveHunkSelector(HunkSelector):
176
 
    def __init__(self, patches, color=None):
 
156
    def __init__(self, patches):
177
157
        self.strings = {}
178
158
        self.strings['status_selected'] = '%d hunks to be shelved'
179
159
        self.strings['status_unselected'] = '%d hunks to be kept'
182
162
        self.strings['finish_desc'] = 'shelve selected changes.'
183
163
        self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
184
164
        self.strings['end_prompt'] = 'Shelve these changes?'
185
 
        HunkSelector.__init__(self, patches, color)
 
165
        HunkSelector.__init__(self, patches)
186
166
 
187
167
class UnshelveHunkSelector(HunkSelector):
188
 
    def __init__(self, patches, color=None):
 
168
    def __init__(self, patches):
189
169
        self.strings = {}
190
170
        self.strings['status_selected'] = '%d hunks to be unshelved'
191
171
        self.strings['status_unselected'] = '%d hunks left on shelf'
195
175
        self.strings['prompt'] = 'Unshelve this change? ' \
196
176
            '(%(count)d of %(total)d)'
197
177
        self.strings['end_prompt'] = 'Unshelve these changes?'
198
 
        HunkSelector.__init__(self, patches, color)
 
178
        HunkSelector.__init__(self, patches)