/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Jelmer Vernooij
  • Date: 2007-03-09 16:54:51 UTC
  • Revision ID: vernooij@lenovo-c29b82cd-20070309165451-v3h0d43a4godr2qa
Add very simple framework for TortoiseBzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
14
14
 
15
 
"""Graphical support for Bazaar using GTK.
16
 
 
17
 
This plugin includes:
18
 
gannotate         GTK+ annotate.
19
 
gbranch           GTK+ branching.
20
 
gcheckout         GTK+ checkout.
21
 
gcommit           GTK+ commit dialog.
22
 
gconflicts        GTK+ conflicts.
23
 
gdiff             Show differences in working tree in a GTK+ Window.
24
 
ginit             Initialise a new branch.
25
 
gloom             GTK+ loom browse dialog
26
 
gmerge            GTK+ merge dialog
27
 
gmissing          GTK+ missing revisions dialog.
28
 
gpreferences      GTK+ preferences dialog.
29
 
gpush             GTK+ push.
30
 
gsend             GTK+ send merge directive.
31
 
gstatus           GTK+ status dialog.
32
 
gtags             Manage branch tags.
33
 
visualise         Graphically visualise this branch.
34
 
"""
35
 
 
36
 
from __future__ import absolute_import
37
 
 
38
 
import os
39
 
import sys
40
 
 
41
 
if getattr(sys, "frozen", None) is not None: # we run bzr.exe
42
 
 
43
 
    # FIXME: Unless a better packaging solution is found, the following
44
 
    # provides a workaround for https://bugs.launchpad.net/bzr/+bug/388790 Also
45
 
    # see https://code.edge.launchpad.net/~vila/bzr-gtk/388790-windows-setup
46
 
    # for more details about while it's needed.
47
 
 
48
 
    # NOTE: _lib must be ahead of bzrlib or sax.saxutils (in olive) fails
49
 
    here = os.path.dirname(__file__)
50
 
    sys.path.insert(0, os.path.join(here, '_lib'))
51
 
    sys.path.append(os.path.join(here, '_lib/gtk-2.0'))
52
 
 
 
15
"""GTK+ frontends to Bazaar commands """
53
16
 
54
17
import bzrlib
55
 
import bzrlib.api
56
 
from bzrlib.commands import plugin_cmds
57
 
 
58
 
from bzrlib.plugins.gtk.info import (
59
 
    bzr_plugin_version as version_info,
60
 
    bzr_compatible_versions,
61
 
    )
62
 
 
63
 
if version_info[3] == 'final':
64
 
    version_string = '%d.%d.%d' % version_info[:3]
65
 
else:
66
 
    version_string = '%d.%d.%d%s%d' % version_info
67
 
__version__ = version_string
68
 
 
69
 
bzrlib.api.require_any_api(bzrlib, bzr_compatible_versions)
70
 
 
 
18
 
 
19
__version__ = '0.16.0'
 
20
version_info = tuple(int(n) for n in __version__.split('.'))
 
21
 
 
22
 
 
23
def check_bzrlib_version(desired):
 
24
    """Check that bzrlib is compatible.
 
25
 
 
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.
 
30
    """
 
31
    desired_plus = (desired[0], desired[1]+1)
 
32
    bzrlib_version = bzrlib.version_info[:2]
 
33
    if bzrlib_version == desired:
 
34
        return
 
35
    try:
 
36
        from bzrlib.trace import warning
 
37
    except ImportError:
 
38
        # get the message out any way we can
 
39
        from warnings import warn as warning
 
40
    if bzrlib_version < desired:
 
41
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
 
42
                ' %s.' % (bzrlib.__version__, __version__))
 
43
        # Not using BzrNewError, because it may not exist.
 
44
        raise Exception, ('Version mismatch', version_info)
 
45
    else:
 
