/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/shelf_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
import contextlib
18
 
import patiencediff
 
17
from __future__ import absolute_import
 
18
 
19
19
import shutil
20
20
import sys
21
21
import tempfile
22
22
 
23
 
from io import BytesIO
24
 
 
25
23
from . import (
26
24
    builtins,
27
25
    delta,
29
27
    errors,
30
28
    osutils,
31
29
    patches,
 
30
    patiencediff,
32
31
    shelf,
33
32
    textfile,
34
33
    trace,
36
35
    workingtree,
37
36
)
38
37
from .i18n import gettext
 
38
from .sixish import (
 
39
    BytesIO,
 
40
    )
39
41
 
40
42
 
41
43
class UseEditor(Exception):
47
49
    vocab = {'add file': gettext('Shelve adding file "%(path)s"?'),
48
50
             'binary': gettext('Shelve binary changes?'),
49
51
             'change kind': gettext('Shelve changing "%s" from %(other)s'
50
 
                                    ' to %(this)s?'),
 
52
             ' to %(this)s?'),
51
53
             'delete file': gettext('Shelve removing file "%(path)s"?'),
52
54
             'final': gettext('Shelve %d change(s)?'),
53
55
             'hunk': gettext('Shelve?'),
54
56
             'modify target': gettext('Shelve changing target of'
55
 
                                      ' "%(path)s" from "%(other)s" to "%(this)s"?'),
 
57
             ' "%(path)s" from "%(other)s" to "%(this)s"?'),
56
58
             'rename': gettext('Shelve renaming "%(other)s" =>'
57
 
                               ' "%(this)s"?')
 
59
                        ' "%(this)s"?')
58
60
             }
59
61
 
60
62
    invert_diff = False
99
101
    vocab = {'add file': gettext('Delete file "%(path)s"?'),
100
102
             'binary': gettext('Apply binary changes?'),
101
103
             'change kind': gettext('Change "%(path)s" from %(this)s'
102
 
                                    ' to %(other)s?'),
 
104
             ' to %(other)s?'),
103
105
             'delete file': gettext('Add file "%(path)s"?'),
104
106
             'final': gettext('Apply %d change(s)?'),
105
107
             'hunk': gettext('Apply change?'),
106
108
             'modify target': gettext('Change target of'
107
 
                                      ' "%(path)s" from "%(this)s" to "%(other)s"?'),
 
109
             ' "%(path)s" from "%(this)s" to "%(other)s"?'),
108
110
             'rename': gettext('Rename "%(this)s" => "%(other)s"?'),
109
111
             }
110
112
 
177
179
        tree, path = workingtree.WorkingTree.open_containing(directory)
178
180
        # Ensure that tree is locked for the lifetime of target_tree, as
179
181
        # target tree may be reading from the same dirstate.
180
 
        with tree.lock_tree_write():
 
182
        tree.lock_tree_write()
 
183
        try:
181
184
            target_tree = builtins._get_one_revision_tree('shelf2', revision,
182
 
                                                          tree.branch, tree)
 
185
                tree.branch, tree)
183
186
            files = tree.safe_relpath_files(file_list)
184
187
            return klass(tree, target_tree, diff_writer, all, all, files,
185
188
                         message, destroy)
 
189
        finally:
 
190
            tree.unlock()
186
191
 
187
192
    def run(self):
188
193
        """Interactively shelve the changes."""
207
212
            if changes_shelved > 0:
208
213
                self.reporter.selected_changes(creator.work_transform)
209
214
                if (self.auto_apply or self.prompt_bool(
210
 
                        self.reporter.vocab['final'] % changes_shelved)):
 
215
                    self.reporter.vocab['final'] % changes_shelved)):
211
216
                    if self.destroy:
212
217
                        creator.transform()
213
218
                        self.reporter.changes_destroyed()
226
231
            self.change_editor.finish()
227
232
        self.work_tree.unlock()
228
233
 
 
234
 
229
235
    def get_parsed_patch(self, file_id, invert=False):
230
236
        """Return a parsed version of a file's patch.
231
237
 
243
249
            new_tree = self.work_tree
244
250
        old_path = old_tree.id2path(file_id)
245
251
        new_path = new_tree.id2path(file_id)
246
 
        path_encoding = osutils.get_terminal_encoding()
247
252
        text_differ = diff.DiffText(old_tree, new_tree, diff_file,
248
 
                                    path_encoding=path_encoding)
249
 
        patch = text_differ.diff(old_path, new_path, 'file', 'file')
 
253
            path_encoding=osutils.get_terminal_encoding())
 
254
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
250
255
        diff_file.seek(0)
251
256
        return patches.parse_patch(diff_file)
252
257
 
295
300
        :param file_id: The id of the file that was modified.
296
301
        :return: The number of changes.
297
302
        """
