/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
49 by Jelmer Vernooij
Merge in Dan Loda's gannotate plugin and put it in annotate/
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 """
73 by Jelmer Vernooij
Release 0.9, list myself as maintainer.
16
66.2.18 by Aaron Bentley
Gannotate works with branches, not just trees
17
from bzrlib import errors
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
18
from bzrlib.commands import Command, register_command, display_command
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
19
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
20
from bzrlib.commands import Command, register_command
21
from bzrlib.option import Option
22
from bzrlib.branch import Branch
23
from bzrlib.workingtree import WorkingTree
24
from bzrlib.bzrdir import BzrDir
25
126.1.5 by Szilveszter Farkas (Phanatic)
bzr gbranch should work now (Fixed: #77751)
26
import os.path
27
66.2.12 by Jelmer Vernooij
Release 0.13.0.
28
__version__ = '0.13.0'
73 by Jelmer Vernooij
Release 0.9, list myself as maintainer.
29
66.2.20 by Aaron Bentley
Nicer error when PyGTK not installed
30
31
def import_pygtk():
32
    try:
33
        import pygtk
34
    except ImportError:
35
        raise errors.BzrCommandError("PyGTK not installed.")
36
    pygtk.require('2.0')
37
    return pygtk
38
39
133 by Jelmer Vernooij
Actually use the ui factory.
40
def set_ui_factory():
41
    pygtk = import_pygtk()
42
    from olive.ui import GtkUIFactory
43
    import bzrlib.ui
44
    bzrlib.ui.ui_factory = GtkUIFactory()
45
46
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
47
class cmd_gbranch(Command):
48
    """GTK+ branching.
49
    
50
    """
51
52
    def run(self):
66.2.20 by Aaron Bentley
Nicer error when PyGTK not installed
53
        pygtk = import_pygtk()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
54
        try:
55
            import gtk
56
        except RuntimeError, e:
57
            if str(e) == "could not open display":
58
                raise NoDisplayError
59
90 by Jelmer Vernooij
Use Olive's clone dialog in nautilus-bzr; remove the old Clone dialog
60
        from bzrlib.plugins.gtk.olive.branch import BranchDialog
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
61
133 by Jelmer Vernooij
Actually use the ui factory.
62
        set_ui_factory()
126.1.5 by Szilveszter Farkas (Phanatic)
bzr gbranch should work now (Fixed: #77751)
63
        dialog = BranchDialog(os.path.abspath('.'))
64
        dialog.window.connect("destroy", lambda w: gtk.main_quit())
65
        dialog.display()
66
        
67
        gtk.main()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
68
69
register_command(cmd_gbranch)
70
71
class cmd_gdiff(Command):
72
    """Show differences in working tree in a GTK+ Window.
73
    
74
    Otherwise, all changes for the tree are listed.
75
    """
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
76
    takes_args = ['filename?']
58.1.1 by Aaron Bentley
gdiff takes -r arguments
77
    takes_options = ['revision']
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
78
79
    @display_command
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
80
    def run(self, revision=None, filename=None):
133 by Jelmer Vernooij
Actually use the ui factory.
81
        set_ui_factory()
58.1.1 by Aaron Bentley
gdiff takes -r arguments
82
        wt = WorkingTree.open_containing(".")[0]
83
        branch = wt.branch
84
        if revision is not None:
85
            if len(revision) == 1:
86
                tree1 = wt
87
                revision_id = revision[0].in_history(branch).rev_id
88
                tree2 = branch.repository.revision_tree(revision_id)
89
            elif len(revision) == 2:
90
                revision_id_0 = revision[0].in_history(branch).rev_id
91
                tree2 = branch.repository.revision_tree(revision_id_0)
92
                revision_id_1 = revision[1].in_history(branch).rev_id
93
                tree1 = branch.repository.revision_tree(revision_id_1)
94
        else:
95
            tree1 = wt
96
            tree2 = tree1.basis_tree()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
97
93.1.8 by Alexander Belchenko
Fix absolute import in gdiff command.
98
        from viz.diffwin import DiffWindow
58 by Jelmer Vernooij
[merge] Home
99
        import gtk
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
100
        window = DiffWindow()
101
        window.connect("destroy", lambda w: gtk.main_quit())
102
        window.set_diff("Working Tree", tree1, tree2)
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
103
        if filename is not None:
93.1.16 by Alexander Belchenko
Partial fix for problem with encodings in diff window
104
            tree_filename = wt.relpath(filename)
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
105
            try:
106
                window.set_file(tree_filename)
107
            except NoSuchFile:
108
                if (tree1.inventory.path2id(tree_filename) is None and 
109
                    tree2.inventory.path2id(tree_filename) is None):
110
                    raise NotVersionedError(filename)
111
                raise BzrCommandError('No changes found for file "%s"' % 
112
                                      filename)
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
113
        window.show()
114
115
        gtk.main()
116
117
register_command(cmd_gdiff)
118
119
class cmd_visualise(Command):
120
    """Graphically visualise this branch.
121
122
    Opens a graphical window to allow you to see the history of the branch
123
    and relationships between revisions in a visual manner,
124
125
    The default starting point is latest revision on the branch, you can
126
    specify a starting point with -r revision.
