/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 __init__.py

  • Committer: Jasper Groenewegen
  • Date: 2008-07-27 12:01:40 UTC
  • mfrom: (576.3.2 improve-merge)
  • mto: This revision was merged to the branch mainline in revision 579.
  • Revision ID: colbrac@xs4all.nl-20080727120140-1agdlzkc9fumjk5f
Merge merge dialog improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
"""Graphical support for Bazaar using GTK.
16
16
 
17
17
This plugin includes:
18
 
commit-notify     Start the graphical notifier of commits.
19
18
gannotate         GTK+ annotate. 
20
19
gbranch           GTK+ branching. 
21
20
gcheckout         GTK+ checkout. 
22
21
gcommit           GTK+ commit dialog.
23
22
gconflicts        GTK+ conflicts. 
24
23
gdiff             Show differences in working tree in a GTK+ Window. 
25
 
ghandle-patch     Display and optionally merge a merge directive or patch.
26
24
ginit             Initialise a new branch.
 
25
gmerge            GTK+ merge dialog
27
26
gmissing          GTK+ missing revisions dialog. 
28
27
gpreferences      GTK+ preferences dialog. 
29
28
gpush             GTK+ push.
37
36
 
38
37
import bzrlib
39
38
 
40
 
version_info = (0, 94, 0, 'dev', 0)
 
39
version_info = (0, 95, 0, 'dev', 1)
41
40
 
42
41
if version_info[3] == 'final':
43
42
    version_string = '%d.%d.%d' % version_info[:3]
105
104
    bzrlib.ui.ui_factory = GtkUIFactory()
106
105
 
107
106
 
108
 
def data_path():
109
 
    return os.path.dirname(__file__)
110
 
 
111
 
 
112
 
def icon_path(*args):
113
 
    basedirs = [os.path.join(data_path()),
 
107
def data_basedirs():
 
108
    return [os.path.dirname(__file__),
114
109
             "/usr/share/bzr-gtk", 
115
110
             "/usr/local/share/bzr-gtk"]
116
 
    for basedir in basedirs:
117
 
        path = os.path.join(basedir, 'icons', *args)
 
111
 
 
112
 
 
113
def data_path(*args):
 
114
    for basedir in data_basedirs():
 
115
        path = os.path.join(basedir, *args)
118
116
        if os.path.exists(path):
119
117
            return path
120
118
    return None
121
119
 
122
120
 
 
121
def icon_path(*args):
 
122
    return data_path(os.path.join('icons', *args))
 
123
 
 
124
 
 
125
def open_display():
 
126
    pygtk = import_pygtk()
 
127
    try:
 
128
        import gtk
 
129
    except RuntimeError, e:
 
130
        if str(e) == "could not open display":
 
131
            raise NoDisplayError
 
132
    set_ui_factory()
 
133
    return gtk
 
134
 
 
135
 
123
136
class GTKCommand(Command):
124
137
    """Abstract class providing GTK specific run commands."""
125
138
 
126
 
    def open_display(self):
127
 
        pygtk = import_pygtk()
128
 
        try:
129
 
            import gtk
130
 
        except RuntimeError, e:
131
 
            if str(e) == "could not open display":
132
 
                raise NoDisplayError
133
 
        set_ui_factory()
134
 
        return gtk
135
 
 
136
139
    def run(self):
137
 
        self.open_display()
 
140
        open_display()
138
141
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
139
142
        dialog.run()
140
143
 
168
171
 
169
172
    def run(self, location="."):
170
173
        (br, path) = branch.Branch.open_containing(location)
171
 
        self.open_display()
 
174
        open_display()
172
175
        from push import PushDialog
173
176
        dialog = PushDialog(br.repository, br.last_revision(), br)
174
177
        dialog.run()
193
196
            if revision is not None:
194
197
                if len(revision) == 1:
195
198
                    tree1 = wt
196
 
                    revision_id = revision[0].in_history(branch).rev_id
 
199
                    revision_id = revision[0].as_revision_id(tree1.branch)
197
200
                    tree2 = branch.repository.revision_tree(revision_id)
198
201
                elif len(revision) == 2:
199
 
                    revision_id_0 = revision[0].in_history(branch).rev_id
 
202
                    revision_id_0 = revision[0].as_revision_id(branch)
200
203
                    tree2 = branch.repository.revision_tree(revision_id_0)
201
 
                    revision_id_1 = revision[1].in_history(branch).rev_id
 
204
                    revision_id_1 = revision[1].as_revision_id(branch)
202
205
                    tree1 = branch.repository.revision_tree(revision_id_1)
203
206
            else:
204
207
                tree1 = wt
261
264
            if revision is None:
262
265
                revids.append(br.last_revision())
263
266
            else:
264
 
                (revno, revid) = revision[0].in_history(br)
265
 
                revids.append(revid)
 
267
                revids.append(revision[0].as_revision_id(br))
266
268
        import gtk
267
269
        pp = start_viz_window(br, revids, limit)
268
270
        pp.connect("destroy", lambda w: gtk.main_quit())
287
289
    aliases = ["gblame", "gpraise"]
288
290
    
289
291
    def run(self, filename, all=False, plain=False, line='1', revision=None):
290
 
        gtk = self.open_display()
 
292
        gtk = open_display()
291
293
 
292
294
        try:
293
295
            line = int(line)
312
314
        if revision is not None:
313
315
            if len(revision) != 1:
314
316
                raise BzrCommandError("Only 1 revion may be specified.")
315
 
            revision_id = revision[0].in_history(br).rev_id
 
317
            revision_id = revision[0].as_revision_id(br)
316
318
            tree = br.repository.revision_tree(revision_id)
317
319
        else:
318
320
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
319
321
 
320
 
        window = GAnnotateWindow(all, plain)
 
322
        window = GAnnotateWindow(all, plain, branch=br)
321
323
        window.connect("destroy", lambda w: gtk.main_quit())
322
324
        config = GAnnotateConfig(window)
323
325
        window.show()
346
348
 
347
349
    def run(self, filename=None):
348
350
        import os
349
 
        self.open_display()
 
351
        open_display()
350
352
        from commit import CommitDialog
351
353
        from bzrlib.errors import (BzrCommandError,
352
354
                                   NotBranchError,
359
361
            br = wt.branch
360
362
        except NoWorkingTree, e:
361
363
            from dialog import error_dialog
362
 
            error_dialog(_('Directory does not have a working tree'),
363
 
                         _('Operation aborted.'))
 
364
            error_dialog(_i18n('Directory does not have a working tree'),
 
365
                         _i18n('Operation aborted.'))
364
366
            return 1 # should this be retval=3?
365
367
 
366
368
        # It is a good habit to keep things locked for the duration, but it
383
385
    
384
386
    aliases = [ "gst" ]
385
387
    takes_args = ['PATH?']
386
 
    takes_options = []
 
388
    takes_options = ['revision']
387
389
 
388
 
    def run(self, path='.'):
 
390
    def run(self, path='.', revision=None):
389
391
        import os
390
 
        gtk = self.open_display()
 
392
        gtk = open_display()
391
393
        from status import StatusDialog
392
394
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
393
 
        status = StatusDialog(wt, wt_path)
 
395
        
 
396
        if revision is not None:
 
397
            try:
 
398
                revision_id = revision[0].as_revision_id(wt.branch)
 
399
            except:
 
400
                from bzrlib.errors import BzrError
 
401
                raise BzrError('Revision %r doesn\'t exist' % revision[0].user_spec )
 
402
        else:
 
403
            revision_id = None
 
404
 
 
405
        status = StatusDialog(wt, wt_path, revision_id)
394
406
        status.connect("destroy", gtk.main_quit)
395
407
        status.run()
396
408
 
401
413
    """
