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
"""GTK+ frontends to Bazaar commands """
19
__version__ = '0.17.0'
20
version_info = tuple(int(n) for n in __version__.split('.'))
23
def check_bzrlib_version(desired):
24
"""Check that bzrlib is compatible.
26
If version is < bzr-gtk version, assume incompatible.
27
If version == bzr-gtk version, assume completely compatible
28
If version == bzr-gtk version + 1, assume compatible, with deprecations
29
Otherwise, assume incompatible.
31
desired_plus = (desired[0], desired[1]+1)
32
bzrlib_version = bzrlib.version_info[:2]
33
if bzrlib_version == desired or (bzrlib_version == desired_plus and
34
bzrlib.version_info[3] == 'dev'):
37
from bzrlib.trace import warning
39
# get the message out any way we can
40
from warnings import warn as warning
41
if bzrlib_version < desired:
42
from bzrlib.errors import BzrError
43
warning('Installed bzr version %s is too old to be used with bzr-gtk'
44
' %s.' % (bzrlib.__version__, __version__))
45
raise BzrError('Version mismatch: %r' % version_info)
47
warning('bzr-gtk is not up to date with installed bzr version %s.'
48
' \nThere should be a newer version available, e.g. %i.%i.'
49
% (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
50
if bzrlib_version != desired_plus:
51
raise Exception, 'Version mismatch'
54
check_bzrlib_version(version_info[:2])
56
from bzrlib.trace import warning
57
if __name__ != 'bzrlib.plugins.gtk':
58
warning("Not running as bzrlib.plugins.gtk, things may break.")
60
from bzrlib.lazy_import import lazy_import
61
lazy_import(globals(), """
69
from bzrlib.commands import Command, register_command, display_command
70
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
71
from bzrlib.option import Option
79
raise errors.BzrCommandError("PyGTK not installed.")
86
from ui import GtkUIFactory
88
bzrlib.ui.ui_factory = GtkUIFactory()
91
class GTKCommand(Command):
92
"""Abstract class providing GTK specific run commands."""
94
def open_display(self):
95
pygtk = import_pygtk()
98
except RuntimeError, e:
99
if str(e) == "could not open display":
106
dialog = self.get_gtk_dialog(os.path.abspath('.'))
110
class cmd_gbranch(GTKCommand):
115
def get_gtk_dialog(self, path):
116
from bzrlib.plugins.gtk.branch import BranchDialog
117
return BranchDialog(path)
120
class cmd_gcheckout(GTKCommand):
125
def get_gtk_dialog(self, path):
126
from bzrlib.plugins.gtk.checkout import CheckoutDialog
127
return CheckoutDialog(path)
131
class cmd_gpush(GTKCommand):
135
takes_args = [ "location?" ]
137
def run(self, location="."):
138
(br, path) = branch.Branch.open_containing(location)
140
from push import PushDialog
141
dialog = PushDialog(br)
146
class cmd_gdiff(GTKCommand):
147
"""Show differences in working tree in a GTK+ Window.
149
Otherwise, all changes for the tree are listed.
151
takes_args = ['filename?']
152
takes_options = ['revision']
155
def run(self, revision=None, filename=None):
157
wt = workingtree.WorkingTree.open_containing(".")[0]
161
if revision is not None:
162
if len(revision) == 1:
164
revision_id = revision[0].in_history(branch).rev_id
165
tree2 = branch.repository.revision_tree(revision_id)
166
elif len(revision) == 2:
167
revision_id_0 = revision[0].in_history(branch).rev_id
168
tree2 = branch.repository.revision_tree(revision_id_0)
169
revision_id_1 = revision[1].in_history(branch).rev_id
170
tree1 = branch.repository.revision_tree(revision_id_1)
173
tree2 = tree1.basis_tree()
175
from diff import DiffWindow
177
window = DiffWindow()
178
window.connect("destroy", gtk.main_quit)
179
window.set_diff("Working Tree", tree1, tree2)
180
if filename is not None:
181
tree_filename = wt.relpath(filename)
183
window.set_file(tree_filename)
185
if (tree1.path2id(tree_filename) is None and
186
tree2.path2id(tree_filename) is None):
187
raise NotVersionedError(filename)
188
raise BzrCommandError('No changes found for file "%s"' %
197
class cmd_visualise(Command):
198
"""Graphically visualise this branch.
200
Opens a graphical window to allow you to see the history of the branch
201
and relationships between revisions in a visual manner,
203
The default starting point is latest revision on the branch, you can
204
specify a starting point with -r revision.
208
Option('limit', "maximum number of revisions to display",
210
takes_args = [ "location?" ]
211
aliases = [ "visualize", "vis", "viz" ]
213
def run(self, location=".", revision=None, limit=None):
215
(br, path) = branch.Branch.open_containing(location)
217
br.repository.lock_read()
220
revid = br.last_revision()
224
(revno, revid) = revision[0].in_history(br)
226
from viz.branchwin import BranchWindow
230
pp.set_branch(br, revid, limit)
231
pp.connect("destroy", lambda w: gtk.main_quit())
235
br.repository.unlock()
239
class cmd_gannotate(GTKCommand):
242
Browse changes to FILENAME line by line in a GTK+ window.
245
takes_args = ["filename", "line?"]
247
Option("all", help="show annotations on all lines"),
248
Option("plain", help="don't highlight annotation lines"),
249
Option("line", type=int, argname="lineno",
250
help="jump to specified line number"),
253
aliases = ["gblame", "gpraise"]
255
def run(self, filename, all=False, plain=False, line='1', revision=None):
256
gtk = self.open_display()
261
raise BzrCommandError('Line argument ("%s") is not a number.' %
264
from annotate.gannotate import GAnnotateWindow
265
from annotate.config import GAnnotateConfig
266
from bzrlib.bzrdir import BzrDir
268
wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
272
tree = br.basis_tree()
274
file_id = tree.path2id(path)
277
raise NotVersionedError(filename)
278
if revision is not None:
279
if len(revision) != 1:
280
raise BzrCommandError("Only 1 revion may be specified.")
281
revision_id = revision[0].in_history(br).rev_id
282
tree = br.repository.revision_tree(revision_id)
284
revision_id = getattr(tree, 'get_revision_id', lambda: None)()
286
window = GAnnotateWindow(all, plain)
287
window.connect("destroy", lambda w: gtk.main_quit())
288
window.set_title(path + " - gannotate")
289
config = GAnnotateConfig(window)
295
window.annotate(tree, br, file_id)
296
window.jump_to_line(line)
305
class cmd_gcommit(GTKCommand):
306
"""GTK+ commit dialog
308
Graphical user interface for committing revisions"""
314
def run(self, filename=None):
317
from commit import CommitDialog
318
from bzrlib.errors import (BzrCommandError,
325
(wt, path) = workingtree.WorkingTree.open_containing(filename)
327
except NoWorkingTree, e:
329
(br, path) = branch.Branch.open_containing(path)
331
commit = CommitDialog(wt, path, not br)
336
class cmd_gstatus(GTKCommand):
337
"""GTK+ status dialog
339
Graphical user interface for showing status
343
takes_args = ['PATH?']
346
def run(self, path='.'):
348
gtk = self.open_display()
349
from status import StatusDialog
350
(wt, wt_path) = workingtree.WorkingTree.open_containing(path)
351
status = StatusDialog(wt, wt_path)
352
status.connect("destroy", gtk.main_quit)
357
class cmd_gconflicts(GTKCommand):
362
(wt, path) = workingtree.WorkingTree.open_containing('.')
364
from bzrlib.plugins.gtk.conflicts import ConflictsDialog
365
dialog = ConflictsDialog(wt)
370
class cmd_gpreferences(GTKCommand):
371
""" GTK+ preferences dialog.
376
from bzrlib.plugins.gtk.preferences import PreferencesWindow
377
dialog = PreferencesWindow()
382
class cmd_gmissing(Command):
383
""" GTK+ missing revisions dialog.
386
takes_args = ["other_branch?"]
387
def run(self, other_branch=None):
388
pygtk = import_pygtk()
391
except RuntimeError, e:
392
if str(e) == "could not open display":
395
from bzrlib.plugins.gtk.missing import MissingWindow
396
from bzrlib.branch import Branch
398
local_branch = Branch.open_containing(".")[0]
399
if other_branch is None:
400
other_branch = local_branch.get_parent()
402
if other_branch is None:
403
raise errors.BzrCommandError("No peer location known or specified.")
404
remote_branch = Branch.open_containing(other_branch)[0]
406
local_branch.lock_read()
408
remote_branch.lock_read()
410
dialog = MissingWindow(local_branch, remote_branch)
413
remote_branch.unlock()
415
local_branch.unlock()
418
class cmd_ginit(GTKCommand):
421
from initialize import InitDialog
422
dialog = InitDialog(os.path.abspath(os.path.curdir))
426
class cmd_gtags(GTKCommand):
428
br = branch.Branch.open_containing('.')[0]
430
gtk = self.open_display()
431
from tags import TagsWindow
432
window = TagsWindow(br)
454
register_command(cmd)
457
class cmd_commit_notify(GTKCommand):
458
"""Run the bzr commit notifier.
460
This is a background program which will pop up a notification on the users
461
screen when a commit occurs.
465
gtk = self.open_display()
470
from bzrlib.bzrdir import BzrDir
471
from bzrlib import errors
472
from bzrlib.osutils import format_date
473
from bzrlib.transport import get_transport
474
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
476
from bzrlib.plugins.dbus import activity
477
bus = dbus.SessionBus()
478
# get the object so we can subscribe to callbacks from it.
479
broadcast_service = bus.get_object(
480
activity.Broadcast.DBUS_NAME,
481
activity.Broadcast.DBUS_PATH)
482
def catch_branch(revision_id, urls):
483
# TODO: show all the urls, or perhaps choose the 'best'.
486
if isinstance(revision_id, unicode):
487
revision_id = revision_id.encode('utf8')
488
transport = get_transport(url)
489
a_dir = BzrDir.open_from_transport(transport)
490
branch = a_dir.open_branch()
491
revno = branch.revision_id_to_revno(revision_id)
492
revision = branch.repository.get_revision(revision_id)
493
summary = 'New revision %d in %s' % (revno, url)
494
body = 'Committer: %s\n' % revision.committer
495
body += 'Date: %s\n' % format_date(revision.timestamp,
498
body += revision.message
499
body = cgi.escape(body)
500
nw = pynotify.Notification(summary, body)
506
broadcast_service.connect_to_signal("Revision", catch_branch,
507
dbus_interface=activity.Broadcast.DBUS_INTERFACE)
508
pynotify.init("bzr commit-notify")
511
register_command(cmd_commit_notify)
515
gettext.install('olive-gtk')
518
class NoDisplayError(BzrCommandError):
519
"""gtk could not find a proper display"""
522
return "No DISPLAY. Unable to run GTK+ application."
526
from unittest import TestSuite
529
default_encoding = sys.getdefaultencoding()
532
result.addTest(tests.test_suite())
534
if sys.getdefaultencoding() != default_encoding:
536
sys.setdefaultencoding(default_encoding)