/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: Vincent Ladeuil
  • Date: 2008-05-05 18:16:46 UTC
  • mto: (487.1.1 gtk)
  • mto: This revision was merged to the branch mainline in revision 490.
  • Revision ID: v.ladeuil+lp@free.fr-20080505181646-n95l8ltw2u6jtr26
Fix bug #187283 fix replacing _() by _i18n().

* genpot.sh 
Remove duplication. Add the ability to specify the genrated pot
file on command-line for debugging purposes.

* po/olive-gtk.pot:
Regenerated.

* __init__.py, branch.py, branchview/treeview.py, checkout.py,
commit.py, conflicts.py, diff.py, errors.py, initialize.py,
merge.py, nautilus-bzr.py, olive/__init__.py, olive/add.py,
olive/bookmark.py, olive/guifiles.py, olive/info.py,
olive/menu.py, olive/mkdir.py, olive/move.py, olive/remove.py,
olive/rename.py, push.py, revbrowser.py, status.py, tags.py:
Replace all calls to _() by calls to _i18n(), the latter being
defined in __init__.py and imported in the other modules from
there. This fix the problem encountered countless times when
running bzr selftest and getting silly error messages about
boolean not being callables.

Show diffs side-by-side

added added

removed removed

Lines of Context:
328
328
        """
329
329
        # The diffs of the  selected file: a scrollable source or
330
330
        # text view
331
 
 
332
 
    def set_diff_text_sections(self, sections):
333
331
        self.diff_view = DiffFileView()
334
332
        self.diff_view.show()
335
333
        self.pack2(self.diff_view)
336
 
        for oldname, newname, patch in sections:
 
334
        self.model.append(None, [ "Complete Diff", "" ])
 
335
        self.diff_view._diffs[None] = ''.join(lines)
 
336
        for patch in parse_patches(lines):
 
337
            oldname = patch.oldname.split('\t')[0]
 
338
            newname = patch.newname.split('\t')[0]
 
339
            self.model.append(None, [oldname, newname])
337
340
            self.diff_view._diffs[newname] = str(patch)
338
 
            if newname is None:
339
 
                newname = ''
340
 
            self.model.append(None, [oldname, newname])
341
341
        self.diff_view.show_diff(None)
342
342
 
343
343
    def set_diff(self, rev_tree, parent_tree):
413
413
    differences between two revisions on a branch.
414
414
    """
415
415
 
416
 
    def __init__(self, parent=None, operations=None):
 
416
    def __init__(self, parent=None):
417
417
        Window.__init__(self, parent)
418
418
        self.set_border_width(0)
419
419
        self.set_title("bzrk diff")
424
424
        width = int(monitor.width * 0.66)
425
425
        height = int(monitor.height * 0.66)
426
426
        self.set_default_size(width, height)
427
 
        self.construct(operations)
428
 
 
429
 
    def construct(self, operations):
 
427
 
 
428
        self.construct()
 
429
 
 
430
    def construct(self):
430
431
        """Construct the window contents."""
431
432
        self.vbox = gtk.VBox()
432
433
        self.add(self.vbox)
433
434
        self.vbox.show()
434
 
        hbox = self._get_button_bar(operations)
 
435
        hbox = self._get_button_bar()
435
436
        if hbox is not None:
436
437
            self.vbox.pack_start(hbox, expand=False, fill=True)
437
438
        self.diff = DiffWidget()
438
439
        self.vbox.add(self.diff)
439
440
        self.diff.show_all()
440
441
 
441
 
    def _get_button_bar(self, operations):
 
442
    def _get_button_bar(self):
442
443
        """Return a button bar to use.
443
444
 
444
445
        :return: None, meaning that no button bar will be used.
445
446
        """
446
 
        if operations is None:
447
 
            return None
 
447
        return None
 
448
 
 
449
    def set_diff_text(self, description, lines):
 
450
        """Set the diff from a text.
 
451
 
 
452
        The diff must be in unified diff format, and will be parsed to
 
453
        determine filenames.
 
454
        """
 
455
        self.diff.set_diff_text(lines)
 
456
        self.set_title(description + " - bzrk diff")
 
457
 
 
458
    def set_diff(self, description, rev_tree, parent_tree):
 
459
        """Set the differences showed by this window.
 
460
 
 
461
        Compares the two trees and populates the window with the
 
462
        differences.
 
463
        """
 
464
        self.diff.set_diff(rev_tree, parent_tree)
 
465
        self.set_title(description + " - bzrk diff")
 
