14
12
# along with this program; if not, write to the Free Software
15
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""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, 93, 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(), """
19
93
from bzrlib.commands import Command, register_command, display_command
20
94
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
21
from bzrlib.commands import Command, register_command
22
95
from bzrlib.option import Option
23
from bzrlib.branch import Branch
24
from bzrlib.workingtree import WorkingTree
25
from bzrlib.bzrdir import BzrDir
29
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):
30
139
"""GTK+ branching.
39
except RuntimeError, e:
40
if str(e) == "could not open display":
43
from clone import CloneDialog
45
window = CloneDialog()
46
if window.run() == gtk.RESPONSE_OK:
47
bzrdir = BzrDir.open(window.url)
48
bzrdir.sprout(window.dest_path)
50
register_command(cmd_gbranch)
52
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):
53
175
"""Show differences in working tree in a GTK+ Window.
55
177
Otherwise, all changes for the tree are listed.
61
183
def run(self, revision=None, filename=None):
62
wt = WorkingTree.open_containing(".")[0]
64
if revision is not None:
65
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)
67
revision_id = revision[0].in_history(branch).rev_id
68
tree2 = branch.repository.revision_tree(revision_id)
69
elif len(revision) == 2:
70
revision_id_0 = revision[0].in_history(branch).rev_id
71
tree2 = branch.repository.revision_tree(revision_id_0)
72
revision_id_1 = revision[1].in_history(branch).rev_id
73
tree1 = branch.repository.revision_tree(revision_id_1)
76
tree2 = tree1.basis_tree()
78
from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
81
window.connect("destroy", lambda w: gtk.main_quit())
82
window.set_diff("Working Tree", tree1, tree2)
83
if filename is not None:
84
tree_filename = tree1.relpath(filename)
86
window.set_file(tree_filename)
88
if (tree1.inventory.path2id(tree_filename) is None and
89
tree2.inventory.path2id(tree_filename) is None):
90
raise NotVersionedError(filename)
91
raise BzrCommandError('No changes found for file "%s"' %
97
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)
99
234
class cmd_visualise(Command):
100
235
"""Graphically visualise this branch.
170
292
from annotate.gannotate import GAnnotateWindow
171
293
from annotate.config import GAnnotateConfig
173
(wt, path) = WorkingTree.open_containing(filename)
176
file_id = wt.path2id(path)
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)
178
304
if file_id is None:
179
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)()
181
314
window = GAnnotateWindow(all, plain)
182
315
window.connect("destroy", lambda w: gtk.main_quit())
183
316
window.set_title(path + " - gannotate")
184
317
config = GAnnotateConfig(window)
188
window.annotate(branch, file_id)
323
window.annotate(tree, br, file_id)
324
window.jump_to_line(line)
191
window.jump_to_line(line)
195
register_command(cmd_gannotate)
197
class cmd_gcommit(Command):
333
class cmd_gcommit(GTKCommand):
198
334
"""GTK+ commit dialog
200
336
Graphical user interface for committing revisions"""
203
340
takes_options = []
205
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:
356
from dialog import error_dialog
357
error_dialog(_('Directory does not have a working tree'),
358
_('Operation aborted.'))
359
return 1 # should this be retval=3?
361
# It is a good habit to keep things locked for the duration, but it
362
# could cause difficulties if someone wants to do things in another
363
# window... We could lock_read() until we actually go to commit
364
# changes... Just a thought.
367
dlg = CommitDialog(wt)
373
class cmd_gstatus(GTKCommand):
374
"""GTK+ status dialog
376
Graphical user interface for showing status
380
takes_args = ['PATH?']
383
def run(self, path='.'):
385
gtk = self.open_display()
386
from status import StatusDialog
387
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
388
status = StatusDialog(wt, wt_path)
389
status.connect("destroy", gtk.main_quit)
393
class cmd_gsend(GTKCommand):
394
"""GTK+ send merge directive.
398
(br, path) = branch.Branch.open_containing(".")
399
gtk = self.open_display()
400
from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
401
from StringIO import StringIO
402
dialog = SendMergeDirectiveDialog(br)
403
if dialog.run() == gtk.RESPONSE_OK:
405
outf.writelines(dialog.get_merge_directive().to_lines())
406
mail_client = br.get_config().get_mail_client()
407
mail_client.compose_merge_request(dialog.get_mail_to(), "[MERGE]",
413
class cmd_gconflicts(GTKCommand):
416
Select files from the list of conflicts and run an external utility to
420
(wt, path) = workingtree.WorkingTree.open_containing('.')
422
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
423
dialog = ConflictsDialog(wt)
427
class cmd_gpreferences(GTKCommand):
428
""" GTK+ preferences dialog.
433
from bzrlib.plugins.gtk.preferences import PreferencesWindow
434
dialog = PreferencesWindow()
438
class cmd_gmissing(Command):
439
""" GTK+ missing revisions dialog.
442
takes_args = ["other_branch?"]
443
def run(self, other_branch=None):
444
pygtk = import_pygtk()
211
447
except RuntimeError, e:
212
448
if str(e) == "could not open display":
213
449
raise NoDisplayError
215
from commit import GCommitDialog
216
from bzrlib.commit import Commit
217
from bzrlib.errors import (BzrCommandError, PointlessCommit, ConflictsInTree,
220
(wt, path) = WorkingTree.open_containing(filename)
223
file_id = wt.path2id(path)
226
raise NotVersionedError(filename)
228
dialog = GCommitDialog(wt)
229
dialog.set_title(path + " - Commit")
230
if dialog.run() != gtk.RESPONSE_CANCEL:
231
Commit().commit(working_tree=wt,message=dialog.message,
232
specific_files=dialog.specific_files)
234
register_command(cmd_gcommit)
451
from bzrlib.plugins.gtk.missing import MissingWindow
452
from bzrlib.branch import Branch
454
local_branch = Branch.open_containing(".")[0]
455
if other_branch is None:
456
other_branch = local_branch.get_parent()
458
if other_branch is None:
459
raise errors.BzrCommandError("No peer location known or specified.")
460
remote_branch = Branch.open_containing(other_branch)[0]
462
local_branch.lock_read()
464
remote_branch.lock_read()
466
dialog = MissingWindow(local_branch, remote_branch)
469
remote_branch.unlock()
471
local_branch.unlock()
474
class cmd_ginit(GTKCommand):
477
from initialize import InitDialog
478
dialog = InitDialog(os.path.abspath(os.path.curdir))
482
class cmd_gtags(GTKCommand):
484
br = branch.Branch.open_containing('.')[0]
486
gtk = self.open_display()
487
from tags import TagsWindow
488
window = TagsWindow(br)
511
register_command(cmd)
514
class cmd_commit_notify(GTKCommand):
515
"""Run the bzr commit notifier.
517
This is a background program which will pop up a notification on the users
518
screen when a commit occurs.
522
from notify import NotifyPopupMenu
523
gtk = self.open_display()
524
menu = NotifyPopupMenu()
525
icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
526
icon.connect('popup-menu', menu.display)
532
from bzrlib.bzrdir import BzrDir
533
from bzrlib import errors
534
from bzrlib.osutils import format_date
535
from bzrlib.transport import get_transport
536
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
538
from bzrlib.plugins.dbus import activity
539
bus = dbus.SessionBus()
540
# get the object so we can subscribe to callbacks from it.
541
broadcast_service = bus.get_object(
542
activity.Broadcast.DBUS_NAME,
543
activity.Broadcast.DBUS_PATH)
545
def catch_branch(revision_id, urls):
546
# TODO: show all the urls, or perhaps choose the 'best'.
549
if isinstance(revision_id, unicode):
550
revision_id = revision_id.encode('utf8')
551
transport = get_transport(url)
552
a_dir = BzrDir.open_from_transport(transport)
553
branch = a_dir.open_branch()
554
revno = branch.revision_id_to_revno(revision_id)
555
revision = branch.repository.get_revision(revision_id)
556
summary = 'New revision %d in %s' % (revno, url)
557
body = 'Committer: %s\n' % revision.committer
558
body += 'Date: %s\n' % format_date(revision.timestamp,
561
body += revision.message
562
body = cgi.escape(body)
563
nw = pynotify.Notification(summary, body)
564
def start_viz(notification=None, action=None, data=None):
565
"""Start the viz program."""
566
pp = start_viz_window(branch, revision_id)
568
def start_branch(notification=None, action=None, data=None):
569
"""Start a Branch dialog"""
570
from bzrlib.plugins.gtk.branch import BranchDialog
571
bd = BranchDialog(remote_path=url)
573
nw.add_action("inspect", "Inspect", start_viz, None)
574
nw.add_action("branch", "Branch", start_branch, None)
580
broadcast_service.connect_to_signal("Revision", catch_branch,
581
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
582
pynotify.init("bzr commit-notify")
585
register_command(cmd_commit_notify)
588
class cmd_gselftest(GTKCommand):
589
"""Version of selftest that displays a notification at the end"""
591
takes_args = builtins.cmd_selftest.takes_args
592
takes_options = builtins.cmd_selftest.takes_options
593
_see_also = ['selftest']
595
def run(self, *args, **kwargs):
598
default_encoding = sys.getdefaultencoding()
599
# prevent gtk from blowing up later
601
# prevent gtk from messing with default encoding
603
if sys.getdefaultencoding() != default_encoding:
605
sys.setdefaultencoding(default_encoding)
606
result = builtins.cmd_selftest().run(*args, **kwargs)
609
body = 'Selftest succeeded in "%s"' % os.getcwd()
612
body = 'Selftest failed in "%s"' % os.getcwd()
613
pynotify.init("bzr gselftest")
614
note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
615
note.set_timeout(pynotify.EXPIRES_NEVER)
619
register_command(cmd_gselftest)
622
class cmd_test_gtk(GTKCommand):
623
"""Version of selftest that just runs the gtk test suite."""
625
takes_options = ['verbose',
626
Option('one', short_name='1',
627
help='Stop when one test fails.'),
628
Option('benchmark', help='Run the benchmarks.'),
629
Option('lsprof-timed',
630
help='Generate lsprof output for benchmarked'
631
' sections of code.'),
633
help='List the tests instead of running them.'),
634
Option('randomize', type=str, argname="SEED",
635
help='Randomize the order of tests using the given'
636
' seed or "now" for the current time.'),
638
takes_args = ['testspecs*']
640
def run(self, verbose=None, one=False, benchmark=None,
641
lsprof_timed=None, list_only=False, randomize=None,
642
testspecs_list=None):
643
from bzrlib import __path__ as bzrlib_path
644
from bzrlib.tests import selftest
646
print '%10s: %s' % ('bzrlib', bzrlib_path[0])
648
print 'No benchmarks yet'
651
test_suite_factory = bench_suite
654
# TODO: should possibly lock the history file...
655
benchfile = open(".perf_history", "at", buffering=1)
657
test_suite_factory = test_suite
662
if testspecs_list is not None:
663
pattern = '|'.join(testspecs_list)
668
result = selftest(verbose=verbose,
671
test_suite_factory=test_suite_factory,
672
lsprof_timed=lsprof_timed,
673
bench_history=benchfile,
675
random_seed=randomize,
678
if benchfile is not None:
681
register_command(cmd_test_gtk)
685
gettext.install('olive-gtk')
236
688
class NoDisplayError(BzrCommandError):
237
689
"""gtk could not find a proper display"""
239
691
def __str__(self):
240
return "No DISPLAY. gannotate is disabled."
692
return "No DISPLAY. Unable to run GTK+ application."
696
from unittest import TestSuite
699
default_encoding = sys.getdefaultencoding()
704
except errors.BzrCommandError:
706
result.addTest(tests.test_suite())
708
if sys.getdefaultencoding() != default_encoding:
710
sys.setdefaultencoding(default_encoding)