/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 with mainline.

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 (
 
34
    merge as _mod_merge,
 
35
    osutils,
 
36
    progress,
 
37
    urlutils,
 
38
    workingtree,
 
39
)
34
40
from bzrlib.diff import show_diff_trees, internal_diff
35
41
from bzrlib.errors import NoSuchFile
 
42
from bzrlib.patches import parse_patches
36
43
from bzrlib.trace import warning
37
44
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."""
 
45
from dialog import error_dialog, info_dialog, warning_dialog
 
46
 
 
47
 
 
48
class SelectCancelled(Exception):
 
49
 
 
50
    pass
 
51
 
 
52
 
 
53
class DiffFileView(gtk.ScrolledWindow):
 
54
    """Window for displaying diffs from a diff file"""
42
55
 
43
56
    def __init__(self):
44
57
        gtk.ScrolledWindow.__init__(self)
45
 
 
46
58
        self.construct()
47
 
        self.rev_tree = None
48
 
        self.parent_tree = None
 
59
        self._diffs = {}
49
60
 
50
61
    def construct(self):
51
62
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
134
145
 
135
146
            lang.set_tag_style(tag_id, style)
136
147
 
137
 
    @staticmethod
138
 
    def apply_colordiff_colors(lang):
 
148
    @classmethod
 
149
    def apply_colordiff_colors(klass, lang):
139
150
        """Set style colors for lang using the colordiff configuration file.
140
151
 
141
152
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
152
163
                except IOError, e:
153
164
                    warning('could not open file %s: %s' % (f, str(e)))
154
165
                else:
155
 
                    colors.update(DiffView.parse_colordiffrc(f))
 
166
                    colors.update(klass.parse_colordiffrc(f))
156
167
                    f.close()
157
168
 
158
169
        if not colors:
216
227
#        self.parent_tree.lock_read()
217
228
#        self.rev_tree.lock_read()
218
229
#        try:
219
 
#            self.delta = _iter_changes_to_status(self.parent_tree, self.rev_tree)
 
230
#            self.delta = iter_changes_to_status(self.parent_tree, self.rev_tree)
220
231
#            self.path_to_status = {}
221
232
#            self.path_to_diff = {}
222
233
#            source_inv = self.parent_tree.inventory
237
248
#            self.parent_tree.unlock()
238
249
 
239
250
    def show_diff(self, specific_files):
 
251
        sections = []
 
252
        if specific_files is None:
 
253
            self.buffer.set_text(self._diffs[None])
 
254
        else:
 
255
            for specific_file in specific_files:
 
256
                sections.append(self._diffs[specific_file])
 
257
            self.buffer.set_text(''.join(sections))
 
258
 
 
259
 
 
260
class DiffView(DiffFileView):
 
261
    """This is the soft and chewy filling for a DiffWindow."""
 
262
 
 
263
    def __init__(self):
 
264
        DiffFileView.__init__(self)
 
265
        self.rev_tree = None
 
266
        self.parent_tree = None
 
267
 
 
268
    def show_diff(self, specific_files):
 
269
        """Show the diff for the specified files"""
240
270
        s = StringIO()
241
271
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
242
272
                        old_label='', new_label='',
261
291
        self.buffer.set_text(decoded.encode('UTF-8'))
262
292
 
263
293
 
264
 
class DiffWindow(Window):
265
 
    """Diff window.
 
294
class DiffWidget(gtk.HPaned):
 
295
    """Diff widget
266
296
 
267
 
    This object represents and manages a single window containing the
268
 
    differences between two revisions on a branch.
269
297
    """
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()
 
298
    def __init__(self):
 
299
        super(DiffWidget, self).__init__()
293
300
 
294
301
        # The file hierarchy: a scrollable treeview
295
302
        scrollwin = gtk.ScrolledWindow()
296
303
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
297
304
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
298
 
        pane.pack1(scrollwin)
 
305
        self.pack1(scrollwin)
299
306
        scrollwin.show()
300
307
 
301
308
        self.model = gtk.TreeStore(str, str)
313
320
        column.add_attribute(cell, "text", 0)
314
321
        self.treeview.append_column(column)
315
322
 
 
323
    def set_diff_text(self, lines):
 
324
        """Set the current diff from a list of lines
 
325
 
 
326
        :param lines: The diff to show, in unified diff format
 
