/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 commands.py

  • Committer: Jelmer Vernooij
  • Date: 2008-06-29 18:12:29 UTC
  • mto: This revision was merged to the branch mainline in revision 519.
  • Revision ID: jelmer@samba.org-20080629181229-1l2m4cf7vvbyh8qg
Simplify progress bar code, use embedded progress bar inside viz window.

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
 
from bzrlib import (
16
 
    branch,
17
 
    builtins,
18
 
    merge_directive,
19
 
    workingtree,
20
 
    )
21
 
from bzrlib.commands import (
22
 
    Command,
23
 
    display_command,
24
 
    )
25
 
from bzrlib.errors import (
26
 
    BzrCommandError,
27
 
    NotVersionedError,
28
 
    NoSuchFile,
29
 
    )
30
 
from bzrlib.option import Option
31
 
 
32
 
from bzrlib.plugins.gtk import (
33
 
    open_display,
34
 
    import_pygtk,
35
 
    set_ui_factory,
36
 
    )
37
 
 
38
 
class GTKCommand(Command):
39
 
    """Abstract class providing GTK specific run commands."""
40
 
 
41
 
    def run(self):
42
 
        open_display()
43
 
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
44
 
        dialog.run()
45
 
 
46
 
 
47
 
class cmd_gbranch(GTKCommand):
48
 
    """GTK+ branching.
49
 
    
50
 
    """
51
 
 
52
 
    def get_gtk_dialog(self, path):
53
 
        from bzrlib.plugins.gtk.branch import BranchDialog
54
 
        return BranchDialog(path)
55
 
 
56
 
 
57
 
class cmd_gcheckout(GTKCommand):
58
 
    """ GTK+ checkout.
59
 
    
60
 
    """
61
 
    
62
 
    def get_gtk_dialog(self, path):
63
 
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
64
 
        return CheckoutDialog(path)
65
 
 
66
 
 
67
 
 
68
 
class cmd_gpush(GTKCommand):
69
 
    """ GTK+ push.
70
 
    
71
 
    """
72
 
    takes_args = [ "location?" ]
73
 
 
74
 
    def run(self, location="."):
75
 
        (br, path) = branch.Branch.open_containing(location)
76
 
        open_display()
77
 
        from bzrlib.plugins.gtk.push import PushDialog
78
 
        dialog = PushDialog(br.repository, br.last_revision(), br)
79
 
        dialog.run()
80
 
 
81
 
 
82
 
class cmd_gloom(GTKCommand):
83
 
    """ GTK+ loom.
84
 
    
85
 
    """
86
 
    takes_args = [ "location?" ]
87
 
 
88
 
    def run(self, location="."):
89
 
        try:
90
 
            (tree, path) = workingtree.WorkingTree.open_containing(location)
91
 
            br = tree.branch
92
 
        except NoWorkingTree, e:
93
 
            (br, path) = branch.Branch.open_containing(location)
94
 
            tree = None
95
 
        open_display()
96
 
        from bzrlib.plugins.gtk.loom import LoomDialog
97
 
        dialog = LoomDialog(br, tree)
98
 
        dialog.run()
99
 
 
100
 
 
101
 
class cmd_gdiff(GTKCommand):
102
 
    """Show differences in working tree in a GTK+ Window.
103
 
    
104
 
    Otherwise, all changes for the tree are listed.
105
 
    """
106
 
    takes_args = ['filename?']
107
 
    takes_options = ['revision']
108
 
 
109
 
    @display_command
110
 
    def run(self, revision=None, filename=None):
111
 
        set_ui_factory()
112
 
        wt = workingtree.WorkingTree.open_containing(".")[0]
113
 
        wt.lock_read()
114
 
        try:
115
 
            branch = wt.branch
116
 
            if revision is not None:
117
 
                if len(revision) == 1:
118
 
                    tree1 = wt
119
 
                    revision_id = revision[0].as_revision_id(tree1.branch)
120
 
                    tree2 = branch.repository.revision_tree(revision_id)
121
 
                elif len(revision) == 2:
122
 
                    revision_id_0 = revision[0].as_revision_id(branch)
123
 
                    tree2 = branch.repository.revision_tree(revision_id_0)
124
 
                    revision_id_1 = revision[1].as_revision_id(branch)
125
 
                    tree1 = branch.repository.revision_tree(revision_id_1)
126
 
            else:
127
 
                tree1 = wt
128
 
                tree2 = tree1.basis_tree()
129
 
 
130
 
            from diff import DiffWindow
131
 
            import gtk
132
 
            window = DiffWindow()
