/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: 2007-07-15 15:22:29 UTC
  • Revision ID: jelmer@samba.org-20070715152229-clmlen0vpd8d2pzx
Add docstrings, remove unused code.

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 Jelmer Vernooij
 
6
#
 
7
# Published under the GNU GPL
 
8
 
 
9
import nautilus
 
10
import bzrlib
 
11
from bzrlib.bzrdir import BzrDir
 
12
from bzrlib.errors import NotBranchError
 
13
from bzrlib.workingtree import WorkingTree
 
14
from bzrlib.tree import file_status
 
15
 
 
16
from bzrlib.plugin import load_plugins
 
17
load_plugins()
 
18
 
 
19
from bzrlib.plugins.gtk import cmd_visualise, cmd_gannotate
 
20
 
 
21
class BzrExtension(nautilus.MenuProvider, nautilus.ColumnProvider, nautilus.InfoProvider):
 
22
    def __init__(self):
 
23
        pass
 
24
 
 
25
    def add_cb(self, menu, vfs_file):
 
26
        # We can only cope with local files
 
27
        if vfs_file.get_uri_scheme() != 'file':
 
28
            return
 
29
 
 
30
        file = vfs_file.get_uri()
 
31
        try:
 
32
            tree, path = WorkingTree.open_containing(file)
 
33
        except NotBranchError:
 
34
            return
 
35
 
 
36
        tree.add(path)
 
37
 
 
38
        return
 
39
 
 
40
    def ignore_cb(self, menu, vfs_file):
 
41
        # We can only cope with local files
 
42
        if vfs_file.get_uri_scheme() != 'file':
 
43
            return
 
44
 
 
45
        file = vfs_file.get_uri()
 
46
        try:
 
47
            tree, path = WorkingTree.open_containing(file)
 
48
        except NotBranchError:
 
49
            return
 
50
 
 
51
        #FIXME
 
52
 
 
53
        return
 
54
 
 
55
    def unignore_cb(self, menu, vfs_file):
 
56
        # We can only cope with local files
 
57
        if vfs_file.get_uri_scheme() != 'file':
 
58
            return
 
59
 
 
60
        file = vfs_file.get_uri()
 
61
        try:
 
62
            tree, path = WorkingTree.open_containing(file)
 
63
        except NotBranchError:
 
64
            return
 
65
 
 
66
        #FIXME
 
67
 
 
68
        return
 
69
 
 
70
    def diff_cb(self, menu, vfs_file):
 
71
        # We can only cope with local files
 
72
        if vfs_file.get_uri_scheme() != 'file':
 
73
            return
 
74
 
 
75
        file = vfs_file.get_uri()
 
76
        try:
 
77
            tree, path = WorkingTree.open_containing(file)
 
78
        except NotBranchError:
 
79
            return
 
80
 
 
81
        from bzrlib.plugins.gtk.viz.diff import DiffWindow
 
82
        window = DiffWindow()
 
83
        window.set_diff(tree.branch.nick, tree, tree.branch.basis_tree())
 
84
        window.show()
 
85
 
 
86
        return
 
87
 
 
88
    def newtree_cb(self, menu, vfs_file):
 
89
        # We can only cope with local files
 
90
        if vfs_file.get_uri_scheme() != 'file':
 
91
            return
 
92
 
 
93
        file = vfs_file.get_uri()
 
94
 
 
95
        # We only want to continue here if we get a NotBranchError
 
96
        try:
 
97
            tree, path = WorkingTree.open_containing(file)
 
98
        except NotBranchError:
 
99
            BzrDir.create_branch_and_repo(file)
 
100
 
 
101
    def remove_cb(self, menu, vfs_file):
 
102
        # We can only cope with local files
 
103
        if vfs_file.get_uri_scheme() != 'file':
 
104
            return
 
105
 
 
106
        file = vfs_file.get_uri()
 
107
        try:
 
108
            tree, path = WorkingTree.open_containing(file)
 
109
        except NotBranchError:
 
110
            return
 
111
 
 
112
        tree.remove(path)
 
113
 
 
114
    def annotate_cb(self, menu, vfs_file):
 
115
        # We can only cope with local files
 
116
        if vfs_file.get_uri_scheme() != 'file':
 
117
            return
 
118
 
 
119
        file = vfs_file.get_uri()
 
120
 
 
121
        vis = cmd_gannotate()
 
122
        vis.run(file)
 
123
 
 
124
    def clone_cb(self, menu, vfs_file=None):
 
125
        # We can only cope with local files
 
126
        if vfs_file.get_uri_scheme() != 'file':
 
127
            return
 
128
 
 
129
        from bzrlib.plugins.gtk.branch import BranchDialog
 
130
        
 
131
        dialog = BranchDialog(vfs_file.get_name())
 