402
414
    def run(self):
403
415
        (br, path) = branch.Branch.open_containing(".")
404
 
        gtk = self.open_display()
 
416
        gtk = open_display()
405
417
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
406
418
        from StringIO import StringIO
407
419
        dialog = SendMergeDirectiveDialog(br)
423
435
    """
424
436
    def run(self):
425
437
        (wt, path) = workingtree.WorkingTree.open_containing('.')
426
 
        self.open_display()
 
438
        open_display()
427
439
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
428
440
        dialog = ConflictsDialog(wt)
429
441
        dialog.run()
434
446
 
435
447
    """
436
448
    def run(self):
437
 
        self.open_display()
 
449
        open_display()
438
450
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
439
451
        dialog = PreferencesWindow()
440
452
        dialog.run()
441
453
 
442
454
 
 
455
class cmd_gmerge(Command):
 
456
    """ GTK+ merge dialog
 
457
    
 
458
    """
 
459
    takes_args = ["merge_from_path?"]
 
460
    def run(self, merge_from_path=None):
 
461
        from bzrlib import workingtree
 
462
        from bzrlib.plugins.gtk.dialog import error_dialog
 
463
        from bzrlib.plugins.gtk.merge import MergeDialog
 
464
        
 
465
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
466
        old_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
 
467
        delta = wt.changes_from(old_tree)
 
