1
# This program is free software; you can redistribute it and/or modify
 
 
2
# it under the terms of the GNU General Public License as published by
 
 
3
# the Free Software Foundation; either version 2 of the License, or
 
 
4
# (at your option) any later version.
 
 
6
# This program is distributed in the hope that it will be useful,
 
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
9
# GNU General Public License for more details.
 
 
11
# You should have received a copy of the GNU General Public License
 
 
12
# along with this program; if not, write to the Free Software
 
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
15
"""GTK+ frontends to Bazaar commands """
 
 
19
__version__ = '0.15.2'
 
 
20
version_info = tuple(int(n) for n in __version__.split('.'))
 
 
23
def check_bzrlib_version(desired):
 
 
24
    """Check that bzrlib is compatible.
 
 
26
    If version is < bzr-gtk version, assume incompatible.
 
 
27
    If version == bzr-gtk version, assume completely compatible
 
 
28
    If version == bzr-gtk version + 1, assume compatible, with deprecations
 
 
29
    Otherwise, assume incompatible.
 
 
31
    desired_plus = (desired[0], desired[1]+1)
 
 
32
    bzrlib_version = bzrlib.version_info[:2]
 
 
33
    if bzrlib_version == desired:
 
 
36
        from bzrlib.trace import warning
 
 
38
        # get the message out any way we can
 
 
39
        from warnings import warn as warning
 
 
40
    if bzrlib_version < desired:
 
 
41
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
 
 
42
                ' %s.' % (bzrlib.__version__, __version__))
 
 
43
        # Not using BzrNewError, because it may not exist.
 
 
44
        raise Exception, ('Version mismatch', version_info)
 
 
46
        warning('bzr-gtk is not up to date with installed bzr version %s.'
 
 
47
                ' \nThere should be a newer version available, e.g. %i.%i.' 
 
 
48
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
 
 
49
        if bzrlib_version != desired_plus:
 
 
50
            raise Exception, 'Version mismatch'
 
 
53
check_bzrlib_version(version_info[:2])
 
 
55
from bzrlib.trace import warning
 
 
56
if __name__ != 'bzrlib.plugins.gtk':
 
 
57
    warning("Not running as bzrlib.plugins.gtk, things may break.")
 
 
59
from bzrlib.lazy_import import lazy_import
 
 
60
lazy_import(globals(), """
 
 
68
from bzrlib.commands import Command, register_command, display_command
 
 
69
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
 
 
70
from bzrlib.option import Option
 
 
78
        raise errors.BzrCommandError("PyGTK not installed.")
 
 
85
    from ui import GtkUIFactory
 
 
87
    bzrlib.ui.ui_factory = GtkUIFactory()
 
 
90
class GTKCommand(Command):
 
 
91
    """Abstract class providing GTK specific run commands."""
 
 
93
    def open_display(self):
 
 
94
        pygtk = import_pygtk()
 
 
97
        except RuntimeError, e:
 
 
98
            if str(e) == "could not open display":
 
 
105
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
 
 
109
class cmd_gbranch(GTKCommand):
 
 
114
    def get_gtk_dialog(self, path):
 
 
115
        from bzrlib.plugins.gtk.branch import BranchDialog
 
 
116
        return BranchDialog(path)
 
 
119
class cmd_gcheckout(GTKCommand):
 
 
124
    def get_gtk_dialog(self, path):
 
 
125
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
 
126
        return CheckoutDialog(path)
 
 
130
class cmd_gpush(GTKCommand):
 
 
134
    takes_args = [ "location?" ]
 
 
136
    def run(self, location="."):
 
 
137
        (br, path) = branch.Branch.open_containing(location)
 
 
139
        from push import PushDialog
 
 
140
        dialog = PushDialog(br)
 
 