132
        dialog.display()
 
133
 
 
134
    def commit_cb(self, menu, vfs_file=None):
 
135
        # We can only cope with local files
 
136
        if vfs_file.get_uri_scheme() != 'file':
 
137
            return
 
138
 
 
139
        file = vfs_file.get_uri()
 
140
        try:
 
141
            tree, path = WorkingTree.open_containing(file)
 
142
        except NotBranchError:
 
143
            return
 
144
 
 
145
        from bzrlib.plugins.gtk.commit import CommitDialog
 
146
        dialog = CommitDialog(tree, path)
 
147
        dialog.display()
 
148
        gtk.main()
 
149
 
 
150
    def log_cb(self, menu, vfs_file):
 
151
        # We can only cope with local files
 
152
        if vfs_file.get_uri_scheme() != 'file':
 
153
            return
 
154
 
 
155
        file = vfs_file.get_uri()
 
156
 
 
157
        # We only want to continue here if we get a NotBranchError
 
158
        try:
 
159
            tree, path = WorkingTree.open_containing(file)
 
160
        except NotBranchError:
 
161
            return
 
162
 
 
163
        vis = cmd_visualise()
 
164
        vis.run(file)
 
165
 
 
166
        return
 
167
 
 
168
    def pull_cb(self, menu, vfs_file):
 
169
        # We can only cope with local files
 
170
        if vfs_file.get_uri_scheme() != 'file':
 
171
            return
 
172
 
 
173
        file = vfs_file.get_uri()
 
174
 
 
175
        # We only want to continue here if we get a NotBranchError
 
176
        try:
 
177
            tree, path = WorkingTree.open_containing(file)
 
178
        except NotBranchError:
 
179
            return
 
180
 
 
181
        from bzrlib.plugins.gtk.pull import PullDialog
 
182
        dialog = PullDialog(tree, path)
 
183
        dialog.display()
 
184
        gtk.main()
 
185
 
 
186
    def merge_cb(self, menu, vfs_file):
 
187
        # We can only cope with local files
 
188
        if vfs_file.get_uri_scheme() != 'file':
 
189
            return
 
190
 
 
191
        file = vfs_file.get_uri()
 
192
 
 
193
        # We only want to continue here if we get a NotBranchError
 
194
        try:
 
195
            tree, path = WorkingTree.open_containing(file)
 
196
        except NotBranchError:
 
197
            return
 
198
 
 
199
        from bzrlib.plugins.gtk.merge import MergeDialog
 
200
        dialog = MergeDialog(tree, path)
 
201
        dialog.display()
 
202
        gtk.main()
 
203
 
 
204
    def get_background_items(self, window, vfs_file):
 
205
        items = []
 
206
        file = vfs_file.get_uri()
 
207
        try:
 
208
            tree, path = WorkingTree.open_containing(file)
 
209
        except NotBranchError:
 
210
            item = nautilus.MenuItem('BzrNautilus::newtree',
 
211
                                 'Make directory versioned',
 
212
                                 'Create new Bazaar tree in this folder')
 
213
            item.connect('activate', self.newtree_cb, vfs_file)
 
214
            items.append(item)
 
215
 
 
216
            item = nautilus.MenuItem('BzrNautilus::clone',
 
217
                                 'Checkout Bazaar branch',
 
218
                                 'Checkout Existing Bazaar Branch')
 
219
            item.connect('activate', self.clone_cb, vfs_file)
 
220
            items.append(item)
 
221
 
 
222
            return items
 
223
 
 
224
        item = nautilus.MenuItem('BzrNautilus::log',
 
225
                             'Log',
 
226
                             'Show Bazaar history')
 
227
        item.connect('activate', self.log_cb, vfs_file)
 
228
        items.append(item)
 
229
 
 
230
        item = nautilus.MenuItem('BzrNautilus::pull',
 
231
                             'Pull',
 
232
                             'Pull from another branch')
 
233
        item.connect('activate', self.pull_cb, vfs_file)
 
234
        items.append(item)
 
235
 
 
236
        item = nautilus.MenuItem('BzrNautilus::merge',
 
237
                             'Merge',
 
238
                             'Merge from another branch')
 
239
        item.connect('activate', self.merge_cb, vfs_file)
 
240
        items.append(item)
 
241
 
 
242
        item = nautilus.MenuItem('BzrNautilus::commit',
 
243
                             'Commit',
 
244
                             'Commit Changes')
 
245
        item.connect('activate', self.commit_cb, vfs_file)
 
246
        items.append(item)
 
247
 
 
248
        return items
 
249
 
 
250
 
 
251
    def get_file_items(self, window, files):
 
252
        items = []
 
253
 
 
254
        for vfs_file in files:
 
