~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Vincent Ladeuil
  • Date: 2009-05-05 15:31:34 UTC
  • mto: (4343.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4344.
  • Revision ID: v.ladeuil+lp@free.fr-20090505153134-q4bp4is9gywsmzrv
Clean up test for log formats.

* bzrlib/tests/blackbox/test_logformats.py:
Update tests to actual style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
)
36
36
 
37
37
 
38
 
class ShelfReporter(object):
39
 
 
40
 
    vocab = {'add file': 'Shelve adding file "%(path)s"?',
41
 
             'binary': 'Shelve binary changes?',
42
 
             'change kind': 'Shelve changing "%s" from %(other)s'
43
 
             ' to %(this)s?',
44
 
             'delete file': 'Shelve removing file "%(path)s"?',
45
 
             'final': 'Shelve %d change(s)?',
46
 
             'hunk': 'Shelve?',
47
 
             'modify target': 'Shelve changing target of'
48
 
             ' "%(path)s" from "%(other)s" to "%(this)s"?',
49
 
             'rename': 'Shelve renaming "%(other)s" =>'
50
 
                        ' "%(this)s"?'
51
 
             }
52
 
 
53
 
    invert_diff = False
54
 
 
55
 
    def __init__(self):
56
 
        self.delta_reporter = delta._ChangeReporter()
57
 
 
58
 
    def no_changes(self):
59
 
        """Report that no changes were selected to apply."""
60
 
        trace.warning('No changes to shelve.')
61
 
 
62
 
    def shelved_id(self, shelf_id):
63
 
        """Report the id changes were shelved to."""
64
 
        trace.note('Changes shelved with id "%d".' % shelf_id)
65
 
 
66
 
    def changes_destroyed(self):
67
 
        """Report that changes were made without shelving."""
68
 
        trace.note('Selected changes destroyed.')
69
 
 
70
 
    def selected_changes(self, transform):
71
 
        """Report the changes that were selected."""
72
 
        trace.note("Selected changes:")
73
 
        changes = transform.iter_changes()
74
 
        delta.report_changes(changes, self.delta_reporter)
75
 
 
76
 
    def prompt_change(self, change):
77
 
        """Determine the prompt for a change to apply."""
78
 
        if change[0] == 'rename':
79
 
            vals = {'this': change[3], 'other': change[2]}
80
 
        elif change[0] == 'change kind':
81
 
            vals = {'path': change[4], 'other': change[2], 'this': change[3]}
82
 
        elif change[0] == 'modify target':
83
 
            vals = {'path': change[2], 'other': change[3], 'this': change[4]}
84
 
        else:
85
 
            vals = {'path': change[3]}
86
 
        prompt = self.vocab[change[0]] % vals
87
 
        return prompt
88
 
 
89
 
 
90
 
class ApplyReporter(ShelfReporter):
91
 
 
92
 
    vocab = {'add file': 'Delete file "%(path)s"?',
93
 
             'binary': 'Apply binary changes?',
94
 
             'change kind': 'Change "%(path)s" from %(this)s'
95
 
             ' to %(other)s?',
96
 
             'delete file': 'Add file "%(path)s"?',
97
 
             'final': 'Apply %d change(s)?',
98
 
             'hunk': 'Apply change?',
99
 
             'modify target': 'Change target of'
100
 
             ' "%(path)s" from "%(this)s" to "%(other)s"?',
101
 
             'rename': 'Rename "%(this)s" => "%(other)s"?',
102
 
             }
103
 
 
104
 
    invert_diff = True
105
 
 
106
 
    def changes_destroyed(self):
107
 
        pass
108
 
 
109
 
 
110
38
class Shelver(object):
111
39
    """Interactively shelve the changes in a working tree."""
112
40
 
113
41
    def __init__(self, work_tree, target_tree, diff_writer=None, auto=False,
114
42
                 auto_apply=False, file_list=None, message=None,
115
 
                 destroy=False, manager=None, reporter=None):
 