46
        warning('bzr-gtk is not up to date with installed bzr version %s.'
 
47
                ' \nThere should be a newer version available, e.g. %i.%i.' 
 
48
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
 
49
        if bzrlib_version != desired_plus:
 
50
            raise Exception, 'Version mismatch'
 
51
 
 
52
 
 
53
check_bzrlib_version(version_info[:2])
 
54
 
 
55
from bzrlib.trace import warning
71
56
if __name__ != 'bzrlib.plugins.gtk':
72
 
    from bzrlib.trace import warning
73
57
    warning("Not running as bzrlib.plugins.gtk, things may break.")
74
58
 
 
59
from bzrlib.lazy_import import lazy_import
 
60
lazy_import(globals(), """
 
61
from bzrlib import (
 
62
    branch,
 
63
    errors,
 
64
    workingtree,
 
65
    )
 
66
""")
 
67
 
 
68
from bzrlib.commands import Command, register_command, display_command
 
69
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
 
70
from bzrlib.commands import Command, register_command
 
71
from bzrlib.option import Option
 
72
from bzrlib.bzrdir import BzrDir
 
73
 
 
74
import os.path
 
75
 
 
76
def import_pygtk():
 
77
    try:
 
78
        import pygtk
 
79
    except ImportError:
 
80
        raise errors.BzrCommandError("PyGTK not installed.")
 
81
    pygtk.require('2.0')
 
82
    return pygtk
 
83
 
75
84
 
76
85
def set_ui_factory():
77
 
    from bzrlib.plugins.gtk.ui import GtkUIFactory
 
86
    pygtk = import_pygtk()
 
87
    from ui import GtkUIFactory
78
88
    import bzrlib.ui
79
89
    bzrlib.ui.ui_factory = GtkUIFactory()
80
90
 
81
91
 
82
 
def data_basedirs():
83
 
    return [os.path.dirname(__file__),
84
 
             "/usr/share/bzr-gtk", 
85
 
             "/usr/local/share/bzr-gtk"]
86
 
 
87
 
 
88
 
def data_path(*args):
89
 
    for basedir in data_basedirs():
90
 
        path = os.path.join(basedir, *args)
91
 
        if os.path.exists(path):
92
 
            return path
93
 
    return None
94
 
 
95
 
 
96
 
def icon_path(*args):
97
 
    return data_path(os.path.join('icons', *args))
98
 
 
99
 
 
100
 
commands = {
101
 
    "gannotate": ["gblame", "gpraise"],
102
 
    "gbranch": [],
103
 
    "gcheckout": [],
104
 
    "gcommit": ["gci"],
105
 
    "gconflicts": [],
106
 
    "gdiff": [],
107
 
    "ginit": [],
108
 
    "gmerge": [],
109
 
    "gmissing": [],
110
 
    "gpreferences": [],
111
 
    "gpush": [],
112
 
    "gsend": [],
113
 
    "gstatus": ["gst"],
114
 
    "gtags": [],
115
 
    "visualise": ["visualize", "vis", "viz", 'glog'],
116
 
    }
117
 
 
118
 
try:
119
 
    from bzrlib.plugins import loom
120
 
except ImportError:
121
 
    pass # Loom plugin doesn't appear to be present
122
 
else:
123
 
    commands["gloom"] = []
124
 
 
125
 
for cmd, aliases in commands.iteritems():
126
 
    plugin_cmds.register_lazy("cmd_%s" % cmd, aliases,
127
 
                              "bzrlib.plugins.gtk.commands")
128
 
 
129
 
def save_commit_messages(*args):
130
 
    from bzrlib.plugins.gtk import commitmsgs
131
 
    commitmsgs.save_commit_messages(*args)
132
 
 
133
 
try:
134
 
    from bzrlib.hooks import install_lazy_named_hook
135
 
except ImportError:
136
 
    from bzrlib.branch import Branch
137
 
    Branch.hooks.install_named_hook('post_uncommit',
138
 
                                    save_commit_messages,
139
 
                                    "Saving commit messages for gcommit")
140
 
else:
141
 
    install_lazy_named_hook("bzrlib.branch", "Branch.hooks",
142
 
        'post_uncommit', save_commit_messages, "Saving commit messages for gcommit")
143
 
 
144
 
try:
145
 
    from bzrlib.registry import register_lazy
146
 
except ImportError:
147
 
    from bzrlib import config
148
 
    option_registry = getattr(config, "option_registry", None)
149
 
    if option_registry is not None:
150
 
        config.option_registry.register_lazy('nautilus_integration',
151
 
                'bzrlib.plugins.gtk.config', 'opt_nautilus_integration')
152
 
else:
153
 
    register_lazy("bzrlib.config", "option_registry",
154
 
        'nautilus_integration', 'bzrlib.plugins.gtk.config',
155
 
        'opt_nautilus_integration')
156
 
 
157
 
 
158
 
def load_tests(basic_tests, module, loader):
159
 
    testmod_names = [
160
 
        'tests',
161
 
        ]
 
92
class cmd_gbranch(Command):
 
93
    """GTK+ branching.
 
94
    
 
95
    """
 
96
 
 
97
    def run(self):
 
98
        pygtk = import_pygtk()
 
99
        try:
 
100
            import gtk
 
101
        except RuntimeError, e:
 
102
            if str(e) == "could not open display":
 
103
                raise NoDisplayError
 
104
 
 
105
        from bzrlib.plugins.gtk.branch import BranchDialog
 
106
 
 
107
        set_ui_factory()
 
108
        dialog = BranchDialog(os.path.abspath('.'))
 
109
        dialog.run()
 
110
 
 
111
register_command(cmd_gbranch)
 
112
 
 
113
class cmd_gcheckout(Command):
 
114
    """ GTK+ checkout.
 
115
    
 
116
    """
 
117
    
 
118
    def run(self):
 
119
        pygtk = import_pygtk()
 
120
        try:
 
121
            import gtk
 
122
        except RuntimeError, e:
 
123
            if str(e) == "could not open display":
 
124
                raise NoDisplayError
 
125
 
 
126
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
127
 
 
128
        set_ui_factory()
 
129
        dialog = CheckoutDialog(os.path.abspath('.'))
 
130
        dialog.run()
 
131
 
 
132
register_command(cmd_gcheckout)
 
133
 
 
134
class cmd_gpush(Command):
 
135
    """ GTK+ push.
 
136
    
 
137
    """
 
138
    takes_args = [ "location?" ]
 
139
 
 
140
    def run(self, location="."):
 
141
        (br, path) = branch.Branch.open_containing(location)
 
142
 
 
143
        pygtk = import_pygtk()
 
144
        try:
 
145
            import gtk
 
146
        except RuntimeError, e:
 
147
            if str(e) == "could not open display":
 
148
                raise NoDisplayError
 
149
 
 
150
        from push import PushDialog
 
151
 
 
152
        set_ui_factory()
 
153
        dialog = PushDialog(br)
 
154
        dialog.run()
 
155
 
 
156
register_command(cmd_gpush)
 
157
 
 
158
class cmd_gdiff(Command):
 
159
    """Show differences in working tree in a GTK+ Window.
 
160
    
 
161
    Otherwise, all changes for the tree are listed.
 
162
    """
 
163
    takes_args = ['filename?']
 
164
    takes_options = ['revision']
 
165
 
 
166
    @display_command
 
167
    def run(self, revision=None, filename=None):
 
168
        set_ui_factory()
 
169
        wt = workingtree.WorkingTree.open_containing(".")[0]
 
170
        wt.lock_read()
 
171
        try:
 
172
            branch = wt.branch
 
173
            if revision is not None:
 
174
                if len(revision) == 1:
 
175
                    tree1 = wt
 
176
                    revision_id = revision[0].in_history(branch).rev_id
 
177
                    tree2 = branch.repository.revision_tree(revision_id)
 
178
                elif len(revision) == 2:
 
179
                    revision_id_0 = revision[0].in_history(branch).rev_id
 
180
                    tree2 = branch.repository.revision_tree(revision_id_0)
 
181
                    revision_id_1 = revision[1].in_history(branch).rev_id
 
182
                    tree1 = branch.repository.revision_tree(revision_id_1)
 
183
            else:
 
184
                tree1 = wt
 
185
                tree2 = tree1.basis_tree()
 
186
 
 
187
            from diff import DiffWindow
 
188
            import gtk
 
189
            window = DiffWindow()
 
190
            window.connect("destroy", gtk.main_quit)
 
191
            window.set_diff("Working Tree", tree1, tree2)
 
192
            if filename is not None:
 
193
                tree_filename = wt.relpath(filename)
 
194
                try:
 
195
                    window.set_file(tree_filename)
 
196
                except NoSuchFile:
 
197
                    if (tree1.inventory.path2id(tree_filename) is None and 
 
198
                        tree2.inventory.path2id(tree_filename) is None):
 
199
                        raise NotVersionedError(filename)
 
200
                    raise BzrCommandError('No changes found for file "%s"' % 
 
201
                                          filename)
 
202
            window.show()
 
203
 
 
204
            gtk.main()
 
205
        finally:
 
206
            wt.unlock()
 
207
 
 
208
register_command(cmd_gdiff)
 
209
 
 
210
class cmd_visualise(Command):
 
211
    """Graphically visualise this branch.
 
212
 
 
213
    Opens a graphical window to allow you to see the history of the branch
 
214
    and relationships between revisions in a visual manner,
 
215
 
 
216
    The default starting point is latest revision on the branch, you can
 
217
    specify a starting point with -r revision.
 
218
    """
 
219
    takes_options = [
 
220
        "revision",
 
221
        Option('limit', "maximum number of revisions to display",
 
222
               int, 'count')]
 
223
    takes_args = [ "location?" ]
 
224
    aliases = [ "visualize", "vis", "viz" ]
 
225
 
 
226
    def run(self, location=".", revision=None, limit=None):
 
227
        set_ui_factory()
 
228
        (br, path) = branch.Branch.open_containing(location)
 
229
        br.lock_read()
 
230
        br.repository.lock_read()
 
231
        try:
 
232
            if revision is None:
 
233
                revid = br.last_revision()
 
234
                if revid is None:
 
235
                    return
 
236
            else:
 
237
                (revno, revid) = revision[0].in_history(br)
 
238
 
 
239
            from viz.branchwin import BranchWindow
 
240
            import gtk
 
241
                
 
242
            pp = BranchWindow()
 
243
            pp.set_branch(br, revid, limit)
 
244
            pp.connect("destroy", lambda w: gtk.main_quit())
 
245
            pp.show()
 
246
            gtk.main()
 
247
        finally:
 
248
            br.repository.unlock()
 
249
            br.unlock()
 
250
 
 
251
 
 
252
register_command(cmd_visualise)
 
253
 
 
254
class cmd_gannotate(Command):
 
255
    """GTK+ annotate.
 
256
    
 
257
    Browse changes to FILENAME line by line in a GTK+ window.
 
258
    """
 
259
 
 
260
    takes_args = ["filename", "line?"]
 
261
    takes_options = [
 
262
        Option("all", help="show annotations on all lines"),
 
263
        Option("plain", help="don't highlight annotation lines"),
 
264
        Option("line", type=int, argname="lineno",
 
265
               help="jump to specified line number"),
 
266
        "revision",
 
267
    ]
 
268
    aliases = ["gblame", "gpraise"]
 
269
    
 
270
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
271
        pygtk = import_pygtk()
 
272
 
 
273
        try:
 
274
            import gtk
 
275
        except RuntimeError, e:
 
276
            if str(e) == "could not open display":
 
277
                raise NoDisplayError
 
278
        set_ui_factory()
 
279
 
 
280
        try:
 
281
            line = int(line)
 
282
        except ValueError:
 
283
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
 
284
                                  line)
 
