/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
#!/usr/bin/python
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""GTK+ frontends to Bazaar commands """
73 by Jelmer Vernooij
Release 0.9, list myself as maintainer.
18
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
19
from bzrlib.commands import Command, register_command, display_command
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
20
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
21
from bzrlib.commands import Command, register_command
22
from bzrlib.option import Option
23
from bzrlib.branch import Branch
24
from bzrlib.workingtree import WorkingTree
25
from bzrlib.bzrdir import BzrDir
26
77 by Jelmer Vernooij
Prepare for 0.10.0
27
__version__ = '0.10.0'
73 by Jelmer Vernooij
Release 0.9, list myself as maintainer.
28
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
29
class cmd_gbranch(Command):
30
    """GTK+ branching.
31
    
32
    """
33
34
    def run(self):
35
        import pygtk
36
        pygtk.require("2.0")
37
        try:
38
            import gtk
39
        except RuntimeError, e:
40
            if str(e) == "could not open display":
41
                raise NoDisplayError
42
55.1.3 by Jelmer Vernooij
Implement simple command for cloning branches
43
        from clone import CloneDialog
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
44
55.1.3 by Jelmer Vernooij
Implement simple command for cloning branches
45
        window = CloneDialog()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
46
        if window.run() == gtk.RESPONSE_OK:
55.1.3 by Jelmer Vernooij
Implement simple command for cloning branches
47
            bzrdir = BzrDir.open(window.url)
48
            bzrdir.sprout(window.dest_path)
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
49
50
register_command(cmd_gbranch)
51
52
class cmd_gdiff(Command):
53
    """Show differences in working tree in a GTK+ Window.
54
    
55
    Otherwise, all changes for the tree are listed.
56
    """
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
57
    takes_args = ['filename?']
58.1.1 by Aaron Bentley
gdiff takes -r arguments
58
    takes_options = ['revision']
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
59
60
    @display_command
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
61
    def run(self, revision=None, filename=None):
58.1.1 by Aaron Bentley
gdiff takes -r arguments
62
        wt = WorkingTree.open_containing(".")[0]
63
        branch = wt.branch
64
        if revision is not None:
65
            if len(revision) == 1:
66
                tree1 = wt
67
                revision_id = revision[0].in_history(branch).rev_id
68
                tree2 = branch.repository.revision_tree(revision_id)
69
            elif len(revision) == 2:
70
                revision_id_0 = revision[0].in_history(branch).rev_id
71
                tree2 = branch.repository.revision_tree(revision_id_0)
72
                revision_id_1 = revision[1].in_history(branch).rev_id
73
                tree1 = branch.repository.revision_tree(revision_id_1)
74
        else:
75
            tree1 = wt
76
            tree2 = tree1.basis_tree()
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
77
58 by Jelmer Vernooij
[merge] Home
78
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
79
        import gtk
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
80
        window = DiffWindow()
81
        window.connect("destroy", lambda w: gtk.main_quit())
82
        window.set_diff("Working Tree", tree1, tree2)
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
83
        if filename is not None:
84
            tree_filename = tree1.relpath(filename)
85
            try:
86
                window.set_file(tree_filename)
87
            except NoSuchFile:
88
                if (tree1.inventory.path2id(tree_filename) is None and 
89
                    tree2.inventory.path2id(tree_filename) is None):
90
                    raise NotVersionedError(filename)
91
                raise BzrCommandError('No changes found for file "%s"' % 
92
                                      filename)
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
93
        window.show()
94
95
        gtk.main()
96
97
register_command(cmd_gdiff)
98
99
class cmd_visualise(Command):
100
    """Graphically visualise this branch.
101
102
    Opens a graphical window to allow you to see the history of the branch
103
    and relationships between revisions in a visual manner,
104
105
    The default starting point is latest revision on the branch, you can
106
    specify a starting point with -r revision.