327
        """
316
328
        # The diffs of the  selected file: a scrollable source or
317
329
        # text view
 
330
        self.diff_view = DiffFileView()
 
331
        self.diff_view.show()
 
332
        self.pack2(self.diff_view)
 
333
        self.model.append(None, [ "Complete Diff", "" ])
 
334
        self.diff_view._diffs[None] = ''.join(lines)
 
335
        for patch in parse_patches(lines):
 
336
            oldname = patch.oldname.split('\t')[0]
 
337
            newname = patch.newname.split('\t')[0]
 
338
            self.model.append(None, [oldname, newname])
 
339
            self.diff_view._diffs[newname] = str(patch)
 
340
        self.diff_view.show_diff(None)
 
341
 
 
342
    def set_diff(self, rev_tree, parent_tree):
 
343
        """Set the differences showed by this window.
 
344
 
 
345
        Compares the two trees and populates the window with the
 
346
        differences.
 
347
        """
318
348
        self.diff_view = DiffView()
319
 
        pane.pack2(self.diff_view)
 
349
        self.pack2(self.diff_view)
320
350
        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
351
        self.diff_view.set_trees(rev_tree, parent_tree)
329
352
        self.rev_tree = rev_tree
330
353
        self.parent_tree = parent_tree
356
379
                self.model.append(titer, [ path, path ])
357
380
 
358
381
        self.treeview.expand_all()
359
 
        self.set_title(description + " - bzrk diff")
360
382
 
361
383
    def set_file(self, file_path):
 
384
        """Select the current file to display"""
362
385
        tv_path = None
363
386
        for data in self.model:
364
387
            for child in data.iterchildren():
382
405
        self.diff_view.show_diff(specific_files)
383
406
 
384
407
 
385
 
def _iter_changes_to_status(source, target):
 
408
class DiffWindow(Window):
 
409
    """Diff window.
 
410
 
 
411
    This object represents and manages a single window containing the
 
412
    differences between two revisions on a branch.
 
413
    """
 
414
 
 
415
    def __init__(self, parent=None):
 
416
        Window.__init__(self, parent)
 
417
        self.set_border_width(0)
 
418
        self.set_title("bzrk diff")
 
419
 
 
420
        # Use two thirds of the screen by default
 
421
        screen = self.get_screen()
 
422
        monitor = screen.get_monitor_geometry(0)
 
423
        width = int(monitor.width * 0.66)
 
424
        height = int(monitor.height * 0.66)
 
425
        self.set_default_size(width, height)
 
426
 
 
427
        self.construct()
 
428
 
 
429
    def construct(self):
 
430
        """Construct the window contents."""
 
431
        self.vbox = gtk.VBox()
 
432
        self.add(self.vbox)
 
433
        self.vbox.show()
 
434
        hbox = self._get_button_bar()
 
435
        if hbox is not None:
 
436
            self.vbox.pack_start(hbox, expand=False, fill=True)
 
437
        self.diff = DiffWidget()
 
438
        self.vbox.add(self.diff)
 
439
        self.diff.show_all()
 
440
 
 
441
    def _get_button_bar(self):
 
442
        """Return a button bar to use.
 
443
 
 
444
        :return: None, meaning that no button bar will be used.
 
445
        """
 
446
        return None
 
447
 
 
448
    def set_diff_text(self, description, lines):
 
449
        """Set the diff from a text.
 
450
 
 
451
        The diff must be in unified diff format, and will be parsed to
 
452
        determine filenames.
 
453
        """
 
454
        self.diff.set_diff_text(lines)
 
455
        self.set_title(description + " - bzrk diff")
 
456
 
 
457
    def set_diff(self, description, rev_tree, parent_tree):
 
458
        """Set the differences showed by this window.
 
459
 
 
460
        Compares the two trees and populates the window with the
 
461
        differences.
 
