12
12
# along with this program; if not, write to the Free Software
13
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
"""GTK+ frontends to Bazaar commands """
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
gsend GTK+ send merge directive.
30
gstatus GTK+ status dialog.
31
gtags Manage branch tags.
32
visualise Graphically visualise this branch.
39
version_info = (0, 92, 0, 'dev', 0)
41
if version_info[3] == 'final':
42
version_string = '%d.%d.%d' % version_info[:3]
44
version_string = '%d.%d.%d%s%d' % version_info
45
__version__ = version_string
47
def check_bzrlib_version(desired):
48
"""Check that bzrlib is compatible.
50
If version is < bzr-gtk version, assume incompatible.
51
If version == bzr-gtk version, assume completely compatible
52
If version == bzr-gtk version + 1, assume compatible, with deprecations
53
Otherwise, assume incompatible.
55
desired_plus = (desired[0], desired[1]+1)
56
bzrlib_version = bzrlib.version_info[:2]
57
if bzrlib_version == desired or (bzrlib_version == desired_plus and
58
bzrlib.version_info[3] == 'dev'):
61
from bzrlib.trace import warning
63
# get the message out any way we can
64
from warnings import warn as warning
65
if bzrlib_version < desired:
66
from bzrlib.errors import BzrError
67
warning('Installed Bazaar version %s is too old to be used with bzr-gtk'
68
' %s.' % (bzrlib.__version__, __version__))
69
raise BzrError('Version mismatch: %r, %r' % (version_info, bzrlib.version_info) )
71
warning('bzr-gtk is not up to date with installed bzr version %s.'
72
' \nThere should be a newer version available, e.g. %i.%i.'
73
% (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
76
if version_info[2] == "final":
77
check_bzrlib_version(version_info[:2])
79
from bzrlib.trace import warning
80
if __name__ != 'bzrlib.plugins.gtk':
81
warning("Not running as bzrlib.plugins.gtk, things may break.")
83
from bzrlib.lazy_import import lazy_import
84
lazy_import(globals(), """
17
93
from bzrlib.commands import Command, register_command, display_command
18
94
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
19
from bzrlib.commands import Command, register_command
20
95
from bzrlib.option import Option
21
from bzrlib.branch import Branch
22
from bzrlib.workingtree import WorkingTree
23
from bzrlib.bzrdir import BzrDir
25
__version__ = '0.13.0'
27
class cmd_gbranch(Command):
103
raise errors.BzrCommandError("PyGTK not installed.")
108
def set_ui_factory():
110
from ui import GtkUIFactory
112
bzrlib.ui.ui_factory = GtkUIFactory()
116
return os.path.dirname(__file__)
119
class GTKCommand(Command):
120
"""Abstract class providing GTK specific run commands."""
122
def open_display(self):
123
pygtk = import_pygtk()
126
except RuntimeError, e:
127
if str(e) == "could not open display":
134
dialog = self.get_gtk_dialog(os.path.abspath('.'))
138
class cmd_gbranch(GTKCommand):
28
139
"""GTK+ branching.
37
except RuntimeError, e:
38
if str(e) == "could not open display":
41
from bzrlib.plugins.gtk.olive.branch import BranchDialog
43
window = BranchDialog('.')
46
register_command(cmd_gbranch)
48
class cmd_gdiff(Command):
143
def get_gtk_dialog(self, path):
144
from bzrlib.plugins.gtk.branch import BranchDialog
145
return BranchDialog(path)
148
class cmd_gcheckout(GTKCommand):
153
def get_gtk_dialog(self, path):
154
from bzrlib.plugins.gtk.checkout import CheckoutDialog
155
return CheckoutDialog(path)
159
class cmd_gpush(GTKCommand):
163
takes_args = [ "location?" ]
165
def run(self, location="."):
166
(br, path) = branch.Branch.open_containing(location)
168
from push import PushDialog
169
dialog = PushDialog(br.repository, br.last_revision(), br)
174
class cmd_gdiff(GTKCommand):
49
175
"""Show differences in working tree in a GTK+ Window.
51
177
Otherwise, all changes for the tree are listed.
57
183
def run(self, revision=None, filename=None):
58
wt = WorkingTree.open_containing(".")[0]
60
if revision is not None:
61
if len(revision) == 1:
185
wt = workingtree.WorkingTree.open_containing(".")[0]
189
if revision is not None:
190
if len(revision) == 1:
192
revision_id = revision[0].in_history(branch).rev_id
193
tree2 = branch.repository.revision_tree(revision_id)
194
elif len(revision) == 2:
195
revision_id_0 = revision[0].in_history(branch).rev_id
196
tree2 = branch.repository.revision_tree(revision_id_0)
197
revision_id_1 = revision[1].in_history(branch).rev_id
198
tree1 = branch.repository.revision_tree(revision_id_1)
63
revision_id = revision[0].in_history(branch).rev_id
64
tree2 = branch.repository.revision_tree(revision_id)
65
elif len(revision) == 2:
66
revision_id_0 = revision[0].in_history(branch).rev_id
67
tree2 = branch.repository.revision_tree(revision_id_0)
68
revision_id_1 = revision[1].in_history(branch).rev_id
69
tree1 = branch.repository.revision_tree(revision_id_1)
72
tree2 = tree1.basis_tree()
74
from viz.diffwin import DiffWindow
77
window.connect("destroy", lambda w: gtk.main_quit())
78
window.set_diff("Working Tree", tree1, tree2)
79
if filename is not None:
80
tree_filename = wt.relpath(filename)
82
window.set_file(tree_filename)
84
if (tree1.inventory.path2id(tree_filename) is None and
85
tree2.inventory.path2id(tree_filename) is None):
86
raise NotVersionedError(filename)
87
raise BzrCommandError('No changes found for file "%s"' %
93
register_command(cmd_gdiff)
201
tree2 = tree1.basis_tree()
203
from diff import DiffWindow
205
window = DiffWindow()
206
window.connect("destroy", gtk.main_quit)
207
window.set_diff("Working Tree", tree1, tree2)
208
if filename is not None:
209
tree_filename = wt.relpath(filename)
211
window.set_file(tree_filename)
213
if (tree1.path2id(tree_filename) is None and
214
tree2.path2id(tree_filename) is None):
215
raise NotVersionedError(filename)
216
raise BzrCommandError('No changes found for file "%s"' %
225
def start_viz_window(branch, revision, limit=None):
226
"""Start viz on branch with revision revision.
228
:return: The viz window object.
230
from viz.branchwin import BranchWindow
231
return BranchWindow(branch, revision, limit)
95
234
class cmd_visualise(Command):
96
235
"""Graphically visualise this branch.
167
296
from annotate.gannotate import GAnnotateWindow
168
297
from annotate.config import GAnnotateConfig
170
(wt, path) = WorkingTree.open_containing(filename)
173
file_id = wt.path2id(path)
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)
175
308
if file_id is None:
176
309
raise NotVersionedError(filename)
177
310
if revision is not None:
178
311
if len(revision) != 1:
179
312
raise BzrCommandError("Only 1 revion may be specified.")
180
revision_id = revision[0].in_history(branch).rev_id
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)()
184
318
window = GAnnotateWindow(all, plain)
185
319
window.connect("destroy", lambda w: gtk.main_quit())
186
320
window.set_title(path + " - gannotate")
187
321
config = GAnnotateConfig(window)
191
window.annotate(branch, file_id, revision_id)
327
window.annotate(tree, br, file_id)
328
window.jump_to_line(line)
194
window.jump_to_line(line)
198
register_command(cmd_gannotate)
200
class cmd_gcommit(Command):
337
class cmd_gcommit(GTKCommand):
201
338
"""GTK+ commit dialog
203
340
Graphical user interface for committing revisions"""
206
344
takes_options = []
208
346
def run(self, filename=None):
215
except RuntimeError, e:
216
if str(e) == "could not open display":
219
from olive.commit import CommitDialog
220
from bzrlib.commit import Commit
349
from commit import CommitDialog
221
350
from bzrlib.errors import (BzrCommandError,
231
(wt, path) = WorkingTree.open_containing(filename)
233
except NotBranchError, e:
357
(wt, path) = workingtree.WorkingTree.open_containing(filename)
235
359
except NoWorkingTree, e:
238
(branch, path) = Branch.open_containing(path)
239
except NotBranchError, e:
242
dialog = CommitDialog(wt, path, not branch)
244
dialog.window.connect("destroy", lambda w: gtk.main_quit())
247
register_command(cmd_gcommit)
360
from dialog import error_dialog
361
error_dialog(_('Directory does not have a working tree'),
362
_('Operation aborted.'))
363
return 1 # should this be retval=3?
365
# It is a good habit to keep things locked for the duration, but it
366
# could cause difficulties if someone wants to do things in another
367
# window... We could lock_read() until we actually go to commit
368
# changes... Just a thought.
371
dlg = CommitDialog(wt)
377
class cmd_gstatus(GTKCommand):
378
"""GTK+ status dialog
380
Graphical user interface for showing status
384
takes_args = ['PATH?']
387
def run(self, path='.'):
389
gtk = self.open_display()
390
from status import StatusDialog
391
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
392
status = StatusDialog(wt, wt_path)
393
status.connect("destroy", gtk.main_quit)
397
class cmd_gsend(GTKCommand):
398
"""GTK+ send merge directive.
402
(br, path) = branch.Branch.open_containing(".")
403
gtk = self.open_display()
404
from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
405
from StringIO import StringIO
406
dialog = SendMergeDirectiveDialog(br)
407
if dialog.run() == gtk.RESPONSE_OK:
409
outf.writelines(dialog.get_merge_directive().to_lines())
410
mail_client = br.get_config().get_mail_client()
411
mail_client.compose_merge_request(dialog.get_mail_to(), "[MERGE]",
417
class cmd_gconflicts(GTKCommand):
420
Select files from the list of conflicts and run an external utility to
424
(wt, path) = workingtree.WorkingTree.open_containing('.')
426
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
427
dialog = ConflictsDialog(wt)
431
class cmd_gpreferences(GTKCommand):
432
""" GTK+ preferences dialog.
437
from bzrlib.plugins.gtk.preferences import PreferencesWindow
438
dialog = PreferencesWindow()
442
class cmd_gmissing(Command):
443
""" GTK+ missing revisions dialog.
446
takes_args = ["other_branch?"]
447
def run(self, other_branch=None):
448
pygtk = import_pygtk()
451
except RuntimeError, e:
452
if str(e) == "could not open display":
455
from bzrlib.plugins.gtk.missing import MissingWindow
456
from bzrlib.branch import Branch
458
local_branch = Branch.open_containing(".")[0]
459
if other_branch is None:
460
other_branch = local_branch.get_parent()
462
if other_branch is None:
463
raise errors.BzrCommandError("No peer location known or specified.")
464
remote_branch = Branch.open_containing(other_branch)[0]
466
local_branch.lock_read()
468
remote_branch.lock_read()
470
dialog = MissingWindow(local_branch, remote_branch)
473
remote_branch.unlock()
475
local_branch.unlock()
478
class cmd_ginit(GTKCommand):
481
from initialize import InitDialog
482
dialog = InitDialog(os.path.abspath(os.path.curdir))
486
class cmd_gtags(GTKCommand):
488
br = branch.Branch.open_containing('.')[0]
490
gtk = self.open_display()
491
from tags import TagsWindow
492
window = TagsWindow(br)
515
register_command(cmd)
518
class cmd_commit_notify(GTKCommand):
519
"""Run the bzr commit notifier.
521
This is a background program which will pop up a notification on the users
522
screen when a commit occurs.
526
from notify import NotifyPopupMenu
527
gtk = self.open_display()
528
menu = NotifyPopupMenu()
529
icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
530
icon.connect('popup-menu', menu.display)
536
from bzrlib.bzrdir import BzrDir
537
from bzrlib import errors
538
from bzrlib.osutils import format_date
539
from bzrlib.transport import get_transport
540
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
542
from bzrlib.plugins.dbus import activity
543
bus = dbus.SessionBus()
544
# get the object so we can subscribe to callbacks from it.
545
broadcast_service = bus.get_object(
546
activity.Broadcast.DBUS_NAME,
547
activity.Broadcast.DBUS_PATH)
549
def catch_branch(revision_id, urls):
550
# TODO: show all the urls, or perhaps choose the 'best'.
553
if isinstance(revision_id, unicode):
554
revision_id = revision_id.encode('utf8')
555
transport = get_transport(url)
556
a_dir = BzrDir.open_from_transport(transport)
557
branch = a_dir.open_branch()
558
revno = branch.revision_id_to_revno(revision_id)
559
revision = branch.repository.get_revision(revision_id)
560
summary = 'New revision %d in %s' % (revno, url)
561
body = 'Committer: %s\n' % revision.committer
562
body += 'Date: %s\n' % format_date(revision.timestamp,
565
body += revision.message
566
body = cgi.escape(body)
567
nw = pynotify.Notification(summary, body)
568
def start_viz(notification=None, action=None, data=None):
569
"""Start the viz program."""
570
pp = start_viz_window(branch, revision_id)
572
def start_branch(notification=None, action=None, data=None):
573
"""Start a Branch dialog"""
574
from bzrlib.plugins.gtk.branch import BranchDialog
575
bd = BranchDialog(remote_path=url)
577
nw.add_action("inspect", "Inspect", start_viz, None)
578
nw.add_action("branch", "Branch", start_branch, None)
584
broadcast_service.connect_to_signal("Revision", catch_branch,
585
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
586
pynotify.init("bzr commit-notify")
589
register_command(cmd_commit_notify)
592
class cmd_gselftest(GTKCommand):
593
"""Version of selftest that displays a notification at the end"""
595
takes_args = builtins.cmd_selftest.takes_args
596
takes_options = builtins.cmd_selftest.takes_options
597
_see_also = ['selftest']
599
def run(self, *args, **kwargs):
602
default_encoding = sys.getdefaultencoding()
603
# prevent gtk from blowing up later
605
# prevent gtk from messing with default encoding
607
if sys.getdefaultencoding() != default_encoding:
609
sys.setdefaultencoding(default_encoding)
610
result = builtins.cmd_selftest().run(*args, **kwargs)
613
body = 'Selftest succeeded in "%s"' % os.getcwd()
616
body = 'Selftest failed in "%s"' % os.getcwd()
617
pynotify.init("bzr gselftest")
618
note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
619
note.set_timeout(pynotify.EXPIRES_NEVER)
623
register_command(cmd_gselftest)
626
class cmd_test_gtk(GTKCommand):
627
"""Version of selftest that just runs the gtk test suite."""
629
takes_options = ['verbose',
630
Option('one', short_name='1',
631
help='stop when one test fails'),
632
Option('benchmark', help='run the benchmarks.'),
633
Option('lsprof-timed',
634
help='generate lsprof output for benchmarked'
635
' sections of code.'),
637
help='list the tests instead of running them'),
638
Option('randomize', type=str, argname="SEED",
639
help='randomize the order of tests using the given'
640
' seed or "now" for the current time'),
642
takes_args = ['testspecs*']
644
def run(self, verbose=None, one=False, benchmark=None,
645
lsprof_timed=None, list_only=False, randomize=None,
646
testspecs_list=None):
647
from bzrlib import __path__ as bzrlib_path
648
from bzrlib.tests import selftest
650
print '%10s: %s' % ('bzrlib', bzrlib_path[0])
652
print 'No benchmarks yet'
655
test_suite_factory = bench_suite
658
# TODO: should possibly lock the history file...
659
benchfile = open(".perf_history", "at", buffering=1)
661
test_suite_factory = test_suite
666
if testspecs_list is not None:
667
pattern = '|'.join(testspecs_list)
672
result = selftest(verbose=verbose,
675
test_suite_factory=test_suite_factory,
676
lsprof_timed=lsprof_timed,
677
bench_history=benchfile,
679
random_seed=randomize,
682
if benchfile is not None:
685
register_command(cmd_test_gtk)
689
gettext.install('olive-gtk')
249
692
class NoDisplayError(BzrCommandError):
250
693
"""gtk could not find a proper display"""
252
695
def __str__(self):
253
return "No DISPLAY. gannotate is disabled."
696
return "No DISPLAY. Unable to run GTK+ application."
700
from unittest import TestSuite
703
default_encoding = sys.getdefaultencoding()
706
result.addTest(tests.test_suite())
708
if sys.getdefaultencoding() != default_encoding:
710
sys.setdefaultencoding(default_encoding)