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
228
return BranchWindow(branch, revision, limit)
231
class cmd_visualise(Command):
232
"""Graphically visualise this branch.
234
Opens a graphical window to allow you to see the history of the branch
235
and relationships between revisions in a visual manner,
237
The default starting point is latest revision on the branch, you can
238
specify a starting point with -r revision.
242
Option('limit', "Maximum number of revisions to display.",
244
takes_args = [ "location?" ]
245
aliases = [ "visualize", "vis", "viz" ]
247
def run(self, location=".", revision=None, limit=None):
249
(br, path) = branch.Branch.open_containing(location)
253
revid = br.last_revision()
257
(revno, revid) = revision[0].in_history(br)
260
pp = start_viz_window(br, revid, limit)
261
pp.connect("destroy", lambda w: gtk.main_quit())
268
class cmd_gannotate(GTKCommand):
271
Browse changes to FILENAME line by line in a GTK+ window.
274
takes_args = ["filename", "line?"]
276
Option("all", help="Show annotations on all lines."),
277
Option("plain", help="Don't highlight annotation lines."),
278
Option("line", type=int, argname="lineno",
279
help="Jump to specified line number."),
282
aliases = ["gblame", "gpraise"]
284
def run(self, filename, all=False, plain=False, line='1', revision=None):
285
gtk = self.open_display()
290
raise BzrCommandError('Line argument ("%s") is not a number.' %
293
from annotate.gannotate import GAnnotateWindow
294
from annotate.config import GAnnotateConfig
295
from bzrlib.bzrdir import BzrDir
297
wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
301
tree = br.basis_tree()
303
file_id = tree.path2id(path)
306
raise NotVersionedError(filename)
307
if revision is not None:
308
if len(revision) != 1:
309
raise BzrCommandError("Only 1 revion may be specified.")
310
revision_id = revision[0].in_history(br).rev_id
311
tree = br.repository.revision_tree(revision_id)
313
revision_id = getattr(tree, 'get_revision_id', lambda: None)()
315
window = GAnnotateWindow(all, plain)
316
window.connect("destroy", lambda w: gtk.main_quit())
317
window.set_title(path + " - gannotate")
318
config = GAnnotateConfig(window)
324
window.annotate(tree, br, file_id)
325
window.jump_to_line(line)
334
class cmd_gcommit(GTKCommand):
335
"""GTK+ commit dialog
337
Graphical user interface for committing revisions"""
343
def run(self, filename=None):
346
from commit import CommitDialog
347
from bzrlib.errors import (BzrCommandError,
354
(wt, path) = workingtree.WorkingTree.open_containing(filename)
356
except NoWorkingTree, e:
358
(br, path) = branch.Branch.open_containing(path)
360
commit = CommitDialog(wt, path, not br)
365
class cmd_gstatus(GTKCommand):
366
"""GTK+ status dialog
368
Graphical user interface for showing status
372
takes_args = ['PATH?']
375
def run(self, path='.'):
377
gtk = self.open_display()
378
from status import StatusDialog
379
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
380
status = StatusDialog(wt, wt_path)
381
status.connect("destroy", gtk.main_quit)
386
class cmd_gconflicts(GTKCommand):
389
Select files from the list of conflicts and run an external utility to
393
(wt, path) = workingtree.WorkingTree.open_containing('.')
395
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
396
dialog = ConflictsDialog(wt)
401
class cmd_gpreferences(GTKCommand):
402
""" GTK+ preferences dialog.
407
from bzrlib.plugins.gtk.preferences import PreferencesWindow
408
dialog = PreferencesWindow()
413
class cmd_gmissing(Command):
414
""" GTK+ missing revisions dialog.
417
takes_args = ["other_branch?"]
418
def run(self, other_branch=None):
419
pygtk = import_pygtk()
422
except RuntimeError, e:
423
if str(e) == "could not open display":
426
from bzrlib.plugins.gtk.missing import MissingWindow
427
from bzrlib.branch import Branch
429
local_branch = Branch.open_containing(".")[0]
430
if other_branch is None:
431
other_branch = local_branch.get_parent()
433
if other_branch is None:
434
raise errors.BzrCommandError("No peer location known or specified.")
435
remote_branch = Branch.open_containing(other_branch)[0]
437
local_branch.lock_read()
439
remote_branch.lock_read()
441
dialog = MissingWindow(local_branch, remote_branch)
444
remote_branch.unlock()
446
local_branch.unlock()
449
class cmd_ginit(GTKCommand):
452
from initialize import InitDialog
453
dialog = InitDialog(os.path.abspath(os.path.curdir))
457
class cmd_gtags(GTKCommand):
459
br = branch.Branch.open_containing('.')[0]
461
gtk = self.open_display()
462
from tags import TagsWindow
463
window = TagsWindow(br)
485
register_command(cmd)
488
class cmd_commit_notify(GTKCommand):
489
"""Run the bzr commit notifier.
491
This is a background program which will pop up a notification on the users
492
screen when a commit occurs.
496
from notify import NotifyPopupMenu
497
gtk = self.open_display()
498
menu = NotifyPopupMenu()
499
icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
500
icon.connect('popup-menu', menu.display)
506
from bzrlib.bzrdir import BzrDir
507
from bzrlib import errors
508
from bzrlib.osutils import format_date
509
from bzrlib.transport import get_transport
510
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
512
from bzrlib.plugins.dbus import activity
513
bus = dbus.SessionBus()
514
# get the object so we can subscribe to callbacks from it.
515
broadcast_service = bus.get_object(
516
activity.Broadcast.DBUS_NAME,
517
activity.Broadcast.DBUS_PATH)
519
def catch_branch(revision_id, urls):
520
# TODO: show all the urls, or perhaps choose the 'best'.
523
if isinstance(revision_id, unicode):
524
revision_id = revision_id.encode('utf8')
525
transport = get_transport(url)
526
a_dir = BzrDir.open_from_transport(transport)
527
branch = a_dir.open_branch()
528
revno = branch.revision_id_to_revno(revision_id)
529
revision = branch.repository.get_revision(revision_id)
530
summary = 'New revision %d in %s' % (revno, url)
531
body = 'Committer: %s\n' % revision.committer
532
body += 'Date: %s\n' % format_date(revision.timestamp,
535
body += revision.message
536
body = cgi.escape(body)
537
nw = pynotify.Notification(summary, body)
538
def start_viz(notification=None, action=None, data=None):
539
"""Start the viz program."""
540
pp = start_viz_window(branch, revision_id)
542
def start_branch(notification=None, action=None, data=None):
543
"""Start a Branch dialog"""
544
from bzrlib.plugins.gtk.branch import BranchDialog
545
bd = BranchDialog(remote_path=url)
547
nw.add_action("inspect", "Inspect", start_viz, None)
548
nw.add_action("branch", "Branch", start_branch, None)
554
broadcast_service.connect_to_signal("Revision", catch_branch,
555
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
556
pynotify.init("bzr commit-notify")
559
register_command(cmd_commit_notify)
562
class cmd_gselftest(GTKCommand):
563
"""Version of selftest that displays a notification at the end"""
565
takes_args = builtins.cmd_selftest.takes_args
566
takes_options = builtins.cmd_selftest.takes_options
567
_see_also = ['selftest']
569
def run(self, *args, **kwargs):
572
default_encoding = sys.getdefaultencoding()
573
# prevent gtk from blowing up later
575
# prevent gtk from messing with default encoding
577
if sys.getdefaultencoding() != default_encoding:
579
sys.setdefaultencoding(default_encoding)
580
result = builtins.cmd_selftest().run(*args, **kwargs)
583
body = 'Selftest succeeded in "%s"' % os.getcwd()
586
body = 'Selftest failed in "%s"' % os.getcwd()
587
pynotify.init("bzr gselftest")
588
note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
589
note.set_timeout(pynotify.EXPIRES_NEVER)
593
register_command(cmd_gselftest)
597
gettext.install('olive-gtk')
600
class NoDisplayError(BzrCommandError):
601
"""gtk could not find a proper display"""
604
return "No DISPLAY. Unable to run GTK+ application."
608
from unittest import TestSuite
611
default_encoding = sys.getdefaultencoding()
614
result.addTest(tests.test_suite())
616
if sys.getdefaultencoding() != default_encoding:
618
sys.setdefaultencoding(default_encoding)