~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 15:51:25 UTC
  • mto: (4603.1.22 shelve-editor)
  • mto: This revision was merged to the branch mainline in revision 4795.
  • Revision ID: aaron@aaronbentley.com-20090814155125-wt1ox94v6b8gml52
Simplify editing a file with a differ.

Show diffs side-by-side

added added

removed removed

Lines of Context:
175
175
        self.tempdir = tempfile.mkdtemp()
176
176
        changes_shelved = 0
177
177
        try:
178
 
            for change in creator.iter_shelvable():
179
 
                if change[0] == 'modify text':
180
 
                    try:
181
 
                        changes_shelved += self.handle_modify_text(creator,
182
 
                                                                   change[1])
183
 
                    except errors.BinaryFile:
184
 
                        if self.prompt_bool(self.reporter.vocab['binary']):
 
178
            old_tree = self.target_tree
 
179
            new_tree = self.work_tree
 
180
            differ = diff.DiffFromTool.from_string('gvimdiff -f -o',
 
181
                                                   old_tree, new_tree,
 
182
                                                   sys.stdout)
 
183
            differ.force_temp = True
 
184
            differ.allow_write = True
 
185
            try:
 
186
                for change in creator.iter_shelvable():
 
187
                    if change[0] == 'modify text':
 
188
                        try:
 
189
                            changes_shelved += self.handle_modify_text(
 
190
                                creator, change[1], differ)
 
191
                        except errors.BinaryFile:
 
192
                            if self.prompt_bool(self.reporter.vocab['binary']):
 
193
                                changes_shelved += 1
 
194
                                creator.shelve_content_change(change[1])
 
195
                    else:
 
196
                        if self.prompt_bool(self.reporter.prompt_change(change)):
 
197
                            creator.shelve_change(change)
185
198
                            changes_shelved += 1
186
 
                            creator.shelve_content_change(change[1])
 
199
                if changes_shelved > 0:
 
200
                    self.reporter.selected_changes(creator.work_transform)
 
201
                    if (self.auto_apply or self.prompt_bool(
 
202
                        self.reporter.vocab['final'] % changes_shelved)):
 
203
                        if self.destroy:
 
204
                            creator.transform()
 
205
                            self.reporter.changes_destroyed()
 
206
                        else:
 
207
                            shelf_id = self.manager.shelve_changes(creator,
 
208
                                                                   self.message)
 
209
                            self.reporter.shelved_id(shelf_id)
187
210
                else:
188
 
                    if self.prompt_bool(self.reporter.prompt_change(change)):
189
 
                        creator.shelve_change(change)
190
 
                        changes_shelved += 1
191
 
            if changes_shelved > 0:
192
 
                self.reporter.selected_changes(creator.work_transform)
193
 
                if (self.auto_apply or self.prompt_bool(
194
 
                    self.reporter.vocab['final'] % changes_shelved)):
195
 
                    if self.destroy:
196
 
                        creator.transform()
197
 
                        self.reporter.changes_destroyed()
198
 
                    else:
199
 
                        shelf_id = self.manager.shelve_changes(creator,
200
 
                                                               self.message)
201
 
                        self.reporter.shelved_id(shelf_id)
202
 
            else:
203
 
                self.reporter.no_changes()
 
211
                    self.reporter.no_changes()
 
212
            finally:
 
213
                differ.finish()
204
214
        finally:
205
215
            shutil.rmtree(self.tempdir)
206
216
            creator.finalize()
273
283
        else:
274
284
            return False
275
285
 
276
 
    def handle_modify_text(self, creator, file_id):
 
286
    def handle_modify_text(self, creator, file_id, differ):
277
287
        try:
278
288
            lines, change_count = self._select_hunks(creator, file_id)
279
289
        except UseEditor:
280
 
            lines, change_count = self._edit_file(creator, file_id)
 
290
            lines, change_count = self._edit_file(creator, file_id, differ)
281
291
        if change_count != 0:
282
292
            creator.shelve_lines(file_id, lines)
283
293
        return change_count
325
335
        lines = list(patched)
326
336
        return lines, change_count
327
337
 
328
 
    def _edit_file(self, creator, file_id):
329
 
        old_tree = self.target_tree
330
 
        new_tree = self.work_tree
331
 
        differ = diff.DiffFromTool.from_string('gvimdiff -f -o',
332
 
                                               old_tree, new_tree,
333
 
                                               sys.stdout)
334
 
        try:
335
 
            differ.force_temp = True
336
 
            differ.allow_write = True
337
 
            old_path = old_tree.id2path(file_id)
338
 
            new_path = new_tree.id2path(file_id)
339
 
            differ.diff(file_id, old_path, new_path, 'file', 'file')
340
 
            new_abs_path = differ.get_new_path(new_path, abspath=True)
341
 
            new_lines_file = open(new_abs_path, 'r')
342
 
            try:
343
 
                lines = osutils.split_lines(new_lines_file.read())
344
 
                return lines, len(lines)
345
 
            finally:
346
 
                new_lines_file.close()
347
 
        finally:
348
 
            differ.finish()
349
 
 
 
338
    def _edit_file(self, creator, file_id, differ):
 
339
        old_path = differ.old_tree.id2path(file_id)
 
340
        new_path = differ.new_tree.id2path(file_id)
 
341
        differ.diff(file_id, old_path, new_path, 'file', 'file')
 
342
        lines = osutils.split_lines(differ.read_new_file(new_path))
 
343
        return lines, len(lines)
350
344
 
351
345
 
352
346
class Unshelver(object):