145
class cmd_gdiff(GTKCommand):
 
 
146
    """Show differences in working tree in a GTK+ Window.
 
 
148
    Otherwise, all changes for the tree are listed.
 
 
150
    takes_args = ['filename?']
 
 
151
    takes_options = ['revision']
 
 
154
    def run(self, revision=None, filename=None):
 
 
156
        wt = workingtree.WorkingTree.open_containing(".")[0]
 
 
160
            if revision is not None:
 
 
161
                if len(revision) == 1:
 
 
163
                    revision_id = revision[0].in_history(branch).rev_id
 
 
164
                    tree2 = branch.repository.revision_tree(revision_id)
 
 
165
                elif len(revision) == 2:
 
 
166
                    revision_id_0 = revision[0].in_history(branch).rev_id
 
 
167
                    tree2 = branch.repository.revision_tree(revision_id_0)
 
 
168
                    revision_id_1 = revision[1].in_history(branch).rev_id
 
 
169
                    tree1 = branch.repository.revision_tree(revision_id_1)
 
 
172
                tree2 = tree1.basis_tree()
 
 
174
            from diff import DiffWindow
 
 
176
            window = DiffWindow()
 
 
177
            window.connect("destroy", gtk.main_quit)
 
 
178
            window.set_diff("Working Tree", tree1, tree2)
 
 
179
            if filename is not None:
 
 
180
                tree_filename = wt.relpath(filename)
 
 
182
                    window.set_file(tree_filename)
 
 
184
                    if (tree1.inventory.path2id(tree_filename) is None and 
 
 
185
                        tree2.inventory.path2id(tree_filename) is None):
 
 
186
                        raise NotVersionedError(filename)
 
 
187
                    raise BzrCommandError('No changes found for file "%s"' % 
 
 
196
class cmd_visualise(Command):
 
 
197
    """Graphically visualise this branch.
 
 
199
    Opens a graphical window to allow you to see the history of the branch
 
 
200
    and relationships between revisions in a visual manner,
 
 
202
    The default starting point is latest revision on the branch, you can
 
 
203
    specify a starting point with -r revision.
 
 