285
 
 
286
        from annotate.gannotate import GAnnotateWindow
 
287
        from annotate.config import GAnnotateConfig
 
288
 
 
289
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
 
290
        if wt is not None:
 
291
            tree = wt
 
292
        else:
 
293
            tree = br.basis_tree()
 
294
 
 
295
        file_id = tree.path2id(path)
 
296
 
 
297
        if file_id is None:
 
298
            raise NotVersionedError(filename)
 
299
        if revision is not None:
 
300
            if len(revision) != 1:
 
301
                raise BzrCommandError("Only 1 revion may be specified.")
 
302
            revision_id = revision[0].in_history(br).rev_id
 
303
            tree = br.repository.revision_tree(revision_id)
 
304
        else:
 
305
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
 
306
 
 
307
        window = GAnnotateWindow(all, plain)
 
308
        window.connect("destroy", lambda w: gtk.main_quit())
 
309
        window.set_title(path + " - gannotate")
 
310
        config = GAnnotateConfig(window)
 
311
        window.show()
 
312
        br.lock_read()
 
313
        if wt is not None:
 
314
            wt.lock_read()
 
315
        try:
 
316
            window.annotate(tree, br, file_id)
 
317
            window.jump_to_line(line)
 
318
            gtk.main()
 
319
        finally:
 