127
    """
128
    takes_options = [
129
        "revision",
130
        Option('limit', "maximum number of revisions to display",
131
               int, 'count')]
132
    takes_args = [ "location?" ]
133
    aliases = [ "visualize", "vis", "viz" ]
134
135
    def run(self, location=".", revision=None, limit=None):
133 by Jelmer Vernooij
Actually use the ui factory.
136
        set_ui_factory()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
137
        (branch, path) = Branch.open_containing(location)
138
        branch.lock_read()
139
        branch.repository.lock_read()
140
        try:
141
            if revision is None:
142
                revid = branch.last_revision()
143
                if revid is None:
144
                    return
145
            else:
146
                (revno, revid) = revision[0].in_history(branch)
147
148
            from viz.bzrkapp import BzrkApp
149
                
150
            app = BzrkApp()
151
            app.show(branch, revid, limit)
152
        finally:
153
            branch.repository.unlock()
154
            branch.unlock()
155
        app.main()
156
157
158
register_command(cmd_visualise)
159
160
class cmd_gannotate(Command):
161
    """GTK+ annotate.
162
    
163
    Browse changes to FILENAME line by line in a GTK+ window.
164
    """
165
59.2.1 by Aaron Bentley
Gannotate takes a line number
166
    takes_args = ["filename", "line?"]
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
167
    takes_options = [
168
        Option("all", help="show annotations on all lines"),
169
        Option("plain", help="don't highlight annotation lines"),
170
        Option("line", type=int, argname="lineno",
66.2.1 by Aaron Bentley
Gannotate takes a revision argument
171
               help="jump to specified line number"),
172
        "revision",
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
173
    ]
174
    aliases = ["gblame", "gpraise"]
175
    
66.2.1 by Aaron Bentley
Gannotate takes a revision argument
176
    def run(self, filename, all=False, plain=False, line='1', revision=None):
66.2.20 by Aaron Bentley
Nicer error when PyGTK not installed
177
        pygtk = import_pygtk()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
178
179
        try:
180
            import gtk
181
        except RuntimeError, e:
182
            if str(e) == "could not open display":
183
                raise NoDisplayError
133 by Jelmer Vernooij
Actually use the ui factory.
184
        set_ui_factory()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
185
59.2.1 by Aaron Bentley
Gannotate takes a line number
186
        try:
187
            line = int(line)
188
        except ValueError:
189
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
190
                                  line)
191
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
192
        from annotate.gannotate import GAnnotateWindow
193
        from annotate.config import GAnnotateConfig
194
66.2.18 by Aaron Bentley
Gannotate works with branches, not just trees
195
        try:
196
            (tree, path) = WorkingTree.open_containing(filename)
197
            branch = tree.branch
198
        except errors.NoWorkingTree:
199
            (branch, path) = Branch.open_containing(filename)
200
            tree = branch.basis_tree()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
201
66.2.18 by Aaron Bentley
Gannotate works with branches, not just trees
202
        file_id = tree.path2id(path)
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
203
204
        if file_id is None:
205
            raise NotVersionedError(filename)
66.2.1 by Aaron Bentley
Gannotate takes a revision argument
206
        if revision is not None:
207
            if len(revision) != 1:
208
                raise BzrCommandError("Only 1 revion may be specified.")
209
            revision_id = revision[0].in_history(branch).rev_id
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
210
            tree = branch.repository.revision_tree(revision_id)
66.2.1 by Aaron Bentley
Gannotate takes a revision argument
211
        else:
66.2.18 by Aaron Bentley
Gannotate works with branches, not just trees
212
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
213
214
        window = GAnnotateWindow(all, plain)
215
        window.connect("destroy", lambda w: gtk.main_quit())
216
        window.set_title(path + " - gannotate")
217
        config = GAnnotateConfig(window)
218
        window.show()
219
        branch.lock_read()
220
        try:
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
221
            window.annotate(tree, branch, file_id)
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
222
        finally:
223
            branch.unlock()
224
        window.jump_to_line(line)
225
        
226
        gtk.main()
227
228
register_command(cmd_gannotate)
229
230
class cmd_gcommit(Command):
231
    """GTK+ commit dialog
232
233
    Graphical user interface for committing revisions"""
234
    
235
    takes_args = []
236
    takes_options = []
237
238
    def run(self, filename=None):
93.1.17 by Alexander Belchenko
gcommit reworked again.
239
        import os
66.2.20 by Aaron Bentley
Nicer error when PyGTK not installed
240
        pygtk = import_pygtk()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
241
242
        try:
243
            import gtk
244
        except RuntimeError, e:
245
            if str(e) == "could not open display":
246
                raise NoDisplayError
247
133 by Jelmer Vernooij
Actually use the ui factory.
248
        set_ui_factory()
90 by Jelmer Vernooij
Use Olive's clone dialog in nautilus-bzr; remove the old Clone dialog
249
        from olive.commit import CommitDialog
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
250
        from bzrlib.commit import Commit
93.1.17 by Alexander Belchenko
gcommit reworked again.
251
        from bzrlib.errors import (BzrCommandError,
252
                                   NotBranchError,
253
                                   NoWorkingTree,
254
                                   PointlessCommit,
255
                                   ConflictsInTree,
256
                                   StrictCommitFailed)
257
258
        wt = None
259
        branch = None
260
        try:
261
            (wt, path) = WorkingTree.open_containing(filename)
262
            branch = wt.branch
263
        except NotBranchError, e:
264
            path = e.path
265
        except NoWorkingTree, e:
266
            path = e.base
267
            try:
268
                (branch, path) = Branch.open_containing(path)
269
            except NotBranchError, e:
270
                path = e.path
271
135 by Jelmer Vernooij
Throw out the old CommitDialog code and use the new code instead, also for 'gcommit'.
272
273
        commit = CommitDialog(wt, path, not branch)
274
        commit.run()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
275
276
register_command(cmd_gcommit)
277
278
class NoDisplayError(BzrCommandError):
279
    """gtk could not find a proper display"""
280
281
    def __str__(self):
133 by Jelmer Vernooij
Actually use the ui factory.
282
        return "No DISPLAY. Unable to run GTK+ application."
283
284