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
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__ = '0.18.0'
37
version_info = tuple(int(n) for n in __version__.split('.'))
40
def check_bzrlib_version(desired):
41
"""Check that bzrlib is compatible.
43
If version is < bzr-gtk version, assume incompatible.
44
If version == bzr-gtk version, assume completely compatible
45
If version == bzr-gtk version + 1, assume compatible, with deprecations
46
Otherwise, assume incompatible.
48
desired_plus = (desired[0], desired[1]+1)
49
bzrlib_version = bzrlib.version_info[:2]
50
if bzrlib_version == desired or (bzrlib_version == desired_plus and
51
bzrlib.version_info[3] == 'dev'):
54
from bzrlib.trace import warning
56
# get the message out any way we can
57
from warnings import warn as warning
58
if bzrlib_version < desired:
59
from bzrlib.errors import BzrError
60
warning('Installed bzr version %s is too old to be used with bzr-gtk'
61
' %s.' % (bzrlib.__version__, __version__))
62
raise BzrError('Version mismatch: %r' % version_info)
64
warning('bzr-gtk is not up to date with installed bzr version %s.'
65
' \nThere should be a newer version available, e.g. %i.%i.'
66
% (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
67
if bzrlib_version != desired_plus:
68
raise Exception, 'Version mismatch'
71
check_bzrlib_version(version_info[:2])
73
from bzrlib.trace import warning
74
if __name__ != 'bzrlib.plugins.gtk':
75
warning("Not running as bzrlib.plugins.gtk, things may break.")
77
from bzrlib.lazy_import import lazy_import
78
lazy_import(globals(), """
86
from bzrlib.commands import Command, register_command, display_command
87
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
88
from bzrlib.option import Option
96
raise errors.BzrCommandError("PyGTK not installed.")
101
def set_ui_factory():
103
from ui import GtkUIFactory
105
bzrlib.ui.ui_factory = GtkUIFactory()
108
class GTKCommand(Command):
109
"""Abstract class providing GTK specific run commands."""
111
def open_display(self):
112
pygtk = import_pygtk()
115
except RuntimeError, e:
116
if str(e) == "could not open display":
123
dialog = self.get_gtk_dialog(os.path.abspath('.'))
127
class cmd_gbranch(GTKCommand):
132
def get_gtk_dialog(self, path):
133
from bzrlib.plugins.gtk.branch import BranchDialog
134
return BranchDialog(path)
137
class cmd_gcheckout(GTKCommand):
142
def get_gtk_dialog(self, path):
143
from bzrlib.plugins.gtk.checkout import CheckoutDialog
144
return CheckoutDialog(path)
148
class cmd_gpush(GTKCommand):
152
takes_args = [ "location?" ]
154
def run(self, location="."):
155
(br, path) = branch.Branch.open_containing(location)
157
from push import PushDialog
158
dialog = PushDialog(br)
163
class cmd_gdiff(GTKCommand):
164
"""Show differences in working tree in a GTK+ Window.
166
Otherwise, all changes for the tree are listed.
168
takes_args = ['filename?']
169
takes_options = ['revision']
172
def run(self, revision=None, filename=None):
174
wt = workingtree.WorkingTree.open_containing(".")[0]
178
if revision is not None:
179
if len(revision) == 1:
181
revision_id = revision[0].in_history(branch).rev_id
182
tree2 = branch.repository.revision_tree(revision_id)
183
elif len(revision) == 2:
184
revision_id_0 = revision[0].in_history(branch).rev_id
185
tree2 = branch.repository.revision_tree(revision_id_0)
186
revision_id_1 = revision[1].in_history(branch).rev_id
187
tree1 = branch.repository.revision_tree(revision_id_1)
190
tree2 = tree1.basis_tree()
192
from diff import DiffWindow
194
window = DiffWindow()
195
window.connect("destroy", gtk.main_quit)
196
window.set_diff("Working Tree", tree1, tree2)
197
if filename is not None:
198
tree_filename = wt.relpath(filename)
200
window.set_file(tree_filename)
202
if (tree1.path2id(tree_filename) is None and
203
tree2.path2id(tree_filename) is None):
204
raise NotVersionedError(filename)
205
raise BzrCommandError('No changes found for file "%s"' %
214
def start_viz_window(branch, revision, limit=None):
215
"""Start viz on branch with revision revision.
217
:return: The viz window object.
219
from viz.branchwin import BranchWindow
222
pp.set_branch(branch, revision, limit)
223
# cleanup locks when the window is closed
224
pp.connect("destroy", lambda w: branch.unlock())
228
class cmd_visualise(Command):
229
"""Graphically visualise this branch.
231
Opens a graphical window to allow you to see the history of the branch
232
and relationships between revisions in a visual manner,
234
The default starting point is latest revision on the branch, you can
235
specify a starting point with -r revision.
239
Option('limit', "maximum number of revisions to display",
241
takes_args = [ "location?" ]
242
aliases = [ "visualize", "vis", "viz" ]
244
def run(self, location=".", revision=None, limit=None):
246
(br, path) = branch.Branch.open_containing(location)
250
revid = br.last_revision()
254
(revno, revid) = revision[0].in_history(br)
257
pp = start_viz_window(br, revid, limit)
258
pp.connect("destroy", lambda w: gtk.main_quit())
265
class cmd_gannotate(GTKCommand):
268
Browse changes to FILENAME line by line in a GTK+ window.
271
takes_args = ["filename", "line?"]
273
Option("all", help="show annotations on all lines"),
274
Option("plain", help="don't highlight annotation lines"),
275
Option("line", type=int, argname="lineno",
276
help="jump to specified line number"),
279
aliases = ["gblame", "gpraise"]
281
def run(self, filename, all=False, plain=False, line='1', revision=None):
282
gtk = self.open_display()
287
raise BzrCommandError('Line argument ("%s") is not a number.' %
290
from annotate.gannotate import GAnnotateWindow
291
from annotate.config import GAnnotateConfig
292
from bzrlib.bzrdir import BzrDir
294
wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
298
tree = br.basis_tree()
300
file_id = tree.path2id(path)
303
raise NotVersionedError(filename)
304
if revision is not None:
305
if len(revision) != 1:
306
raise BzrCommandError("Only 1 revion may be specified.")
307
revision_id = revision[0].in_history(br).rev_id
308
tree = br.repository.revision_tree(revision_id)
310
revision_id = getattr(tree, 'get_revision_id', lambda: None)()
312
window = GAnnotateWindow(all, plain)
313
window.connect("destroy", lambda w: gtk.main_quit())
314
window.set_title(path + " - gannotate")
315
config = GAnnotateConfig(window)
321
window.annotate(tree, br, file_id)
322
window.jump_to_line(line)
331
class cmd_gcommit(GTKCommand):
332
"""GTK+ commit dialog
334
Graphical user interface for committing revisions"""
340
def run(self, filename=None):
343
from commit import CommitDialog
344
from bzrlib.errors import (BzrCommandError,
351
(wt, path) = workingtree.WorkingTree.open_containing(filename)
353
except NoWorkingTree, e:
355
(br, path) = branch.Branch.open_containing(path)
357
commit = CommitDialog(wt, path, not br)
362
class cmd_gstatus(GTKCommand):
363
"""GTK+ status dialog
365
Graphical user interface for showing status
369
takes_args = ['PATH?']
372
def run(self, path='.'):
374
gtk = self.open_display()
375
from status import StatusDialog
376
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
377
status = StatusDialog(wt, wt_path)
378
status.connect("destroy", gtk.main_quit)
383
class cmd_gconflicts(GTKCommand):
388
(wt, path) = workingtree.WorkingTree.open_containing('.')
390
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
391
dialog = ConflictsDialog(wt)
396
class cmd_gpreferences(GTKCommand):
397
""" GTK+ preferences dialog.
402
from bzrlib.plugins.gtk.preferences import PreferencesWindow
403
dialog = PreferencesWindow()
408
class cmd_gmissing(Command):
409
""" GTK+ missing revisions dialog.
412
takes_args = ["other_branch?"]
413
def run(self, other_branch=None):
414
pygtk = import_pygtk()
417
except RuntimeError, e:
418
if str(e) == "could not open display":
421
from bzrlib.plugins.gtk.missing import MissingWindow
422
from bzrlib.branch import Branch
424
local_branch = Branch.open_containing(".")[0]
425
if other_branch is None:
426
other_branch = local_branch.get_parent()
428
if other_branch is None:
429
raise errors.BzrCommandError("No peer location known or specified.")
430
remote_branch = Branch.open_containing(other_branch)[0]
432
local_branch.lock_read()
434
remote_branch.lock_read()
436
dialog = MissingWindow(local_branch, remote_branch)
439
remote_branch.unlock()
441
local_branch.unlock()
444
class cmd_ginit(GTKCommand):
447
from initialize import InitDialog
448
dialog = InitDialog(os.path.abspath(os.path.curdir))
452
class cmd_gtags(GTKCommand):
454
br = branch.Branch.open_containing('.')[0]
456
gtk = self.open_display()
457
from tags import TagsWindow
458
window = TagsWindow(br)
480
register_command(cmd)
483
class cmd_commit_notify(GTKCommand):
484
"""Run the bzr commit notifier.
486
This is a background program which will pop up a notification on the users
487
screen when a commit occurs.
491
gtk = self.open_display()
496
from bzrlib.bzrdir import BzrDir
497
from bzrlib import errors
498
from bzrlib.osutils import format_date
499
from bzrlib.transport import get_transport
500
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
502
from bzrlib.plugins.dbus import activity
503
bus = dbus.SessionBus()
504
# get the object so we can subscribe to callbacks from it.
505
broadcast_service = bus.get_object(
506
activity.Broadcast.DBUS_NAME,
507
activity.Broadcast.DBUS_PATH)
508
def catch_branch(revision_id, urls):
509
# TODO: show all the urls, or perhaps choose the 'best'.
512
if isinstance(revision_id, unicode):
513
revision_id = revision_id.encode('utf8')
514
transport = get_transport(url)
515
a_dir = BzrDir.open_from_transport(transport)
516
branch = a_dir.open_branch()
517
revno = branch.revision_id_to_revno(revision_id)
518
revision = branch.repository.get_revision(revision_id)
519
summary = 'New revision %d in %s' % (revno, url)
520
body = 'Committer: %s\n' % revision.committer
521
body += 'Date: %s\n' % format_date(revision.timestamp,
524
body += revision.message
525
body = cgi.escape(body)
526
nw = pynotify.Notification(summary, body)
527
def start_viz(notification=None, action=None, data=None):
528
"""Start the viz program."""
529
pp = start_viz_window(branch, revision_id)
531
def start_branch(notification=None, action=None, data=None):
532
"""Start a Branch dialog"""
533
from bzrlib.plugins.gtk.branch import BranchDialog
534
bd = BranchDialog(remote_path=url)
536
nw.add_action("inspect", "Inspect", start_viz, None)
537
nw.add_action("branch", "Branch", start_branch, None)
543
broadcast_service.connect_to_signal("Revision", catch_branch,
544
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
545
pynotify.init("bzr commit-notify")
548
register_command(cmd_commit_notify)
552
gettext.install('olive-gtk')
555
class NoDisplayError(BzrCommandError):
556
"""gtk could not find a proper display"""
559
return "No DISPLAY. Unable to run GTK+ application."
563
from unittest import TestSuite
566
default_encoding = sys.getdefaultencoding()
569
result.addTest(tests.test_suite())
571
if sys.getdefaultencoding() != default_encoding:
573
sys.setdefaultencoding(default_encoding)