320
            br.unlock()
 
321
            if wt is not None:
 
322
                wt.unlock()
 
323
 
 
324
register_command(cmd_gannotate)
 
325
 
 
326
class cmd_gcommit(Command):
 
327
    """GTK+ commit dialog
 
328
 
 
329
    Graphical user interface for committing revisions"""
 
330
    
 
331
    aliases = [ "gci" ]
 
332
    takes_args = []
 
333
    takes_options = []
 
334
 
 
335
    def run(self, filename=None):
 
336
        import os
 
337
        pygtk = import_pygtk()
 
338
 
 
339
        try:
 
340
            import gtk
 
341
        except RuntimeError, e:
 
342
            if str(e) == "could not open display":
 
343
                raise NoDisplayError
 
344
 
 
345
        set_ui_factory()
 
346
        from commit import CommitDialog
 
347
        from bzrlib.commit import Commit
 
348
        from bzrlib.errors import (BzrCommandError,
 
349
                                   NotBranchError,
 
350
                                   NoWorkingTree,
 
351
                                   PointlessCommit,
 
352
                                   ConflictsInTree,
 
353
                                   StrictCommitFailed)
 
354
 
 
355
        wt = None
 
356
        br = None
 
357
        try:
 
358
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
359
            br = wt.branch
 
