/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 brzlib/shelf_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

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