462
        """
 
463
        self.diff.set_diff(rev_tree, parent_tree)
 
464
        self.set_title(description + " - bzrk diff")
 
465
 
 
466
    def set_file(self, file_path):
 
467
        self.diff.set_file(file_path)
 
468
 
 
469
 
 
470
class MergeDirectiveWindow(DiffWindow):
 
471
 
 
472
    def __init__(self, directive, path):
 
473
        DiffWindow.__init__(self, None)
 
474
        self._merge_target = None
 
475
        self.directive = directive
 
476
        self.path = path
 
477
 
 
478
    def _get_button_bar(self):
 
479
        """The button bar has only the Merge button"""
 
480
        merge_button = gtk.Button('Merge')
 
481
        merge_button.show()
 
482
        merge_button.set_relief(gtk.RELIEF_NONE)
 
483
        merge_button.connect("clicked", self.perform_merge)
 
484
 
 
485
        save_button = gtk.Button('Save')
 
486
        save_button.show()
 
487
        save_button.set_relief(gtk.RELIEF_NONE)
 
488
        save_button.connect("clicked", self.perform_save)
 
489
 
 
490
        hbox = gtk.HButtonBox()
 
491
        hbox.set_layout(gtk.BUTTONBOX_START)
 
492
        hbox.pack_start(merge_button, expand=False, fill=True)
 
493
        hbox.pack_start(save_button, expand=False, fill=True)
 
494
        hbox.show()
 
495
        return hbox
 
496
 
 
497
    def perform_merge(self, window):
 
498
        try:
 
499
            tree = self._get_merge_target()
 
500
        except SelectCancelled:
 
501
            return
 
502
        tree.lock_write()
 
503
        try:
 
504
            try:
 
505
                merger, verified = _mod_merge.Merger.from_mergeable(tree,
 
506
                    self.directive, progress.DummyProgress())
 
507
                merger.check_basis(True)
 
508
                merger.merge_type = _mod_merge.Merge3Merger
 
509
                conflict_count = merger.do_merge()
 
510
                merger.set_pending()
 
511
                if conflict_count == 0:
 
512
                    # No conflicts found.
 
513
                    info_dialog(_('Merge successful'),
 
514
                                _('All changes applied successfully.'))
 
515
                else:
 
516
                    # There are conflicts to be resolved.
 
517
                    warning_dialog(_('Conflicts encountered'),
 
518
                                   _('Please resolve the conflicts manually'
 
519
                                     ' before committing.'))
 
520
                self.destroy()
 
521
            except Exception, e:
 
522
                error_dialog('Error', str(e))
 
523
        finally:
 
524
            tree.unlock()
 
525
 
 
526
    def _get_merge_target(self):
 
527
        if self._merge_target is not None:
 
528
            return self._merge_target
 
529
        d = gtk.FileChooserDialog('Merge branch', self,
 
530
                                  gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
 
531
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
532
                                           gtk.STOCK_CANCEL,
 
533
                                           gtk.RESPONSE_CANCEL,))
 
534
        try:
 
535
            result = d.run()
 
536
            if result != gtk.RESPONSE_OK:
 
537
                raise SelectCancelled()
 
538
            uri = d.get_current_folder_uri()
 
539
        finally:
 
540
            d.destroy()
 
541
        return workingtree.WorkingTree.open(uri)
 
542
 
 
543
    def perform_save(self, window):
 
544
        d = gtk.FileChooserDialog('Save As', self,
 
545
                                  gtk.FILE_CHOOSER_ACTION_SAVE,
 
546
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
547
                                           gtk.STOCK_CANCEL,
 
548
                                           gtk.RESPONSE_CANCEL,))
 
549
        d.set_current_name(osutils.basename(self.path))
 
550
        try:
 
551
            try:
 
552
                result = d.run()
 
553
                if result != gtk.RESPONSE_OK:
 
554
                    raise SelectCancelled()
 
555
                uri = d.get_uri()
 
556
            finally:
 
557
                d.destroy()
 
558
        except SelectCancelled:
 
559
            return
 
560
        source = open(self.path, 'rb')
 
561
        try:
 
562
            target = open(urlutils.local_path_from_url(uri), 'wb')
 
563
            try:
 
564
                target.write(source.read())
 
565
            finally:
 
566
                target.close()
 
567
        finally:
 
568
            source.close()
 
569
 
 
570
 
 
571
def iter_changes_to_status(source, target):
386
572
    """Determine the differences between trees.
387
573
 
388
 
    This is a wrapper around _iter_changes which just yields more
 
574
    This is a wrapper around iter_changes which just yields more
389
575
    understandable results.
390
576
 
391
577
    :param source: The source tree (basis tree)
407
593
        source.lock_read()
408
594
        try:
409
595
            for (file_id, paths, changed_content, versioned, parent_ids, names,
410
 
                 kinds, executables) in target._iter_changes(source):
 
596
                 kinds, executables) in target.iter_changes(source):
411
597
 
412
598
                # Skip the root entry if it isn't very interesting
413
599
                if parent_ids == (None, None):