468
        if len(delta.added) or len(delta.removed) or len(delta.renamed) or len(delta.modified):
 
469
            error_dialog(_i18n('There are local changes in the branch'),
 
470
                         _i18n('Please commit or revert the changes before merging.'))
 
471
        else:
 
472
            parent_branch_path = wt.branch.get_parent()
 
473
            merge = MergeDialog(wt, path, parent_branch_path)
 
474
            response = merge.run()
 
475
            merge.destroy()
 
476
 
 
477
 
443
478
class cmd_gmissing(Command):
444
479
    """ GTK+ missing revisions dialog.
445
480
 
478
513
 
479
514
class cmd_ginit(GTKCommand):
480
515
    def run(self):
481
 
        self.open_display()
 
516
        open_display()
482
517
        from initialize import InitDialog
483
518
        dialog = InitDialog(os.path.abspath(os.path.curdir))
484
519
        dialog.run()
488
523
    def run(self):
489
524
        br = branch.Branch.open_containing('.')[0]
490
525
        
491
 
        gtk = self.open_display()
 
526
        gtk = open_display()
492
527
        from tags import TagsWindow
493
528
        window = TagsWindow(br)
494
529
        window.show()
503
538
    cmd_gconflicts, 
504
539
    cmd_gdiff,
505
540
    cmd_ginit,
 
541
    cmd_gmerge,
506
542
    cmd_gmissing, 
507
543
    cmd_gpreferences, 
508
544
    cmd_gpush, 
516
552
    register_command(cmd)
517
553
 
518
554
 
519
 
class cmd_commit_notify(GTKCommand):
520
 
    """Run the bzr commit notifier.
521
 
 
522
 
    This is a background program which will pop up a notification on the users
523
 
    screen when a commit occurs.
