/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-04-07 20:34:51 UTC
  • mfrom: (450.6.13 bugs)
  • mto: (463.2.1 bug.78765)
  • mto: This revision was merged to the branch mainline in revision 462.
  • Revision ID: daniel.schierbeck@gmail.com-20080407203451-2i6el7jf9t0k9y64
Merged bug page improvements.

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='',
290
320
        column.add_attribute(cell, "text", 0)
291
321
        self.treeview.append_column(column)
292
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
        """
293
328
        # The diffs of the  selected file: a scrollable source or
294
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
        """
295
348
        self.diff_view = DiffView()
296
349
        self.pack2(self.diff_view)
297
350
        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
351
        self.diff_view.set_trees(rev_tree, parent_tree)
306
352
        self.rev_tree = rev_tree
307
353
        self.parent_tree = parent_tree
335
381
        self.treeview.expand_all()
336
382
 
337
383
    def set_file(self, file_path):
 
384
        """Select the current file to display"""
338
385
        tv_path = None
339
386
        for data in self.model:
340
387
            for child in data.iterchildren():
381
428
 
382
429
    def construct(self):
383
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)
384
437
        self.diff = DiffWidget()
385
 
        self.add(self.diff)
 
438
        self.vbox.add(self.diff)
386
439
        self.diff.show_all()
387
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
 
388
457
    def set_diff(self, description, rev_tree, parent_tree):
389
458
        """Set the differences showed by this window.
390
459
 
398
467
        self.diff.set_file(file_path)
399
468
 
400
469
 
401
 
def _iter_changes_to_status(source, target):
 
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):
402
572
    """Determine the differences between trees.
403
573
 
404
 
    This is a wrapper around _iter_changes which just yields more
 
574
    This is a wrapper around iter_changes which just yields more
405
575
    understandable results.
406
576
 
407
577
    :param source: The source tree (basis tree)
423
593
        source.lock_read()
424
594
        try:
425
595
            for (file_id, paths, changed_content, versioned, parent_ids, names,
426
 
                 kinds, executables) in target._iter_changes(source):
 
596
                 kinds, executables) in target.iter_changes(source):
427
597
 
428
598
                # Skip the root entry if it isn't very interesting
429
599
                if parent_ids == (None, None):