133
 
            window.connect("destroy", gtk.main_quit)
134
 
            window.set_diff("Working Tree", tree1, tree2)
135
 
            if filename is not None:
136
 
                tree_filename = wt.relpath(filename)
137
 
                try:
138
 
                    window.set_file(tree_filename)
139
 
                except NoSuchFile:
140
 
                    if (tree1.path2id(tree_filename) is None and 
141
 
                        tree2.path2id(tree_filename) is None):
142
 
                        raise NotVersionedError(filename)
143
 
                    raise BzrCommandError('No changes found for file "%s"' % 
144
 
                                          filename)
145
 
            window.show()
146
 
 
147
 
            gtk.main()
148
 
        finally:
149
 
            wt.unlock()
150
 
 
151
 
 
152
 
def start_viz_window(branch, revisions, limit=None):
153
 
    """Start viz on branch with revision revision.
154
 
    
155
 
    :return: The viz window object.
156
 
    """
157
 
    from bzrlib.plugins.gtk.viz import BranchWindow
158
 
    return BranchWindow(branch, revisions, limit)
159
 
 
160
 
 
161
 
class cmd_visualise(Command):
162
 
    """Graphically visualise this branch.
163
 
 
164
 
    Opens a graphical window to allow you to see the history of the branch
165
 
    and relationships between revisions in a visual manner,
166
 
 
167
 
    The default starting point is latest revision on the branch, you can
168
 
    specify a starting point with -r revision.
169
 
    """
170
 
    takes_options = [
171
 
        "revision",
172
 
        Option('limit', "Maximum number of revisions to display.",
173
 
               int, 'count')]
174
 
    takes_args = [ "locations*" ]
175
 
    aliases = [ "visualize", "vis", "viz" ]
176
 
 
177
 
    def run(self, locations_list, revision=None, limit=None):
178
 
        set_ui_factory()
179
 
        if locations_list is None:
180
 
            locations_list = ["."]
181
 
        revids = []
182
 
        for location in locations_list:
183
 
            (br, path) = branch.Branch.open_containing(location)
184
 
            if revision is None:
185
 
                revids.append(br.last_revision())
186
 
            else:
187
 
                revids.append(revision[0].as_revision_id(br))
188
 
        import gtk
189
 
        pp = start_viz_window(br, revids, limit)
190
 
        pp.connect("destroy", lambda w: gtk.main_quit())
191
 
        pp.show()
192
 
        gtk.main()
193
 
 
194
 
 
195
 
class cmd_gannotate(GTKCommand):
196
 
    """GTK+ annotate.
197
 
    
198
 
    Browse changes to FILENAME line by line in a GTK+ window.
199
 
    """
200
 
 
201
 
    takes_args = ["filename", "line?"]
202
 
    takes_options = [
203
 
        Option("all", help="Show annotations on all lines."),
204
 
        Option("plain", help="Don't highlight annotation lines."),
205
 
        Option("line", type=int, argname="lineno",
206
 
               help="Jump to specified line number."),
207
 
        "revision",
208
 
    ]
209
 
    aliases = ["gblame", "gpraise"]
210
 
    
211
 
    def run(self, filename, all=False, plain=False, line='1', revision=None):
212
 
        gtk = open_display()
213
 
 
214
 
        try:
215
 
            line = int(line)
216
 
        except ValueError:
217
 
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
218
 
                                  line)
219
 
 
220
 
        from annotate.gannotate import GAnnotateWindow
221
 
        from annotate.config import GAnnotateConfig
222
 
        from bzrlib.bzrdir import BzrDir
223
 
 
224
 
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
225
 
        if wt is not None:
226
 
            tree = wt
227
 
        else:
228
 
            tree = br.basis_tree()
229
 
 
230
 
        file_id = tree.path2id(path)
231
 
 
232
 
        if file_id is None:
233
 
            raise NotVersionedError(filename)
234
 
        if revision is not None:
235
 
            if len(revision) != 1:
236
 
                raise BzrCommandError("Only 1 revion may be specified.")
237
 
            revision_id = revision[0].as_revision_id(br)
238
 
            tree = br.repository.revision_tree(revision_id)
239
 
        else:
240
 
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
241
 
 
242
 
        window = GAnnotateWindow(all, plain, branch=br)
243
 
        window.connect("destroy", lambda w: gtk.main_quit())
244
 
        config = GAnnotateConfig(window)
245
 
        window.show()
246
 
        br.lock_read()
247
 
        if wt is not None:
248
 
            wt.lock_read()
249
 
        try:
250
 
            window.annotate(tree, br, file_id)
