/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

Merged Aaron Bentley's patch handler.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
except ImportError:
31
31
    have_gconf = False
32
32
 
33
 
from bzrlib import osutils
 
33
from bzrlib import merge as _mod_merge, osutils, progress, workingtree
34
34
from bzrlib.diff import show_diff_trees, internal_diff
35
35
from bzrlib.errors import NoSuchFile
 
36
from bzrlib.patches import parse_patches
36
37
from bzrlib.trace import warning
37
38
from bzrlib.plugins.gtk.window import Window
38
 
 
39
 
 
40
 
class DiffView(gtk.ScrolledWindow):
41
 
    """This is the soft and chewy filling for a DiffWindow."""
 
39
from dialog import error_dialog, info_dialog, warning_dialog
 
40
 
 
41
 
 
42
class SelectCancelled(Exception):
 
43
 
 
44
    pass
 
45
 
 
46
 
 
47
class DiffFileView(gtk.ScrolledWindow):
 
48
    """Window for displaying diffs from a diff file"""
42
49
 
43
50
    def __init__(self):
44
51
        gtk.ScrolledWindow.__init__(self)
45
 
 
46
52
        self.construct()
47
 
        self.rev_tree = None
48
 
        self.parent_tree = None
 
53
        self._diffs = {}
49
54
 
50
55
    def construct(self):
51
56
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
134
139
 
135
140
            lang.set_tag_style(tag_id, style)
136
141
 
137
 
    @staticmethod
138
 
    def apply_colordiff_colors(lang):
 
142
    @classmethod
 
143
    def apply_colordiff_colors(klass, lang):
139
144
        """Set style colors for lang using the colordiff configuration file.
140
145
 
141
146
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
152
157
                except IOError, e:
153
158
                    warning('could not open file %s: %s' % (f, str(e)))
154
159
                else:
155
 
                    colors.update(DiffView.parse_colordiffrc(f))
 
160
                    colors.update(klass.parse_colordiffrc(f))
156
161
                    f.close()
157
162
 
158
163
        if not colors:
237
242
#            self.parent_tree.unlock()
238
243
 
239
244
    def show_diff(self, specific_files):
 
245
        sections = []
 
246
        if specific_files is None:
 
247
            self.buffer.set_text(self._diffs[None])
 
248
        else:
 
249
            for specific_file in specific_files:
 
250
                sections.append(self._diffs[specific_file])
 
251
            self.buffer.set_text(''.join(sections))
 
252
 
 
253
 
 
254
class DiffView(DiffFileView):
 
255
    """This is the soft and chewy filling for a DiffWindow."""
 
256
 
 
257
    def __init__(self):
 
258
        DiffFileView.__init__(self)
 
259
        self.rev_tree = None
 
260
        self.parent_tree = None
 
261
 
 
262
    def show_diff(self, specific_files):
 
263
        """Show the diff for the specified files"""
240
264
        s = StringIO()
241
265
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
242
266
                        old_label='', new_label='',
290
314
        column.add_attribute(cell, "text", 0)
291
315
        self.treeview.append_column(column)
292
316
 
 
317
    def set_diff_text(self, lines):
 
318
        """Set the current diff from a list of lines
 
319
 
 
320
        :param lines: The diff to show, in unified diff format
 
321
        """
293
322
        # The diffs of the  selected file: a scrollable source or
294
323
        # text view
 
324
        self.diff_view = DiffFileView()
 
325
        self.diff_view.show()
 
326
        self.pack2(self.diff_view)
 
327
        self.model.append(None, [ "Complete Diff", "" ])
 
328
        self.diff_view._diffs[None] = ''.join(lines)
 
329
        for patch in parse_patches(lines):
 
330
            oldname = patch.oldname.split('\t')[0]
 
331
            newname = patch.newname.split('\t')[0]
 
332
            self.model.append(None, [oldname, newname])
 
333
            self.diff_view._diffs[newname] = str(patch)
 
334
        self.diff_view.show_diff(None)
 
335
 
 
336
    def set_diff(self, rev_tree, parent_tree):
 
337
        """Set the differences showed by this window.
 
338
 
 
339
        Compares the two trees and populates the window with the
 
340
        differences.
 
341
        """
295
342
        self.diff_view = DiffView()
296
343
        self.pack2(self.diff_view)
297
344
        self.diff_view.show()
298
 
 
299
 
    def set_diff(self, rev_tree, parent_tree):
300
 
        """Set the differences showed by this window.
301
 
 
302
 
        Compares the two trees and populates the window with the
303
 
        differences.