255
            # We can only cope with local files
 
256
            if vfs_file.get_uri_scheme() != 'file':
 
257
                return
 
258
 
 
259
            file = vfs_file.get_uri()
 
260
            try:
 
261
                tree, path = WorkingTree.open_containing(file)
 
262
            except NotBranchError:
 
263
                if not vfs_file.is_directory():
 
264
                    return
 
265
                item = nautilus.MenuItem('BzrNautilus::newtree',
 
266
                                     'Make directory versioned',
 
267
                                     'Create new Bazaar tree in %s' % vfs_file.get_name())
 
268
                item.connect('activate', self.newtree_cb, vfs_file)
 
269
                return item,
 
270
 
 
271
            file_class = tree.file_class(path)
 
272
 
 
273
            if file_class == '?':
 
274
                item = nautilus.MenuItem('BzrNautilus::add',
 
275
                                     'Add',
 
276
                                     'Add as versioned file')
 
277
                item.connect('activate', self.add_cb, vfs_file)
 
278
                items.append(item)
 
279
 
 
280
                item = nautilus.MenuItem('BzrNautilus::ignore',
 
281
                                     'Ignore',
 
282
                                     'Ignore file for versioning')
 
283
                item.connect('activate', self.ignore_cb, vfs_file)
 
284
                items.append(item)
 
285
            elif file_class == 'I':
 
286
                item = nautilus.MenuItem('BzrNautilus::unignore',
 
287
                                     'Unignore',
 
288
                                     'Unignore file for versioning')
 
289
                item.connect('activate', self.unignore_cb, vfs_file)
 
290
                items.append(item)
 
291
            elif file_class == 'V':
 
292
                item = nautilus.MenuItem('BzrNautilus::log',
 
293
                                 'Log',
 
294
                                 'List changes')
 
295
                item.connect('activate', self.log_cb, vfs_file)
 
296
                items.append(item)
 
297
 
 
298
                item = nautilus.MenuItem('BzrNautilus::diff',
 
299
                                 'Diff',
 
300
                                 'Show differences')
 
301
                item.connect('activate', self.diff_cb, vfs_file)
 
302
                items.append(item)
 
303
 
 
304
                item = nautilus.MenuItem('BzrNautilus::remove',
 
305
                                     'Remove',
 
306
                                     'Remove this file from versioning')
 
307
                item.connect('activate', self.remove_cb, vfs_file)
 
308
                items.append(item)
 
309
 
 
310
                item = nautilus.MenuItem('BzrNautilus::annotate',
 
311
                             'Annotate',
 
312
                             'Annotate File Data')
 
313
                item.connect('activate', self.annotate_cb, vfs_file)
 
314
                items.append(item)
 
315
 
 
316
                item = nautilus.MenuItem('BzrNautilus::commit',
 
317
                             'Commit',
 
318
                             'Commit Changes')
 
319
                item.connect('activate', self.commit_cb, vfs_file)
 
320
                items.append(item)
 
321
 
 
322
        return items
 
323
 
 
324
    def get_columns(self):
 
325
        return nautilus.Column("BzrNautilus::bzr_status",
 
326
                               "bzr_status",
 
327
                               "Bzr Status",
 
328
                               "Version control status"),
 
329
 
 
330
    def update_file_info(self, file):
 
331
        if file.get_uri_scheme() != 'file':
 
332
            return
 
333
        
 
334
        try:
 
335
            tree, path = WorkingTree.open_containing(file.get_uri())
 
336
        except NotBranchError:
 
337
            return
 
338
 
 
339
        emblem = None
 
340
        status = None
 
341
 
 
342
        if tree.has_filename(path):
 
343
            emblem = 'cvs-controlled'
 
344
            status = 'unchanged'
 
345
            id = tree.path2id(path)
 
346
 
 
347
            delta = tree.changes_from(tree.branch.basis_tree())
 
348
            if delta.touches_file_id(id):
 
349
                emblem = 'cvs-modified'
 
350
                status = 'modified'
 
351
            for f, _, _ in delta.added:
 
352
                if f == path:
 
353
                    emblem = 'cvs-added'
 
354
                    status = 'added'
 
355
 
 
356
            for of, f, _, _, _, _ in delta.renamed:
 
357
                if f == path:
 
358
                    status = 'renamed from %s' % f
 
359
 
 
360
        elif tree.branch.basis_tree().has_filename(path):
 
361
            emblem = 'cvs-removed'
 
362
            status = 'removed'
 
363
        else:
 
364
            # FIXME: Check for ignored files
 
365
            status = 'unversioned'
 
366
        
 
367
        if emblem is not None:
 
368
            file.add_emblem(emblem)
 
369
        file.add_string_attribute('bzr_status', status)