/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 nautilus-bzr.py

  • Committer: Curtis Hovey
  • Date: 2011-09-04 20:04:03 UTC
  • mto: This revision was merged to the branch mainline in revision 741.
  • Revision ID: sinzui.is@verizon.net-20110904200403-t38t3o2q1j600dho
Added missing tests for BranchTreeModel and CellRendererGraph.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Trivial Bazaar plugin for Nautilus
 
2
#
 
3
# Copyright (C) 2006 Jeff Bailey
 
4
# Copyright (C) 2006 Wouter van Heyst
 
5
# Copyright (C) 2006-2008 Jelmer Vernooij <jelmer@samba.org>
 
6
#
 
7
# Published under the GNU GPL
 
8
 
 
9
from gi.repository import Gtk
1
10
import nautilus
2
 
import bzrlib
 
11
from bzrlib.branch import Branch
3
12
from bzrlib.bzrdir import BzrDir
4
 
from bzrlib.errors import NotBranchError
 
13
from bzrlib.errors import (
 
14
    NotBranchError,
 
15
    NoWorkingTree,
 
16
    UnsupportedProtocol,
 
17
    )
5
18
from bzrlib.workingtree import WorkingTree
6
 
from bzrlib.tree import file_status
 
19
from bzrlib.config import GlobalConfig
7
20
 
8
21
from bzrlib.plugin import load_plugins
9
22
load_plugins()
10
23
 
11
 
from bzrlib.plugins.gtk import cmd_visualise, cmd_gannotate
 
24
from bzrlib.plugins.gtk.commands import (
 
25
    cmd_gannotate,
 
26
    start_viz_window,
 
27
    )
 
28
 
 
29
print "Bazaar nautilus module initialized"
 
30
 
12
31
 
13
32
class BzrExtension(nautilus.MenuProvider, nautilus.ColumnProvider, nautilus.InfoProvider):
 
33
 
14
34
    def __init__(self):
15
35
        pass
16
36
 
70
90
        except NotBranchError:
71
91
            return
72
92
 
73
 
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
 
93
        from bzrlib.plugins.gtk.diff import DiffWindow
74
94
        window = DiffWindow()
75
 
        window.set_diff(tree.branch.nick, tree, tree.branch.basis_tree())
 
95
        window.set_diff(tree.branch._get_nick(local=True), tree, 
 
96
                        tree.branch.basis_tree())
76
97
        window.show()
77
98
 
78
99
        return
88
109
        try:
89
110
            tree, path = WorkingTree.open_containing(file)
90
111
        except NotBranchError:
91
 
            BzrDir.create_branch_and_repo(file)
 
112
            BzrDir.create_standalone_workingtree(file)
92
113
 
93
114
    def remove_cb(self, menu, vfs_file):
94
115
        # We can only cope with local files
118
139
        if vfs_file.get_uri_scheme() != 'file':
119
140
            return
120
141
 
121
 
        from bzrlib.plugins.gtk.olive.branch import BranchDialog
 
142
        from bzrlib.plugins.gtk.branch import BranchDialog
122
143
        
123
144
        dialog = BranchDialog(vfs_file.get_name())
124
 
        dialog.display()
 
145
        response = dialog.run()
 
146
        if response != Gtk.ResponseType.NONE:
 
147
            dialog.hide()
 
148
            dialog.destroy()
125
149
 
126
150
    def commit_cb(self, menu, vfs_file=None):
127
151
        # We can only cope with local files
129
153
            return
130
154
 
131
155
        file = vfs_file.get_uri()
 
156
        tree = None
 
157
        branch = None
132
158
        try:
133
159
            tree, path = WorkingTree.open_containing(file)
134
 
        except NotBranchError:
135
 
            return
 
160
            branch = tree.branch
 
161
        except NotBranchError, e:
 
162
            path = e.path
 
163
            #return
 
164
        except NoWorkingTree, e:
 
165
            path = e.base
 
166
            try:
 
167
                (branch, path) = Branch.open_containing(path)
 
168
            except NotBranchError, e:
 
