/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-06 13:48:35 UTC
  • mfrom: (157.1.9 trunk)
  • Revision ID: jelmer@samba.org-20070306134835-4e3gg9sq0ovvzwad
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
5
 
 
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.
 
10
 
 
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
 
14
 
 
15
"""GTK+ frontends to Bazaar commands """
 
16
 
 
17
import bzrlib
 
18
 
 
19
__version__ = '0.15.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
 
56
if __name__ != 'bzrlib.plugins.gtk':
 
57
    warning("Not running as bzrlib.plugins.gtk, things may break.")
 
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
 
 
84
 
 
85
def set_ui_factory():
 
86
    pygtk = import_pygtk()
 
87
    from ui import GtkUIFactory
 
88
    import bzrlib.ui
 
89
    bzrlib.ui.ui_factory = GtkUIFactory()
 
90
 
 
91
 
 
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
        branch = wt.branch
 
171
        if revision is not None:
 
172
            if len(revision) == 1:
 
173
                tree1 = wt
 
174
                revision_id = revision[0].in_history(branch).rev_id
 
175
                tree2 = branch.repository.revision_tree(revision_id)
 
176
            elif len(revision) == 2:
 
177
                revision_id_0 = revision[0].in_history(branch).rev_id
 
178
                tree2 = branch.repository.revision_tree(revision_id_0)
 
179
                revision_id_1 = revision[1].in_history(branch).rev_id
 
180
                tree1 = branch.repository.revision_tree(revision_id_1)
 
181
        else:
 
182
            tree1 = wt
 
183
            tree2 = tree1.basis_tree()
 
184
 
 
185
        from diff import DiffWindow
 
186
        import gtk
 
187
        window = DiffWindow()
 
188
        window.connect("destroy", gtk.main_quit)
 
189
        window.set_diff("Working Tree", tree1, tree2)
 
190
        if filename is not None:
 
191
            tree_filename = wt.relpath(filename)
 
192
            try:
 
193
                window.set_file(tree_filename)
 
194
            except NoSuchFile:
 
195
                if (tree1.inventory.path2id(tree_filename) is None and 
 
196
                    tree2.inventory.path2id(tree_filename) is None):
 
197
                    raise NotVersionedError(filename)
 
198
                raise BzrCommandError('No changes found for file "%s"' % 
 
199
                                      filename)
 
200
        window.show()
 
201
 
 
202
        gtk.main()
 
203
 
 
204
register_command(cmd_gdiff)
 
205
 
 
206
class cmd_visualise(Command):
 
207
    """Graphically visualise this branch.
 
208
 
 
209
    Opens a graphical window to allow you to see the history of the branch
 
210
    and relationships between revisions in a visual manner,
 
211
 
 
212
    The default starting point is latest revision on the branch, you can
 
213
    specify a starting point with -r revision.
 
214
    """
 
215
    takes_options = [
 
216
        "revision",
 
217
        Option('limit', "maximum number of revisions to display",
 
218
               int, 'count')]
 
219
    takes_args = [ "location?" ]
 
220
    aliases = [ "visualize", "vis", "viz" ]
 
221
 
 
222
    def run(self, location=".", revision=None, limit=None):
 
223
        set_ui_factory()
 
224
        (br, path) = branch.Branch.open_containing(location)
 
225
        br.lock_read()
 
226
        br.repository.lock_read()
 
227
        try:
 
228
            if revision is None:
 
229
                revid = br.last_revision()
 
230
                if revid is None:
 
231
                    return
 
232
            else:
 
233
                (revno, revid) = revision[0].in_history(br)
 
234
 
 
235
            from viz.branchwin import BranchWindow
 
236
            import gtk
 
237
                
 
238
            pp = BranchWindow()
 
239
            pp.set_branch(br, revid, limit)
 
240
            pp.connect("destroy", lambda w: gtk.main_quit())
 
241
            pp.show()
 
242
            gtk.main()
 
243
        finally:
 
244
            br.repository.unlock()
 
245
            br.unlock()
 
246
 
 
247
 
 
248
register_command(cmd_visualise)
 
249
 
 
250
class cmd_gannotate(Command):
 
251
    """GTK+ annotate.
 
252
    
 
253
    Browse changes to FILENAME line by line in a GTK+ window.
 
254
    """
 
255
 
 
256
    takes_args = ["filename", "line?"]
 
257
    takes_options = [
 
258
        Option("all", help="show annotations on all lines"),
 
259
        Option("plain", help="don't highlight annotation lines"),
 
260
        Option("line", type=int, argname="lineno",
 
261
               help="jump to specified line number"),
 
262
        "revision",
 
263
    ]
 
264
    aliases = ["gblame", "gpraise"]
 
265
    
 
266
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
267
        pygtk = import_pygtk()
 
268
 
 
269
        try:
 
270
            import gtk
 
271
        except RuntimeError, e:
 
272
            if str(e) == "could not open display":
 
273
                raise NoDisplayError
 
274
        set_ui_factory()
 
275
 
 
276
        try:
 
277
            line = int(line)
 
