/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

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2008-03-03 17:24:22 UTC
  • mto: This revision was merged to the branch mainline in revision 435.
  • Revision ID: szilveszter.farkas@gmail.com-20080303172422-y1u0gg7kyirrxuun
Installing an olive package is pretty useless. (Fixed: #136432)

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='',
261
285
        self.buffer.set_text(decoded.encode('UTF-8'))
262
286
 
263
287
 
264
 
class DiffWindow(Window):
265
 
    """Diff window.
 
288
class DiffWidget(gtk.HPaned):
 
289
    """Diff widget
266
290
 
267
 
    This object represents and manages a single window containing the
268
 
    differences between two revisions on a branch.
269
291
    """
270
 
 
271
 
    def __init__(self, parent=None):
272
 
        Window.__init__(self, parent)
273
 
        self.set_border_width(0)
274
 
        self.set_title("bzrk diff")
275
 
 
276
 
        # Use two thirds of the screen by default
277
 
        screen = self.get_screen()
278
 
        monitor = screen.get_monitor_geometry(0)
279
 
        width = int(monitor.width * 0.66)
280
 
        height = int(monitor.height * 0.66)
281
 
        self.set_default_size(width, height)
282
 
 
283
 
        self.construct()
284
 
 
285
 
    def construct(self):
286
 
        """Construct the window contents."""
287
 
        # The   window  consists  of   a  pane   containing:  the
288
 
        # hierarchical list  of files on  the left, and  the diff
289
 
        # for the currently selected file on the right.
290
 
        pane = gtk.HPaned()
291
 
        self.add(pane)
292
 
        pane.show()
 
292
    def __init__(self):
 
293
        super(DiffWidget, self).__init__()
293
294
 
294
295
        # The file hierarchy: a scrollable treeview
295
296
        scrollwin = gtk.ScrolledWindow()
296
297
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
297
298
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
298
 
        pane.pack1(scrollwin)
 
299
        self.pack1(scrollwin)
299
300
        scrollwin.show()
300
301
 
301
302
        self.model = gtk.TreeStore(str, str)
313
314
        column.add_attribute(cell, "text", 0)
314
315
        self.treeview.append_column(column)
315
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
        """
316
322
        # The diffs of the  selected file: a scrollable source or
317
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
        """
318
342
        self.diff_view = DiffView()
319
 
        pane.pack2(self.diff_view)
 
343
        self.pack2(self.diff_view)
320
344
        self.diff_view.show()
321
 
 
322
 
    def set_diff(self, description, rev_tree, parent_tree):
323
 
        """Set the differences showed by this window.
324
 
 
325
 
        Compares the two trees and populates the window with the
326
 
        differences.
327
 
        """
328
345
        self.diff_view.set_trees(rev_tree, parent_tree)
329
346
        self.rev_tree = rev_tree
330
347
        self.parent_tree = parent_tree
356
373
                self.model.append(titer, [ path, path ])
357
374
 
358
375
        self.treeview.expand_all()
359
 
        self.set_title(description + " - bzrk diff")
360
376
 
361
377
    def set_file(self, file_path):
 
378
        """Select the current file to display"""
362
379
        tv_path = None
363
380
        for data in self.model:
364
381
            for child in data.iterchildren():
382
399
        self.diff_view.show_diff(specific_files)
383
400
 
384
401
 
 
402
class DiffWindow(Window):
 
403
    """Diff window.
 
404
 
 
405
    This object represents and manages a single window containing the
 
406
    differences between two revisions on a branch.
 
407
    """
 
408
 
 
409
    def __init__(self, parent=None):
 
410
        Window.__init__(self, parent)
 
411
        self.set_border_width(0)
 
412
        self.set_title("bzrk diff")
 
413
 
 
414
        # Use two thirds of the screen by default
 
415
        screen = self.get_screen()
 
416
        monitor = screen.get_monitor_geometry(0)
 
417
        width = int(monitor.width * 0.66)
 
418
        height = int(monitor.height * 0.66)
 
419
        self.set_default_size(width, height)
 
420
 
 
421
        self.construct()
 
422
 
 
423
    def construct(self):
 
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)
 
431
        self.diff = DiffWidget()
 
432
        self.vbox.add(self.diff)
 
433
        self.diff.show_all()
 
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
 
 
451
    def set_diff(self, description, rev_tree, parent_tree):
 
452
        """Set the differences showed by this window.
 
453
 
 
454
        Compares the two trees and populates the window with the
 
455
        differences.
 
456
        """
 
457
        self.diff.set_diff(rev_tree, parent_tree)
 
458
        self.set_title(description + " - bzrk diff")
 
459
 
 
460
    def set_file(self, file_path):
 
461
        self.diff.set_file(file_path)
 
462
 
 
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
 
385
531
def _iter_changes_to_status(source, target):
386
532
    """Determine the differences between trees.
387
533