3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Graphical support for Bazaar using GTK.
20
commit-notify Start the graphical notifier of commits.
21
gannotate GTK+ annotate.
22
gbranch GTK+ branching.
23
gcheckout GTK+ checkout.
24
gcommit GTK+ commit dialog
26
gdiff Show differences in working tree in a GTK+ Window.
27
ginit Initialise a new branch.
28
gmissing GTK+ missing revisions dialog.
29
gpreferences GTK+ preferences dialog.
31
gstatus GTK+ status dialog
32
gtags Manage branch tags.
33
visualise Graphically visualise this branch.
38
__version__ = '0.18.0'
39
version_info = tuple(int(n) for n in __version__.split('.'))
42
def check_bzrlib_version(desired):
43
"""Check that bzrlib is compatible.
45
If version is < bzr-gtk version, assume incompatible.
46
If version == bzr-gtk version, assume completely compatible
47
If version == bzr-gtk version + 1, assume compatible, with deprecations
48
Otherwise, assume incompatible.
50
desired_plus = (desired[0], desired[1]+1)
51
bzrlib_version = bzrlib.version_info[:2]
52
if bzrlib_version == desired or (bzrlib_version == desired_plus and
53
bzrlib.version_info[3] == 'dev'):
56
from bzrlib.trace import warning
58
# get the message out any way we can
59
from warnings import warn as warning
60
if bzrlib_version < desired:
61
from bzrlib.errors import BzrError
62
warning('Installed bzr version %s is too old to be used with bzr-gtk'
63
' %s.' % (bzrlib.__version__, __version__))
64
raise BzrError('Version mismatch: %r' % version_info)
66
warning('bzr-gtk is not up to date with installed bzr version %s.'
67
' \nThere should be a newer version available, e.g. %i.%i.'
68
% (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
69
if bzrlib_version != desired_plus:
70
raise Exception, 'Version mismatch'
73
check_bzrlib_version(version_info[:2])
75
from bzrlib.trace import warning
76
if __name__ != 'bzrlib.plugins.gtk':
77
warning("Not running as bzrlib.plugins.gtk, things may break.")
79
from bzrlib.lazy_import import lazy_import
80
lazy_import(globals(), """
88
from bzrlib.commands import Command, register_command, display_command
89
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
90
from bzrlib.option import Option
98
raise errors.BzrCommandError("PyGTK not installed.")
103
def set_ui_factory():
105
from ui import GtkUIFactory
107
bzrlib.ui.ui_factory = GtkUIFactory()
110
class GTKCommand(Command):
111
"""Abstract class providing GTK specific run commands."""
113
def open_display(self):
114
pygtk = import_pygtk()
117
except RuntimeError, e:
118
if str(e) == "could not open display":
125
dialog = self.get_gtk_dialog(os.path.abspath('.'))
129
class cmd_gbranch(GTKCommand):
134
def get_gtk_dialog(self, path):
135
from bzrlib.plugins.gtk.branch import BranchDialog
136
return BranchDialog(path)
139
class cmd_gcheckout(GTKCommand):
144
def get_gtk_dialog(self, path):
145
from bzrlib.plugins.gtk.checkout import CheckoutDialog
146
return CheckoutDialog(path)
150
class cmd_gpush(GTKCommand):
154
takes_args = [ "location?" ]
156
def run(self, location="."):
157
(br, path) = branch.Branch.open_containing(location)
159
from push import PushDialog
160
dialog = PushDialog(br)
165
class cmd_gdiff(GTKCommand):
166
"""Show differences in working tree in a GTK+ Window.
168
Otherwise, all changes for the tree are listed.
170
takes_args = ['filename?']
171
takes_options = ['revision']
174
def run(self, revision=None, filename=None):
176
wt = workingtree.WorkingTree.open_containing(".")[0]
180
if revision is not None:
181
if len(revision) == 1:
183
revision_id = revision[0].in_history(branch).rev_id
184
tree2 = branch.repository.revision_tree(revision_id)
185
elif len(revision) == 2:
186
revision_id_0 = revision[0].in_history(branch).rev_id
187
tree2 = branch.repository.revision_tree(revision_id_0)
188
revision_id_1 = revision[1].in_history(branch).rev_id
189
tree1 = branch.repository.revision_tree(revision_id_1)
192
tree2 = tree1.basis_tree()
194
from diff import DiffWindow
196
window = DiffWindow()
197
window.connect("destroy", gtk.main_quit)
198
window.set_diff("Working Tree", tree1, tree2)
199
if filename is not None:
200
tree_filename = wt.relpath(filename)
202
window.set_file(tree_filename)
204
if (tree1.path2id(tree_filename) is None and
205
tree2.path2id(tree_filename) is None):
206
raise NotVersionedError(filename)
207
raise BzrCommandError('No changes found for file "%s"' %
216
def start_viz_window(branch, revision, limit=None):
217
"""Start viz on branch with revision revision.
219
:return: The viz window object.
221
from viz.branchwin import BranchWindow
224
pp.set_branch(branch, revision, limit)
225
# cleanup locks when the window is closed
226
pp.connect("destroy", lambda w: branch.unlock())
230
class cmd_visualise(Command):
231
"""Graphically visualise this branch.
233
Opens a graphical window to allow you to see the history of the branch
234
and relationships between revisions in a visual manner,
236
The default starting point is latest revision on the branch, you can
237
specify a starting point with -r revision.
241
Option('limit', "Maximum number of revisions to display.",
243
takes_args = [ "location?" ]
244
aliases = [ "visualize", "vis", "viz" ]
246
def run(self, location=".", revision=None, limit=None):
248
(br, path) = branch.Branch.open_containing(location)
252
revid = br.last_revision()
256
(revno, revid) = revision[0].in_history(br)
259
pp = start_viz_window(br, revid, limit)
260
pp.connect("destroy", lambda w: gtk.main_quit())
267
class cmd_gannotate(GTKCommand):
270
Browse changes to FILENAME line by line in a GTK+ window.
273
takes_args = ["filename", "line?"]
275
Option("all", help="Show annotations on all lines."),
276
Option("plain", help="Don't highlight annotation lines."),
277
Option("line", type=int, argname="lineno",
278
help="Jump to specified line number."),
281
aliases = ["gblame", "gpraise"]
283
def run(self, filename, all=False, plain=False, line='1', revision=None):
284
gtk = self.open_display()
289
raise BzrCommandError('Line argument ("%s") is not a number.' %
292
from annotate.gannotate import GAnnotateWindow
293
from annotate.config import GAnnotateConfig
294
from bzrlib.bzrdir import BzrDir
296
wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
300
tree = br.basis_tree()
302
file_id = tree.path2id(path)
305
raise NotVersionedError(filename)
306
if revision is not None:
307
if len(revision) != 1:
308
raise BzrCommandError("Only 1 revion may be specified.")
309
revision_id = revision[0].in_history(br).rev_id
310
tree = br.repository.revision_tree(revision_id)
312
revision_id = getattr(tree, 'get_revision_id', lambda: None)()
314
window = GAnnotateWindow(all, plain)
315
window.connect("destroy", lambda w: gtk.main_quit())
316
window.set_title(path + " - gannotate")
317
config = GAnnotateConfig(window)
323
window.annotate(tree, br, file_id)
324
window.jump_to_line(line)
333
class cmd_gcommit(GTKCommand):
334
"""GTK+ commit dialog
336
Graphical user interface for committing revisions"""
342
def run(self, filename=None):
345
from commit import CommitDialog
346
from bzrlib.errors import (BzrCommandError,
353
(wt, path) = workingtree.WorkingTree.open_containing(filename)
355
except NoWorkingTree, e:
357
(br, path) = branch.Branch.open_containing(path)
359
commit = CommitDialog(wt, path, not br)
364
class cmd_gstatus(GTKCommand):
365
"""GTK+ status dialog
367
Graphical user interface for showing status
371
takes_args = ['PATH?']
374
def run(self, path='.'):
376
gtk = self.open_display()
377
from status import StatusDialog
378
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
379
status = StatusDialog(wt, wt_path)
380
status.connect("destroy", gtk.main_quit)
385
class cmd_gconflicts(GTKCommand):
390
(wt, path) = workingtree.WorkingTree.open_containing('.')
392
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
393
dialog = ConflictsDialog(wt)
398
class cmd_gpreferences(GTKCommand):
399
""" GTK+ preferences dialog.
404
from bzrlib.plugins.gtk.preferences import PreferencesWindow
405
dialog = PreferencesWindow()
410
class cmd_gmissing(Command):
411
""" GTK+ missing revisions dialog.
414
takes_args = ["other_branch?"]
415
def run(self, other_branch=None):
416
pygtk = import_pygtk()
419
except RuntimeError, e:
420
if str(e) == "could not open display":
423
from bzrlib.plugins.gtk.missing import MissingWindow
424
from bzrlib.branch import Branch
426
local_branch = Branch.open_containing(".")[0]
427
if other_branch is None:
428
other_branch = local_branch.get_parent()
430
if other_branch is None:
431
raise errors.BzrCommandError("No peer location known or specified.")
432
remote_branch = Branch.open_containing(other_branch)[0]
434
local_branch.lock_read()
436
remote_branch.lock_read()
438
dialog = MissingWindow(local_branch, remote_branch)
441
remote_branch.unlock()
443
local_branch.unlock()
446
class cmd_ginit(GTKCommand):
449
from initialize import InitDialog
450
dialog = InitDialog(os.path.abspath(os.path.curdir))
454
class cmd_gtags(GTKCommand):
456
br = branch.Branch.open_containing('.')[0]
458
gtk = self.open_display()
459
from tags import TagsWindow
460
window = TagsWindow(br)
482
register_command(cmd)
485
class cmd_commit_notify(GTKCommand):
486
"""Run the bzr commit notifier.
488
This is a background program which will pop up a notification on the users
489
screen when a commit occurs.
493
from notify import NotifyPopupMenu
494
gtk = self.open_display()
495
menu = NotifyPopupMenu()
496
icon = gtk.status_icon_new_from_file("bzr-icon-64.png")
497
icon.connect('popup-menu', menu.display)
503
from bzrlib.bzrdir import BzrDir
504
from bzrlib import errors
505
from bzrlib.osutils import format_date
506
from bzrlib.transport import get_transport
507
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
509
from bzrlib.plugins.dbus import activity
510
bus = dbus.SessionBus()
511
# get the object so we can subscribe to callbacks from it.
512
broadcast_service = bus.get_object(
513
activity.Broadcast.DBUS_NAME,
514
activity.Broadcast.DBUS_PATH)
516
def catch_branch(revision_id, urls):
517
# TODO: show all the urls, or perhaps choose the 'best'.
520
if isinstance(revision_id, unicode):
521
revision_id = revision_id.encode('utf8')
522
transport = get_transport(url)
523
a_dir = BzrDir.open_from_transport(transport)
524
branch = a_dir.open_branch()
525
revno = branch.revision_id_to_revno(revision_id)
526
revision = branch.repository.get_revision(revision_id)
527
summary = 'New revision %d in %s' % (revno, url)
528
body = 'Committer: %s\n' % revision.committer
529
body += 'Date: %s\n' % format_date(revision.timestamp,
532
body += revision.message
533
body = cgi.escape(body)
534
nw = pynotify.Notification(summary, body)
535
def start_viz(notification=None, action=None, data=None):
536
"""Start the viz program."""
537
pp = start_viz_window(branch, revision_id)
539
def start_branch(notification=None, action=None, data=None):
540
"""Start a Branch dialog"""
541
from bzrlib.plugins.gtk.branch import BranchDialog
542
bd = BranchDialog(remote_path=url)
544
nw.add_action("inspect", "Inspect", start_viz, None)
545
nw.add_action("branch", "Branch", start_branch, None)
551
broadcast_service.connect_to_signal("Revision", catch_branch,
552
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
553
pynotify.init("bzr commit-notify")
556
register_command(cmd_commit_notify)
560
gettext.install('olive-gtk')
563
class NoDisplayError(BzrCommandError):
564
"""gtk could not find a proper display"""
567
return "No DISPLAY. Unable to run GTK+ application."
571
from unittest import TestSuite
574
default_encoding = sys.getdefaultencoding()
577
result.addTest(tests.test_suite())
579
if sys.getdefaultencoding() != default_encoding:
581
sys.setdefaultencoding(default_encoding)