169
                path = e.path
136
170
 
137
 
        from bzrlib.plugins.gtk.olive.commit import CommitDialog
 
171
        from bzrlib.plugins.gtk.commit import CommitDialog
138
172
        dialog = CommitDialog(tree, path)
139
 
        dialog.display()
140
 
        gtk.main()
 
173
        response = dialog.run()
 
174
        if response != Gtk.ResponseType.NONE:
 
175
            dialog.hide()
 
176
            dialog.destroy()
141
177
 
142
178
    def log_cb(self, menu, vfs_file):
143
179
        # We can only cope with local files
148
184
 
149
185
        # We only want to continue here if we get a NotBranchError
150
186
        try:
151
 
            tree, path = WorkingTree.open_containing(file)
 
187
            branch, path = Branch.open_containing(file)
152
188
        except NotBranchError:
153
189
            return
154
190
 
155
 
        vis = cmd_visualise()
156
 
        vis.run(file)
157
 
 
158
 
        return
 
191
        pp = start_viz_window(branch, [branch.last_revision()])
 
192
        pp.show()
 
193
        Gtk.main()
159
194
 
160
195
    def pull_cb(self, menu, vfs_file):
161
196
        # We can only cope with local files
170
205
        except NotBranchError:
171
206
            return
172
207
 
173
 
        from bzrlib.plugins.gtk.olive.pull import PullDialog
 
208
        from bzrlib.plugins.gtk.pull import PullDialog
174
209
        dialog = PullDialog(tree, path)
175
210
        dialog.display()
176
 
        gtk.main()
 
211
        Gtk.main()
177
212
 
178
213
    def merge_cb(self, menu, vfs_file):
179
214
        # We can only cope with local files
188
223
        except NotBranchError:
189
224
            return
190
225
 
191
 
        from bzrlib.plugins.gtk.olive.merge import MergeDialog
 
226
        from bzrlib.plugins.gtk.merge import MergeDialog
192
227
        dialog = MergeDialog(tree, path)
193
 
        dialog.display()
194
 
        gtk.main()
 
228
        dialog.run()
 
229
        dialog.destroy()
195
230
 
196
231
    def get_background_items(self, window, vfs_file):
197
232
        items = []
198
233
        file = vfs_file.get_uri()
 
234
 
199
235
        try:
200
236
            tree, path = WorkingTree.open_containing(file)
 
237
            disabled_flag = self.check_branch_enabled(tree.branch)
 
238
        except UnsupportedProtocol:
 
239
            return
201
240
        except NotBranchError:
 
241
            disabled_flag = self.check_branch_enabled()
202
242
            item = nautilus.MenuItem('BzrNautilus::newtree',
203
243
                                 'Make directory versioned',
204
244
                                 'Create new Bazaar tree in this folder')
206
246
            items.append(item)
207
247
 
208
248
            item = nautilus.MenuItem('BzrNautilus::clone',
209
 
                                 'Checkout Bazaar branch',
 
249
                                 'Checkout Bazaar branch ...',
210
250
                                 'Checkout Existing Bazaar Branch')
211
251
            item.connect('activate', self.clone_cb, vfs_file)
212
252
            items.append(item)
213
253
 
214
254
            return items
 
255
        except NoWorkingTree:
 
256
            return
 
257
        
 
258
        if disabled_flag == 'False':
 
259
            item = nautilus.MenuItem('BzrNautilus::enable',
 
260
                                     'Enable Bazaar Plugin for this Branch',
 
261
                                     'Enable Bazaar plugin for nautilus')
 
262
            item.connect('activate', self.toggle_integration, 'True', vfs_file)
 
263
            return item,
 
264
        else:
 
265
            item = nautilus.MenuItem('BzrNautilus::disable',
 
266
                                      'Disable Bazaar Plugin this Branch',
 
267
                                      'Disable Bazaar plugin for nautilus')
 
268
            item.connect('activate', self.toggle_integration, 'False', vfs_file)
 
269
            items.append(item)
215
270
 