207
        Option('limit', "maximum number of revisions to display",
 
 
209
    takes_args = [ "location?" ]
 
 
210
    aliases = [ "visualize", "vis", "viz" ]
 
 
212
    def run(self, location=".", revision=None, limit=None):
 
 
214
        (br, path) = branch.Branch.open_containing(location)
 
 
216
        br.repository.lock_read()
 
 
219
                revid = br.last_revision()
 
 
223
                (revno, revid) = revision[0].in_history(br)
 
 
225
            from viz.branchwin import BranchWindow
 
 
229
            pp.set_branch(br, revid, limit)
 
 
230
            pp.connect("destroy", lambda w: gtk.main_quit())
 
 
234
            br.repository.unlock()
 
 
238
class cmd_gannotate(GTKCommand):
 
 
241
    Browse changes to FILENAME line by line in a GTK+ window.
 
 
244
    takes_args = ["filename", "line?"]
 
 
246
        Option("all", help="show annotations on all lines"),
 
 
247
        Option("plain", help="don't highlight annotation lines"),
 
 
248
        Option("line", type=int, argname="lineno",
 
 
249
               help="jump to specified line number"),
 
 
252
    aliases = ["gblame", "gpraise"]
 
 
254
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
 
255
        gtk = self.open_display()
 
 
260
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
 
 
263
        from annotate.gannotate import GAnnotateWindow
 
 
264
        from annotate.config import GAnnotateConfig
 
 
265
        from bzrlib.bzrdir import BzrDir
 
 
267
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
 
 
271
            tree = br.basis_tree()
 
 
273
        file_id = tree.path2id(path)
 
 
276
            raise NotVersionedError(filename)
 
 
277
        if revision is not None:
 
 
278
            if len(revision) != 1:
 
 
279
                raise BzrCommandError("Only 1 revion may be specified.")
 
 
280
            revision_id = revision[0].in_history(br).rev_id
 
 
281
            tree = br.repository.revision_tree(revision_id)
 
 
283
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
 
 
285
        window = GAnnotateWindow(all, plain)
 
 
286
        window.connect("destroy", lambda w: gtk.main_quit())
 
 
287
        window.set_title(path + " - gannotate")
 
 
288
        config = GAnnotateConfig(window)
 
 
294
            window.annotate(tree, br, file_id)
 
 
295
            window.jump_to_line(line)
 
 
304
class cmd_gcommit(GTKCommand):
 
 
305
    """GTK+ commit dialog
 
 
307
    Graphical user interface for committing revisions"""
 
 
313
    def run(self, filename=None):
 
 
316
        from commit import CommitDialog
 
 
317
        from bzrlib.errors import (BzrCommandError,
 
 
324
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
 
326
        except NotBranchError, e:
 
 
328
        except NoWorkingTree, e:
 
 
331
                (br, path) = branch.Branch.open_containing(path)
 
 
332
            except NotBranchError, e:
 
 
335
        commit = CommitDialog(wt, path, not br)
 
 
340
class cmd_gstatus(GTKCommand):
 
 
341
    """GTK+ status dialog
 
 
343
    Graphical user interface for showing status 
 
 
347
    takes_args = ['PATH?']
 
 
350
    def run(self, path='.'):
 
 
352
        gtk = self.open_display()
 
 
353
        from status import StatusDialog
 
 
354
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
 
 
355
        status = StatusDialog(wt, wt_path)
 
 
356
        status.connect("destroy", gtk.main_quit)
 
 
361
class cmd_gconflicts(GTKCommand):
 
 
366
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
 
368
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
 
369
        dialog = ConflictsDialog(wt)
 
 
374
class cmd_gpreferences(GTKCommand):
 
 
375
    """ GTK+ preferences dialog.
 
 
380
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
 
 
381
        dialog = PreferencesWindow()
 
 
386
class cmd_gmissing(Command):
 
 
387
    """ GTK+ missing revisions dialog.
 
 
390
    takes_args = ["other_branch?"]
 
 
391
    def run(self, other_branch=None):
 
 
392
        pygtk = import_pygtk()
 
 
395
        except RuntimeError, e:
 
 
396
            if str(e) == "could not open display":
 
 
399
        from bzrlib.plugins.gtk.missing import MissingWindow
 
 
400
        from bzrlib.branch import Branch
 
 
402
        local_branch = Branch.open_containing(".")[0]
 
 
403
        if other_branch is None:
 
 
404
            other_branch = local_branch.get_parent()
 
 
406
            if other_branch is None:
 
 
407
                raise errors.BzrCommandError("No peer location known or specified.")
 
 
408
        remote_branch = Branch.open_containing(other_branch)[0]
 
 
410
        local_branch.lock_read()
 
 
412
            remote_branch.lock_read()
 
 
414
                dialog = MissingWindow(local_branch, remote_branch)
 
 
417
                remote_branch.unlock()
 
 
419
            local_branch.unlock()
 
 
437
    register_command(cmd)
 
 
440
class cmd_commit_notify(GTKCommand):
 
 
441
    """Run the bzr commit notifier.
 
 
443
    This is a background program which will pop up a notification on the users
 
 
444
    screen when a commit occurs.
 
 
448
        gtk = self.open_display()
 
 
453
        from bzrlib.bzrdir import BzrDir
 
 
454
        from bzrlib import errors
 
 
455
        from bzrlib.osutils import format_date
 
 
456
        from bzrlib.transport import get_transport
 
 
457
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
 
 
459
        from bzrlib.plugins.dbus import activity
 
 
460
        bus = dbus.SessionBus()
 
 
461
        # get the object so we can subscribe to callbacks from it.
 
 
462
        broadcast_service = bus.get_object(
 
 
463
            activity.Broadcast.DBUS_NAME,
 
 
464
            activity.Broadcast.DBUS_PATH)
 
 
465
        def catch_branch(revision_id, urls):
 
 
466
            # TODO: show all the urls, or perhaps choose the 'best'.
 
 
469
                if isinstance(revision_id, unicode):
 
 
470
                    revision_id = revision_id.encode('utf8')
 
 
471
                transport = get_transport(url)
 
 
472
                a_dir = BzrDir.open_from_transport(transport)
 
 
473
                branch = a_dir.open_branch()
 
 
474
                revno = branch.revision_id_to_revno(revision_id)
 
 
475
                revision = branch.repository.get_revision(revision_id)
 
 
476
                summary = 'New revision %d in %s' % (revno, url)
 
 
477
                body  = 'Committer: %s\n' % revision.committer
 
 
478
                body += 'Date: %s\n' % format_date(revision.timestamp,
 
 
481
                body += revision.message
 
 
482
                body = cgi.escape(body)
 
 
483
                nw = pynotify.Notification(summary, body)
 
 
489
        broadcast_service.connect_to_signal("Revision", catch_branch,
 
 
490
            dbus_interface=activity.Broadcast.DBUS_INTERFACE)
 
 
491
        pynotify.init("bzr commit-notify")
 
 
494
register_command(cmd_commit_notify)
 
 
498
gettext.install('olive-gtk')
 
 
501
class NoDisplayError(BzrCommandError):
 
 
502
    """gtk could not find a proper display"""
 
 
505
        return "No DISPLAY. Unable to run GTK+ application."
 
 
509
    from unittest import TestSuite
 
 
512
    default_encoding = sys.getdefaultencoding()
 
 
515
        result.addTest(tests.test_suite())
 
 
517
        if sys.getdefaultencoding() != default_encoding:
 
 
519
            sys.setdefaultencoding(default_encoding)