304
 
        """
305
345
        self.diff_view.set_trees(rev_tree, parent_tree)
306
346
        self.rev_tree = rev_tree
307
347
        self.parent_tree = parent_tree
335
375
        self.treeview.expand_all()
336
376
 
337
377
    def set_file(self, file_path):
 
378
        """Select the current file to display"""
338
379
        tv_path = None
339
380
        for data in self.model:
340
381
            for child in data.iterchildren():
381
422
 
382
423
    def construct(self):
383
424
        """Construct the window contents."""
 
425
        self.vbox = gtk.VBox()
 
426
        self.add(self.vbox)
 
427
        self.vbox.show()
 
428
        hbox = self._get_button_bar()
 
429
        if hbox is not None:
 
430
            self.vbox.pack_start(hbox, expand=False, fill=True)
384
431
        self.diff = DiffWidget()
385
 
        self.add(self.diff)
 
432
        self.vbox.add(self.diff)
386
433
        self.diff.show_all()
387
434
 
 
435
    def _get_button_bar(self):
 
436
        """Return a button bar to use.
 
437
 
 
438
        :return: None, meaning that no button bar will be used.
 
439
        """
 
440
        return None
 
441
 
 
442
    def set_diff_text(self, description, lines):
 
443
        """Set the diff from a text.
 
444
 
 
445
        The diff must be in unified diff format, and will be parsed to
 
446
        determine filenames.
 
447
        """
 
448
        self.diff.set_diff_text(lines)
 
449
        self.set_title(description + " - bzrk diff")
 
450
 
388
451
    def set_diff(self, description, rev_tree, parent_tree):
389
452
        """Set the differences showed by this window.
390
453
 
398
461
        self.diff.set_file(file_path)
399
462
 
400
463
 
 
464
class MergeDirectiveWindow(DiffWindow):
 
465
 
 
466
    def __init__(self, directive, parent=None):
 
467
        DiffWindow.__init__(self, parent)
 
468
        self._merge_target = None
 
469
        self.directive = directive
 
470
 
 
471
    def _get_button_bar(self):
 
472
        """The button bar has only the Merge button"""
 
473
        merge_button = gtk.Button('Merge')
 
474
        merge_button.show()
 
475
        merge_button.set_relief(gtk.RELIEF_NONE)
 
476
        merge_button.connect("clicked", self.perform_merge)
 
477
 
 
478
        hbox = gtk.HButtonBox()
 
479
        hbox.set_layout(gtk.BUTTONBOX_START)
 
480
        hbox.pack_start(merge_button, expand=False, fill=True)
 
481
        hbox.show()
 
482
        return hbox
 
483
 
 
484
    def perform_merge(self, window):
 
485
        try:
 
486
            tree = self._get_merge_target()
 
487
        except SelectCancelled:
 
488
            return
 
489
        tree.lock_write()
 
490
        try:
 
491
            try:
 
492
                merger, verified = _mod_merge.Merger.from_mergeable(tree,
 
493
                    self.directive, progress.DummyProgress())
 
494
                merger.check_basis(True)
 
495
                merger.merge_type = _mod_merge.Merge3Merger
 
496
                conflict_count = merger.do_merge()
 
497
                merger.set_pending()
 
498
                if conflict_count == 0:
 
499
                    # No conflicts found.
 
500
                    info_dialog(_('Merge successful'),
 
501
                                _('All changes applied successfully.'))
 
502
                else:
 
503
                    # There are conflicts to be resolved.
 
504
                    warning_dialog(_('Conflicts encountered'),
 
505
                                   _('Please resolve the conflicts manually'
 
506
                                     ' before committing.'))
 
507
                self.destroy()
 
508
            except Exception, e:
 
509
                error_dialog('Error', str(e))
 
510
        finally:
 
511
            tree.unlock()
 
512
 
 
513
    def _get_merge_target(self):
 
514
        if self._merge_target is not None:
 
515
            return self._merge_target
 
516
        d = gtk.FileChooserDialog('Merge branch', self,
 
517
                                  gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
 
518
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
519
                                           gtk.STOCK_CANCEL,
 
520
                                           gtk.RESPONSE_CANCEL,))
 
521
        try:
 
522
            result = d.run()
 
523
            if result != gtk.RESPONSE_OK:
 
524
                raise SelectCancelled()
 
525
            uri = d.get_current_folder_uri()
 
526
        finally:
 
527
            d.destroy()
 
528
        return workingtree.WorkingTree.open(uri)
 
529
 
 
530
 
401
531
def _iter_changes_to_status(source, target):
402
532
    """Determine the differences between trees.
403
533