43
                 destroy=False):
116
44
        """Constructor.
117
45
 
118
46
        :param work_tree: The working tree to shelve changes from.
124
52
        :param message: The message to associate with the shelved changes.
125
53
        :param destroy: Change the working tree without storing the shelved
126
54
            changes.
127
 
        :param manager: The shelf manager to use.
128
 
        :param reporter: Object for reporting changes to user.
129
55
        """
130
56
        self.work_tree = work_tree
131
57
        self.target_tree = target_tree
132
58
        self.diff_writer = diff_writer
133
59
        if self.diff_writer is None:
134
60
            self.diff_writer = sys.stdout
135
 
        if manager is None:
136
 
            manager = work_tree.get_shelf_manager()
137
 
        self.manager = manager
 
61
        self.manager = work_tree.get_shelf_manager()
138
62
        self.auto = auto
139
63
        self.auto_apply = auto_apply
140
64
        self.file_list = file_list
141
65
        self.message = message
142
66
        self.destroy = destroy
143
 
        if reporter is None:
144
 
            reporter = ShelfReporter()
145
 
        self.reporter = reporter
146
67
 
147
68
    @classmethod
148
69
    def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
177
98
                        changes_shelved += self.handle_modify_text(creator,
178
99
                                                                   change[1])
179
100
                    except errors.BinaryFile:
180
 
                        if self.prompt_bool(self.reporter.vocab['binary']):
 
101
                        if self.prompt_bool('Shelve binary changes?'):
181
102
                            changes_shelved += 1
182
103
                            creator.shelve_content_change(change[1])
183
 
                else:
184
 
                    if self.prompt_bool(self.reporter.prompt_change(change)):
185
 
                        creator.shelve_change(change)
 
104
                if change[0] == 'add file':
 
105
                    if self.prompt_bool('Shelve adding file "%s"?'
 
106
                                        % change[3]):
 
107
                        creator.shelve_creation(change[1])
 
108
                        changes_shelved += 1
 
109
                if change[0] == 'delete file':
 
110
                    if self.prompt_bool('Shelve removing file "%s"?'
 
111
                                        % change[3]):
 
112
                        creator.shelve_deletion(change[1])
 
113
                        changes_shelved += 1
 
114
                if change[0] == 'change kind':
 
115
                    if self.prompt_bool('Shelve changing "%s" from %s to %s? '
 
116
                                        % (change[4], change[2], change[3])):
 
117
                        creator.shelve_content_change(change[1])
 
118
                        changes_shelved += 1
 
119
                if change[0] == 'rename':
 
120
                    if self.prompt_bool('Shelve renaming "%s" => "%s"?' %
 
121
                                   change[2:]):
 
122
                        creator.shelve_rename(change[1])
 
123
                        changes_shelved += 1
 
124
                if change[0] == 'modify target':
 
125
                    if self.prompt_bool('Shelve changing target of "%s" '
 
126
                            'from "%s" to "%s"?' % change[2:]):
 
127
                        creator.shelve_modify_target(change[1])
186
128
                        changes_shelved += 1
187
129
            if changes_shelved > 0:
188
 
                self.reporter.selected_changes(creator.work_transform)
 
130
                trace.note("Selected changes:")
 
131
                changes = creator.work_transform.iter_changes()
 
132
                reporter = delta._ChangeReporter()
 
133
                delta.report_changes(changes, reporter)
189
134
                if (self.auto_apply or self.prompt_bool(
190
 
                    self.reporter.vocab['final'] % changes_shelved)):
 
135
                    'Shelve %d change(s)?' % changes_shelved)):
191
136
                    if self.destroy:
192
137
                        creator.transform()
193
 
                        self.reporter.changes_destroyed()
 
138
                        trace.note('Selected changes destroyed.')
194
139
                    else:
