Source

pida-main / pida / services / versioncontrol / versioncontrol.py

Full commit
  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
# -*- coding: utf-8 -*-
"""
    :copyright: 2005-2008 by The PIDA Project
    :license: GPL 2 or later (see README/COPYING/LICENSE)
"""

import gtk

# PIDA Imports
from pida.core.service import Service
from pida.core.features import FeaturesConfig
from pida.core.commands import CommandsConfig
from pida.core.events import EventsConfig
from pida.core.actions import ActionsConfig

from pida.ui.actions import PidaRememberToggle


from pygtkhelpers.gthreads import AsyncTask, gcall

import pida.services.filemanager.filehiddencheck as filehiddencheck

# locale
from pida.core.locale import Locale
locale = Locale('versioncontrol')
_ = locale.gettext


from .views import (
    VersionControlLog,
    CommitViewer,
    DiffViewer,
)

try:
    from anyvc.workdir import open as open_wd
    has_anyvc = True
except ImportError, e:
    print e
    has_anyvc = False
    def open_wd(*k):
        return None


class VersioncontrolFeaturesConfig(FeaturesConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('filemanager', 'file_hidden_check',
            self.versioncontrol)
        self.subscribe_foreign('contexts', 'file-menu',
            (self.svc, 'versioncontrol-file-menu.xml'))
        self.subscribe_foreign('contexts', 'dir-menu',
            (self.svc, 'versioncontrol-dir-menu.xml'))

    @filehiddencheck.fhc(filehiddencheck.SCOPE_GLOBAL,
        _("Hide Ignored Files by Version Control"))
    def versioncontrol(self, name, path, state):
        return not (state == "hidden" or state == "ignored")


class VersionControlEvents(EventsConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('buffer', 'document-changed',
            self.svc.on_document_changed)
        self.subscribe_foreign('project', 'project_switched',
            self.svc.on_project_changed)
        self.subscribe_foreign('contexts', 'show-menu',
            self.on_contexts__show_menu)
        self.subscribe_foreign('contexts', 'menu-deactivated',
            self.on_contexts__menu_deactivated)

    def on_contexts__show_menu(self, menu, context, **kw):
        under_vc = False
        if (context == 'file-menu'):
            path = kw['file_name']
            if path is not None:
                under_vc = open_wd(path) is not None
            self.svc.get_action('diff_for_file').set_visible(under_vc)
            self.svc.get_action('revert_for_file').set_visible(under_vc)
        elif (context == 'dir-menu'):
            path = kw['dir_name']
            under_vc = open_wd(path) is not None
            self.svc.get_action('diff_for_directory').set_visible(under_vc)
            self.svc.get_action('revert_for_dir').set_visible(under_vc)
        self.svc.get_action('more_vc_menu').set_visible(under_vc)

    def on_contexts__menu_deactivated(self, menu, context, **kw):
        self.svc.get_action('more_vc_menu').set_visible(True)


class VersioncontrolCommandsConfig(CommandsConfig):

    def get_workdirmanager(self, path):
        return open_wd(path)

    def list_file_states(self, path):
        return self.svc.list_file_states(path)


class VersionControlActions(ActionsConfig):


    actions = [
        PidaRememberToggle('show_vc_log',
            _('Version Control Log'),
            _('Show the version control log'),
            gtk.STOCK_CONNECT),
        gtk.ToggleAction('show_commit',
            _('Commit Message'),
            _('Show the commit message'),
            gtk.STOCK_GO_UP),
        gtk.Action('more_vc_menu',
            _('More Version Control'),
            _('More Version Control Commands'),
            gtk.STOCK_CONNECT),
        gtk.Action('diff_document',
            _('Differences'),
            _('Version Control differences for the current document'),
            gtk.STOCK_COPY),
        gtk.Action('diff_project',
            _('Differences'),
            _('Get the version control differences for the current project'),
            gtk.STOCK_COPY),
        gtk.Action('diff_for_file',
            _('Differences'),
            _('Get the version control diff on this file'),
            gtk.STOCK_COPY),
        gtk.Action('diff_for_directory',
            _('Differences'),
            _('Get the version control diff on this directory'),
            gtk.STOCK_COPY),
        gtk.Action('commit_document',
            _('Commit'),
            _('Commit the current document'),
            gtk.STOCK_GO_UP),
        gtk.Action('commit_project',
            _('Commit'),
            _('Commit the current project'),
            gtk.STOCK_GO_UP),
        gtk.Action('commit_for_file',
            _('Commit'),
            _('Commit the selected file'),
            gtk.STOCK_GO_UP),
        gtk.Action('commit_for_dir',
            _('Commit'),
            _('Commit the selected directory'),
            gtk.STOCK_GO_UP),
        gtk.Action('update_document',
            _('Update'),
            _('Update the current document'),
            gtk.STOCK_GO_DOWN),
        gtk.Action('update_project',
            _('Update'),
            _('Update the current project'),
            gtk.STOCK_GO_DOWN),
        gtk.Action('update_for_file',
            _('Update'),
            _('Update the selected file'),
            gtk.STOCK_GO_DOWN),
        gtk.Action('update_for_dir',
            _('Update'),
            _('Update the selected file'),
            gtk.STOCK_GO_DOWN),
        gtk.Action('add_document',
            _('Add'),
            _('Add the current document'),
            gtk.STOCK_ADD),
        gtk.Action('add_for_file',
            _('Add'),
            _('Add the selected file'),
            gtk.STOCK_ADD),
        gtk.Action('add_for_dir',
            _('Add'),
            _('Add the selected file'),
            gtk.STOCK_ADD),
        gtk.Action('remove_document',
            _('Remove'),
            _('Remove the current document'),
            gtk.STOCK_DELETE),
        gtk.Action('remove_for_file',
            _('Remove'),
            _('Remove the selected file'),
            gtk.STOCK_DELETE),
        gtk.Action('remove_for_dir',
            _('Remove'),
            _('Remove the selected directory'),
            gtk.STOCK_DELETE),
        gtk.Action('revert_document',
            _('Revert'),
            _('Revert the current document'),
            gtk.STOCK_UNDO),
        gtk.Action('revert_project',
            _('Revert'),
            _('Revert the current project'),
            gtk.STOCK_UNDO),
        gtk.Action('revert_for_file',
            _('Revert'),
            _('Revert the selected file'),
            gtk.STOCK_UNDO),
        gtk.Action('revert_for_dir',
            _('Revert'),
            _('Revert the selected directory'),
            gtk.STOCK_UNDO),
    ]

    accels = {
        'show_vc_log': '',
        'diff_document': '<Shift><Control>d',
    }

    global_accels = {
    }

    def on_show_vc_log(self, action):
        if action.get_active():
            self.svc.show_log()
        else:
            self.svc.hide_log()

    def on_show_commit(self, action):
        if action.get_active():
            self.svc.show_commit()
        else:
            self.svc.hide_commit()

    def on_diff_document(self, action):
        path = self.svc.current_document.filename
        self.svc.diff_path(path)

    def on_diff_project(self, action):
        path = self.svc.current_project.source_directory
        self.svc.diff_path(path)

    def on_diff_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.diff_path(path)

    def on_diff_for_directory(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.diff_path(path)

    def on_commit_document(self, action):
        path = self.svc.current_document.filename
        self.svc.commit_path_dialog(path)

    def on_commit_project(self, action):
        path = self.svc.current_project.source_directory
        self.svc.commit_path_dialog(path)

    def on_commit_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.commit_path_dialog(path)

    def on_commit_for_dir(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.commit_path_dialog(path)

    def on_update_document(self, action):
        path = self.svc.current_document.filename
        self.svc.update_path(path)

    def on_update_project(self, action):
        path = self.svc.current_project.source_directory
        self.svc.update_path(path)

    def on_update_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.update_path(path)

    def on_update_for_dir(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.update_path(path)

    def on_add_document(self, action):
        path = self.svc.current_document.filename
        self.svc.add_path(path)

    def on_add_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.add_path(path)

    def on_add_for_dir(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.add_path(path)

    def on_remove_document(self, action):
        path = self.svc.current_document.filename
        self.svc.remove_path(path)

    def on_remove_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.remove_path(path)

    def on_remove_for_dir(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.remove_path(path)

    def on_revert_document(self, action):
        path = self.svc.current_document.filename
        self.svc.revert_path(path)

    def on_revert_project(self, action):
        path = self.svc.current_project.source_directory
        self.svc.revert_path(path)

    def on_revert_for_file(self, action):
        path = action.contexts_kw['file_name']
        self.svc.revert_path(path)

    def on_revert_for_dir(self, action):
        path = action.contexts_kw['dir_name']
        self.svc.revert_path(path)


# Service class
class Versioncontrol(Service):
    """The Versioncontrol service"""

    features_config = VersioncontrolFeaturesConfig
    commands_config = VersioncontrolCommandsConfig
    actions_config = VersionControlActions
    events_config = VersionControlEvents

    def pre_start(self):
        self.on_document_changed(None)
        self.on_project_changed(None)

    def start(self):

        self._log = VersionControlLog(self)
        self._commit = CommitViewer(self)

        if not has_anyvc:  # the fake is a function
            # make the vcs actions insensitive if anyvc is missing
            self.actions.group.set_sensitive(False)

            self.boss.get_service('notify').notify(
                            "versioncontrol integration disabled",
                            title="Can't find anyvc",
                            stock=gtk.STOCK_DIALOG_ERROR,
                            )

    def ignored_file_checker(self, path, name, state):
        return not (state == "hidden" or state == "ignored")

    def list_file_states(self, path):
        wd = open_wd(path)

        if wd is not None:
            for item in wd.status(paths=[path], recursive=False):
                path = item.abspath
                yield path.basename, path.dirpath().strpath, item.state

    def diff_path(self, path):
        self._log.append_action('Diffing', path, gtk.STOCK_COPY)
        task = AsyncTask(self._do_diff, self._done_diff)
        task.start(path)

    def _do_diff(self, path):
        vc = open_wd(path)
        if vc is None:
            return (None,)
        return vc.diff(paths=[path])

    def _done_diff(self, diff):
        if diff is None:
            return self.error_dlg(_('File or directory is not versioned.'))
        view = DiffViewer(self)
        self.boss.cmd('window', 'add_view', paned='Terminal', view=view)
        view.set_diff(diff)

    def execute(self, action, path, stock_id, **kw):
        vc = open_wd(path)
        if vc is None:
            return self.error_dlg(_('File or directory is not versioned.'))
        self._log.append_action(action.capitalize(), path, stock_id)
        act = getattr(vc, action)

        def do():
            return act(paths=[path], **kw)

        def done(output):
            self._log.append_result(output)
            self.boss.cmd('notify', 'notify',
                title=_('Version Control %(action)s Completed') % {
                    'action': action.capitalize()
                },
                data=path,
                stock=stock_id)
            self.boss.cmd('filemanager', 'refresh')
        AsyncTask(do, done).start()

    def update_path(self, path):
        self.execute('update', path, gtk.STOCK_GO_DOWN)

    def commit_path(self, path, message=None):
        self.execute('commit', path, gtk.STOCK_GO_UP, message=message)

    def commit_path_dialog(self, path):
        vc = open_wd(path)
        if vc is None:
            return self.error_dlg(_('File or directory is not versioned.'))
        self._commit.set_path(path)
        self.ensure_commit_visible()

    def revert_path(self, path):
        self.execute('revert', path, gtk.STOCK_UNDO)

    def add_path(self, path):
        self.execute('add', path, gtk.STOCK_ADD)

    def remove_path(self, path):
        self.execute('remove', path, gtk.STOCK_REMOVE)

    def on_document_changed(self, document):
        for action in ['diff_document', 'revert_document', 'add_document',
        'remove_document', 'update_document', 'commit_document']:
            self.get_action(action).set_sensitive(document is not None)
        self.current_document = document

    def on_project_changed(self, project):
        for action  in ['diff_project', 'revert_project', 'update_project',
        'commit_project']:
            self.get_action(action).set_sensitive(project is not None)
        self.current_project = project

    def show_log(self):
        self.boss.cmd('window', 'add_view', paned='Terminal', view=self._log)

    def hide_log(self):
        self.boss.cmd('window', 'remove_view', view=self._log)

    def ensure_log_visible(self):
        action = self.get_action('show_vc_log')
        if not action.get_active():
            action.set_active(True)
        else:
            self.boss.cmd('window', 'present_view', view=self._log)

    def show_commit(self):
        self.boss.cmd('window', 'add_view',
                      paned='Terminal',
                      view=self._commit)

    def hide_commit(self):
        self.boss.cmd('window', 'remove_view',
                      view=self._commit)

    def ensure_commit_visible(self):
        action = self.get_action('show_commit')
        if not action.get_active():
            action.set_active(True)
        else:
            self.boss.cmd('window', 'present_view', view=self._commit)


# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: