/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: 2011-11-30 21:45:54 UTC
  • Revision ID: jelmer@samba.org-20111130214554-5kfx0b5y7t5zh033
Extend branch preferences, show location widget.

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-2011 Jelmer Vernooij <jelmer@samba.org>
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
 
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
 
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
# Installation:
 
22
# setup.py can install nautilus-bzr to the right system folder, if pkg-config
 
23
# is present.
 
24
#
 
25
# You can also install nautilus-bzr manually by copying it (or linking it from)
 
26
# ~/.local/share/nautilus-python/extensions/nautilus-bzr.py
 
27
 
 
28
from gi.repository import Gtk, GObject, Nautilus
 
29
from bzrlib.controldir import ControlDir
 
30
from bzrlib.errors import (
 
31
    NotBranchError,
 
32
    NoWorkingTree,
 
33
    )
 
34
from bzrlib.ignores import tree_ignores_add_patterns
 
35
from bzrlib.tree import InterTree
 
36
 
 
37
from bzrlib.plugin import load_plugins
 
38
load_plugins()
 
39
 
 
40
 
 
41
class BazaarExtension(Nautilus.MenuProvider, Nautilus.ColumnProvider,
 
42
        Nautilus.InfoProvider, Nautilus.PropertyPageProvider,
 
43
        Nautilus.LocationWidgetProvider, GObject.GObject):
 
44
    """Nautilus extension providing Bazaar integration."""
 
45
 
 
46
    def __init__(self):
 
47
        pass
 
48
 
 
49
    @classmethod
 
50
    def _open_bzrdir(cls, vfs_file):
 
51
        uri = vfs_file.get_uri()
 
52
        controldir, path = ControlDir.open_containing(uri)
 
53
        return controldir, path
 
54
 
 
55
    @classmethod
 
56
    def _open_tree(cls, vfs_file):
 
57
        controldir, path = cls._open_bzrdir(vfs_file)
 
58
        return controldir.open_workingtree(), path
 
59
 
 
60
    def add_cb(self, menu, tree, path):
 
61
        tree.add(path)
 
62
 
 
63
    def ignore_cb(self, menu, tree, path):
 
64
        # We can only cope with local files
 
65
        tree_ignores_add_patterns(tree, [path])
 
66
        #FIXME: Add path to ignore file
 
67
 
 
68
    def unignore_cb(self, menu, tree, path):
 
69
        pass
 
70
        # We can only cope with local files
 
71
        #FIXME
 
72
 
 
73
    def diff_cb(self, menu, tree, path=None):
 
74
        from bzrlib.plugins.gtk.diff import DiffWindow
 
75
        window = DiffWindow()
 
76
        window.set_diff(tree.branch._get_nick(local=True), tree, 
 
77
                        tree.branch.basis_tree())
 
78
        window.show()
 
79
 
 
80
    def newtree_cb(self, menu, vfs_file):
 
81
        controldir, path = self._open_bzrdir(vfs_file)
 
82
        controldir.create_workingtree()
 
83
 
 
84
    def remove_cb(self, menu, tree, path):
 
85
        tree.remove(path)
 
86
 
 
87
    def annotate_cb(self, menu, tree, path, file_id):
 
88
        from bzrlib.plugins.gtk.annotate.gannotate import GAnnotateWindow
 
89
        win = GAnnotateWindow()
 
90
        win.show()
 
91
        win.annotate(tree, tree.branch, file_id)
 
92
        Gtk.main()
 
93
 
 
94
    def clone_cb(self, menu, vfs_file=None):
 
95
        from bzrlib.plugins.gtk.branch import BranchDialog
 
96
        controldir, path = self._open_bzrdir(vfs_file)
 
97
 
 
98
        dialog = BranchDialog(vfs_file.get_name())
 
99
        response = dialog.run()
 
100
        if response != Gtk.ResponseType.NONE:
 
101
            dialog.hide()
 
102
            dialog.destroy()
 
103
 
 
104
    def commit_cb(self, menu, tree, path=None):
 
105
        from bzrlib.plugins.gtk.commit import CommitDialog
 
106
        dialog = CommitDialog(tree, path)
 
107
        response = dialog.run()
 
108
        if response != Gtk.ResponseType.NONE:
 
109
            dialog.hide()
 
110
            dialog.destroy()
 
111
 
 
112
    def log_cb(self, menu, controldir, path=None):
 
113
        from bzrlib.plugins.gtk.viz import BranchWindow
 
114
        branch = controldir.open_branch()
 
115
        pp = BranchWindow(branch, [branch.last_revision()], None)
 
116
        pp.show()
 
117
        Gtk.main()
 
118
 
 
119
    def pull_cb(self, menu, controldir, path=None):
 
120
        from bzrlib.plugins.gtk.pull import PullDialog
 
121
        dialog = PullDialog(controldir.open_workingtree(), path)
 
122
        dialog.display()
 
123
        Gtk.main()
 
124
 
 
125
    def merge_cb(self, menu, tree, path=None):
 
126
        from bzrlib.plugins.gtk.merge import MergeDialog
 
127
        dialog = MergeDialog(tree, path)
 
128
        dialog.run()
 
129
        dialog.destroy()
 
130
 
 
131
    def create_tree_cb(self, menu, controldir):
 
132
        controldir.create_workingtree()
 
133
 
 
134
    def get_background_items(self, window, vfs_file):
 
135
        try:
 
136
            controldir, path = self._open_bzrdir(vfs_file)
 
137
        except NotBranchError:
 
138
            return
 
139
        try:
 
140
            branch = controldir.open_branch()
 
141
        except NotBranchError:
 
142
            items = []
 
143
            item = Nautilus.MenuItem(name='BzrNautilus::newtree',
 
144
                                 label='Make directory versioned',
 
145
                                 tip='Create new Bazaar tree in this folder',
 
146
                                 icon='')
 
147
            item.connect('activate', self.newtree_cb, vfs_file)
 
148
            items.append(item)
 
149
 
 
150
            item = Nautilus.MenuItem(name='BzrNautilus::clone',
 
151
                                 label='Checkout Bazaar branch ...',
 
152
                                 tip='Checkout Existing Bazaar Branch',
 
153
                                 icon='')
 
154
            item.connect('activate', self.clone_cb, vfs_file)
 
155
            items.append(item)
 
156
            return items
 
157
 
 
158
        items = []
 
159
 
 
160
        nautilus_integration = self.check_branch_enabled(branch)
 
161
        if not nautilus_integration:
 
162
            item = Nautilus.MenuItem(name='BzrNautilus::enable',
 
163
                                     label='Enable Bazaar Plugin for this Branch',
 
164
                                     tip='Enable Bazaar plugin for nautilus',
 
165
                                     icon='')
 
166
            item.connect('activate', self.toggle_integration, True, branch)
 
167
            return [item]
 
168
        else:
 
169
            item = Nautilus.MenuItem(name='BzrNautilus::disable',
 
170
                                     label='Disable Bazaar Plugin this Branch',
 
171
                                     tip='Disable Bazaar plugin for nautilus',
 
172
                                     icon='')
 
173
            item.connect('activate', self.toggle_integration, False, branch)
 
174
            items.append(item)
 
175
 
 
176
        item = Nautilus.MenuItem(name='BzrNautilus::log',
 
177
                             label='History ...',
 
178
                             tip='Show Bazaar history',
 
179
                             icon='')
 
180
        item.connect('activate', self.log_cb, controldir)
 
181
        items.append(item)
 
182
 
 
183
        item = Nautilus.MenuItem(name='BzrNautilus::pull',
 
184
                             label='Pull ...',
 
185
                             tip='Pull from another branch',
 
186
                             icon='')
 
187
        item.connect('activate', self.pull_cb, controldir)
 
188
        items.append(item)
 
189
 
 
190
        try:
 
191
            tree = controldir.open_workingtree()
 
192
        except NoWorkingTree:
 
193
            item = Nautilus.MenuItem(name='BzrNautilus::create_tree',
 
194
                                 label='Create working tree...',
 
195
                                 tip='Create a working tree for this branch',
 
196
                                 icon='')
 
197
            item.connect('activate', self.create_tree_cb, controldir)
 
198
            items.append(item)
 
199
        else:
 
200
            item = Nautilus.MenuItem(name='BzrNautilus::merge',
 
201
                                 label='Merge ...',
 
202
                                 tip='Merge from another branch',
 
203
                                 icon='')
 
204
            item.connect('activate', self.merge_cb, tree, path)
 
205
            items.append(item)
 
206
 
 
207
            item = Nautilus.MenuItem(name='BzrNautilus::commit',
 
208
                                 label='Commit ...',
 
209
                                 tip='Commit Changes',
 
210
                                 icon='')
 
211
            item.connect('activate', self.commit_cb, tree, path)
 
212
            items.append(item)
 
213
 
 
214
        return items
 
215
 
 
216
    def _get_file_menuitems(self, tree, intertree, path):
 
217
        file_id = tree.path2id(path)
 
218
        if file_id is None:
 
219
            item = Nautilus.MenuItem(name='BzrNautilus::add',
 
220
                                 label='Add',
 
221
                                 tip='Add as versioned file',
 
222
                                 icon='')
 
223
            item.connect('activate', self.add_cb, tree, path)
 
224
            yield item
 
225
 
 
226
            item = Nautilus.MenuItem(name='BzrNautilus::ignore',
 
227
                                 label='Ignore',
 
228
                                 tip='Ignore file for versioning',
 
229
                                 icon='')
 
230
            item.connect('activate', self.ignore_cb, tree, path)
 
231
            yield item
 
232
        elif tree.is_ignored(path):
 
233
            item = Nautilus.MenuItem(name='BzrNautilus::unignore',
 
234
                                 label='Unignore',
 
235
                                 tip='Unignore file for versioning',
 
236
                                 icon='')
 
237
            item.connect('activate', self.unignore_cb, tree, path)
 
238
            yield item
 
239
        else:
 
240
            item = Nautilus.MenuItem(name='BzrNautilus::log',
 
241
                             label='History ...',
 
242
                             tip='List changes',
 
243
                             icon='')
 
244
            item.connect('activate', self.log_cb, tree.bzrdir, path)
 
245
            yield item
 
246
 
 
247
            if not intertree.file_content_matches(file_id, file_id):
 
248
                item = Nautilus.MenuItem(name='BzrNautilus::diff',
 
249
                                 label='View Changes ...',
 
250
                                 tip='Show differences',
 
251
                                 icon='')
 
252
                item.connect('activate', self.diff_cb, tree, path)
 
253
                yield item
 
254
 
 
255
                item = Nautilus.MenuItem(name='BzrNautilus::commit',
 
256
                             label='Commit ...',
 
257
                             tip='Commit Changes',
 
258
                             icon='')
 
259
                item.connect('activate', self.commit_cb, tree, path)
 
260
                yield item
 
261
 
 
262
            item = Nautilus.MenuItem(name='BzrNautilus::remove',
 
263
                                 label='Remove',
 
264
                                 tip='Remove this file from versioning',
 
265
                                 icon='')
 
266
            item.connect('activate', self.remove_cb, tree, path)
 
267
            yield item
 
268
 
 
269
            item = Nautilus.MenuItem(name='BzrNautilus::annotate',
 
270
                         label='Annotate ...',
 
271
                         tip='Annotate File Data',
 
272
                         icon='')
 
273
            item.connect('activate', self.annotate_cb, tree, path, file_id)
 
274
            yield item
 
275
 
 
276
    def get_file_items(self, window, files):
 
277
        items = []
 
278
        trees = {}
 
279
 
 
280
        try:
 
281
            for vfs_file in files:
 
282
                controldir, path = self._open_bzrdir(vfs_file)
 
283
 
 
284
                try:
 
285
                    tree = trees[controldir.user_url]
 
286
                except KeyError:
 
287
                    try:
 
288
                        tree = controldir.open_workingtree()
 
289
                    except NoWorkingTree:
 
290
                        continue
 
291
                    trees[controldir.user_url] = tree
 
292
                    tree.lock_read()
 
293
 
 
294
                nautilus_integration = self.check_branch_enabled(tree.branch)
 
295
                if not nautilus_integration:
 
296
                    continue
 
297
 
 
298
                intertree = InterTree.get(tree.basis_tree(), tree)
 
299
                items.extend(list(self._get_file_menuitems(tree, intertree, path)))
 
300
        finally:
 
301
            for tree in trees.itervalues():
 
302
                tree.unlock()
 
303
 
 
304
        return items
 
305
 
 
306
    def get_columns(self):
 
307
        return [
 
308
            Nautilus.Column(name="BzrNautilus::bzr_status",
 
309
                            attribute="bzr_status",
 
310
                            label="Status",
 
311
                            description="Version control status"),
 
312
            Nautilus.Column(name="BzrNautilus::bzr_revision",
 
313
                            attribute="bzr_revision",
 
314
                            label="Revision",
 
315
                            description="Last change revision"),
 
316
            ]
 
317
 
 
318
    def _file_summary(self, tree, basis_tree, intertree, path):
 
319
        file_revision = ""
 
320
        emblem = None
 
321
 
 
322
        file_id = tree.path2id(path)
 
323
        if file_id is None:
 
324
            if tree.is_ignored(path):
 
325
                status = 'ignored'
 
326
                emblem = 'bzr-ignored'
 
327
            else:
 
328
                status = 'unversioned'
 
329
            file_revision = "N/A"
 
330
        elif tree.has_filename(path): # Still present
 
331
            if not intertree.file_content_matches(file_id, file_id):
 
