File: nautilus-bzr.py

package info (click to toggle)
bzr-gtk 0.103.0+bzr792-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,524 kB
  • sloc: python: 9,616; makefile: 16; sh: 10
file content (546 lines) | stat: -rw-r--r-- 20,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# Trivial Bazaar plugin for Nautilus
#
# Copyright (C) 2006 Jeff Bailey
# Copyright (C) 2006 Wouter van Heyst
# Copyright (C) 2006-2011 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Installation:
# setup.py can install nautilus-bzr to the right system folder, if pkg-config
# is present.
#
# You can also install nautilus-bzr manually by copying it (or linking it from)
# ~/.local/share/nautilus-python/extensions/nautilus-bzr.py

from gi.repository import Gtk, GObject, Nautilus
from bzrlib.controldir import ControlDir
from bzrlib.errors import (
    NotBranchError,
    NoWorkingTree,
    )
from bzrlib.ignores import tree_ignores_add_patterns
from bzrlib.tree import InterTree

from bzrlib.plugin import load_plugins
load_plugins()

from bzrlib.plugins.gtk.i18n import _i18n


class BazaarExtension(Nautilus.MenuProvider, Nautilus.ColumnProvider,
        Nautilus.InfoProvider, Nautilus.PropertyPageProvider,
        Nautilus.LocationWidgetProvider, GObject.GObject):
    """Nautilus extension providing Bazaar integration."""

    def __init__(self):
        pass

    @classmethod
    def _open_bzrdir(cls, vfs_file):
        uri = vfs_file.get_uri()
        controldir, path = ControlDir.open_containing(uri)
        return controldir, path

    @classmethod
    def _open_tree(cls, vfs_file):
        controldir, path = cls._open_bzrdir(vfs_file)
        return controldir.open_workingtree(), path

    def add_cb(self, menu, tree, path):
        tree.add(path)

    def ignore_cb(self, menu, tree, path):
        # We can only cope with local files
        tree_ignores_add_patterns(tree, [path])
        #FIXME: Add path to ignore file

    def unignore_cb(self, menu, tree, path):
        pass
        # We can only cope with local files
        #FIXME

    def diff_cb(self, menu, tree, path=None):
        from bzrlib.plugins.gtk.diff import DiffWindow
        window = DiffWindow()
        window.set_diff(tree.branch._get_nick(local=True), tree, 
                        tree.branch.basis_tree())
        window.show()

    def newtree_cb(self, menu, vfs_file):
        controldir, path = self._open_bzrdir(vfs_file)
        controldir.create_workingtree()

    def remove_cb(self, menu, tree, path):
        tree.remove(path)

    def annotate_cb(self, menu, tree, path, file_id):
        from bzrlib.plugins.gtk.annotate.gannotate import GAnnotateWindow
        win = GAnnotateWindow()
        win.show()
        win.annotate(tree, tree.branch, file_id)
        Gtk.main()

    def clone_cb(self, menu, vfs_file=None):
        from bzrlib.plugins.gtk.branch import BranchDialog
        controldir, path = self._open_bzrdir(vfs_file)

        dialog = BranchDialog(vfs_file.get_name())
        response = dialog.run()
        if response != Gtk.ResponseType.NONE:
            dialog.hide()
            dialog.destroy()

    def commit_cb(self, menu, tree, path=None):
        from bzrlib.plugins.gtk.commit import CommitDialog
        dialog = CommitDialog(tree, path)
        response = dialog.run()
        if response != Gtk.ResponseType.NONE:
            dialog.hide()
            dialog.destroy()

    def log_cb(self, menu, controldir, path=None):
        from bzrlib.plugins.gtk.viz import BranchWindow
        branch = controldir.open_branch()
        pp = BranchWindow(branch, [branch.last_revision()], None)
        pp.show()
        Gtk.main()

    def pull_cb(self, menu, controldir, path=None):
        from bzrlib.plugins.gtk.pull import PullDialog
        dialog = PullDialog(controldir.open_workingtree(), path)
        dialog.display()
        Gtk.main()

    def push_cb(self, menu, controldir, path=None):
        from bzrlib.plugins.gtk.push import PushDialog
        dialog = PushDialog(branch=controldir.open_workingtree().branch)
        dialog.display()
        Gtk.main()

    def merge_cb(self, menu, tree, path=None):
        from bzrlib.plugins.gtk.merge import MergeDialog
        dialog = MergeDialog(tree, path)
        dialog.run()
        dialog.destroy()

    def create_tree_cb(self, menu, controldir):
        controldir.create_workingtree()

    def get_background_items(self, window, vfs_file):
        try:
            controldir, path = self._open_bzrdir(vfs_file)
        except NotBranchError:
            return
        try:
            branch = controldir.open_branch()
        except NotBranchError:
            items = []
            item = Nautilus.MenuItem(name='BzrNautilus::newtree',
                                 label='Make directory versioned',
                                 tip='Create new Bazaar tree in this folder',
                                 icon='')
            item.connect('activate', self.newtree_cb, vfs_file)
            items.append(item)

            item = Nautilus.MenuItem(name='BzrNautilus::clone',
                                 label='Checkout Bazaar branch ...',
                                 tip='Checkout Existing Bazaar Branch',
                                 icon='')
            item.connect('activate', self.clone_cb, vfs_file)
            items.append(item)
            return items

        items = []

        nautilus_integration = self.check_branch_enabled(branch)
        if not nautilus_integration:
            item = Nautilus.MenuItem(name='BzrNautilus::enable',
                                     label='Enable Bazaar Plugin for this Branch',
                                     tip='Enable Bazaar plugin for nautilus',
                                     icon='')
            item.connect('activate', self.toggle_integration, True, branch)
            return [item]
        else:
            item = Nautilus.MenuItem(name='BzrNautilus::disable',
                                     label='Disable Bazaar Plugin this Branch',
                                     tip='Disable Bazaar plugin for nautilus',
                                     icon='')
            item.connect('activate', self.toggle_integration, False, branch)
            items.append(item)

        item = Nautilus.MenuItem(name='BzrNautilus::log',
                             label='History ...',
                             tip='Show Bazaar history',
                             icon='')
        item.connect('activate', self.log_cb, controldir)
        items.append(item)

        item = Nautilus.MenuItem(name='BzrNautilus::pull',
                             label='Pull ...',
                             tip='Pull from another branch',
                             icon='')
        item.connect('activate', self.pull_cb, controldir)
        items.append(item)

        try:
            tree = controldir.open_workingtree()
        except NoWorkingTree:
            item = Nautilus.MenuItem(name='BzrNautilus::create_tree',
                                 label='Create working tree...',
                                 tip='Create a working tree for this branch',
                                 icon='')
            item.connect('activate', self.create_tree_cb, controldir)
            items.append(item)
        else:
            item = Nautilus.MenuItem(name='BzrNautilus::merge',
                                 label='Merge ...',
                                 tip='Merge from another branch',
                                 icon='')
            item.connect('activate', self.merge_cb, tree, path)
            items.append(item)

            item = Nautilus.MenuItem(name='BzrNautilus::commit',
                                 label='Commit ...',
                                 tip='Commit Changes',
                                 icon='')
            item.connect('activate', self.commit_cb, tree, path)
            items.append(item)

        return items

    def _get_file_menuitems(self, tree, intertree, path):
        file_id = tree.path2id(path)
        if file_id is None:
            item = Nautilus.MenuItem(name='BzrNautilus::add',
                                 label='Add',
                                 tip='Add as versioned file',
                                 icon='')
            item.connect('activate', self.add_cb, tree, path)
            yield item

            item = Nautilus.MenuItem(name='BzrNautilus::ignore',
                                 label='Ignore',
                                 tip='Ignore file for versioning',
                                 icon='')
            item.connect('activate', self.ignore_cb, tree, path)
            yield item
        elif tree.is_ignored(path):
            item = Nautilus.MenuItem(name='BzrNautilus::unignore',
                                 label='Unignore',
                                 tip='Unignore file for versioning',
                                 icon='')
            item.connect('activate', self.unignore_cb, tree, path)
            yield item
        else:
            kind = tree.kind(file_id)
            item = Nautilus.MenuItem(name='BzrNautilus::log',
                             label='History ...',
                             tip='List changes',
                             icon='')
            item.connect('activate', self.log_cb, tree.bzrdir, path)
            yield item

            if not intertree.file_content_matches(file_id, file_id):
                item = Nautilus.MenuItem(name='BzrNautilus::diff',
                                 label='View Changes ...',
                                 tip='Show differences',
                                 icon='')
                item.connect('activate', self.diff_cb, tree, path)
                yield item

                item = Nautilus.MenuItem(name='BzrNautilus::commit',
                             label='Commit ...',
                             tip='Commit Changes',
                             icon='')
                item.connect('activate', self.commit_cb, tree, path)
                yield item

            item = Nautilus.MenuItem(name='BzrNautilus::remove',
                                 label='Remove',
                                 tip='Remove this file from versioning',
                                 icon='')
            item.connect('activate', self.remove_cb, tree, path)
            yield item

            if kind == 'file':
                item = Nautilus.MenuItem(name='BzrNautilus::annotate',
                             label='Annotate ...',
                             tip='Annotate File Data',
                             icon='')
                item.connect('activate', self.annotate_cb, tree, path, file_id)
                yield item

    def get_file_items(self, window, files):
        items = []
        trees = {}

        try:
            for vfs_file in files:
                controldir, path = self._open_bzrdir(vfs_file)

                try:
                    tree = trees[controldir.user_url]
                except KeyError:
                    try:
                        tree = controldir.open_workingtree()
                    except NoWorkingTree:
                        continue
                    trees[controldir.user_url] = tree
                    tree.lock_read()

                nautilus_integration = self.check_branch_enabled(tree.branch)
                if not nautilus_integration:
                    continue

                intertree = InterTree.get(tree.basis_tree(), tree)
                items.extend(list(self._get_file_menuitems(tree, intertree, path)))
        finally:
            for tree in trees.itervalues():
                tree.unlock()

        return items

    def get_columns(self):
        return [
            Nautilus.Column(name="BzrNautilus::bzr_status",
                            attribute="bzr_status",
                            label="Status",
                            description="Version control status"),
            Nautilus.Column(name="BzrNautilus::bzr_revision",
                            attribute="bzr_revision",
                            label="Revision",
                            description="Last change revision"),
            ]

    def _file_summary(self, tree, basis_tree, intertree, path):
        file_revision = ""
        emblem = None

        file_id = tree.path2id(path)
        if file_id is None:
            if tree.is_ignored(path):
                status = 'ignored'
                emblem = 'bzr-ignored'
            else:
                status = 'unversioned'
            file_revision = "N/A"
        elif tree.has_filename(path): # Still present
            if not intertree.file_content_matches(file_id, file_id):
                if not basis_tree.has_id(file_id):
                    emblem = 'bzr-added'
                    status = 'added'
                    file_revision = "new file"
                elif basis_tree.path2id(file_id) != path:
                    status = 'bzr-renamed'
                    status = 'renamed from %s' % basis_tree.path2id(file_id)
                else:
                    emblem = 'bzr-modified'
                    status = 'modified'
            else:
                emblem = 'bzr-controlled'
                status = 'unchanged'
        elif basis_tree.has_filename(path):
            emblem = 'bzr-removed'
            status = 'removed'
        else:
            # FIXME: Check for ignored files
            status = 'unversioned'
        return (status, emblem, file_revision)

    def update_file_info(self, vfs_file):
        try:
            controldir, path = self._open_bzrdir(vfs_file)
        except NotBranchError:
            return

        try:
            tree = controldir.open_workingtree()
        except NoWorkingTree:
            return

        tree.lock_read()
        try:
            nautilus_integration = self.check_branch_enabled(tree.branch)
            if not nautilus_integration:
                return

            basis_tree = tree.basis_tree()
            intertree = InterTree.get(basis_tree, tree)

            basis_tree.lock_read()
            try:
                (status, emblem, file_revision) = self._file_summary(tree, basis_tree, intertree, path)
            finally:
                basis_tree.unlock()
            if emblem is not None:
                vfs_file.add_emblem(emblem)
            vfs_file.add_string_attribute('bzr_status', status)
            vfs_file.add_string_attribute('bzr_revision', file_revision)
        finally:
            tree.unlock()

    def check_branch_enabled(self, branch):
        # Supports global disable, but there is currently no UI to do this
        config = branch.get_config_stack()
        return config.get("nautilus_integration")

    def toggle_integration(self, menu, action, branch):
        config = branch.get_config_stack()
        config.set("nautilus_integration", action)

    def get_property_pages(self, files):
        pages = []
        for vfs_file in files:
            try:
                controldir, path = self._open_bzrdir(vfs_file)
            except NotBranchError:
                continue

            try:
                tree = controldir.open_workingtree()
            except NoWorkingTree:
                continue

            tree.lock_read()
            try:
                file_id = tree.path2id(path)
                pages.append(PropertyPageFile(tree, file_id, path))
                pages.append(PropertyPageBranch(tree.branch))
            finally:
                tree.unlock()
        return pages

    def get_widget(self, uri, window):
        controldir, path = ControlDir.open_containing(uri)
        try:
            tree = controldir.open_workingtree()
        except NoWorkingTree:
            return
        ret = Gtk.HBox(False, 4)
        text = 'This is a Bazaar working tree. '
        get_shelf_manager = getattr(tree, 'get_shelf_manager', None)
        if get_shelf_manager is not None:
            manager = get_shelf_manager()
            shelves = manager.active_shelves()
            if len(shelves) == 0:
                pass
            elif len(shelves) == 1:
                text += '1 shelf exists. '
            else:
                text += '%d shelf exists. ' % len(shelves)
        label = Gtk.Label(text)
        label.show()
        ret.pack_start(label, True, True, 0)
        ret.show_all()
        return ret