251
 
            window.jump_to_line(line)
252
 
            gtk.main()
253
 
        finally:
254
 
            br.unlock()
255
 
            if wt is not None:
256
 
                wt.unlock()
257
 
 
258
 
 
259
 
 
260
 
class cmd_gcommit(GTKCommand):
261
 
    """GTK+ commit dialog
262
 
 
263
 
    Graphical user interface for committing revisions"""
264
 
 
265
 
    aliases = [ "gci" ]
266
 
    takes_args = []
267
 
    takes_options = []
268
 
 
269
 
    def run(self, filename=None):
270
 
        import os
271
 
        open_display()
272
 
        from commit import CommitDialog
273
 
        from bzrlib.errors import (BzrCommandError,
274
 
                                   NotBranchError,
275
 
                                   NoWorkingTree)
276
 
 
277
 
        wt = None
278
 
        br = None
279
 
        try:
280
 
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
281
 
            br = wt.branch
282
 
        except NoWorkingTree, e:
283
 
            from dialog import error_dialog
284
 
            error_dialog(_i18n('Directory does not have a working tree'),
285
 
                         _i18n('Operation aborted.'))
286
 
            return 1 # should this be retval=3?
287
 
 
288
 
        # It is a good habit to keep things locked for the duration, but it
289
 
        # could cause difficulties if someone wants to do things in another
290
 
        # window... We could lock_read() until we actually go to commit
291
 
        # changes... Just a thought.
292
 
        wt.lock_write()
293
 
        try:
294
 
            dlg = CommitDialog(wt)
295
 
            return dlg.run()
296
 
        finally:
297
 
            wt.unlock()
298
 
 
299
 
 
300
 
class cmd_gstatus(GTKCommand):
301
 
    """GTK+ status dialog
302
 
 
303
 
    Graphical user interface for showing status 
304
 
    information."""
305
 
 
306
 
    aliases = [ "gst" ]
307
 
    takes_args = ['PATH?']
308
 
    takes_options = ['revision']
309
 
 
310
 
    def run(self, path='.', revision=None):
311
 
        import os
312
 
        gtk = open_display()
313
 
        from bzrlib.plugins.gtk.status import StatusWindow
314
 
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
315
 
 
316
 
        if revision is not None:
317
 
            try:
318
 
                revision_id = revision[0].as_revision_id(wt.branch)
319
 
            except:
320
 
                from bzrlib.errors import BzrError
321
 
                raise BzrError('Revision %r doesn\'t exist'
322
 
                               % revision[0].user_spec )
323
 
        else:
324
 
            revision_id = None
325
 
 
326
 
        status = StatusWindow(wt, wt_path, revision_id)
327
 
        status.connect("destroy", gtk.main_quit)
328
 
        status.show()
329
 
        gtk.main()
330
 
 
331
 
 
332
 
class cmd_gsend(GTKCommand):
333
 
    """GTK+ send merge directive.
334
 
 
335
 
    """
336
 
    def run(self):
337
 
        (br, path) = branch.Branch.open_containing(".")
338
 
        gtk = open_display()
339
 
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
340
 
        from StringIO import StringIO
341
 
        dialog = SendMergeDirectiveDialog(br)
342
 
        if dialog.run() == gtk.RESPONSE_OK:
343
 
            outf = StringIO()
344
 
            outf.writelines(dialog.get_merge_directive().to_lines())
345
 
            mail_client = br.get_config().get_mail_client()
346
 
            mail_client.compose_merge_request(dialog.get_mail_to(), "[MERGE]", 
347
 
                outf.getvalue())
348
 
 
349
 
            
350
 
 
351
 
 
352
 
class cmd_gconflicts(GTKCommand):
353
 
    """GTK+ conflicts.
354
 
    
355
 
    Select files from the list of conflicts and run an external utility to
356
 
    resolve them.
357
 
    """
358
 
    def run(self):
359
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
360
 
        open_display()
361
 
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
362
 
        dialog = ConflictsDialog(wt)
363
 
        dialog.run()
364
 
 
365
 
 
366
 
class cmd_gpreferences(GTKCommand):
367
 
    """ GTK+ preferences dialog.
368
 
 
369
 
    """
370
 
    def run(self):
371
 
        open_display()
372
 
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
373
 
        dialog = PreferencesWindow()
374
 
        dialog.run()
375
 
 
376
 
 
377
 
class cmd_ginfo(Command):
378
 
    """ GTK+ info dialog
379
 
    
380
 
    """
381
 
    def run(self):
382
 
        from bzrlib import workingtree
383
 
        from bzrlib.plugins.gtk.olive.info import InfoDialog