216
271
        item = nautilus.MenuItem('BzrNautilus::log',
217
 
                             'Log',
 
272
                             'History ...',
218
273
                             'Show Bazaar history')
219
274
        item.connect('activate', self.log_cb, vfs_file)
220
275
        items.append(item)
221
276
 
222
277
        item = nautilus.MenuItem('BzrNautilus::pull',
223
 
                             'Pull',
 
278
                             'Pull ...',
224
279
                             'Pull from another branch')
225
280
        item.connect('activate', self.pull_cb, vfs_file)
226
281
        items.append(item)
227
282
 
228
283
        item = nautilus.MenuItem('BzrNautilus::merge',
229
 
                             'Merge',
 
284
                             'Merge ...',
230
285
                             'Merge from another branch')
231
286
        item.connect('activate', self.merge_cb, vfs_file)
232
287
        items.append(item)
233
288
 
234
289
        item = nautilus.MenuItem('BzrNautilus::commit',
235
 
                             'Commit',
 
290
                             'Commit ...',
236
291
                             'Commit Changes')
237
292
        item.connect('activate', self.commit_cb, vfs_file)
238
293
        items.append(item)
239
294
 
240
295
        return items
241
296
 
242
 
 
243
297
    def get_file_items(self, window, files):
244
298
        items = []
245
 
 
 
299
        
 
300
        wtfiles = {}
246
301
        for vfs_file in files:
247
302
            # We can only cope with local files
248
303
            if vfs_file.get_uri_scheme() != 'file':
249
 
                return
 
304
                continue
250
305
 
251
306
            file = vfs_file.get_uri()
252
307
            try:
253
308
                tree, path = WorkingTree.open_containing(file)
 
309
                disabled_flag = self.check_branch_enabled(tree.branch)
254
310
            except NotBranchError:
 
311
                disabled_flag = self.check_branch_enabled()
255
312
                if not vfs_file.is_directory():
 
313
                    continue
 
314
 
 
315
                if disabled_flag == 'False':
256
316
                    return
 
317
 
257
318
                item = nautilus.MenuItem('BzrNautilus::newtree',
258
319
                                     'Make directory versioned',
259
320
                                     'Create new Bazaar tree in %s' % vfs_file.get_name())
260
321
                item.connect('activate', self.newtree_cb, vfs_file)
261
322
                return item,
262
 
 
263
 
            file_class = tree.file_class(path)
264
 
 
265
 
            if file_class == '?':
 
323
            except NoWorkingTree:
 
324
                continue
 
325
            # Refresh the list of filestatuses in the working tree
 
326
            if path not in wtfiles.keys():
 
327
                tree.lock_read()
 
328
                for rpath, file_class, kind, id, entry in tree.list_files():
 
329
                    wtfiles[rpath] = file_class
 
330
                tree.unlock()
 
331
                wtfiles[u''] = 'V'
 
332
 
 
333
            if wtfiles[path] == '?':
266
334
                item = nautilus.MenuItem('BzrNautilus::add',
267
335
                                     'Add',
268
336
                                     'Add as versioned file')
274
342
                                     'Ignore file for versioning')
275
343
                item.connect('activate', self.ignore_cb, vfs_file)
276
344
                items.append(item)
277
 
            elif file_class == 'I':
 
345
            elif wtfiles[path] == 'I':
278
346
                item = nautilus.MenuItem('BzrNautilus::unignore',
279
347
                                     'Unignore',
280
348
                                     'Unignore file for versioning')
281
349
                item.connect('activate', self.unignore_cb, vfs_file)
282
350
                items.append(item)
283
 
            elif file_class == 'V':
 
351
            elif wtfiles[path] == 'V':
284
352
                item = nautilus.MenuItem('BzrNautilus::log',
285
 
                                 'Log',
 
353
                                 'History ...',
286
354
                                 'List changes')
287
355
                item.connect('activate', self.log_cb, vfs_file)
288
356
                items.append(item)
289
357
 
290
358
                item = nautilus.MenuItem('BzrNautilus::diff',
291
 
                                 'Diff',
 
359
                                 'View Changes ...',
292
360
                                 'Show differences')