195
140
                        shelf_id = self.manager.shelve_changes(creator,
196
141
                                                               self.message)
197
 
                        self.reporter.shelved_id(shelf_id)
 
142
                        trace.note('Changes shelved with id "%d".' % shelf_id)
198
143
            else:
199
 
                self.reporter.no_changes()
 
144
                trace.warning('No changes to shelve.')
200
145
        finally:
201
146
            shutil.rmtree(self.tempdir)
202
147
            creator.finalize()
203
148
 
204
 
    def get_parsed_patch(self, file_id, invert=False):
 
149
    def get_parsed_patch(self, file_id):
205
150
        """Return a parsed version of a file's patch.
206
151
 
207
152
        :param file_id: The id of the file to generate a patch for.
208
 
        :param invert: If True, provide an inverted patch (insertions displayed
209
 
            as removals, removals displayed as insertions).
210
153
        :return: A patches.Patch.
211
154
        """
 
155
        old_path = self.target_tree.id2path(file_id)
 
156
        new_path = self.work_tree.id2path(file_id)
212
157
        diff_file = StringIO()
213
 
        if invert:
214
 
            old_tree = self.work_tree
215
 
            new_tree = self.target_tree
216
 
        else:
217
 
            old_tree = self.target_tree
218
 
            new_tree = self.work_tree
219
 
        old_path = old_tree.id2path(file_id)
220
 
        new_path = new_tree.id2path(file_id)
221
 
        text_differ = diff.DiffText(old_tree, new_tree, diff_file)
 
158
        text_differ = diff.DiffText(self.target_tree, self.work_tree,
 
159
                                    diff_file)
222
160
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
223
161
        diff_file.seek(0)
224
162
        return patches.parse_patch(diff_file)
265
203
    def handle_modify_text(self, creator, file_id):
266
204
        """Provide diff hunk selection for modified text.
267
205
 
268
 
        If self.reporter.invert_diff is True, the diff is inverted so that
269
 
        insertions are displayed as removals and vice versa.
270
 
 
271
206
        :param creator: a ShelfCreator
272
207
        :param file_id: The id of the file to shelve.
273
208
        :return: number of shelved hunks.
274
209
        """
275
 
        if self.reporter.invert_diff:
276
 
            target_lines = self.work_tree.get_file_lines(file_id)
277
 
        else:
278
 
            target_lines = self.target_tree.get_file_lines(file_id)
 
210
        target_lines = self.target_tree.get_file_lines(file_id)
279
211
        textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
280
212
        textfile.check_text_lines(target_lines)
281
 
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
 
213
        parsed = self.get_parsed_patch(file_id)
282
214
        final_hunks = []
283
215
        if not self.auto:
284
216
            offset = 0
285
217
            self.diff_writer.write(parsed.get_header())
286
218
            for hunk in parsed.hunks:
287
219
                self.diff_writer.write(str(hunk))
288
 
                selected = self.prompt_bool(self.reporter.vocab['hunk'])
289
 
                if not self.reporter.invert_diff:
290
 
                    selected = (not selected)
291
 
                if selected:
 
220
                if not self.prompt_bool('Shelve?'):
292
221
                    hunk.mod_pos += offset
293
222
                    final_hunks.append(hunk)
294
223
                else:
295
224
                    offset -= (hunk.mod_range - hunk.orig_range)
296
225
        sys.stdout.flush()
297
 
        if not self.reporter.invert_diff and (
298
 
            len(parsed.hunks) == len(final_hunks)):
299
 
            return 0
300
 
        if self.reporter.invert_diff and len(final_hunks) == 0:
 
226
        if len(parsed.hunks) == len(final_hunks):
301
227
            return 0
302
228
        patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
303
229
        creator.shelve_lines(file_id, list(patched))
304
 
        if self.reporter.invert_diff:
305
 
            return len(final_hunks)
306
230
        return len(parsed.hunks) - len(final_hunks)
307
231
 
308
232