class PropertyPageFile(Nautilus.PropertyPage):

    def __init__(self, tree, file_id, path):
        self.tree = tree
        self.file_id = file_id
        self.path = path
        label = Gtk.Label('File Version')
        label.show()

        table = self._create_table()

        super(PropertyPageFile, self).__init__(label=label,
            name="BzrNautilus::file_page", page=table)

    def _create_table(self):
        table = Gtk.Table(homogeneous=False, columns=2, rows=3)

        table.attach(Gtk.Label(_i18n('File id:')), 0, 1, 0, 1)
        table.attach(Gtk.Label(self.file_id), 1, 2, 0, 1)

        table.attach(Gtk.Label(_i18n('SHA1Sum:')), 0, 1, 1, 2)
        table.attach(Gtk.Label(self.tree.get_file_sha1(self.file_id, self.path)), 1, 1, 1, 2)

        basis_tree = self.tree.revision_tree(self.tree.last_revision())
        last_revision = basis_tree.get_file_revision(self.file_id)

        table.attach(Gtk.Label(_i18n('Last Change Revision:')), 0, 1, 2, 3)
        revno = ".".join([str(x) for x in
            self.tree.branch.revision_id_to_dotted_revno(last_revision)])
        table.attach(Gtk.Label(revno), 1, 1, 2, 3)

        table.attach(Gtk.Label(_i18n('Last Change Author:')), 0, 1, 3, 4)
        rev = self.tree.branch.repository.get_revision(last_revision)
        table.attach(Gtk.Label("\n".join(rev.get_apparent_authors())), 1, 1, 3, 4)

        table.show_all()
        return table


