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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-12-21 06:03:07 UTC
  • mfrom: (4665.7.3 serve-init)
  • Revision ID: pqm@pqm.ubuntu.com-20091221060307-uvja3vdy1o6dzzy0
(mbp) example debian init script

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from bzrlib import (
24
24
    builtins,
 
25
    commands,
25
26
    delta,
26
27
    diff,
27
28
    errors,
28
29
    osutils,
29
30
    patches,
 
31
    patiencediff,
30
32
    shelf,
31
33
    textfile,
32
34
    trace,
35
37
)
36
38
 
37
39
 
 
40
class UseEditor(Exception):
 
41
    """Use an editor instead of selecting hunks."""
 
42
 
 
43
 
38
44
class ShelfReporter(object):
39
45
 
40
46
    vocab = {'add file': 'Shelve adding file "%(path)s"?',
143
149
        if reporter is None:
144
150
            reporter = ShelfReporter()
145
151
        self.reporter = reporter
 
152
        config = self.work_tree.branch.get_config()
 
153
        self.change_editor = config.get_change_editor(target_tree, work_tree)
 
154
        self.work_tree.lock_tree_write()
146
155
 
147
156
    @classmethod
148
157
    def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
168
177
            target_tree = builtins._get_one_revision_tree('shelf2', revision,
169
178
                tree.branch, tree)
170
179
            files = builtins.safe_relpath_files(tree, file_list)
171
 
        except:
 
180
            return klass(tree, target_tree, diff_writer, all, all, files,
 
181
                         message, destroy)
 
182
        finally:
172
183
            tree.unlock()
173
 
            raise
174
 
        return klass(tree, target_tree, diff_writer, all, all, files, message,
175
 
                     destroy)
176
184
 
177
185
    def run(self):
178
186
        """Interactively shelve the changes."""
211
219
            shutil.rmtree(self.tempdir)
212
220
            creator.finalize()
213
221
 
 
222
    def finalize(self):
 
223
        if self.change_editor is not None:
 
224
            self.change_editor.finish()
 
225
        self.work_tree.unlock()
 
226
 
 
227
 
214
228
    def get_parsed_patch(self, file_id, invert=False):
215
229
        """Return a parsed version of a file's patch.
216
230
 
239
253
        :param message: The message to prompt a user with.
240
254
        :return: A character.
241
255
        """
 
256
        if not sys.stdin.isatty():
 
257
            # Since there is no controlling terminal we will hang when trying
 
258
            # to prompt the user, better abort now.  See
 
259
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
 
260
            # for more context.
 
261
            raise errors.BzrError("You need a controlling terminal.")
242
262
        sys.stdout.write(message)
243
263
        char = osutils.getchar()
244
264
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
245
265
        sys.stdout.flush()
246
266
        return char
247
267
 
248
 
    def prompt_bool(self, question, long=False):
 
268
    def prompt_bool(self, question, long=False, allow_editor=False):
249
269
        """Prompt the user with a yes/no question.
250
270
 
251
271
        This may be overridden by self.auto.  It may also *set* self.auto.  It
255
275
        """
256
276
        if self.auto:
257
277
            return True
 
278
        editor_string = ''
258
279
        if long:
259
 
            prompt = ' [(y)es, (N)o, (f)inish, or (q)uit]'
 
280
            if allow_editor:
 
281
                editor_string = '(E)dit manually, '
 
282
            prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
260
283
        else:
261
 
            prompt = ' [yNfq?]'
 
284
            if allow_editor:
 
285
                editor_string = 'e'
 
286
            prompt = ' [yN%sfq?]' % editor_string
262
287
        char = self.prompt(question + prompt)
263
288
        if char == 'y':
264
289
            return True
 
290
        elif char == 'e' and allow_editor:
 
291
            raise UseEditor
265
292
        elif char == 'f':
266
293
            self.auto = True
267
294
            return True
273
300
            return False
274
301
 
275
302
    def handle_modify_text(self, creator, file_id):
 
303
        """Handle modified text, by using hunk selection or file editing.
 
304
 
 
305
        :param creator: A ShelfCreator.
 
306
        :param file_id: The id of the file that was modified.
 
307
        :return: The number of changes.
 
308
        """
 
309
        work_tree_lines = self.work_tree.get_file_lines(file_id)
 
310
        try:
 
311
            lines, change_count = self._select_hunks(creator, file_id,
 
312
                                                     work_tree_lines)
 
313
        except UseEditor:
 
314
            lines, change_count = self._edit_file(file_id, work_tree_lines)
 
315
        if change_count != 0:
 
316
            creator.shelve_lines(file_id, lines)
 
317
        return change_count
 
318
 
 
319
    def _select_hunks(self, creator, file_id, work_tree_lines):
276
320
        """Provide diff hunk selection for modified text.
277
321
 
278
322
        If self.reporter.invert_diff is True, the diff is inverted so that
280
324
 
281
325
        :param creator: a ShelfCreator
282
326
        :param file_id: The id of the file to shelve.
 
327
        :param work_tree_lines: Line contents of the file in the working tree.
283
328
        :return: number of shelved hunks.
284
329
        """
285
330
        if self.reporter.invert_diff:
286
 
            target_lines = self.work_tree.get_file_lines(file_id)
 
331
            target_lines = work_tree_lines
287
332
        else:
288
333
            target_lines = self.target_tree.get_file_lines(file_id)
289
 
        textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
 
334
        textfile.check_text_lines(work_tree_lines)
290
335
        textfile.check_text_lines(target_lines)
291
336
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
292
337
        final_hunks = []
295
340
            self.diff_writer.write(parsed.get_header())
296
341
            for hunk in parsed.hunks:
297
342
                self.diff_writer.write(str(hunk))
298
 
                selected = self.prompt_bool(self.reporter.vocab['hunk'])
 
343
                selected = self.prompt_bool(self.reporter.vocab['hunk'],
 
344
                                            allow_editor=(self.change_editor
 
345
                                                          is not None))
299
346
                if not self.reporter.invert_diff:
300
347
                    selected = (not selected)
301
348
                if selected:
304
351
                else:
305
352
                    offset -= (hunk.mod_range - hunk.orig_range)
306
353
        sys.stdout.flush()
307
 
        if not self.reporter.invert_diff and (
308
 
            len(parsed.hunks) == len(final_hunks)):
309
 
            return 0
310
 
        if self.reporter.invert_diff and len(final_hunks) == 0:
311
 
            return 0
312
 
        patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
313
 
        creator.shelve_lines(file_id, list(patched))
314
354
        if self.reporter.invert_diff:
315
 
            return len(final_hunks)
316
 
        return len(parsed.hunks) - len(final_hunks)
 
355
            change_count = len(final_hunks)
 
356
        else:
 
357
            change_count = len(parsed.hunks) - len(final_hunks)
 
358
        patched = patches.iter_patched_from_hunks(target_lines,
 
359
                                                  final_hunks)
 
360
        lines = list(patched)
 
361
        return lines, change_count
 
362
 
 
363
    def _edit_file(self, file_id, work_tree_lines):
 
364
        """
 
365
        :param file_id: id of the file to edit.
 
366
        :param work_tree_lines: Line contents of the file in the working tree.
 
367
        :return: (lines, change_region_count), where lines is the new line
 
368
            content of the file, and change_region_count is the number of
 
369
            changed regions.
 
370
        """
 
371
        lines = osutils.split_lines(self.change_editor.edit_file(file_id))
 
372
        return lines, self._count_changed_regions(work_tree_lines, lines)
 
373
 
 
374
    @staticmethod
 
375
    def _count_changed_regions(old_lines, new_lines):
 
376
        matcher = patiencediff.PatienceSequenceMatcher(None, old_lines,
 
377
                                                       new_lines)
 
378
        blocks = matcher.get_matching_blocks()
 
379
        return len(blocks) - 2
317
380
 
318
381
 
319
382
class Unshelver(object):
344
407
                shelf_id = manager.last_shelf()
345
408
                if shelf_id is None:
346
409
                    raise errors.BzrCommandError('No changes are shelved.')
347
 
                trace.note('Unshelving changes with id "%d".' % shelf_id)
348
410
            apply_changes = True
349
411
            delete_shelf = True
350
412
            read_shelf = True
351
413
            if action == 'dry-run':
352
414
                apply_changes = False
353
415
                delete_shelf = False
354
 
            if action == 'delete-only':
 
416
            elif action == 'delete-only':
355
417
                apply_changes = False
356
418
                read_shelf = False
 
419
            elif action == 'keep':
 
420
                apply_changes = True
 
421
                delete_shelf = False
357
422
        except:
358
423
            tree.unlock()
359
424
            raise
386
451
        cleanups = [self.tree.unlock]
387
452
        try:
388
453
            if self.read_shelf:
 
454
                trace.note('Using changes with id "%d".' % self.shelf_id)
389
455
                unshelver = self.manager.get_unshelver(self.shelf_id)
390
456
                cleanups.append(unshelver.finalize)
391
457
                if unshelver.message is not None:
403
469
                    task.finished()
404
470
            if self.delete_shelf:
405
471
                self.manager.delete_shelf(self.shelf_id)
 
472
                trace.note('Deleted changes with id "%d".' % self.shelf_id)
406
473
        finally:
407
474
            for cleanup in reversed(cleanups):
408
475
                cleanup()