298
 
        path = self.work_tree.id2path(file_id)
299
 
        work_tree_lines = self.work_tree.get_file_lines(path, file_id)
 
303
        work_tree_lines = self.work_tree.get_file_lines(file_id)
300
304
        try:
301
305
            lines, change_count = self._select_hunks(creator, file_id,
302
306
                                                     work_tree_lines)
320
324
        if self.reporter.invert_diff:
321
325
            target_lines = work_tree_lines
322
326
        else:
323
 
            path = self.target_tree.id2path(file_id)
324
 
            target_lines = self.target_tree.get_file_lines(path)
 
327
            target_lines = self.target_tree.get_file_lines(file_id)
325
328
        textfile.check_text_lines(work_tree_lines)
326
329
        textfile.check_text_lines(target_lines)
327
330
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
330
333
            offset = 0
331
334
            self.diff_writer.write(parsed.get_header())
332
335
            for hunk in parsed.hunks:
333
 
                self.diff_writer.write(hunk.as_bytes())
 
336
                self.diff_writer.write(str(hunk))
334
337
                selected = self.prompt_bool(self.reporter.vocab['hunk'],
335
338
                                            allow_editor=(self.change_editor
336
339
                                                          is not None))
359
362
            content of the file, and change_region_count is the number of
360
363
            changed regions.
361
364
        """
362
 
        lines = osutils.split_lines(self.change_editor.edit_file(
363
 
            self.change_editor.old_tree.id2path(file_id),
364
 
            self.change_editor.new_tree.id2path(file_id)))
 
365
        lines = osutils.split_lines(self.change_editor.edit_file(file_id))
365
366
        return lines, self._count_changed_regions(work_tree_lines, lines)
366
367
 
367
368
    @staticmethod
397
398
                try:
398
399
                    shelf_id = int(shelf_id)
399
400
                except ValueError:
400
 
                    raise shelf.InvalidShelfId(shelf_id)
 
401
                    raise errors.InvalidShelfId(shelf_id)
401
402
            else:
402
403
                shelf_id = manager.last_shelf()
403
404
                if shelf_id is None:
404
 
                    raise errors.CommandError(
405
 
                        gettext('No changes are shelved.'))
 
405
                    raise errors.BzrCommandError(gettext('No changes are shelved.'))
406
406
            apply_changes = True
407
407
            delete_shelf = True
408
408
            read_shelf = True
456
456
 
457
457
    def run(self):
458
458
        """Perform the unshelving operation."""
459
 
        with contextlib.ExitStack() as exit_stack:
460
 
            exit_stack.enter_context(self.tree.lock_tree_write())
 
459
        self.tree.lock_tree_write()
 
460
        cleanups = [self.tree.unlock]
 
461
        try:
461
462
            if self.read_shelf:
462
 
                trace.note(gettext('Using changes with id "%d".') %
463
 
                           self.shelf_id)
 
463
                trace.note(gettext('Using changes with id "%d".') % self.shelf_id)
464
464
                unshelver = self.manager.get_unshelver(self.shelf_id)
465
 
                exit_stack.callback(unshelver.finalize)
 
465
                cleanups.append(unshelver.finalize)
466
466
                if unshelver.message is not None:
467
467
                    trace.note(gettext('Message: %s') % unshelver.message)
468
468
                change_reporter = delta._ChangeReporter()
469
 
                merger = unshelver.make_merger()
 
469
                merger = unshelver.make_merger(None)
470
470
                merger.change_reporter = change_reporter
471
471
                if self.apply_changes:
472
472
                    merger.do_merge()
476
476
                    self.show_changes(merger)
477
477
            if self.delete_shelf:
478
478
                self.manager.delete_shelf(self.shelf_id)
479
 
                trace.note(gettext('Deleted changes with id "%d".') %
480
 
                           self.shelf_id)
 
479
                trace.note(gettext('Deleted changes with id "%d".') % self.shelf_id)
 
480
        finally:
 
481
            for cleanup in reversed(cleanups):
 
482
                cleanup()
481
483
 
482
484
    def write_diff(self, merger):
483
485
        """Write this operation's diff to self.write_diff_to."""
485
487
        tt = tree_merger.make_preview_transform()
486
488
        new_tree = tt.get_preview_tree()
487
489
        if self.write_diff_to is None:
488
 
            self.write_diff_to = ui.ui_factory.make_output_stream(
489
 
                encoding_type='exact')
 
490
            self.write_diff_to = ui.ui_factory.make_output_stream(encoding_type='exact')
490
491
        path_encoding = osutils.get_diff_header_encoding()
491
492
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to,
492
 
                             path_encoding=path_encoding)
 
493
            path_encoding=path_encoding)
493
494
        tt.finalize()
494
495
 
495
496
    def show_changes(self, merger):