466
 
 
467
    def set_file(self, file_path):
 
468
        self.diff.set_file(file_path)
 
469
 
 
470
 
 
471
class MergeDirectiveWindow(DiffWindow):
 
472
 
 
473
    def __init__(self, directive, path):
 
474
        DiffWindow.__init__(self, None)
 
475
        self._merge_target = None
 
476
        self.directive = directive
 
477
        self.path = path
 
478
 
 
479
    def _get_button_bar(self):
 
480
        """The button bar has only the Merge button"""
 
481
        merge_button = gtk.Button('Merge')
 
482
        merge_button.show()
 
483
        merge_button.set_relief(gtk.RELIEF_NONE)
 
484
        merge_button.connect("clicked", self.perform_merge)
 
485
 
 
486
        save_button = gtk.Button('Save')
 
487
        save_button.show()
 
488
        save_button.set_relief(gtk.RELIEF_NONE)
 
489
        save_button.connect("clicked", self.perform_save)
 
490
 
448
491
        hbox = gtk.HButtonBox()
449
492
        hbox.set_layout(gtk.BUTTONBOX_START)
450
 
        for title, method in operations:
451
 
            merge_button = gtk.Button(title)
452
 
            merge_button.show()
453
 
            merge_button.set_relief(gtk.RELIEF_NONE)
454
 
            merge_button.connect("clicked", method)
455
 
            hbox.pack_start(merge_button, expand=False, fill=True)
 
493
        hbox.pack_start(merge_button, expand=False, fill=True)
 
494
        hbox.pack_start(save_button, expand=False, fill=True)
456
495
        hbox.show()
457
496
        return hbox
458
497
 
459
 
    def _get_merge_target(self):
460
 
        d = gtk.FileChooserDialog('Merge branch', self,
461
 
                                  gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
462
 
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
463
 
                                           gtk.STOCK_CANCEL,
464
 
                                           gtk.RESPONSE_CANCEL,))
465
 
        try:
466
 
            result = d.run()
467
 
            if result != gtk.RESPONSE_OK:
468
 
                raise SelectCancelled()
469
 
            return d.get_current_folder_uri()
470
 
        finally:
471
 
            d.destroy()
472
 
 
473
 
    def _merge_successful(self):
474
 
        # No conflicts found.
475
 
        info_dialog(_i18n('Merge successful'),
476
 
                    _i18n('All changes applied successfully.'))
477
 
 
478
 
    def _conflicts(self):
479
 
        warning_dialog(_i18n('Conflicts encountered'),
480
 
                       _i18n('Please resolve the conflicts manually'
481
 
                             ' before committing.'))
482
 
 
483
 
    def _handle_error(self, e):
484
 
        error_dialog('Error', str(e))
485
 
 
486
 
    def _get_save_path(self, basename):
487
 
        d = gtk.FileChooserDialog('Save As', self,
488
 
                                  gtk.FILE_CHOOSER_ACTION_SAVE,
489
 
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
490
 
                                           gtk.STOCK_CANCEL,
491
 
                                           gtk.RESPONSE_CANCEL,))
492
 
        d.set_current_name(basename)
493
 
        try:
494
 
            result = d.run()
495
 
            if result != gtk.RESPONSE_OK:
496
 
                raise SelectCancelled()
497
 
            return urlutils.local_path_from_url(d.get_uri())
498
 
        finally:
499
 
            d.destroy()
500
 
 
501
 
    def set_diff(self, description, rev_tree, parent_tree):
502
 
        """Set the differences showed by this window.
503
 
 
504
 
        Compares the two trees and populates the window with the
505
 
        differences.
506
 
        """
507
 
        self.diff.set_diff(rev_tree, parent_tree)
508
 
        self.set_title(description + " - bzrk diff")
509
 
 
510
 
    def set_file(self, file_path):
511
 
        self.diff.set_file(file_path)
512
 
 
513
 
 
514
 
class DiffController(object):
515
 
 
516
 
    def __init__(self, path, patch, window=None):
517
 
        self.path = path
518
 
        self.patch = patch
519
 
        if window is None:
520
 
            window = DiffWindow(operations=self._provide_operations())
521
 
            self.initialize_window(window)
522
 
        self.window = window
523
 
 
524
 
    def initialize_window(self, window):
525
 
        window.diff.set_diff_text_sections(self.get_diff_sections())
526
 
        window.set_title(self.path + " - diff")
527
 
 
528
 
    def get_diff_sections(self):
529
 
        yield "Complete Diff", None, ''.join(self.patch)
