/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: 2008-01-13 14:12:49 UTC
  • mto: (423.1.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 429.
  • Revision ID: daniel.schierbeck@gmail.com-20080113141249-gd0i2lknr3yik55r
Moved branch view to its own package.

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 (
34
 
    merge as _mod_merge,
35
 
    osutils,
36
 
    progress,
37
 
    urlutils,
38
 
    workingtree,
39
 
)
 
33
from bzrlib import osutils
40
34
from bzrlib.diff import show_diff_trees, internal_diff
41
35
from bzrlib.errors import NoSuchFile
42
 
from bzrlib.patches import parse_patches
43
36
from bzrlib.trace import warning
44
37
from bzrlib.plugins.gtk.window import Window
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"""
 
38
 
 
39
 
 
40
class DiffView(gtk.ScrolledWindow):
 
41
    """This is the soft and chewy filling for a DiffWindow."""
55
42
 
56
43
    def __init__(self):
57
44
        gtk.ScrolledWindow.__init__(self)
 
45
 
58
46
        self.construct()
59
 
        self._diffs = {}
 
47
        self.rev_tree = None
 
48
        self.parent_tree = None
60
49
 
61
50
    def construct(self):
62
51
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
145
134
 
146
135
            lang.set_tag_style(tag_id, style)
147
136
 
148
 
    @classmethod
149
 
    def apply_colordiff_colors(klass, lang):
 
137
    @staticmethod
 
138
    def apply_colordiff_colors(lang):
150
139
        """Set style colors for lang using the colordiff configuration file.
151
140
 
152
141
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
163
152
                except IOError, e:
164
153
                    warning('could not open file %s: %s' % (f, str(e)))
165
154
                else:
166
 
                    colors.update(klass.parse_colordiffrc(f))
 
155
                    colors.update(DiffView.parse_colordiffrc(f))
167
156
                    f.close()
168
157
 
169
158
        if not colors:
227
216
#        self.parent_tree.lock_read()
228
217
#        self.rev_tree.lock_read()
229
218
#        try:
230
 
#            self.delta = iter_changes_to_status(self.parent_tree, self.rev_tree)
 
219
#            self.delta = _iter_changes_to_status(self.parent_tree, self.rev_tree)
231
220
#            self.path_to_status = {}
232
221
#            self.path_to_diff = {}
233
222
#            source_inv = self.parent_tree.inventory
248
237
#            self.parent_tree.unlock()
249
238
 
250
239
    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"""
270
240
        s = StringIO()
271
241
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
272
242
                        old_label='', new_label='',
291
261
        self.buffer.set_text(decoded.encode('UTF-8'))
292
262
 
293
263
 
294
 
class DiffWidget(gtk.HPaned):
295
 
    """Diff widget
 
264
class DiffWindow(Window):
 
265
    """Diff window.
296
266
 
 
267
    This object represents and manages a single window containing the
 
268
    differences between two revisions on a branch.
297
269
    """
298
 
    def __init__(self):
299
 
        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()
300
293
 
301
294
        # The file hierarchy: a scrollable treeview
302
295
        scrollwin = gtk.ScrolledWindow()
303
296
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
304
297
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
305
 
        self.pack1(scrollwin)
 
298
        pane.pack1(scrollwin)
306
299
        scrollwin.show()
307
300
 
308
301
        self.model = gtk.TreeStore(str, str)
320
313
        column.add_attribute(cell, "text", 0)
321
314
        self.treeview.append_column(column)
322
315
 
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
 
        """
328
316
        # The diffs of the  selected file: a scrollable source or
329
317
        # 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
 
        """
348
318
        self.diff_view = DiffView()
349
 
        self.pack2(self.diff_view)
 
319
        pane.pack2(self.diff_view)
350
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
        """
351
328
        self.diff_view.set_trees(rev_tree, parent_tree)
352
329
        self.rev_tree = rev_tree
353
330
        self.parent_tree = parent_tree
379
356
                self.model.append(titer, [ path, path ])
380
357
 
381
358
        self.treeview.expand_all()
 
359
        self.set_title(description + " - bzrk diff")
382
360
 
383
361
    def set_file(self, file_path):
384
 
        """Select the current file to display"""
385
362
        tv_path = None
386
363
        for data in self.model:
387
364
            for child in data.iterchildren():
405
382
        self.diff_view.show_diff(specific_files)
406
383
 
407
384
 
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):
 
385
def _iter_changes_to_status(source, target):
572
386
    """Determine the differences between trees.
573
387
 
574
 
    This is a wrapper around iter_changes which just yields more
 
388
    This is a wrapper around _iter_changes which just yields more
575
389
    understandable results.
576
390
 
577
391
    :param source: The source tree (basis tree)
593
407
        source.lock_read()
594
408
        try:
595
409
            for (file_id, paths, changed_content, versioned, parent_ids, names,
596
 
                 kinds, executables) in target.iter_changes(source):
 
410
                 kinds, executables) in target._iter_changes(source):
597
411
 
598
412
                # Skip the root entry if it isn't very interesting
599
413
                if parent_ids == (None, None):