/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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