/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
164 by Jelmer Vernooij
Add copyright information.
1
# Trivial Bazaar plugin for Nautilus
2
#
3
# Copyright (C) 2006 Jeff Bailey
4
# Copyright (C) 2006 Wouter van Heyst
423.13.1 by Jelmer Vernooij
Fix handling of NoWorkingTree error.
5
# Copyright (C) 2006-2008 Jelmer Vernooij <jelmer@samba.org>
164 by Jelmer Vernooij
Add copyright information.
6
#
7
# Published under the GNU GPL
8
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
9
import gtk
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
10
import nautilus
11
import bzrlib
423.13.1 by Jelmer Vernooij
Fix handling of NoWorkingTree error.
12
from bzrlib.branch import Branch
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
13
from bzrlib.bzrdir import BzrDir
423.13.1 by Jelmer Vernooij
Fix handling of NoWorkingTree error.
14
from bzrlib.errors import NotBranchError, NoWorkingTree, UnsupportedProtocol
15
from bzrlib.tree import file_status
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
16
from bzrlib.workingtree import WorkingTree
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
17
0.7.1 by Wouter van Heyst
use the bzrlib method of loading plugins, takes care of ~/.bazaar/plugins
18
from bzrlib.plugin import load_plugins
19
load_plugins()
20
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
21
from bzrlib.plugins.gtk import cmd_visualise, cmd_gannotate
0.6.2 by Jelmer Vernooij
Add 'Annotate' menu entry that uses the gannotate bzr plugin
22
88 by Jelmer Vernooij
Show column with file status.
23
class BzrExtension(nautilus.MenuProvider, nautilus.ColumnProvider, nautilus.InfoProvider):
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
24
    def __init__(self):
25
        pass
26
0.5.4 by jbailey at ubuntu
Add 'add' function. Give framework for other callbacks.
27
    def add_cb(self, menu, vfs_file):
28
        # We can only cope with local files
29
        if vfs_file.get_uri_scheme() != 'file':
30
            return
31
32
        file = vfs_file.get_uri()
33
        try:
34
            tree, path = WorkingTree.open_containing(file)
35
        except NotBranchError:
36
            return
37
38
        tree.add(path)
39
40
        return
41
42
    def ignore_cb(self, menu, vfs_file):
43
        # We can only cope with local files
44
        if vfs_file.get_uri_scheme() != 'file':
45
            return
46
47
        file = vfs_file.get_uri()
48
        try:
49
            tree, path = WorkingTree.open_containing(file)
50
        except NotBranchError:
51
            return
52
0.5.16 by Jelmer Vernooij
Adapt to bzr-gtk's API changes, add 'Clone' dialog
53
        #FIXME
54
0.5.4 by jbailey at ubuntu
Add 'add' function. Give framework for other callbacks.
55
        return
56
57
    def unignore_cb(self, menu, vfs_file):
58
        # We can only cope with local files
59
        if vfs_file.get_uri_scheme() != 'file':
60
            return
61
62
        file = vfs_file.get_uri()
63
        try:
64
            tree, path = WorkingTree.open_containing(file)
65
        except NotBranchError:
66
            return
67
0.5.16 by Jelmer Vernooij
Adapt to bzr-gtk's API changes, add 'Clone' dialog
68
        #FIXME
69
0.5.4 by jbailey at ubuntu
Add 'add' function. Give framework for other callbacks.
70
        return
71
72
    def diff_cb(self, menu, vfs_file):
73
        # We can only cope with local files
74
        if vfs_file.get_uri_scheme() != 'file':
75
            return
76
77
        file = vfs_file.get_uri()
78
        try:
79
            tree, path = WorkingTree.open_containing(file)
80
        except NotBranchError:
81
            return
82
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
83
        from bzrlib.plugins.gtk.diff import DiffWindow
0.5.13 by Jelmer Vernooij
Use the gtk plugin rather than separate bzrk and gannotate
84
        window = DiffWindow()
87 by Jelmer Vernooij
Fix diff window in nautilus.
85
        window.set_diff(tree.branch.nick, tree, tree.branch.basis_tree())
0.5.13 by Jelmer Vernooij
Use the gtk plugin rather than separate bzrk and gannotate
86
        window.show()
87
0.5.4 by jbailey at ubuntu
Add 'add' function. Give framework for other callbacks.
88
        return
89
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
90
    def newtree_cb(self, menu, vfs_file):
91
        # We can only cope with local files
92
        if vfs_file.get_uri_scheme() != 'file':
93
            return
94
95
        file = vfs_file.get_uri()
96
97
        # We only want to continue here if we get a NotBranchError
98
        try:
99
            tree, path = WorkingTree.open_containing(file)
100
        except NotBranchError:
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
101
            BzrDir.create_standalone_workingtree(file)
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
102
0.5.4 by jbailey at ubuntu
Add 'add' function. Give framework for other callbacks.
103
    def remove_cb(self, menu, vfs_file):
104
        # We can only cope with local files
105
        if vfs_file.get_uri_scheme() != 'file':
106
            return
107
108
        file = vfs_file.get_uri()
109
        try:
110
            tree, path = WorkingTree.open_containing(file)
111
        except NotBranchError:
112
            return
113
0.5.6 by jbailey at ubuntu
Implement remove
114
        tree.remove(path)
115
0.6.2 by Jelmer Vernooij
Add 'Annotate' menu entry that uses the gannotate bzr plugin
116
    def annotate_cb(self, menu, vfs_file):
117
        # We can only cope with local files
118
        if vfs_file.get_uri_scheme() != 'file':
119
            return
120
121
        file = vfs_file.get_uri()
122
123
        vis = cmd_gannotate()
124
        vis.run(file)
0.5.16 by Jelmer Vernooij
Adapt to bzr-gtk's API changes, add 'Clone' dialog
125
126
    def clone_cb(self, menu, vfs_file=None):
127
        # We can only cope with local files
128
        if vfs_file.get_uri_scheme() != 'file':
129
            return
130
142 by Jelmer Vernooij
Move some files to the top-level directory, add first test.
131
        from bzrlib.plugins.gtk.branch import BranchDialog
90 by Jelmer Vernooij
Use Olive's clone dialog in nautilus-bzr; remove the old Clone dialog
132
        
133
        dialog = BranchDialog(vfs_file.get_name())
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
134
        response = dialog.run()
135
        if response != gtk.RESPONSE_NONE:
136
            dialog.hide()
137
            dialog.destroy()
0.6.2 by Jelmer Vernooij
Add 'Annotate' menu entry that uses the gannotate bzr plugin
138
 
0.5.15 by Jelmer Vernooij
Add 'Commit' entries
139
    def commit_cb(self, menu, vfs_file=None):
140
        # We can only cope with local files
141
        if vfs_file.get_uri_scheme() != 'file':
142
            return
143
144
        file = vfs_file.get_uri()
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
145
        tree = None
146
        branch = None
0.5.15 by Jelmer Vernooij
Add 'Commit' entries
147
        try:
148
            tree, path = WorkingTree.open_containing(file)
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
149
            branch = tree.branch
150
        except NotBranchError, e:
151
            path = e.path
152
            #return
153
        except NoWorkingTree, e:
154
            path = e.base
155
            try:
156
                (branch, path) = Branch.open_containing(path)
157
            except NotBranchError, e:
158
                path = e.path
0.5.15 by Jelmer Vernooij
Add 'Commit' entries
159
142 by Jelmer Vernooij
Move some files to the top-level directory, add first test.
160
        from bzrlib.plugins.gtk.commit import CommitDialog
423.13.4 by Jelmer Vernooij
Fix commit dialog from nautilus.
161
        dialog = CommitDialog(tree, path)
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
162
        response = dialog.run()
163
        if response != gtk.RESPONSE_NONE:
164
            dialog.hide()
165
            dialog.destroy()
0.6.2 by Jelmer Vernooij
Add 'Annotate' menu entry that uses the gannotate bzr plugin
166
0.5.12 by Jelmer Vernooij
Rename "Visualise Bazaar Branch" to "Log", which is a term
167
    def log_cb(self, menu, vfs_file):
0.5.8 by jbailey at ubuntu
Add bzrk plugin
168
        # We can only cope with local files
169
        if vfs_file.get_uri_scheme() != 'file':
170
            return
171
172
        file = vfs_file.get_uri()
173
174
        # We only want to continue here if we get a NotBranchError
175
        try:
176
            tree, path = WorkingTree.open_containing(file)
177
        except NotBranchError:
178
            return
179
423.13.3 by Jelmer Vernooij
Remove fork() hack, it breaks X.
180
        vis = cmd_visualise()
181
        vis.run(file)
0.5.8 by jbailey at ubuntu
Add bzrk plugin
182
183
        return
184
91.1.4 by Jelmer Vernooij
List pull and merge in nautilus-bzr.
185
    def pull_cb(self, menu, vfs_file):
186
        # We can only cope with local files
187
        if vfs_file.get_uri_scheme() != 'file':
188
            return
189
190
        file = vfs_file.get_uri()
191
192
        # We only want to continue here if we get a NotBranchError
193
        try:
194
            tree, path = WorkingTree.open_containing(file)
195
        except NotBranchError:
196
            return
197
142 by Jelmer Vernooij
Move some files to the top-level directory, add first test.
198
        from bzrlib.plugins.gtk.pull import PullDialog
91.1.4 by Jelmer Vernooij
List pull and merge in nautilus-bzr.
199
        dialog = PullDialog(tree, path)
200
        dialog.display()
201
        gtk.main()
202
203
    def merge_cb(self, menu, vfs_file):
204
        # We can only cope with local files
205
        if vfs_file.get_uri_scheme() != 'file':
206
            return
207
208
        file = vfs_file.get_uri()
209
210
        # We only want to continue here if we get a NotBranchError
211
        try:
212
            tree, path = WorkingTree.open_containing(file)
213
        except NotBranchError:
214
            return
215
142 by Jelmer Vernooij
Move some files to the top-level directory, add first test.
216
        from bzrlib.plugins.gtk.merge import MergeDialog
91.1.4 by Jelmer Vernooij
List pull and merge in nautilus-bzr.
217
        dialog = MergeDialog(tree, path)
218
        dialog.display()
219
        gtk.main()
220
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
221
    def get_background_items(self, window, vfs_file):
90 by Jelmer Vernooij
Use Olive's clone dialog in nautilus-bzr; remove the old Clone dialog
222
        items = []
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
223
        file = vfs_file.get_uri()
224
        try:
225
            tree, path = WorkingTree.open_containing(file)
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
226
        except UnsupportedProtocol:
227
            return
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
228
        except NotBranchError:
229
            item = nautilus.MenuItem('BzrNautilus::newtree',
91 by Jelmer Vernooij
Some update to TODO.
230
                                 'Make directory versioned',
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
231
                                 'Create new Bazaar tree in this folder')
232
            item.connect('activate', self.newtree_cb, vfs_file)
0.5.16 by Jelmer Vernooij
Adapt to bzr-gtk's API changes, add 'Clone' dialog
233
            items.append(item)
234
235
            item = nautilus.MenuItem('BzrNautilus::clone',
91 by Jelmer Vernooij
Some update to TODO.
236
                                 'Checkout Bazaar branch',
0.5.16 by Jelmer Vernooij
Adapt to bzr-gtk's API changes, add 'Clone' dialog
237
                                 'Checkout Existing Bazaar Branch')
238
            item.connect('activate', self.clone_cb, vfs_file)
239
            items.append(item)
240
241
            return items
423.13.2 by Jelmer Vernooij
Fix handling of NoWorkingTree exception in a couple more places.
242
        except NoWorkingTree:
243
            return
