~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Aaron Bentley
  • Date: 2009-07-14 20:46:59 UTC
  • mfrom: (4536 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4538.
  • Revision ID: aaron@aaronbentley.com-20090714204659-86za047pt2ckstw3
Merged bzr.dev into commit-transform.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
class ShelfReporter(object):
39
39
 
 
40
    vocab = {'add file': 'Shelve adding file "%(path)s"?',
 
41
             'binary': 'Shelve binary changes?',
 
42
             'change kind': 'Shelve changing "%s" from %(other)s'
 
43
             ' to %(this)s?',
 
44
             'delete file': 'Shelve removing file "%(path)s"?',
 
45
             'final': 'Shelve %d change(s)?',
 
46
             'hunk': 'Shelve?',
 
47
             'modify target': 'Shelve changing target of'
 
48
             ' "%(path)s" from "%(other)s" to "%(this)s"?',
 
49
             'rename': 'Shelve renaming "%(other)s" =>'
 
50
                        ' "%(this)s"?'
 
51
             }
 
52
 
 
53
    invert_diff = False
 
54
 
40
55
    def __init__(self):
41
56
        self.delta_reporter = delta._ChangeReporter()
42
57
 
43
58
    def no_changes(self):
 
59
        """Report that no changes were selected to apply."""
44
60
        trace.warning('No changes to shelve.')
45
61
 
46
62
    def shelved_id(self, shelf_id):
 
63
        """Report the id changes were shelved to."""
47
64
        trace.note('Changes shelved with id "%d".' % shelf_id)
48
65
 
 
66
    def changes_destroyed(self):
 
67
        """Report that changes were made without shelving."""
 
68
        trace.note('Selected changes destroyed.')
 
69
 
49
70
    def selected_changes(self, transform):
 
71
        """Report the changes that were selected."""
50
72
        trace.note("Selected changes:")
51
73
        changes = transform.iter_changes()
52
74
        delta.report_changes(changes, self.delta_reporter)
53
75
 
 
76
    def prompt_change(self, change):
 
77
        """Determine the prompt for a change to apply."""
 
78
        if change[0] == 'rename':
 
79
            vals = {'this': change[3], 'other': change[2]}
 
80
        elif change[0] == 'change kind':
 
81
            vals = {'path': change[4], 'other': change[2], 'this': change[3]}
 
82
        elif change[0] == 'modify target':
 
83
            vals = {'path': change[2], 'other': change[3], 'this': change[4]}
 
84
        else:
 
85
            vals = {'path': change[3]}
 
86
        prompt = self.vocab[change[0]] % vals
 
87
        return prompt
 
88
 
 
89
 
 
90
class ApplyReporter(ShelfReporter):
 
91
 
 
92
    vocab = {'add file': 'Delete file "%(path)s"?',
 
93
             'binary': 'Apply binary changes?',
 
94
             'change kind': 'Change "%(path)s" from %(this)s'
 
95
             ' to %(other)s?',
 
96
             'delete file': 'Add file "%(path)s"?',
 
97
             'final': 'Apply %d change(s)?',
 
98
             'hunk': 'Apply change?',
 
99
             'modify target': 'Change target of'
 
100
             ' "%(path)s" from "%(this)s" to "%(other)s"?',
 
101
             'rename': 'Rename "%(this)s" => "%(other)s"?',
 
102
             }
 
103
 
 
104
    invert_diff = True
 
105
 
 
106
    def changes_destroyed(self):
 
107
        pass
 
108
 
54
109
 
55
110
class Shelver(object):
56
111
    """Interactively shelve the changes in a working tree."""
70
125
        :param destroy: Change the working tree without storing the shelved
71
126
            changes.
72
127
        :param manager: The shelf manager to use.
 
128
        :param reporter: Object for reporting changes to user.
73
129
        """
74
130
        self.work_tree = work_tree
75
131
        self.target_tree = target_tree
121
177
                        changes_shelved += self.handle_modify_text(creator,
122
178
                                                                   change[1])
123
179
                    except errors.BinaryFile:
124
 
                        if self.prompt_bool('Shelve binary changes?'):
 
180
                        if self.prompt_bool(self.reporter.vocab['binary']):
125
181
                            changes_shelved += 1
126
182
                            creator.shelve_content_change(change[1])
127
 
                if change[0] == 'add file':
128
 
                    if self.prompt_bool('Shelve adding file "%s"?'
129
 
                                        % change[3]):
130
 
                        creator.shelve_creation(change[1])
131
 
                        changes_shelved += 1
132
 
                if change[0] == 'delete file':
133
 
                    if self.prompt_bool('Shelve removing file "%s"?'
134
 
                                        % change[3]):
135
 
                        creator.shelve_deletion(change[1])
136
 
                        changes_shelved += 1
137
 
                if change[0] == 'change kind':
138
 
                    if self.prompt_bool('Shelve changing "%s" from %s to %s? '
139
 
                                        % (change[4], change[2], change[3])):
140
 
                        creator.shelve_content_change(change[1])
141
 
                        changes_shelved += 1
142
 
                if change[0] == 'rename':
143
 
                    if self.prompt_bool('Shelve renaming "%s" => "%s"?' %
144
 
                                   change[2:]):
145
 
                        creator.shelve_rename(change[1])
146
 
                        changes_shelved += 1
147
 
                if change[0] == 'modify target':
148
 
                    if self.prompt_bool('Shelve changing target of "%s" '
149
 
                            'from "%s" to "%s"?' % change[2:]):
150
 
                        creator.shelve_modify_target(change[1])
 
183
                else:
 
184
                    if self.prompt_bool(self.reporter.prompt_change(change)):
 
185
                        creator.shelve_change(change)
151
186
                        changes_shelved += 1
152
187
            if changes_shelved > 0:
153
188
                self.reporter.selected_changes(creator.work_transform)
154
189
                if (self.auto_apply or self.prompt_bool(
155
 
                    'Shelve %d change(s)?' % changes_shelved)):
 
190
                    self.reporter.vocab['final'] % changes_shelved)):
156
191
                    if self.destroy:
157
192
                        creator.transform()
158
 
                        trace.note('Selected changes destroyed.')
 
193
                        self.reporter.changes_destroyed()
159
194
                    else:
160
195
                        shelf_id = self.manager.shelve_changes(creator,
161
196
                                                               self.message)
166
201
            shutil.rmtree(self.tempdir)
167
202
            creator.finalize()
168
203
 
169
 
    def get_parsed_patch(self, file_id):
 
204
    def get_parsed_patch(self, file_id, invert=False):
170
205
        """Return a parsed version of a file's patch.
171
206
 
172
207
        :param file_id: The id of the file to generate a patch for.
 
208
        :param invert: If True, provide an inverted patch (insertions displayed
 
209
            as removals, removals displayed as insertions).
173
210
        :return: A patches.Patch.
174
211
        """
175
 
        old_path = self.target_tree.id2path(file_id)
176
 
        new_path = self.work_tree.id2path(file_id)
177
212
        diff_file = StringIO()
178
 
        text_differ = diff.DiffText(self.target_tree, self.work_tree,
179
 
                                    diff_file)
 
213
        if invert:
 
214
            old_tree = self.work_tree
 
215
            new_tree = self.target_tree
 
216
        else:
 
217
            old_tree = self.target_tree
 
218
            new_tree = self.work_tree
 
219
        old_path = old_tree.id2path(file_id)
 
220
        new_path = new_tree.id2path(file_id)
 
221
        text_differ = diff.DiffText(old_tree, new_tree, diff_file)
180
222
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
181
223
        diff_file.seek(0)
182
224
        return patches.parse_patch(diff_file)
223
265
    def handle_modify_text(self, creator, file_id):
224
266
        """Provide diff hunk selection for modified text.
225
267
 
 
268
        If self.reporter.invert_diff is True, the diff is inverted so that
 
269
        insertions are displayed as removals and vice versa.
 
270
 
226
271
        :param creator: a ShelfCreator
227
272
        :param file_id: The id of the file to shelve.
228
273
        :return: number of shelved hunks.
229
274
        """
230
 
        target_lines = self.target_tree.get_file_lines(file_id)
 
275
        if self.reporter.invert_diff:
 
276
            target_lines = self.work_tree.get_file_lines(file_id)
 
277
        else:
 
278
            target_lines = self.target_tree.get_file_lines(file_id)
231
279
        textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
232
280
        textfile.check_text_lines(target_lines)
233
 
        parsed = self.get_parsed_patch(file_id)
 
281
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
234
282
        final_hunks = []
235
283
        if not self.auto:
236
284
            offset = 0
237
285
            self.diff_writer.write(parsed.get_header())
238
286
            for hunk in parsed.hunks:
239
287
                self.diff_writer.write(str(hunk))
240
 
                if not self.prompt_bool('Shelve?'):
 
288
                selected = self.prompt_bool(self.reporter.vocab['hunk'])
 
289
                if not self.reporter.invert_diff:
 
290
                    selected = (not selected)
 
291
                if selected:
241
292
                    hunk.mod_pos += offset
242
293
                    final_hunks.append(hunk)
243
294
                else:
244
295
                    offset -= (hunk.mod_range - hunk.orig_range)
245
296
        sys.stdout.flush()
246
 
        if len(parsed.hunks) == len(final_hunks):
 
297
        if not self.reporter.invert_diff and (
 
298
            len(parsed.hunks) == len(final_hunks)):
 
299
            return 0
 
300
        if self.reporter.invert_diff and len(final_hunks) == 0:
247
301
            return 0
248
302
        patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
249
303
        creator.shelve_lines(file_id, list(patched))
 
304
        if self.reporter.invert_diff:
 
305
            return len(final_hunks)
250
306
        return len(parsed.hunks) - len(final_hunks)
251
307
 
252
308