~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Aaron Bentley
  • Date: 2009-08-14 18:17:15 UTC
  • mto: (4603.1.22 shelve-editor)
  • mto: This revision was merged to the branch mainline in revision 4795.
  • Revision ID: aaron@aaronbentley.com-20090814181715-59qnhbov2stgzqt2
Allow configuring change editor.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from bzrlib import (
24
24
    builtins,
 
25
    commands,
25
26
    delta,
26
27
    diff,
27
28
    errors,
117
118
 
118
119
    def __init__(self, work_tree, target_tree, diff_writer=None, auto=False,
119
120
                 auto_apply=False, file_list=None, message=None,
120
 
                 destroy=False, manager=None, reporter=None):
 
121
                 destroy=False, manager=None, reporter=None,
 
122
                 change_editor=None):
121
123
        """Constructor.
122
124
 
123
125
        :param work_tree: The working tree to shelve changes from.
131
133
            changes.
132
134
        :param manager: The shelf manager to use.
133
135
        :param reporter: Object for reporting changes to user.
 
136
        :param change_editor: Editor for changed files.
134
137
        """
135
138
        self.work_tree = work_tree
136
139
        self.target_tree = target_tree
148
151
        if reporter is None:
149
152
            reporter = ShelfReporter()
150
153
        self.reporter = reporter
 
154
        self.change_editor = change_editor
151
155
 
152
156
    @classmethod
153
157
    def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
169
173
        return klass(tree, target_tree, diff_writer, all, all, files, message,
170
174
                     destroy)
171
175
 
 
176
    def set_change_editor(self):
 
177
        config =  self.work_tree.branch.get_config()
 
178
        commandline = config.get_user_option('change_editor')
 
179
        if commandline is None:
 
180
            return
 
181
        command = commands.shlex_split_unicode(commandline)
 
182
        if '%' not in commandline:
 
183
            command.extend(['%(old_path)s', '%(new_path)s'])
 
184
        self.change_editor = diff.DiffFromTool(command, self.target_tree,
 
185
                                               self.work_tree, sys.stdout)
 
186
 
172
187
    def run(self):
173
188
        """Interactively shelve the changes."""
174
189
        creator = shelf.ShelfCreator(self.work_tree, self.target_tree,
176
191
        self.tempdir = tempfile.mkdtemp()
177
192
        changes_shelved = 0
178
193
        try:
179
 
            old_tree = self.target_tree
180
 
            new_tree = self.work_tree
181
 
            command = ['gvimdiff', '-fo', '%(new_path)s', '%(old_path)s']
182
 
            differ = diff.DiffFromTool(command, old_tree, new_tree,
183
 
                                       sys.stdout)
184
 
            try:
185
 
                for change in creator.iter_shelvable():
186
 
                    if change[0] == 'modify text':
187
 
                        try:
188
 
                            changes_shelved += self.handle_modify_text(
189
 
                                creator, change[1], differ)
190
 
                        except errors.BinaryFile:
191
 
                            if self.prompt_bool(self.reporter.vocab['binary']):
192
 
                                changes_shelved += 1
193
 
                                creator.shelve_content_change(change[1])
194
 
                    else:
195
 
                        if self.prompt_bool(self.reporter.prompt_change(change)):
196
 
                            creator.shelve_change(change)
 
194
            for change in creator.iter_shelvable():
 
195
                if change[0] == 'modify text':
 
196
                    try:
 
197
                        changes_shelved += self.handle_modify_text(
 
198
                            creator, change[1])
 
199
                    except errors.BinaryFile:
 
200
                        if self.prompt_bool(self.reporter.vocab['binary']):
197
201
                            changes_shelved += 1
198
 
                if changes_shelved > 0:
199
 
                    self.reporter.selected_changes(creator.work_transform)
200
 
                    if (self.auto_apply or self.prompt_bool(
201
 
                        self.reporter.vocab['final'] % changes_shelved)):
202
 
                        if self.destroy:
203
 
                            creator.transform()
204
 
                            self.reporter.changes_destroyed()
205
 
                        else:
206
 
                            shelf_id = self.manager.shelve_changes(creator,
207
 
                                                                   self.message)
208
 
                            self.reporter.shelved_id(shelf_id)
 
202
                            creator.shelve_content_change(change[1])
209
203
                else:
210
 
                    self.reporter.no_changes()
211
 
            finally:
212
 
                differ.finish()
 
204
                    if self.prompt_bool(self.reporter.prompt_change(change)):
 
205
                        creator.shelve_change(change)
 
206
                        changes_shelved += 1
 
207
            if changes_shelved > 0:
 
208
                self.reporter.selected_changes(creator.work_transform)
 
209
                if (self.auto_apply or self.prompt_bool(
 
210
                    self.reporter.vocab['final'] % changes_shelved)):
 
211
                    if self.destroy:
 
212
                        creator.transform()
 
213
                        self.reporter.changes_destroyed()
 
214
                    else:
 
215
                        shelf_id = self.manager.shelve_changes(creator,
 
216
                                                               self.message)
 
217
                        self.reporter.shelved_id(shelf_id)
 
218
            else:
 
219
                self.reporter.no_changes()
213
220
        finally:
214
221
            shutil.rmtree(self.tempdir)
215
222
            creator.finalize()
282
289
        else:
283
290
            return False
284
291
 
285
 
    def handle_modify_text(self, creator, file_id, differ):
 
292
    def handle_modify_text(self, creator, file_id):
286
293
        try:
287
294
            lines, change_count = self._select_hunks(creator, file_id)
288
295
        except UseEditor:
289
 
            lines, change_count = self._edit_file(creator, file_id, differ)
 
296
            lines, change_count = self._edit_file(creator, file_id)
290
297
        if change_count != 0:
291
298
            creator.shelve_lines(file_id, lines)
292
299
        return change_count
316
323
            for hunk in parsed.hunks:
317
324
                self.diff_writer.write(str(hunk))
318
325
                selected = self.prompt_bool(self.reporter.vocab['hunk'],
319
 
                                            allow_editor=True)
 
326
                                            allow_editor=(self.change_editor
 
327
                                                          is not None))
320
328
                if not self.reporter.invert_diff:
321
329
                    selected = (not selected)
322
330
                if selected:
334
342
        lines = list(patched)
335
343
        return lines, change_count
336
344
 
337
 
    def _edit_file(self, creator, file_id, differ):
 
345
    def _edit_file(self, creator, file_id):
338
346
        work_tree_lines = creator.work_tree.get_file_lines(file_id)
339
 
        lines = osutils.split_lines(differ.edit_file(file_id))
 
347
        lines = osutils.split_lines(self.change_editor.edit_file(file_id))
340
348
        return lines, self._count_changed_regions(work_tree_lines, lines)
341
349
 
342
350
    @staticmethod