0.5.8 by jbailey at ubuntu
Add bzrk plugin
244
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
245
        item = nautilus.MenuItem('BzrNautilus::log',
246
                             'Log',
247
                             'Show Bazaar history')
248
        item.connect('activate', self.log_cb, vfs_file)
249
        items.append(item)
0.5.15 by Jelmer Vernooij
Add 'Commit' entries
250
91.1.4 by Jelmer Vernooij
List pull and merge in nautilus-bzr.
251
        item = nautilus.MenuItem('BzrNautilus::pull',
252
                             'Pull',
253
                             'Pull from another branch')
254
        item.connect('activate', self.pull_cb, vfs_file)
255
        items.append(item)
256
257
        item = nautilus.MenuItem('BzrNautilus::merge',
258
                             'Merge',
259
                             'Merge from another branch')
260
        item.connect('activate', self.merge_cb, vfs_file)
261
        items.append(item)
262
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
263
        item = nautilus.MenuItem('BzrNautilus::commit',
264
                             'Commit',
265
                             'Commit Changes')
266
        item.connect('activate', self.commit_cb, vfs_file)
267
        items.append(item)
0.5.15 by Jelmer Vernooij
Add 'Commit' entries
268
269
        return items
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
270
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
271
    def get_file_items(self, window, files):
272
        items = []
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
273
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
274
        wtfiles = {}
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
275
        for vfs_file in files:
276
            # We can only cope with local files
277
            if vfs_file.get_uri_scheme() != 'file':
423.13.2 by Jelmer Vernooij
Fix handling of NoWorkingTree exception in a couple more places.
278
                continue
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
279
280
            file = vfs_file.get_uri()
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
281
            try:
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
282
                tree, path = WorkingTree.open_containing(file)
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
283
            except NotBranchError:
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
284
                if not vfs_file.is_directory():
423.13.2 by Jelmer Vernooij
Fix handling of NoWorkingTree exception in a couple more places.
285
                    continue
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
286
                item = nautilus.MenuItem('BzrNautilus::newtree',
91 by Jelmer Vernooij
Some update to TODO.
287
                                     'Make directory versioned',
0.5.7 by jbailey at ubuntu
Add hook for creating new bzr trees.
288
                                     'Create new Bazaar tree in %s' % vfs_file.get_name())
289
                item.connect('activate', self.newtree_cb, vfs_file)
290
                return item,
423.13.2 by Jelmer Vernooij
Fix handling of NoWorkingTree exception in a couple more places.
291
            except NoWorkingTree:
292
                continue
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
293
            # Refresh the list of filestatuses in the working tree
294
            if path not in wtfiles.keys():
295
                tree.lock_read()
296
                for rpath, file_class, kind, id, entry in tree.list_files():
297
                    wtfiles[rpath] = file_class
298
                tree.unlock()
299
                wtfiles[u''] = 'V'
300
301
            if wtfiles[path] == '?':
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
302
                item = nautilus.MenuItem('BzrNautilus::add',
303
                                     'Add',
304
                                     'Add as versioned file')
305
                item.connect('activate', self.add_cb, vfs_file)
306
                items.append(item)
307
308
                item = nautilus.MenuItem('BzrNautilus::ignore',
309
                                     'Ignore',
310
                                     'Ignore file for versioning')
311
                item.connect('activate', self.ignore_cb, vfs_file)
312
                items.append(item)
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
313
            elif wtfiles[path] == 'I':
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
314
                item = nautilus.MenuItem('BzrNautilus::unignore',
315
                                     'Unignore',
316
                                     'Unignore file for versioning')
317
                item.connect('activate', self.unignore_cb, vfs_file)
318
                items.append(item)
419 by Szilveszter Farkas
Applied nautilus-bzr patches by Toshio Kuratomi.
319
            elif wtfiles[path] == 'V':
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
320
                item = nautilus.MenuItem('BzrNautilus::log',
321
                                 'Log',
322
                                 'List changes')