332
                if not basis_tree.has_id(file_id):
 
333
                    emblem = 'bzr-added'
 
334
                    status = 'added'
 
335
                    file_revision = "new file"
 
336
                elif basis_tree.path2id(file_id) != path:
 
337
                    status = 'bzr-renamed'
 
338
                    status = 'renamed from %s' % basis_tree.path2id(file_id)
 
339
                else:
 
340
                    emblem = 'bzr-modified'
 
341
                    status = 'modified'
 
342
            else:
 
343
                emblem = 'bzr-controlled'
 
344
                status = 'unchanged'
 
345
        elif basis_tree.has_filename(path):
 
346
            emblem = 'bzr-removed'
 
347
            status = 'removed'
 
348
        else:
 
349
            # FIXME: Check for ignored files
 
350
            status = 'unversioned'
 
351
        return (status, emblem, file_revision)
 
352
 
 
353
    def update_file_info(self, vfs_file):
 
354
        try:
 
355
            controldir, path = self._open_bzrdir(vfs_file)
 
356
        except NotBranchError:
 
357
            return
 
358
 
 
359
        try:
 
360
            tree = controldir.open_workingtree()
 
361
        except NoWorkingTree:
 
362
            return
 
363
 
 
364
        tree.lock_read()
 
365
        try:
 
366
            nautilus_integration = self.check_branch_enabled(tree.branch)
 
367
            if not nautilus_integration:
 
368
                return
 
369
 
 
370
            basis_tree = tree.basis_tree()
 
371
            intertree = InterTree.get(basis_tree, tree)
 
372
 
 
373
            basis_tree.lock_read()
 
374
            try:
 
375
                (status, emblem, file_revision) = self._file_summary(tree, basis_tree, intertree, path)
 
376
            finally:
 
377
                basis_tree.unlock()
 
378
            if emblem is not None:
 
379
                vfs_file.add_emblem(emblem)
 
380
            vfs_file.add_string_attribute('bzr_status', status)
 
381
            vfs_file.add_string_attribute('bzr_revision', file_revision)
 
382
        finally:
 
383
            tree.unlock()
 
384
 
 
385
    def check_branch_enabled(self, branch):
 
386
        # Supports global disable, but there is currently no UI to do this
 
387
        config = branch.get_config_stack()
 
388
        return config.get("nautilus_integration")
 
389
 
 
390
    def toggle_integration(self, menu, action, branch):
 
391
        config = branch.get_config_stack()
 
392
        config.set("nautilus_integration", action)
 
393
 
 
394
    def get_property_pages(self, files):
 
395
        pages = []
 
396
        for vfs_file in files:
 
397
            try:
 
398
                controldir, path = self._open_bzrdir(vfs_file)
 
399
            except NotBranchError:
 
400
                continue
 
401
 
 
402
            try:
 
403
                tree = controldir.open_workingtree()
 
404
            except NoWorkingTree:
 
405
                continue
 
406
 
 
407
            tree.lock_read()
 
408
            try:
 
409
                file_id = tree.path2id(path)
 
410
                pages.append(PropertyPageFile(tree, file_id, path))
 
411
                pages.append(PropertyPageBranch(tree.branch))
 
412
            finally:
 
413
                tree.unlock()
 
414
        return pages
 
415
 
 
416
    def get_widget(self, uri, window):
 
417
        controldir, path = ControlDir.open_containing(uri)
 
418
        try:
 
419
            tree = controldir.open_workingtree()
 
420
        except NoWorkingTree:
 
421
            return
 
422
        ret = Gtk.HBox(False, 4)
 
423
        text = 'This is a Bazaar working tree. '
 
424
        get_shelf_manager = getattr(tree, 'get_shelf_manager', None)
 
425
        if get_shelf_manager is not None:
 
426
            manager = get_shelf_manager()
 
427
            shelves = manager.active_shelves()
 
428
            if len(shelves) == 0:
 
429
                pass
 
430
            elif len(shelves) == 1:
 
431
                text += '1 shelf exists. '
 
432
            else:
 
433
                text += '%d shelf exists. ' % len(shelves)
 
434
        label = Gtk.Label(text)
 
435
        label.show()
 
436
        ret.pack_start(label, True, True, 0)
 
437
        ret.show_all()
 
438
        return ret
 
439
 
 
440
 
 
441
class PropertyPageFile(Nautilus.PropertyPage):
 
442
 
 
443
    def __init__(self, tree, file_id, path):
 
444
        self.tree = tree
 
445
        self.file_id = file_id
 
446
        self.path = path
 
447
        label = Gtk.Label('File Version')
 