530
 
        for patch in parse_patches(self.patch):
531
 
            oldname = patch.oldname.split('\t')[0]
532
 
            newname = patch.newname.split('\t')[0]
533
 
            yield oldname, newname, str(patch)
534
 
 
535
 
    def perform_save(self, window):
536
 
        try:
537
 
            save_path = self.window._get_save_path(osutils.basename(self.path))
538
 
        except SelectCancelled:
539
 
            return
540
 
        source = open(self.path, 'rb')
541
 
        try:
542
 
            target = open(save_path, 'wb')
543
 
            try:
544
 
                osutils.pumpfile(source, target)
545
 
            finally:
546
 
                target.close()
547
 
        finally:
548
 
            source.close()
549
 
 
550
 
    def _provide_operations(self):
551
 
        return [('Save', self.perform_save)]
552
 
 
553
 
 
554
 
class MergeDirectiveController(DiffController):
555
 
 
556
 
    def __init__(self, path, directive, window=None):
557
 
        DiffController.__init__(self, path, directive.patch.splitlines(True),
558
 
                                window)
559
 
        self.directive = directive
560
 
        self.merge_target = None
561
 
 
562
 
    def _provide_operations(self):
563
 
        return [('Merge', self.perform_merge), ('Save', self.perform_save)]
564
 
 
565
498
    def perform_merge(self, window):
566
 
        if self.merge_target is None:
567
 
            try:
568
 
                self.merge_target = self.window._get_merge_target()
569
 
            except SelectCancelled:
570
 
                return
571
 
        tree = workingtree.WorkingTree.open(self.merge_target)
 
499
        try:
 
500
            tree = self._get_merge_target()
 
501
        except SelectCancelled:
 
502
            return
572
503
        tree.lock_write()
573
504
        try:
574
505
            try:
579
510
                conflict_count = merger.do_merge()
580
511
                merger.set_pending()
581
512
                if conflict_count == 0:
582
 
                    self.window._merge_successful()
 
513
                    # No conflicts found.
 
514
                    info_dialog(_i18n('Merge successful'),
 
515
                                _i18n('All changes applied successfully.'))
583
516
                else:
584
 
                    self.window._conflicts()
585
517
                    # There are conflicts to be resolved.
586
 
                self.window.destroy()
 
518
                    warning_dialog(_i18n('Conflicts encountered'),
 
519
                                   _i18n('Please resolve the conflicts manually'
 
520
                                         ' before committing.'))
 
521
                self.destroy()
587
522
            except Exception, e:
588
 
                self.window._handle_error(e)
 
523
                error_dialog('Error', str(e))
589
524
        finally:
590
525
            tree.unlock()
591
526
 
 
527
    def _get_merge_target(self):
 
528
        if self._merge_target is not None:
 
529
            return self._merge_target
 
530
        d = gtk.FileChooserDialog('Merge branch', self,
 
531
                                  gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
 
532
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
533
                                           gtk.STOCK_CANCEL,
 
534
                                           gtk.RESPONSE_CANCEL,))
 
535
        try:
 
536
            result = d.run()
 
537
            if result != gtk.RESPONSE_OK:
 
538
                raise SelectCancelled()
 
539
            uri = d.get_current_folder_uri()
 
540
        finally:
 
541
            d.destroy()
 
542
        return workingtree.WorkingTree.open(uri)
 
543
 
 
544
    def perform_save(self, window):
 
545
        d = gtk.FileChooserDialog('Save As', self,
 
546
                                  gtk.FILE_CHOOSER_ACTION_SAVE,
 
547
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
548
                                           gtk.STOCK_CANCEL,
 
549
                                           gtk.RESPONSE_CANCEL,))
 
550
        d.set_current_name(osutils.basename(self.path))
 
551
        try:
 
552
            try:
 
553
                result = d.run()
 
554
                if result != gtk.RESPONSE_OK:
 
555
                    raise SelectCancelled()
 
556
                uri = d.get_uri()
 
557
            finally:
 
558
                d.destroy()
 
559
        except SelectCancelled:
 
560
            return
 
561
        source = open(self.path, 'rb')
 
562
        try:
 
563
            target = open(urlutils.local_path_from_url(uri), 'wb')
 
564
            try:
 
565
                target.write(source.read())
 
566
            finally:
 
567
                target.close()
 
568
        finally:
 
569
            source.close()
 
570
 
592
571
 
593
572
def iter_changes_to_status(source, target):
594
573
    """Determine the differences between trees.