323
                item.connect('activate', self.log_cb, vfs_file)
324
                items.append(item)
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
325
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
326
                item = nautilus.MenuItem('BzrNautilus::diff',
327
                                 'Diff',
328
                                 'Show differences')
329
                item.connect('activate', self.diff_cb, vfs_file)
330
                items.append(item)
0.5.2 by jbailey at ubuntu
Bring this to a state where it actually works. Specifically:
331
332
                item = nautilus.MenuItem('BzrNautilus::remove',
333
                                     'Remove',
334
                                     'Remove this file from versioning')
335
                item.connect('activate', self.remove_cb, vfs_file)
336
                items.append(item)
0.6.2 by Jelmer Vernooij
Add 'Annotate' menu entry that uses the gannotate bzr plugin
337
81 by Jelmer Vernooij
Remove unnecessary "#!/usr/bin/python" shebang lines (fixes #59125).
338
                item = nautilus.MenuItem('BzrNautilus::annotate',
339
                             'Annotate',
340
                             'Annotate File Data')
341
                item.connect('activate', self.annotate_cb, vfs_file)
342
                items.append(item)
343
344
                item = nautilus.MenuItem('BzrNautilus::commit',
345
                             'Commit',
346
                             'Commit Changes')
347
                item.connect('activate', self.commit_cb, vfs_file)
348
                items.append(item)
349
0.5.1 by Jelmer Vernooij
Start working on bzr integration plugin for nautilus
350
        return items
88 by Jelmer Vernooij
Show column with file status.
351
352
    def get_columns(self):
353
        return nautilus.Column("BzrNautilus::bzr_status",
354
                               "bzr_status",
355
                               "Bzr Status",
356
                               "Version control status"),
357
358
    def update_file_info(self, file):
359
        if file.get_uri_scheme() != 'file':
360
            return
361
        
362
        try:
363
            tree, path = WorkingTree.open_containing(file.get_uri())
364
        except NotBranchError:
365
            return
423.13.1 by Jelmer Vernooij
Fix handling of NoWorkingTree error.
366
        except NoWorkingTree:
367
            return
88 by Jelmer Vernooij
Show column with file status.
368
369
        emblem = None
370
        status = None
371
372
        if tree.has_filename(path):
423.13.5 by Jelmer Vernooij
Change emblems to the new emblems submitted by Martin.
373
            emblem = 'bzr-controlled'
88 by Jelmer Vernooij
Show column with file status.
374
            status = 'unchanged'
375
            id = tree.path2id(path)
376
377
            delta = tree.changes_from(tree.branch.basis_tree())
378
            if delta.touches_file_id(id):
423.13.5 by Jelmer Vernooij
Change emblems to the new emblems submitted by Martin.
379
                emblem = 'bzr-modified'
88 by Jelmer Vernooij
Show column with file status.
380
                status = 'modified'
381
            for f, _, _ in delta.added:
382
                if f == path:
423.13.5 by Jelmer Vernooij
Change emblems to the new emblems submitted by Martin.
383
                    emblem = 'bzr-added'
88 by Jelmer Vernooij
Show column with file status.
384
                    status = 'added'
385
386
            for of, f, _, _, _, _ in delta.renamed:
387
                if f == path:
388
                    status = 'renamed from %s' % f
389
390
        elif tree.branch.basis_tree().has_filename(path):
423.13.5 by Jelmer Vernooij
Change emblems to the new emblems submitted by Martin.
391
            emblem = 'bzr-removed'
88 by Jelmer Vernooij
Show column with file status.
392
            status = 'removed'
393
        else:
394
            # FIXME: Check for ignored files
395
            status = 'unversioned'
423.13.5 by Jelmer Vernooij
Change emblems to the new emblems submitted by Martin.
396
            emblem = 'bzr-unversioned'
88 by Jelmer Vernooij
Show column with file status.
397
        
398
        if emblem is not None:
399
            file.add_emblem(emblem)
400
        file.add_string_attribute('bzr_status', status)