/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: Daniel Schierbeck
  • Date: 2007-12-06 23:37:06 UTC
  • mto: This revision was merged to the branch mainline in revision 417.
  • Revision ID: daniel.schierbeck@gmail.com-20071206233706-eeinks66w86r3gfm
Fixed bug in gmissing.

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 merge as _mod_merge, osutils, progress, workingtree
 
33
from bzrlib import osutils
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
37
36
from bzrlib.trace import warning
38
37
from bzrlib.plugins.gtk.window import Window
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):
 
38
 
 
39
 
 
40
class DiffView(gtk.ScrolledWindow):
 
41
    """This is the soft and chewy filling for a DiffWindow."""
48
42
 
49
43
    def __init__(self):
50
44
        gtk.ScrolledWindow.__init__(self)
 
45
 
51
46
        self.construct()
52
 
        self._diffs = {}
 
47
        self.rev_tree = None
 
48
        self.parent_tree = None
53
49
 
54
50
    def construct(self):
55
51
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
138
134
 
139
135
            lang.set_tag_style(tag_id, style)
140
136
 
141
 
    @classmethod
142
 
    def apply_colordiff_colors(klass, lang):
 
137
    @staticmethod
 
138
    def apply_colordiff_colors(lang):
143
139
        """Set style colors for lang using the colordiff configuration file.
144
140
 
145
141
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
156
152
                except IOError, e:
157
153
                    warning('could not open file %s: %s' % (f, str(e)))
158
154
                else:
159
 
                    colors.update(klass.parse_colordiffrc(f))
 
155
                    colors.update(DiffView.parse_colordiffrc(f))
160
156
                    f.close()
161
157
 
162
158
        if not colors:
241
237
#            self.parent_tree.unlock()
242
238
 
243
239
    def show_diff(self, specific_files):
244
 
        sections = []
245
 
        if specific_files is None:
246
 
            self.buffer.set_text(self._diffs[None])
247
 
        else:
248
 
            for specific_file in specific_files:
249
 
                sections.append(self._diffs[specific_file])
250
 
            self.buffer.set_text(''.join(sections))
251
 
 
252
 
 
253
 
class DiffView(DiffFileView):
254
 
    """This is the soft and chewy filling for a DiffWindow."""
255
 
 
256
 
    def __init__(self):
257
 
        DiffFileView.__init__(self)
258
 
        self.rev_tree = None
259
 
        self.parent_tree = None
260
 
 
261
 
    def show_diff(self, specific_files):
262
240
        s = StringIO()
263
241
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
264
242
                        old_label='', new_label='',
283
261
        self.buffer.set_text(decoded.encode('UTF-8'))
284
262
 
285
263
 
286
 
class DiffWidget(gtk.HPaned):
287
 
    """Diff widget
 
264
class DiffWindow(Window):
 
265
    """Diff window.
288
266
 
 
267
    This object represents and manages a single window containing the
 
268
    differences between two revisions on a branch.
289
269
    """
290
 
    def __init__(self):
291
 
        super(DiffWidget, self).__init__()
 
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
293
 
293
294
        # The file hierarchy: a scrollable treeview
294
295
        scrollwin = gtk.ScrolledWindow()
295
296
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
296
297
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
297
 
        self.pack1(scrollwin)
 
298
        pane.pack1(scrollwin)
298
299
        scrollwin.show()
299
300
 
300
301
        self.model = gtk.TreeStore(str, str)
312
313
        column.add_attribute(cell, "text", 0)
313
314
        self.treeview.append_column(column)
314
315
 
315
 
    def set_diff_text(self, lines):
316
316
        # The diffs of the  selected file: a scrollable source or
317
317
        # text view
318
 
        self.diff_view = DiffFileView()
319
 
        self.diff_view.show()
320
 
        self.pack2(self.diff_view)
321
 
        self.model.append(None, [ "Complete Diff", "" ])
322
 
        self.diff_view._diffs[None] = ''.join(lines)
323
 
        for patch in parse_patches(lines):
324
 
            oldname = patch.oldname.split('\t')[0]
325
 
            newname = patch.newname.split('\t')[0]
326
 
            self.model.append(None, [oldname, newname])
327
 
            self.diff_view._diffs[newname] = str(patch)
328
 
        self.diff_view.show_diff(None)
329
 
 
330
 
    def set_diff(self, rev_tree, parent_tree):
331
 
        """Set the differences showed by this window.
332
 
 
333
 
        Compares the two trees and populates the window with the
334
 
        differences.
335
 
        """
336
318
        self.diff_view = DiffView()
337
 
        self.pack2(self.diff_view)
 
319
        pane.pack2(self.diff_view)
338
320
        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
        """
339
328
        self.diff_view.set_trees(rev_tree, parent_tree)
340
329
        self.rev_tree = rev_tree
341
330
        self.parent_tree = parent_tree
367
356
                self.model.append(titer, [ path, path ])
368
357
 
369
358
        self.treeview.expand_all()
 
359
        self.set_title(description + " - bzrk diff")
370
360
 
371
361
    def set_file(self, file_path):
372
362
        tv_path = None
392
382
        self.diff_view.show_diff(specific_files)
393
383
 
394
384
 
395
 
class DiffWindow(Window):
396
 
    """Diff window.