448
        label.show()
 
449
 
 
450
        table = self._create_table()
 
451
 
 
452
        super(PropertyPageFile, self).__init__(label=label,
 
453
            name="BzrNautilus::file_page", page=table)
 
454
 
 
455
    def _create_table(self):
 
456
        table = Gtk.Table(homogeneous=False, columns=2, rows=3)
 
457
 
 
458
        table.attach(Gtk.Label('File id:'), 0, 1, 0, 1)
 
459
        table.attach(Gtk.Label(self.file_id), 1, 2, 0, 1)
 
460
 
 
461
        table.attach(Gtk.Label('SHA1Sum:'), 0, 1, 1, 2)
 
462
        table.attach(Gtk.Label(self.tree.get_file_sha1(self.file_id, self.path)), 1, 1, 1, 2)
 
463
 
 
464
        basis_tree = self.tree.revision_tree(self.tree.last_revision())
 
465
        last_revision = basis_tree.get_file_revision(self.file_id)
 
466
 
 
467
        table.attach(Gtk.Label('Last Change Revision:'), 0, 1, 2, 3)
 
468
        revno = ".".join([str(x) for x in
 
469
            self.tree.branch.revision_id_to_dotted_revno(last_revision)])
 
470
        table.attach(Gtk.Label(revno), 1, 1, 2, 3)
 
471
 
 
472
        table.attach(Gtk.Label('Last Change Author:'), 0, 1, 3, 4)
 
473
        rev = self.tree.branch.repository.get_revision(last_revision)
 
474
        table.attach(Gtk.Label("\n".join(rev.get_apparent_authors())), 1, 1, 3, 4)
 
475
 
 
476
        table.show_all()
 
477
        return table
 
478
 
 
479
 
 
480
class PropertyPageBranch(Nautilus.PropertyPage):
 
481
 
 
482
    def __init__(self, branch):
 
483
        self.branch = branch
 
484
        label = Gtk.Label('Branch')
 
485
        label.show()
 
486
 
 
487
        table = self._create_table()
 
488
 
 
489
        super(PropertyPageBranch, self).__init__(label=label,
 
490
            name="BzrNautilus::branch_page", page=table)
 
491
 
 
492
    def _create_location_entry(self, get_location, set_location):
 
493
        location = get_location()
 
494
        ret = Gtk.Entry()
 
495
        if location is not None:
 
496
            ret.set_text(location)
 
497
        return ret
 
498
 
 
499
    def _create_table(self):
 
500
        table = Gtk.Table(homogeneous=False, columns=2, rows=6)
 
501
 
 
502
        self._push_location_entry = self._create_location_entry(
 
503
            self.branch.get_push_location, self.branch.set_push_location)
 
504
        self._parent_location_entry = self._create_location_entry(
 
505
            self.branch.get_parent, self.branch.set_parent)
 
506
        self._bound_location_entry = self._create_location_entry(
 
507
            self.branch.get_bound_location, self.branch.set_bound_location)
 
508
        self._public_location_entry = self._create_location_entry(
 
509
            self.branch.get_public_branch, self.branch.set_public_branch)
 
510
        self._submit_location_entry = self._create_location_entry(
 
511
            self.branch.get_submit_branch, self.branch.set_submit_branch)
 
512
 
 
513
        table.attach(Gtk.Label('Push location:'), 0, 1, 0, 1)
 
514
        table.attach(self._push_location_entry, 1, 2, 0, 1)
 
515
 
 
516
        table.attach(Gtk.Label('Parent location:'), 0, 1, 1, 2)
 
517
        table.attach(self._parent_location_entry, 1, 1, 1, 2)
 
518
 
 
519
        table.attach(Gtk.Label('Bound location:'), 0, 1, 2, 3)
 
520
        table.attach(self._bound_location_entry, 1, 1, 2, 3)
 
521
 
 
522
        table.attach(Gtk.Label('Public location:'), 0, 1, 3, 4)
 
523
        table.attach(self._public_location_entry, 1, 1, 3, 4)
 
524
 
 
525
        table.attach(Gtk.Label('Submit location:'), 0, 1, 4, 5)
 
526
        table.attach(self._submit_location_entry, 1, 1, 4, 5)
 
527
 
 
528
        self._append_revisions_only = Gtk.CheckButton('Append revisions only')
 
529
        value = self.branch.get_append_revisions_only()
 
530
        if value is None:
 
531
            value = False
 
532
        self._append_revisions_only.set_active(value)
 
533
        table.attach(self._append_revisions_only, 0, 2, 5, 6)
 
534
 
 
535
        table.show_all()
 
536
        return table