524
 
    """
525
 
 
526
 
    def run(self):
527
 
        from notify import NotifyPopupMenu
528
 
        gtk = self.open_display()
529
 
        menu = NotifyPopupMenu()
530
 
        icon = gtk.status_icon_new_from_file(icon_path("bzr-icon-64.png"))
531
 
        icon.connect('popup-menu', menu.display)
532
 
 
533
 
        import cgi
534
 
        import dbus
535
 
        import dbus.service
536
 
        import pynotify
537
 
        from bzrlib.bzrdir import BzrDir
538
 
        from bzrlib import errors
539
 
        from bzrlib.osutils import format_date
540
 
        from bzrlib.transport import get_transport
541
 
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
542
 
            import dbus.glib
543
 
        BROADCAST_INTERFACE = "org.bazaarvcs.plugins.dbus.Broadcast"
544
 
        bus = dbus.SessionBus()
545
 
 
546
 
        def catch_branch(revision_id, urls):
547
 
            # TODO: show all the urls, or perhaps choose the 'best'.
548
 
            url = urls[0]
549
 
            try:
550
 
                if isinstance(revision_id, unicode):
551
 
                    revision_id = revision_id.encode('utf8')
552
 
                transport = get_transport(url)
553
 
                a_dir = BzrDir.open_from_transport(transport)
554
 
                branch = a_dir.open_branch()
555
 
                revno = branch.revision_id_to_revno(revision_id)
556
 
                revision = branch.repository.get_revision(revision_id)
557
 
                summary = 'New revision %d in %s' % (revno, url)
558
 
                body  = 'Committer: %s\n' % revision.committer
559
 
                body += 'Date: %s\n' % format_date(revision.timestamp,
560
 
                    revision.timezone)
561
 
                body += '\n'
562
 
                body += revision.message
563
 
                body = cgi.escape(body)
564
 
                nw = pynotify.Notification(summary, body)
565
 
                def start_viz(notification=None, action=None, data=None):
566
 
                    """Start the viz program."""
567
 
                    pp = start_viz_window(branch, revision_id)
568
 
                    pp.show()
569
 
                def start_branch(notification=None, action=None, data=None):
570
 
                    """Start a Branch dialog"""
571
 
                    from bzrlib.plugins.gtk.branch import BranchDialog
572
 
                    bd = BranchDialog(remote_path=url)
573
 
                    bd.run()
574
 
                nw.add_action("inspect", "Inspect", start_viz, None)
575
 
                nw.add_action("branch", "Branch", start_branch, None)
576
 
                nw.set_timeout(5000)
577
 
                nw.show()
578
 
            except Exception, e:
579
 
                print e
580
 
                raise
581
 
        bus.add_signal_receiver(catch_branch,
582
 
                                dbus_interface=BROADCAST_INTERFACE,
583
 
                                signal_name="Revision")
584
 
        pynotify.init("bzr commit-notify")
585
 
        gtk.main()
586
 
 
587
 
register_command(cmd_commit_notify)
588
 
 
589
 
 
590
555
class cmd_gselftest(GTKCommand):
591
556
    """Version of selftest that displays a notification at the end"""
592
557
 
683
648
register_command(cmd_test_gtk)
684
649
 
685
650
 
686
 
class cmd_ghandle_patch(GTKCommand):
687
 
    """Display a patch or merge directive, possibly merging.
688
 
 
689
 
    This is a helper, meant to be launched from other programs like browsers
690
 
    or email clients.  Since these programs often do not allow parameters to
691
 
    be provided, a "handle-patch" script is included.
692
 
    """
693
 
 
694
 
    takes_args = ['path']
695
 
 
696
 
    def run(self, path):
697
 
        try:
698
 
            from bzrlib.plugins.gtk.diff import (DiffWindow,
699
 
                                                 MergeDirectiveWindow)
700
 
            lines = open(path, 'rb').readlines()
701
 
            lines = [l.replace('\r\n', '\n') for l in lines]
702
 
            try:
703
 
                directive = merge_directive.MergeDirective.from_lines(lines)
704
 
            except errors.NotAMergeDirective:
705
 
                window = DiffWindow()
706
 
                window.set_diff_text(path, lines)
707
 
            else:
708
 
                window = MergeDirectiveWindow(directive, path)
709
 
                window.set_diff_text(path, directive.patch.splitlines(True))
710
 
            window.show()
711
 
            gtk = self.open_display()
712
 
            window.connect("destroy", gtk.main_quit)
713
 
        except Exception, e:
714
 
            from dialog import error_dialog
715
 
            error_dialog('Error', str(e))
716
 
            raise
717
 
        gtk.main()
718
 
 
719
 
 
720
 
register_command(cmd_ghandle_patch)
721
 
 
722
651
 
723
652
import gettext
724
653
gettext.install('olive-gtk')
725
654
 
 
655
# Let's create a specialized alias to protect '_' from being erased by other
 
656
# uses of '_' as an anonymous variable (think pdb for one).
 
657
_i18n = gettext.gettext
726
658
 
727
659
class NoDisplayError(BzrCommandError):
728
660
    """gtk could not find a proper display"""