293
361
                item.connect('activate', self.diff_cb, vfs_file)
294
362
                items.append(item)
300
368
                items.append(item)
301
369
 
302
370
                item = nautilus.MenuItem('BzrNautilus::annotate',
303
 
                             'Annotate',
 
371
                             'Annotate ...',
304
372
                             'Annotate File Data')
305
373
                item.connect('activate', self.annotate_cb, vfs_file)
306
374
                items.append(item)
307
375
 
308
376
                item = nautilus.MenuItem('BzrNautilus::commit',
309
 
                             'Commit',
 
377
                             'Commit ...',
310
378
                             'Commit Changes')
311
379
                item.connect('activate', self.commit_cb, vfs_file)
312
380
                items.append(item)
320
388
                               "Version control status"),
321
389
 
322
390
    def update_file_info(self, file):
 
391
 
323
392
        if file.get_uri_scheme() != 'file':
324
393
            return
325
394
        
327
396
            tree, path = WorkingTree.open_containing(file.get_uri())
328
397
        except NotBranchError:
329
398
            return
 
399
        except NoWorkingTree:
 
400
            return   
 
401
 
 
402
        disabled_flag = self.check_branch_enabled(tree.branch)
 
403
        if disabled_flag == 'False':
 
404
            return
330
405
 
331
406
        emblem = None
332
407
        status = None
333
408
 
334
 
        if tree.has_filename(path):
335
 
            emblem = 'cvs-controlled'
 
409
        id = tree.path2id(path)
 
410
        if id == None:
 
411
            if tree.is_ignored(path):
 
412
                status = 'ignored'
 
413
                emblem = 'bzr-ignored'
 
414
            else:
 
415
                status = 'unversioned'
 
416
                        
 
417
        elif tree.has_filename(path):
 
418
            emblem = 'bzr-controlled'
336
419
            status = 'unchanged'
337
 
            id = tree.path2id(path)
338
420
 
339
421
            delta = tree.changes_from(tree.branch.basis_tree())
340
422
            if delta.touches_file_id(id):
341
 
                emblem = 'cvs-modified'
 
423
                emblem = 'bzr-modified'
342
424
                status = 'modified'
343
425
            for f, _, _ in delta.added:
344
426
                if f == path:
345
 
                    emblem = 'cvs-added'
 
427
                    emblem = 'bzr-added'
346
428
                    status = 'added'
347
429
 
348
430
            for of, f, _, _, _, _ in delta.renamed:
350
432
                    status = 'renamed from %s' % f
351
433
 
352
434
        elif tree.branch.basis_tree().has_filename(path):
353
 
            emblem = 'cvs-removed'
 
435
            emblem = 'bzr-removed'
354
436
            status = 'removed'
355
437
        else:
356
438
            # FIXME: Check for ignored files
359
441
        if emblem is not None:
360
442
            file.add_emblem(emblem)
361
443
        file.add_string_attribute('bzr_status', status)
 
444
 
 
445
    def check_branch_enabled(self, branch=None):
 
446
        # Supports global disable, but there is currently no UI to do this
 
447
        config = GlobalConfig()
 
448
        disabled_flag = config.get_user_option('nautilus_integration')
 
449
        if disabled_flag != 'False':
 
450
            if branch is not None:
 
451
                config = branch.get_config()
 
452
                disabled_flag = config.get_user_option('nautilus_integration')
 
453
        return disabled_flag
 
454
 
 
455
    def toggle_integration(self, menu, action, vfs_file=None):
 
456
        try:
 
457
            tree, path = WorkingTree.open_containing(vfs_file.get_uri())
 
458
        except NotBranchError:
 
459
            return
 
460
        except NoWorkingTree:
 
461
            return
 
462
        branch = tree.branch
 
463
        if branch is None:
 
464
            config = GlobalConfig()
 
465
        else:
 
466
            config = branch.get_config()
 
467
        config.set_user_option('nautilus_integration', action)
 
468