278
        except ValueError:
 
279
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
 
280
                                  line)
 
281
 
 
282
        from annotate.gannotate import GAnnotateWindow
 
283
        from annotate.config import GAnnotateConfig
 
284
 
 
285
        try:
 
286
            (tree, path) = workingtree.WorkingTree.open_containing(filename)
 
287
            br = tree.branch
 
288
        except errors.NoWorkingTree:
 
289
            (br, path) = branch.Branch.open_containing(filename)
 
290
            tree = br.basis_tree()
 
291
 
 
292
        file_id = tree.path2id(path)
 
293
 
 
294
        if file_id is None:
 
295
            raise NotVersionedError(filename)
 
296
        if revision is not None:
 
297
            if len(revision) != 1:
 
298
                raise BzrCommandError("Only 1 revion may be specified.")
 
299
            revision_id = revision[0].in_history(br).rev_id
 
300
            tree = br.repository.revision_tree(revision_id)
 
301
        else:
 
302
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
 
303
 
 
304
        window = GAnnotateWindow(all, plain)
 
305
        window.connect("destroy", lambda w: gtk.main_quit())
 
306
        window.set_title(path + " - gannotate")
 
307
        config = GAnnotateConfig(window)
 
308
        window.show()
 
309
        br.lock_read()
 
310
        try:
 
311
            window.annotate(tree, br, file_id)
 
312
        finally:
 
313
            br.unlock()
 
314
        window.jump_to_line(line)
 
315
        
 
316
        gtk.main()
 
317
 
 
318
register_command(cmd_gannotate)
 
319
 
 
320
class cmd_gcommit(Command):
 
321
    """GTK+ commit dialog
 
322
 
 
323
    Graphical user interface for committing revisions"""
 
324
    
 
325
    aliases = [ "gci" ]
 
326
    takes_args = []
 
327
    takes_options = []
 
328
 
 
329
    def run(self, filename=None):
 
330
        import os
 
331
        pygtk = import_pygtk()
 
332
 
 
333
        try:
 
334
            import gtk
 
335
        except RuntimeError, e:
 
336
            if str(e) == "could not open display":
 
337
                raise NoDisplayError
 
338
 
 
339
        set_ui_factory()
 
340
        from commit import CommitDialog
 
341
        from bzrlib.commit import Commit
 
342
        from bzrlib.errors import (BzrCommandError,
 
343
                                   NotBranchError,
 
344
                                   NoWorkingTree,
 
345
                                   PointlessCommit,
 
346
                                   ConflictsInTree,
 
347
                                   StrictCommitFailed)
 
348
 
 
349
        wt = None
 
350
        br = None
 
351
        try:
 
352
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
353
            br = wt.branch
 
354
        except NotBranchError, e:
 
355
            path = e.path
 
356
        except NoWorkingTree, e:
 
357
            path = e.base
 
358
            try:
 
359
                (br, path) = branch.Branch.open_containing(path)
 
360
            except NotBranchError, e:
 
361
                path = e.path
 
362
 
 
363
 
 
364
        commit = CommitDialog(wt, path, not br)
 
365
        commit.run()
 
366
 
 
367
register_command(cmd_gcommit)
 
368
 
 
369
class cmd_gstatus(Command):
 
370
    """GTK+ status dialog
 
371
 
 
372
    Graphical user interface for showing status 
 
373
    information."""
 
374
    
 
375
    aliases = [ "gst" ]
 
376
    takes_args = ['PATH?']
 
377
    takes_options = []
 
378
 
 
379
    def run(self, path='.'):
 
380
        import os
 
381
        pygtk = import_pygtk()
 
382
 
 
383
        try:
 
384
            import gtk
 
385
        except RuntimeError, e:
 
386
            if str(e) == "could not open display":
 
387
                raise NoDisplayError
 
388
 
 
389
        set_ui_factory()
 
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)
 
394
        status.run()
 
395
 
 
396
register_command(cmd_gstatus)
 
397
 
 
398
class cmd_gconflicts(Command):
 
399
    """ GTK+ push.
 
400
    
 
401
    """
 
402
    def run(self):
 
403
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
404
        
 
405
        pygtk = import_pygtk()
 
406
        try:
 
407
            import gtk
 
408
        except RuntimeError, e:
 
409
            if str(e) == "could not open display":
 
410
                raise NoDisplayError
 
411
 
 
412
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
413
 
 
414
        set_ui_factory()
 
415
        dialog = ConflictsDialog(wt)
 
416
        dialog.run()
 
417
 
 
418
register_command(cmd_gconflicts)
 
419
 
 
420
import gettext
 
421
gettext.install('olive-gtk')
 
422
 
 
423
class NoDisplayError(BzrCommandError):
 
424
    """gtk could not find a proper display"""
 
425
 
 
426
    def __str__(self):
 
427
        return "No DISPLAY. Unable to run GTK+ application."
 
428
 
 
429
def test_suite():
 
430
    from unittest import TestSuite
 
431
    import tests
 
432
    result = TestSuite()
 
433
    result.addTest(tests.test_suite())
 
434
    return result