class PropertyPageBranch(Nautilus.PropertyPage):

    def __init__(self, branch):
        self.branch = branch
        label = Gtk.Label('Branch')
        label.show()

        table = self._create_table()

        super(PropertyPageBranch, self).__init__(label=label,
            name="BzrNautilus::branch_page", page=table)

    def _create_location_entry(self, get_location, set_location):
        location = get_location()
        ret = Gtk.Entry()
        if location is not None:
            ret.set_text(location)
        return ret

    def _create_table(self):
        table = Gtk.Table(homogeneous=False, columns=2, rows=6)

        self._push_location_entry = self._create_location_entry(
            self.branch.get_push_location, self.branch.set_push_location)
        self._parent_location_entry = self._create_location_entry(
            self.branch.get_parent, self.branch.set_parent)
        self._bound_location_entry = self._create_location_entry(
            self.branch.get_bound_location, self.branch.set_bound_location)
        self._public_location_entry = self._create_location_entry(
            self.branch.get_public_branch, self.branch.set_public_branch)
        self._submit_location_entry = self._create_location_entry(
            self.branch.get_submit_branch, self.branch.set_submit_branch)

        table.attach(Gtk.Label(_i18n('Push location:')), 0, 1, 0, 1)
        table.attach(self._push_location_entry, 1, 2, 0, 1)

        table.attach(Gtk.Label(_i18n('Parent location:')), 0, 1, 1, 2)
        table.attach(self._parent_location_entry, 1, 1, 1, 2)

        table.attach(Gtk.Label(_i18n('Bound location:')), 0, 1, 2, 3)
        table.attach(self._bound_location_entry, 1, 1, 2, 3)

        table.attach(Gtk.Label(_i18n('Public location:')), 0, 1, 3, 4)
        table.attach(self._public_location_entry, 1, 1, 3, 4)

        table.attach(Gtk.Label(_i18n('Submit location:')), 0, 1, 4, 5)
        table.attach(self._submit_location_entry, 1, 1, 4, 5)

        self._append_revisions_only = Gtk.CheckButton(_i18n('Append revisions only'))
        value = self.branch.get_append_revisions_only()
        if value is None:
            value = False
        self._append_revisions_only.set_active(value)
        table.attach(self._append_revisions_only, 0, 2, 5, 6)

        table.show_all()
        return table