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
"""Graphical support for Bazaar using GTK.
18
commit-notify Start the graphical notifier of commits.
19
gannotate GTK+ annotate.
20
gbranch GTK+ branching.
21
gcheckout GTK+ checkout.
22
gcommit GTK+ commit dialog
23
gconflicts GTK+ conflicts.
24
gdiff Show differences in working tree in a GTK+ Window.
25
ginit Initialise a new branch.
26
gmissing GTK+ missing revisions dialog.
27
gpreferences GTK+ preferences dialog.
29
gstatus GTK+ status dialog
30
gtags Manage branch tags.
31
visualise Graphically visualise this branch.
36
version_info = (0, 92, 0, 'dev', 0)
38
if version_info[3] == 'final':
39
version_string = '%d.%d.%d' % version_info[:3]
41
version_string = '%d.%d.%d%s%d' % version_info
42
__version__ = version_string
44
def check_bzrlib_version(desired):
45
"""Check that bzrlib is compatible.
47
If version is < bzr-gtk version, assume incompatible.
48
If version == bzr-gtk version, assume completely compatible
49
If version == bzr-gtk version + 1, assume compatible, with deprecations
50
Otherwise, assume incompatible.
52
desired_plus = (desired[0], desired[1]+1)
53
bzrlib_version = bzrlib.version_info[:2]
54
if bzrlib_version == desired or (bzrlib_version == desired_plus and
55
bzrlib.version_info[3] == 'dev'):
58
from bzrlib.trace import warning
60
# get the message out any way we can
61
from warnings import warn as warning
62
if bzrlib_version < desired:
63
from bzrlib.errors import BzrError
64
warning('Installed Bazaar version %s is too old to be used with bzr-gtk'
65
' %s.' % (bzrlib.__version__, __version__))
66
raise BzrError('Version mismatch: %r, %r' % (version_info, bzrlib.version_info) )
68
warning('bzr-gtk is not up to date with installed bzr version %s.'
69
' \nThere should be a newer version available, e.g. %i.%i.'
70
% (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
73
if version_info[2] == "final":
74
check_bzrlib_version(version_info[:2])
76
from bzrlib.trace import warning
77
if __name__ != 'bzrlib.plugins.gtk':
78
warning("Not running as bzrlib.plugins.gtk, things may break.")
80
from bzrlib.lazy_import import lazy_import
81
lazy_import(globals(), """
90
from bzrlib.commands import Command, register_command, display_command
91
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
92
from bzrlib.option import Option
100
raise errors.BzrCommandError("PyGTK not installed.")
105
def set_ui_factory():
107
from ui import GtkUIFactory
109
bzrlib.ui.ui_factory = GtkUIFactory()
113
return os.path.dirname(__file__)
116
class GTKCommand(Command):
117
"""Abstract class providing GTK specific run commands."""
119
def open_display(self):
120
pygtk = import_pygtk()
123
except RuntimeError, e:
124
if str(e) == "could not open display":
131
dialog = self.get_gtk_dialog(os.path.abspath('.'))
135
class cmd_gbranch(GTKCommand):
140
def get_gtk_dialog(self, path):
141
from bzrlib.plugins.gtk.branch import BranchDialog
142
return BranchDialog(path)
145
class cmd_gcheckout(GTKCommand):
150
def get_gtk_dialog(self, path):
151
from bzrlib.plugins.gtk.checkout import CheckoutDialog
152
return CheckoutDialog(path)
156
class cmd_gpush(GTKCommand):
160
takes_args = [ "location?" ]
162
def run(self, location="."):
163
(br, path) = branch.Branch.open_containing(location)
165
from push import PushDialog
166
dialog = PushDialog(br.repository, br.last_revision(), br)
171
class cmd_gdiff(GTKCommand):
172
"""Show differences in working tree in a GTK+ Window.
174
Otherwise, all changes for the tree are listed.
176
takes_args = ['filename?']
177
takes_options = ['revision']
180
def run(self, revision=None, filename=None):
182
wt = workingtree.WorkingTree.open_containing(".")[0]
186
if revision is not None:
187
if len(revision) == 1:
189
revision_id = revision[0].in_history(branch).rev_id
190
tree2 = branch.repository.revision_tree(revision_id)
191
elif len(revision) == 2:
192
revision_id_0 = revision[0].in_history(branch).rev_id
193
tree2 = branch.repository.revision_tree(revision_id_0)
194
revision_id_1 = revision[1].in_history(branch).rev_id
195
tree1 = branch.repository.revision_tree(revision_id_1)
198
tree2 = tree1.basis_tree()
200
from diff import DiffWindow
202
window = DiffWindow()
203
window.connect("destroy", gtk.main_quit)
204
window.set_diff("Working Tree", tree1, tree2)
205
if filename is not None:
206
tree_filename = wt.relpath(filename)
208
window.set_file(tree_filename)
210
if (tree1.path2id(tree_filename) is None and
211
tree2.path2id(tree_filename) is None):
212
raise NotVersionedError(filename)
213
raise BzrCommandError('No changes found for file "%s"' %
222
def start_viz_window(branch, revision, limit=None):
223
"""Start viz on branch with revision revision.
225
:return: The viz window object.
227
from viz.branchwin import BranchWindow
229
pp.set_branch(branch, revision, limit)
230
# cleanup locks when the window is closed
234
class cmd_visualise(Command):
235
"""Graphically visualise this branch.
237
Opens a graphical window to allow you to see the history of the branch
238
and relationships between revisions in a visual manner,
240
The default starting point is latest revision on the branch, you can
241
specify a starting point with -r revision.
245
Option('limit', "Maximum number of revisions to display.",
247
takes_args = [ "location?" ]
248
aliases = [ "visualize", "vis", "viz" ]
250
def run(self, location=".", revision=None, limit=None):
252
(br, path) = branch.Branch.open_containing(location)
256
revid = br.last_revision()
260
(revno, revid) = revision[0].in_history(br)
263
pp = start_viz_window(br, revid, limit)
264
pp.connect("destroy", lambda w: gtk.main_quit())
271
class cmd_gannotate(GTKCommand):
274
Browse changes to FILENAME line by line in a GTK+ window.
277
takes_args = ["filename", "line?"]
279
Option("all", help="Show annotations on all lines."),
280
Option("plain", help="Don't highlight annotation lines."),
281
Option("line", type=int, argname="lineno",
282
help="Jump to specified line number."),
285
aliases = ["gblame", "gpraise"]
287
def run(self, filename, all=False, plain=False, line='1', revision=None):
288
gtk = self.open_display()
293
raise BzrCommandError('Line argument ("%s") is not a number.' %
296
from annotate.gannotate import GAnnotateWindow
297
from annotate.config import GAnnotateConfig
298
from bzrlib.bzrdir import BzrDir
300
wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
304
tree = br.basis_tree()
306
file_id = tree.path2id(path)
309
raise NotVersionedError(filename)
310
if revision is not None:
311
if len(revision) != 1:
312
raise BzrCommandError("Only 1 revion may be specified.")
313
revision_id = revision[0].in_history(br).rev_id
314
tree = br.repository.revision_tree(revision_id)
316
revision_id = getattr(tree, 'get_revision_id', lambda: None)()
318
window = GAnnotateWindow(all, plain)
319
window.connect("destroy", lambda w: gtk.main_quit())
320
window.set_title(path + " - gannotate")
321
config = GAnnotateConfig(window)
327
window.annotate(tree, br, file_id)
328
window.jump_to_line(line)
337
class cmd_gcommit(GTKCommand):
338
"""GTK+ commit dialog
340
Graphical user interface for committing revisions"""
346
def run(self, filename=None):
349
from commit import CommitDialog
350
from bzrlib.errors import (BzrCommandError,
357
(wt, path) = workingtree.WorkingTree.open_containing(filename)
359
except NoWorkingTree, e:
361
(br, path) = branch.Branch.open_containing(path)
363
commit = CommitDialog(wt, path, not br)
368
class cmd_gstatus(GTKCommand):
369
"""GTK+ status dialog
371
Graphical user interface for showing status
375
takes_args = ['PATH?']
378
def run(self, path='.'):
380
gtk = self.open_display()
381
from status import StatusDialog
382
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
383
status = StatusDialog(wt, wt_path)
384
status.connect("destroy", gtk.main_quit)
389
class cmd_gconflicts(GTKCommand):
392
Select files from the list of conflicts and run an external utility to
396
(wt, path) = workingtree.WorkingTree.open_containing('.')
398
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
399
dialog = ConflictsDialog(wt)
404
class cmd_gpreferences(GTKCommand):
405
""" GTK+ preferences dialog.
410
from bzrlib.plugins.gtk.preferences import PreferencesWindow
411
dialog = PreferencesWindow()
416
class cmd_gmissing(Command):
417
""" GTK+ missing revisions dialog.
420
takes_args = ["other_branch?"]
421
def run(self, other_branch=None):
422
pygtk = import_pygtk()
425
except RuntimeError, e:
426
if str(e) == "could not open display":
429
from bzrlib.plugins.gtk.missing import MissingWindow
430
from bzrlib.branch import Branch
432
local_branch = Branch.open_containing(".")[0]
433
if other_branch is None:
434
other_branch = local_branch.get_parent()
436
if other_branch is None:
437
raise errors.BzrCommandError("No peer location known or specified.")
438
remote_branch = Branch.open_containing(other_branch)[0]
440
local_branch.lock_read()
442
remote_branch.lock_read()
444
dialog = MissingWindow(local_branch, remote_branch)
447
remote_branch.unlock()
449
local_branch.unlock()
452
class cmd_ginit(GTKCommand):
455
from initialize import InitDialog
456
dialog = InitDialog(os.path.abspath(os.path.curdir))
460
class cmd_gtags(GTKCommand):
462
br = branch.Branch.open_containing('.')[0]
464
gtk = self.open_display()
465
from tags import TagsWindow
466
window = TagsWindow(br)
488
register_command(cmd)
491
class cmd_commit_notify(GTKCommand):
492
"""Run the bzr commit notifier.
494
This is a background program which will pop up a notification on the users
495
screen when a commit occurs.
499
from notify import NotifyPopupMenu
500
gtk = self.open_display()
501
menu = NotifyPopupMenu()
502
icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
503
icon.connect('popup-menu', menu.display)
509
from bzrlib.bzrdir import BzrDir
510
from bzrlib import errors
511
from bzrlib.osutils import format_date
512
from bzrlib.transport import get_transport
513
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
515
from bzrlib.plugins.dbus import activity
516
bus = dbus.SessionBus()
517
# get the object so we can subscribe to callbacks from it.
518
broadcast_service = bus.get_object(
519
activity.Broadcast.DBUS_NAME,
520
activity.Broadcast.DBUS_PATH)
522
def catch_branch(revision_id, urls):
523
# TODO: show all the urls, or perhaps choose the 'best'.
526
if isinstance(revision_id, unicode):
527
revision_id = revision_id.encode('utf8')
528
transport = get_transport(url)
529
a_dir = BzrDir.open_from_transport(transport)
530
branch = a_dir.open_branch()
531
revno = branch.revision_id_to_revno(revision_id)
532
revision = branch.repository.get_revision(revision_id)
533
summary = 'New revision %d in %s' % (revno, url)
534
body = 'Committer: %s\n' % revision.committer
535
body += 'Date: %s\n' % format_date(revision.timestamp,
538
body += revision.message
539
body = cgi.escape(body)
540
nw = pynotify.Notification(summary, body)
541
def start_viz(notification=None, action=None, data=None):
542
"""Start the viz program."""
543
pp = start_viz_window(branch, revision_id)
545
def start_branch(notification=None, action=None, data=None):
546
"""Start a Branch dialog"""
547
from bzrlib.plugins.gtk.branch import BranchDialog
548
bd = BranchDialog(remote_path=url)
550
nw.add_action("inspect", "Inspect", start_viz, None)
551
nw.add_action("branch", "Branch", start_branch, None)
557
broadcast_service.connect_to_signal("Revision", catch_branch,
558
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
559
pynotify.init("bzr commit-notify")
562
register_command(cmd_commit_notify)
565
class cmd_gselftest(GTKCommand):
566
"""Version of selftest that displays a notification at the end"""
568
takes_args = builtins.cmd_selftest.takes_args
569
takes_options = builtins.cmd_selftest.takes_options
570
_see_also = ['selftest']
572
def run(self, *args, **kwargs):
575
default_encoding = sys.getdefaultencoding()
576
# prevent gtk from blowing up later
578
# prevent gtk from messing with default encoding
580
if sys.getdefaultencoding() != default_encoding:
582
sys.setdefaultencoding(default_encoding)
583
result = builtins.cmd_selftest().run(*args, **kwargs)
586
body = 'Selftest succeeded in "%s"' % os.getcwd()
589
body = 'Selftest failed in "%s"' % os.getcwd()
590
pynotify.init("bzr gselftest")
591
note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
592
note.set_timeout(pynotify.EXPIRES_NEVER)
596
register_command(cmd_gselftest)
600
gettext.install('olive-gtk')
603
class NoDisplayError(BzrCommandError):
604
"""gtk could not find a proper display"""
607
return "No DISPLAY. Unable to run GTK+ application."
611
from unittest import TestSuite
614
default_encoding = sys.getdefaultencoding()
617
result.addTest(tests.test_suite())
619
if sys.getdefaultencoding() != default_encoding:
621
sys.setdefaultencoding(default_encoding)