360
        except NotBranchError, e:
 
361
            path = e.path
 
362
        except NoWorkingTree, e:
 
363
            path = e.base
 
364
            try:
 
365
                (br, path) = branch.Branch.open_containing(path)
 
366
            except NotBranchError, e:
 
367
                path = e.path
 
368
 
 
369
 
 
370
        commit = CommitDialog(wt, path, not br)
 
371
        commit.run()
 
372
 
 
373
register_command(cmd_gcommit)
 
374
 
 
375
class cmd_gstatus(Command):
 
376
    """GTK+ status dialog
 
377
 
 
378
    Graphical user interface for showing status 
 
379
    information."""
 
380
    
 
381
    aliases = [ "gst" ]
 
382
    takes_args = ['PATH?']
 
383
    takes_options = []
 
384
 
 
385
    def run(self, path='.'):
 
386
        import os
 
387
        pygtk = import_pygtk()
 
388
 
 
389
        try:
 
390
            import gtk
 
391
        except RuntimeError, e:
 
392
            if str(e) == "could not open display":
 
393
                raise NoDisplayError
 
394
 
 
395
        set_ui_factory()
 
396
        from status import StatusDialog
 
397
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
 
398
        status = StatusDialog(wt, wt_path)
 
399
        status.connect("destroy", gtk.main_quit)
 
400
        status.run()
 
401
 
 
402
register_command(cmd_gstatus)
 
403
 
 
404
class cmd_gconflicts(Command):
 
405
    """ GTK+ push.
 
406
    
 
407
    """
 
408
    def run(self):
 
409
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
410
        
 
411
        pygtk = import_pygtk()
 
412
        try:
 
413
            import gtk
 
414
        except RuntimeError, e:
 
415
            if str(e) == "could not open display":
 
416
                raise NoDisplayError
 
417
 
 
418
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
419
 
 
420
        set_ui_factory()
 
421
        dialog = ConflictsDialog(wt)
 
422
        dialog.run()
 
423
 
 
424
register_command(cmd_gconflicts)
 
425
 
 
426
import gettext
 
427
gettext.install('olive-gtk')
 
428
 
 
429
class NoDisplayError(BzrCommandError):
 
430
    """gtk could not find a proper display"""
 
431
 
 
432
    def __str__(self):
 
433
        return "No DISPLAY. Unable to run GTK+ application."
 
434
 
 
435
def test_suite():
 
436
    from unittest import TestSuite
 
437
    import tests
162
438
    import sys
163
439
    default_encoding = sys.getdefaultencoding()
164
440
    try:
165
 
        result = basic_tests
166
 
        try:
167
 
            import gi.repository.Gtk
168
 
        except ImportError:
169
 
            return basic_tests
170
 
        basic_tests.addTest(loader.loadTestsFromModuleNames(
171
 
                ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
 
441
        result = TestSuite()
 
442
        result.addTest(tests.test_suite())
172
443
    finally:
173
 
        if sys.getdefaultencoding() != default_encoding:
174
 
            reload(sys)
175
 
            sys.setdefaultencoding(default_encoding)
176
 
    return basic_tests
177
 
 
 
444
        reload(sys)
 
445
        sys.setdefaultencoding(default_encoding)
 
446
    return result