107
    """
108
    takes_options = [
109
        "revision",
110
        Option('limit', "maximum number of revisions to display",
111
               int, 'count')]
112
    takes_args = [ "location?" ]
113
    aliases = [ "visualize", "vis", "viz" ]
114
115
    def run(self, location=".", revision=None, limit=None):
116
        (branch, path) = Branch.open_containing(location)
117
        branch.lock_read()
118
        branch.repository.lock_read()
119
        try:
120
            if revision is None:
121
                revid = branch.last_revision()
122
                if revid is None:
123
                    return
124
            else:
125
                (revno, revid) = revision[0].in_history(branch)
126
127
            from viz.bzrkapp import BzrkApp
128
                
129
            app = BzrkApp()
130
            app.show(branch, revid, limit)
131
        finally:
132
            branch.repository.unlock()
133
            branch.unlock()
134
        app.main()
135
136
137
register_command(cmd_visualise)
138
139
class cmd_gannotate(Command):
140
    """GTK+ annotate.
141
    
142
    Browse changes to FILENAME line by line in a GTK+ window.
143
    """
144
59.2.1 by Aaron Bentley
Gannotate takes a line number
145
    takes_args = ["filename", "line?"]
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
146
    takes_options = [
147
        Option("all", help="show annotations on all lines"),
148
        Option("plain", help="don't highlight annotation lines"),
149
        Option("line", type=int, argname="lineno",
150
               help="jump to specified line number")
151
    ]
152
    aliases = ["gblame", "gpraise"]
153
    
59.2.1 by Aaron Bentley
Gannotate takes a line number
154
    def run(self, filename, all=False, plain=False, line='1'):
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
155
        import pygtk
156
        pygtk.require("2.0")
157
158
        try:
159
            import gtk
160
        except RuntimeError, e:
161
            if str(e) == "could not open display":
162
                raise NoDisplayError
163
59.2.1 by Aaron Bentley
Gannotate takes a line number
164
        try:
165
            line = int(line)
166
        except ValueError:
167
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
168
                                  line)
169
55.1.2 by Jelmer Vernooij
Move commands to top-level __init__
170
        from annotate.gannotate import GAnnotateWindow
171
        from annotate.config import GAnnotateConfig
172
173
        (wt, path) = WorkingTree.open_containing(filename)
174
        branch = wt.branch
175
176
        file_id = wt.path2id(path)
177
178
        if file_id is None:
179
            raise NotVersionedError(filename)
180
181
        window = GAnnotateWindow(all, plain)
182
        window.connect("destroy", lambda w: gtk.main_quit())
183
        window.set_title(path + " - gannotate")
184
        config = GAnnotateConfig(window)
185
        window.show()
186
        branch.lock_read()
187
        try:
188
            window.annotate(branch, file_id)
189
        finally:
190
            branch.unlock()
191
        window.jump_to_line(line)
192
        
193
        gtk.main()
194
195
register_command(cmd_gannotate)
196
197
class cmd_gcommit(Command):
198
    """GTK+ commit dialog
199
200
    Graphical user interface for committing revisions"""
201
    
202
    takes_args = []
203
    takes_options = []
204
205
    def run(self, filename=None):
206
        import pygtk
207
        pygtk.require("2.0")
208
209
        try:
210
            import gtk
211
        except RuntimeError, e:
212
            if str(e) == "could not open display":
213
                raise NoDisplayError
214
215
        from commit import GCommitDialog
216
        from bzrlib.commit import Commit
217
        from bzrlib.errors import (BzrCommandError, PointlessCommit, ConflictsInTree, 
218
           StrictCommitFailed)
219
220
        (wt, path) = WorkingTree.open_containing(filename)
221
        branch = wt.branch
222
223
        file_id = wt.path2id(path)
224
225
        if file_id is None:
226
            raise NotVersionedError(filename)
227
228
        dialog = GCommitDialog(wt)
229
        dialog.set_title(path + " - Commit")
230
        if dialog.run() != gtk.RESPONSE_CANCEL:
231
            Commit().commit(working_tree=wt,message=dialog.message,
232
                specific_files=dialog.specific_files)
233
234
register_command(cmd_gcommit)
235
236
class NoDisplayError(BzrCommandError):
237
    """gtk could not find a proper display"""
238
239
    def __str__(self):
240
        return "No DISPLAY. gannotate is disabled."