/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: Jelmer Vernooij
  • Date: 2006-09-30 10:21:43 UTC
  • Revision ID: jelmer@samba.org-20060930102143-c0ef64d6ca860c21
Merge some files from Olive and bzr-gtk.

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