384
 
        wt = workingtree.WorkingTree.open_containing('.')[0]
385
 
        info = InfoDialog(wt.branch)
386
 
        info.display()
387
 
        info.window.run()
388
 
 
389
 
 
390
 
class cmd_gmerge(Command):
391
 
    """ GTK+ merge dialog
392
 
    
393
 
    """
394
 
    takes_args = ["merge_from_path?"]
395
 
    def run(self, merge_from_path=None):
396
 
        from bzrlib.plugins.gtk.dialog import error_dialog
397
 
        from bzrlib.plugins.gtk.merge import MergeDialog
398
 
        
399
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
400
 
        old_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
401
 
        delta = wt.changes_from(old_tree)
402
 
        if len(delta.added) or len(delta.removed) or len(delta.renamed) or len(delta.modified):
403
 
            error_dialog(_i18n('There are local changes in the branch'),
404
 
                         _i18n('Please commit or revert the changes before merging.'))
405
 
        else:
406
 
            parent_branch_path = wt.branch.get_parent()
407
 
            merge = MergeDialog(wt, path, parent_branch_path)
408
 
            response = merge.run()
409
 
            merge.destroy()
410
 
 
411
 
 
412
 
class cmd_gmissing(Command):
413
 
    """ GTK+ missing revisions dialog.
414
 
 
415
 
    """
416
 
    takes_args = ["other_branch?"]
417
 
    def run(self, other_branch=None):
418
 
        pygtk = import_pygtk()
419
 
        try:
420
 
            import gtk
421
 
        except RuntimeError, e:
422
 
            if str(e) == "could not open display":
423
 
                raise NoDisplayError
424
 
 
425
 
        from bzrlib.plugins.gtk.missing import MissingWindow
426
 
        from bzrlib.branch import Branch
427
 
 
428
 
        local_branch = Branch.open_containing(".")[0]
429
 
        if other_branch is None:
430
 
            other_branch = local_branch.get_parent()
431
 
            
432
 
            if other_branch is None:
433
 
                raise errors.BzrCommandError("No peer location known or specified.")
434
 
        remote_branch = Branch.open_containing(other_branch)[0]
435
 
        set_ui_factory()
436
 
        local_branch.lock_read()
437
 
        try:
438
 
            remote_branch.lock_read()
439
 
            try:
440
 
                dialog = MissingWindow(local_branch, remote_branch)
441
 
                dialog.run()
442
 
            finally:
443
 
                remote_branch.unlock()
444
 
        finally:
445
 
            local_branch.unlock()
446
 
 
447
 
 
448
 
class cmd_ginit(GTKCommand):
449
 
    def run(self):
450
 
        open_display()
451
 
        from initialize import InitDialog
452
 
        dialog = InitDialog(os.path.abspath(os.path.curdir))
453
 
        dialog.run()
454
 
 
455
 
 
456
 
class cmd_gtags(GTKCommand):
457
 
    def run(self):
458
 
        br = branch.Branch.open_containing('.')[0]
459
 
        
460
 
        gtk = open_display()
461
 
        from tags import TagsWindow
462
 
        window = TagsWindow(br)
463
 
        window.show()
464
 
        gtk.main()
465
 
 
466
 
 
467
 
class cmd_gselftest(GTKCommand):
468
 
    """Version of selftest that displays a notification at the end"""
469
 
 
470
 
    takes_args = builtins.cmd_selftest.takes_args
471
 
    takes_options = builtins.cmd_selftest.takes_options
472
 
    _see_also = ['selftest']
473
 
 
474
 
    def run(self, *args, **kwargs):
475
 
        import cgi
476
 
        import sys
477
 
        default_encoding = sys.getdefaultencoding()
478
 
        # prevent gtk from blowing up later
479
 
        gtk = import_pygtk()
480
 
        # prevent gtk from messing with default encoding
481
 
        import pynotify
482
 
        if sys.getdefaultencoding() != default_encoding:
483
 
            reload(sys)
484
 
            sys.setdefaultencoding(default_encoding)
485
 
        result = builtins.cmd_selftest().run(*args, **kwargs)
486
 
        if result == 0:
487
 
            summary = 'Success'
488
 
            body = 'Selftest succeeded in "%s"' % os.getcwd()
489
 
        if result == 1:
490
 
            summary = 'Failure'
491
 
            body = 'Selftest failed in "%s"' % os.getcwd()
492
 
        pynotify.init("bzr gselftest")
493
 
        note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
494
 
        note.set_timeout(pynotify.EXPIRES_NEVER)
495
 
        note.show()