397
 
 
398
 
    This object represents and manages a single window containing the
399
 
    differences between two revisions on a branch.
400
 
    """
401
 
 
402
 
    def __init__(self, parent=None):
403
 
        Window.__init__(self, parent)
404
 
        self.set_border_width(0)
405
 
        self.set_title("bzrk diff")
406
 
 
407
 
        # Use two thirds of the screen by default
408
 
        screen = self.get_screen()
409
 
        monitor = screen.get_monitor_geometry(0)
410
 
        width = int(monitor.width * 0.66)
411
 
        height = int(monitor.height * 0.66)
412
 
        self.set_default_size(width, height)
413
 
 
414
 
        self.construct()
415
 
 
416
 
    def construct(self):
417
 
        """Construct the window contents."""
418
 
        self.vbox = gtk.VBox()
419
 
        self.add(self.vbox)
420
 
        self.vbox.show()
421
 
        hbox = self._get_button_bar()
422
 
        if hbox is not None:
423
 
            self.vbox.pack_start(hbox, expand=False, fill=True)
424
 
        self.diff = DiffWidget()
425
 
        self.vbox.add(self.diff)
426
 
        self.diff.show_all()
427
 
 
428
 
    def _get_button_bar(self):
429
 
        return None
430
 
 
431
 
    def set_diff_text(self, description, lines):
432
 
        self.diff.set_diff_text(lines)
433
 
        self.set_title(description + " - bzrk diff")
434
 
 
435
 
    def set_diff(self, description, rev_tree, parent_tree):
436
 
        """Set the differences showed by this window.
437
 
 
438
 
        Compares the two trees and populates the window with the
439
 
        differences.
440
 
        """
441
 
        self.diff.set_diff(rev_tree, parent_tree)
442
 
        self.set_title(description + " - bzrk diff")
443
 
 
444
 
    def set_file(self, file_path):
445
 
        self.diff.set_file(file_path)
446
 
 
447
 
 
448
 
class MergeDirectiveWindow(DiffWindow):
449
 
 
450
 
    def __init__(self, directive, parent=None):
451
 
        DiffWindow.__init__(self, parent)
452
 
        self._merge_target = None
453
 
        self.directive = directive
454
 
 
455
 
    def _get_button_bar(self):
456
 
        merge_button = gtk.Button('Merge')
457
 
        merge_button.show()
458
 
        merge_button.set_relief(gtk.RELIEF_NONE)
459
 
        merge_button.connect("clicked", self.perform_merge)
460
 
 
461
 
        hbox = gtk.HButtonBox()
462
 
        hbox.set_layout(gtk.BUTTONBOX_START)
463
 
        hbox.pack_start(merge_button, expand=False, fill=True)
464
 
        hbox.show()
465
 
        return hbox
466
 
 
467
 
    def perform_merge(self, window):
468
 
        try:
469
 
            tree = self._get_merge_target()
470
 
        except SelectCancelled:
471
 
            return
472
 
        tree.lock_write()
473
 
        try:
474
 
            try:
475
 
                merger, verified = _mod_merge.Merger.from_mergeable(tree,
476
 
                    self.directive, progress.DummyProgress())
477
 
                merger.check_basis(True)
478
 
                merger.merge_type = _mod_merge.Merge3Merger
479
 
                conflict_count = merger.do_merge()
480
 
                merger.set_pending()
481
 
                if conflict_count == 0:
482
 
                    # No conflicts found.
483
 
                    info_dialog(_('Merge successful'),
484
 
                                _('All changes applied successfully.'))
485
 
                else:
486
 
                    # There are conflicts to be resolved.
487
 
                    warning_dialog(_('Conflicts encountered'),
488
 
                                   _('Please resolve the conflicts manually'
489
 
                                     ' before committing.'))
490
 
                self.destroy()
491
 
            except Exception, e:
492
 
                error_dialog('Error', str(e))
493
 
        finally:
494
 
            tree.unlock()
495
 
 
496
 
    def _get_merge_target(self):
497
 
        if self._merge_target is not None:
498
 
            return self._merge_target
499
 
        d = gtk.FileChooserDialog('Merge branch', self,
500
 
                                  gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
501
 
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
502
 
                                           gtk.STOCK_CANCEL,
503
 
                                           gtk.RESPONSE_CANCEL,))
504
 
        try:
505
 
            result = d.run()
506
 
            if result == gtk.RESPONSE_OK:
507
 
                uri = d.get_current_folder_uri()
508
 
                return workingtree.WorkingTree.open(uri)
509
 
            else:
510
 
                raise SelectCancelled()
511
 
        finally:
512
 
            d.destroy()
513
 
 
514
 
 
515
385
def _iter_changes_to_status(source, target):
516
386
    """Determine the differences between trees.
517
387