File: core.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (836 lines) | stat: -rw-r--r-- 29,769 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
"""
Base type to implement support for new VCS engines in GPS
"""

import GPS
import os
import gps_utils
import workflows
import time
from workflows.promises import Promise
import types
import difflib
import io
import platform
import six

GPS.VCS2.Status = gps_utils.enum(
        UNMODIFIED=2**0,
        MODIFIED=2**1,
        STAGED_MODIFIED=2**2,
        STAGED_ADDED=2**3,
        DELETED=2**4,
        STAGED_DELETED=2**5,
        STAGED_RENAMED=2**6,
        STAGED_COPIED=2**7,
        UNTRACKED=2**8,
        IGNORED=2**9,
        CONFLICT=2**10,
        LOCAL_LOCKED=2**11,
        LOCKED_BY_OTHER=2**12,
        NEEDS_UPDATE=2**13)
# Valid statuses for files (they can be combined)

GPS.VCS2.Actions = gps_utils.enum(
        DOUBLE_CLICK=0,
        TOOLTIP=1,
        ADD=2,
        REMOVE=3,
        RENAME=4)


Traverse_Limit_Pref = GPS.Preference(":VCS/Traverse-Limit")
Traverse_Limit_Pref.create(
    "Max project depth",
    "integer",
    "How much directories should GPS traverse when looking for " +
    "VCS root counting from project file directory.", 99, 0)


class _Branch(list):
    """
    A convenience class to create the tuples to pass to
    `GPS.VCS2_Task_Visitor.branches`
    """

    def __init__(self, name, active, annotation, id):
        """
        :param str name: name of the branch, as displayed in the Branches view
        :param bool active: whether the branch is "active" (for instance the
            current branch, or a persistent tag,... An active branch is
            highlighted in the Branches view.
        :param str annotation: extra information to display on the right in
            the Branches view. For instance, the number of commits that are
            in this branch but not upstream.
        :param str id: a unique id for the branch. This is used in python
            callbacks, but the exact value of the id is irrelevant outside
            of the plug-in that creates this id. It could for instance be
            the result of `json.dumps` on python data.
        """
        list.__init__(self, (name, active, annotation, id))


class _Commit(list):
    """
    A convenience wrapper to create the tuples to pass to
    `GPS.VCS2_Task_Visitor.history_lines`
    """

    Kind = gps_utils.enum(
        HEAD=0,     # current working dir
        LOCAL=1,    # a local branch name exists for this commit
        REMOTE=2,   # a remote branch name exists for this commit
        TAG=3)      # a tag exists for this commit

    Flags = gps_utils.enum(
        UNPUSHED=2**1,     # an unpushed local commit
        UNCOMMITTED=2**2)  # uncommitted local changes

    def __init__(self, id, author, date, subject, parents, names=None,
                 flags=0):
        """
        :param str id: the unique id for the commit
        :param str author: the author of the commit
        :param str date: the date of the commit
        :param str subject: the first line of the commit message
        :param List(str) parents: a list of commit ids, the parents
           of the commit. There are multiple parents when this is
           a merge commit.
        :param List((str, GPS.VCS2.Commit.Kind)) names: a list of tag names
           or branch names associated with this commit. Each of the items
           indicates the type of the name
        :param GPS.VCS2.Commit.Flags: a combination of flags, which
           influence of the commit is represented in the History view.
        """
        list.__init__(self, (id, author, date, subject, parents, names, flags))

    @property
    def subject(self):
        return self[3]

    @subject.setter
    def set_subject(self, str):
        self[3] = str


GPS.VCS2.Branch = _Branch
GPS.VCS2.Commit = _Commit


def run_in_background(func):
    """
    A decorator to be applied to a method of VCS (below), which monitors
    whether background processing is being done. This is used to avoid
    spawning multiple commands in the background in parallel, in particular
    because the first one could already be computed information required by
    the next one (for instance, with git, a user need status for file1.adb
    and file2.adb -- but since git always compute the status for all files,
    the second command is not needed).

    Use this instead of workflows.run_as_workflow, as in::

        class MyVCS(vcs2.core.VCS):

            @vcs2.core.run_in_background
            def async_fetch_status_for_files(self):
                pass

    :return: a function that when executed returns a promise that is resolved
      to the return of `func`. Until this promise is resolved (in the
      background), the VCS engine is marked as busy, and no other command will
      be started.
    """

    def __func(self, *args, **kwargs):
        r = func(self, *args, **kwargs)
        if isinstance(r, types.GeneratorType):
            if isinstance(self, Extension):
                vcs = self.base
            else:
                vcs = self
            vcs.set_run_in_background(True)
            promise = workflows.driver(r)
            promise.then(lambda x: vcs.set_run_in_background(False),
                         lambda x: vcs.set_run_in_background(False))
        else:
            promise = Promise()
            promise.resolve(r)
        return promise
    return __func


class Profile:
    """
    A Context that runs the function inside the profiler, and display
    the result in the log.
    """

    def __init__(self, time_only=False):
        """
        :param bool time_only: if true, only display the time it topok to
           execute, not the whole profile
        """
        import cProfile
        self.time_only = time_only
        if time_only:
            self.start = time.time()
        else:
            self.c = cProfile.Profile()

    def __enter__(self):
        if not self.time_only:
            self.c.enable()
        return self

    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
        if self.time_only:
            GPS.Logger("VCS2").log(
                "Total time: %ss" % (time.time() - self.start, ))
        else:
            import pstats
            self.c.disable()
            s = six.StringIO()
            ps = pstats.Stats(self.c, stream=s).sort_stats('cumulative')
            ps.print_stats()
            GPS.Logger("VCS2").log(s.getvalue())


class Extension(object):
    """
    A class similar to core.VCS, which is used to decorate an existing VCS
    engine to add more features.
    For instance, it is used to add Gerrit or github support.
    A decorator has the same primitive operations as a VCS engine, which can
    be overridden. However, each VCS engine is responsible for using its
    decorators when appropriate.
    Such extensions must be registered with core.register_extension().
    """

    def __init__(self, base_vcs):
        self.base = base_vcs

    def applies(self):
        """
        Check if self should be applied to its base VCS system.
        """
        return False


class VCS(GPS.VCS2):
    """
    To create a new engine, extend this class, and then call:

        @core.register_vcs
        class MyVCS(core.VCS):
            pass
    """

    _class_extensions = []  # a list of classes derived from Extension

    #######################
    # Overridable methods #
    #######################

    def __init__(self, working_dir, default_status):
        """
        Instances are created in `register` below. If you need additional
        parameters, they need to be given to `register.
        When __init__ is called, `self` is not fully setup and in particular
        you cannot call any of the functions exported by GPS. Do this from
        `setup` instead.

        :param GPS.File working_dir: the location of the working directory,
           computed from `discover_working_dir`
        :param GPS.VCS2.Status: the default assumed status of files.
           See `register_vcs`
        """
        self.working_dir = working_dir
        self.default_status = default_status
        self._extensions = []   # the decorators that apply to self

        # Check which decorators apply
        for d in self._class_extensions:
            inst = d(base_vcs=self)
            if inst.applies():
                GPS.Logger("VCS2").log(
                    "Extension %s applied to %s (%s)" % (
                        inst, self, working_dir))
                self._extensions.append(inst)

    def setup(self):
        """
        Called after `self` has been constructed (via __init__) and all
        functions exported by GPS are available.
        In particular, this can be used to override how statuses are displayed
        via `GPS.VCS2._override_status_display`.
        """
        vcs_action.register_all_vcs_actions(self)

    @staticmethod
    def discover_working_dir(file):
        """
        Starting from file, check whether it could belong to a working
        directory for the engine. Often implemented using
        `find_admin_directory`, or perhaps using an environment
        variable.

        :param GPS.File file:
        :return: a string
        """
        return ''

    def async_fetch_status_for_files(self, files):
        """
        Fetch status information for `file`.
        Use `set_status_for_all_files`.

        :param List[GPS.File] files:
        """
        self.async_fetch_status_for_all_files(from_user=False)

    def async_fetch_status_for_project(self, project):
        """
        Fetch status information for all files in `project`.
        Use `set_status_for_all_files`.

        :param GPS.File file:
        """
        self.async_fetch_status_for_all_files(from_user=False)

    def async_fetch_status_for_all_files(self, from_user):
        """
        Fetch status for all files in the project tree.
        Use `set_status_for_all_files`.

        :param bool from_user: True if this was called as a result of the
            user pressing the 'Reload' button in the VCS views.
        """
        pass

    def stage_or_unstage_files(self, files, stage):
        """
        Mark all the files in the list to be part of the next commit (if
        `stage` is True), or not part of the next commit (if `stage` is
        False).
        Some VCS systems support this natively (git), while for others it
        needs to be emulated.
        Extend the vcs2.core_staging.Emulate_Staging class to emulate.

        :param List(GPS.File) files: The list of files to stage or unstage.
        :param bool stage: whether to stage or unstage.
        """

    def make_file_writable(self, file, writable):
        """
        You can override the method named `make_file_writable` if you need
        a special operation to make a file writable on the disk.

        This must be SYNCHRONOUS (no background operation).

        :param GPS.File file: the file to make writable
        :param bool writable: whether the file should be made writable or
            read-only.
        :return: if it returns True, any needed operation is assumed to have
            been performed (synchronously). If False, the default behavior is
            applied, i.e. simply change permissions on the disk.
        """
        return False

    def async_discard_local_changes(self, files):
        """
        Revert the local changes for all of the files in the list.
        This can be run asynchronously.

        :param [GPS.File] files: the list of files
        """
        GPS.Console().write("Revert not supported for %s" % (self.name, ))

    def async_commit_staged_files(self, visitor, message):
        """
        Commit all staged files.
        The cached status of files is automatically refreshed on exit.

        :param GPS.VCS2_Task_Visitor visitor: the object used to report
           success via `visitor.success`.
        :param str message: the commit message
        """

    def async_fetch_history(self, visitor, filter):
        """
        Fetch history for the whole repository asynchronously.
        For each line in the history, should call `self._add_log_line`

        :param GPS.VCS2_Task_Visitor visitor: the object used to report
           when new lines have been pared for the history.
        :param List filter: A list of various filters to apply. This list
           is currently defined as:
              [lines               : int,
               file                : GPS.File,
               filter              : str,
               current_branch_only : bool,
               branch_commits      : bool]
           where `lines` is the number of lines that will be displayed
           (returning more is useless), `file` is set if the log should be for
           a specific file, `filter` is a string that is interpreted by the
           VCS system, `current_branch_only` is set if a single branch
           should be examined (as opposed to all branches) and
           `branch_commits` is true if only commits related to branching
           points should be returned.
        """

    def async_fetch_commit_details(self, ids, visitor):
        """
        Fetch the details for each of the commits in the list.
        These details are returned asynchronously to GPS by calling
        `visitor.set_details`.

        :param List(str) ids: the list of commits for which we want the
          details.
        :param GPS.VCS2_Task_Visitor visitor: the object used to
          report the details.
        """

    def async_diff(self, visitor, ref, file):
        """
        Compute a diff.

        :param GPS.VCS2_Task.Visitor visitor: the object used to
           report the diff, via its 'diff_computed` method.
        :param str ref: the ref to which we want to compare. This is
           typically the id of a commit (as returned from
           `async_fetch_history`, although it can also be the name of a
           branch, or "HEAD" to indicate the last commit done on the
           current branch.
        :param GPS.File file: the file for which we want a diff. This is
            set to None to get a full repository diff.
        """

    def async_view_file(self, visitor, ref, file):
        """
        Show the full contents of the file for the given revision.

        :param GPS.VCS2_Task.Visitor visitor: the object used to
           report the diff, via its 'file_computed` method.
        :param str ref: the ref to which we want to compare. This is
           typically the id of a commit (as returned from
           `async_fetch_history`, although it can also be the name of a
           branch, or "HEAD" to indicate the last commit done on the
           current branch.
        :param GPS.File file: the file for which we want a diff.
        """

    def async_annotations(self, visitor, file):
        """
        Compute the information to display on the side of editors for
        the given file. This information should include last commit date,
        author, commit id,...

        :param GPS.VCS2_Task_Visitor visitor: the object used to report
           the information, via its `annotation` method.
        :param GPS.File file: the file for which the information should be
           computed
        """

    def async_branches(self, visitor):
        """
        Retrieve the list of branches, tags, ... available for self.

        :param GPS.VCS2_Task_Visitor visitor: the object used to report
           the information, via its `branches` method.
        """

    def async_action_on_branch(self, visitor, action, category, id, text=''):
        """
        React to a double-click action in the Branches view.

        :param GPS.VCS2_Task_Visitor visitor: the object used to report
           asynchronously.
           If action is ACTION_TOOLTIP, use `visitor.tooltip`.
        :param GPS.VCS2.Action action: the action to perform
        :param str category: the upper-cased category, i.e. the first
           parameter to `visitor.branches` in the call to `async_branches`.
        :param str id: the id of the specific line that was selected.
        :param str text: the new name, when action is ACTION_RENAME
        """

    ############
    # Services #
    ############

    def set_status_for_all_files(self, files=set()):
        """
        A proxy that lets you set statuses of individual files, and on
        exit automatically set the status of remaining files to unmodified::

            with self.set_status_for_all_files(project.sources()) as s:
                s.set_status(file1, ...)
                s.set_status(file2, ...)
            # on exit, automatically set status of remaining files

        You can also use the returned value as a standard object:

            s = self.set_status_for_all_files()
            s.set_status('file1.adb', ...)
            # does nothing when you are done, unless you call
            s.set_status_for_remaining_files(['file1.adb', 'file2.adb',...])

        The default status comes from the call to `register_vcs`.

        This function takes into account emulated staging: when a VCS does not
        natively support staging (like git does), GPS emulates it by saving
        some data across session. This function takes into account this saved
        data and modifies the status as needed.

        :param Set(GPS.File): the set of files to update. This parameter is
           only used when using this function as a context manager (the 'with'
           statement in python).
        """

        vcs = self

        class _CM(object):
            def __init__(self):
                self._seen = set()
                self._cache = {}    # (status,version,repo_version) -> [File]

            def __enter__(self):
                return self

            @property
            def files_with_explicit_status(self):
                """
                Return the set of files for which an explicit status was set
                """
                return self._seen

            def set_status(
                    self, file,
                    status,
                    version="",
                    repo_version=""):
                """
                Set the status for one file
                :param GPS.File file:
                :param GPS.VCS2.Status status:
                :param GPS.VCS2.Attributes attributes:
                :param str version:
                :param str repo_version:
                """
                self._seen.add(file)
                self._cache.setdefault(
                    (status, version, repo_version), []).append(file)

            def set_status_for_remaining_files(self, files=set()):
                """
                Set the status for all files in `files` for which no status
                has been set yet.

                :param set(GPS.File)|list(GPS.File) files:
                """
                GPS.Logger("VCS2").log("Emit file statuses")
                for s, s_files in six.iteritems (self._cache):
                    vcs._set_file_status(s_files, s[0], s[1], s[2])
                GPS.Logger("VCS2").log("Done emit file statuses")

                to_set = []
                for f in files:
                    if f not in self._seen:
                        to_set.append(f)
                vcs._set_file_status(to_set, vcs.default_status)
                GPS.Logger("VCS2").log("Done emit default statuses")

            def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
                self.set_status_for_remaining_files(files)
                return False   # do not suppress exceptions

        return _CM()

    def _relpath(self, path):
        """
        Return a relative filepath to path from the working dir.

        :param str path:
        """
        relpath = os.path.relpath(path, self.working_dir.path)

        if platform.system() == 'Windows':
            #  Use posix path for cygwin tools arguments
            return relpath.replace('\\', '/')
        else:
            return relpath

    @classmethod
    def register_extension(klass, extension):
        klass._class_extensions.append(extension)

    def extensions(self, name, *args, **kwargs):
        """
        Execute all extension's method, if they exists.
        Typically, a method that executes in the background will do
        something like::

            def method(self, ...):
                def _internal():
                    ...
                yield join(_internal(), *self.extensions('method', ...))

        to execute the extensions

        :returntype: a list of generators, one for each method that is
           executing in the background.
        """
        result = []
        for ext in self._extensions:
            m = getattr(ext, name, None)
            if m is not None:
                gen = m(*args, **kwargs)
                if isinstance(gen, types.GeneratorType):
                    result.append(gen)
        return result


class File_Based_VCS(VCS):
    """
    Abstract base class for file-based vcs systems.
    """

    def _compute_status(self, all_files, args=[]):
        """
        Run a "status" command with extra args

        :param List(GPS.File) all_files: all files for which a status
           should be set.
        :param List(str) args: extra arguments to 'cvs/svn/... status'
        """

    def async_fetch_status_for_files(self, files):
        self._compute_status(
            all_files=files,
            args=[f.path for f in files])

    def async_fetch_status_for_project(self, project):
        self._compute_status(
            all_files=project.sources(recursive=False),
            args=[d for d in project.source_dirs(recursive=False)])

    def async_fetch_status_for_all_files(self, from_user):
        self._compute_status([])  # all files


class register_vcs:
    """
    A decorator to register a new VCS engine
    :param str name: the name of the engine, as used in project properties
    :param default_status: the VCS status to use for files not specifically
       set from "status". See `set_status_for_all_files`. This is also the
       status applied by GPS for files not in the cache yet. This value has
       a significant impact on the initial loading of the status for all
       files.
    :param args: passed to the class constructor
    :param kwargs: pass to the class constructor
    """

    def __init__(self, default_status, name="", *args, **kwargs):
        self.default_status = default_status
        self.name = name
        self.args = args
        self.kwargs = kwargs

    def __call__(self, klass):
        GPS.VCS2._register(
            self.name or klass.__name__,
            construct=lambda working_dir: klass(
                working_dir, self.default_status, *self.args, **self.kwargs),
            default_status=self.default_status,
            discover_working_dir=klass.discover_working_dir)
        return klass


def find_admin_directory(file, basename, allow_file=False):
    """
    Starting from the location of `file`, move up the directory tree to
    find a directory named `basename`.
    Used for the implementation of discoved_working_dir

    :param bool allow_file: if true, search for files with the given name,
      not just directories.
    :return: A str
      The parent directory `basename`, i.e. the root repository
    """
    limit = os.path.dirname(GPS.Project.root().file().path)
    for step in range(-1, Traverse_Limit_Pref.get()):
        limit = os.path.dirname(limit)

    parent = os.path.expanduser('~')
    prev = file.path
    dir = os.path.dirname(prev)
    while dir not in [prev, parent, limit]:
        d = os.path.join(dir, basename)
        if os.path.isdir(d) or (allow_file and os.path.isfile(d)):
            return os.path.normpath(os.path.join(d, '..'))
        prev = dir
        dir = os.path.dirname(prev)
    return ""


class vcs_action:
    """
    A decorator to create actions associated with a VCS.
    These actions only exist while at least one instance of the VCS is in
    use in the project, and are automatically unregistered otherwise::

        @core.register_vcs(...)
        class MyVCS(core.VCS):

            @core.vcs_action(...)
            def _mymethod(self):
                pass   # standard method, or generator with yield statements

    :param func: the function to execute for this action. It receives the
       instance of klass as a parameter. This should thus in general be
       a method of the class.
    :param str name: name of the action.
    :param klass: The action is only enabled when the selected VCS is
       an instance of klass.
    :param str toolbar: if set, this action will be added to the local
       toolbar of the corresponding view.
    :param str toolbar_section: what part of the toolbar this should be
       added to.
    """

    _actions = set()  # all registered actions

    def __init__(self, name, icon='', toolbar='', toolbar_section='',
                 menu='', after=''):
        self.name = name
        self.icon = icon
        self.menu = menu
        self.after = after
        self.toolbar = toolbar
        self.toolbar_section = toolbar_section

    def __call__(self, func):
        func._vcs2_is_action = self  # Mark for later
        return func

    @staticmethod
    def create_action(method, vcs_name, extension_class=None):
        """
        Register a GPS action.
        Nothing is done if the action has already been registered for another
        instance of the same VCS kind.

        :param Func method: the function to call.
           It receives either the instance of the active VCS or the instance
           of the extension, depending on whether extension_name is specified.
           This must be a method that was decorated with @vcs_action.
        :param str vcs_name: the name of the VCS system to which the action
           applies. The action is only enabled when this is the current VCS
           (any instance of that VCS, in case for instance the user has
           multiple git working directories in a project.
        :param type extension_class: the class of the VCS extension to which
           this action applies. When specified, the parameter given to the
           method is the instance of the VCS extension, not the VCS itself.
        """

        action = method._vcs2_is_action
        if action.name in vcs_action._actions:
            return

        vcs_action._actions.add(action.name)

        class __Proxy:
            def __init__(self, method):
                self.method = run_in_background(method)

            def filter(self, context):
                return (GPS.VCS2.active_vcs() and
                        GPS.VCS2.active_vcs().name == vcs_name)

            def __call__(self):
                v = GPS.VCS2.active_vcs()
                if extension_class is None:
                    self.method(v)
                else:
                    for e in v._extensions:
                        if e.__class__ == extension_class:
                            self.method(e)
                            break

        p = __Proxy(method)
        gps_utils.make_interactive(
            p, description=method.__doc__,
            name=action.name, category='VCS2', menu=action.menu,
            after=action.after, icon=action.icon, filter=p.filter)

        if action.toolbar:
            act = GPS.Action(action.name)
            act.button(toolbar=action.toolbar,
                       section=action.toolbar_section,
                       hide=True)

    @staticmethod
    def register_all_vcs_actions(vcs):
        """
        Called internally by GPS.
        This makes sure that all actions registered for a VCS class are active
        only when a VCS is in use for the current project.

        :param VCS vcs: an instance of the VCS class
        """
        for name, meth in six.iteritems (vcs.__class__.__dict__):
            if hasattr(meth, "_vcs2_is_action"):
                vcs_action.create_action(meth, vcs.name, None)

        for d in vcs._extensions:
            for name, meth in six.iteritems (d.__class__.__dict__):
                if hasattr(meth, "_vcs2_is_action"):
                    vcs_action.create_action(meth, vcs.name, d.__class__)


@staticmethod
def _diff(left_file, right_file, result_file):
    """
    Compare files line by line and save into result file.
    Result is the same as for "diff --normal left right".

        :param GPS.File left_file: first file to compare
        :param GPS.File right_file: second file to compare
        :param GPS.File result_file: file with result
    """
    f = io.open(left_file.path, "rb")
    left = f.readlines()
    f.close()
    f = io.open(right_file.path, "rb")
    right = f.readlines()
    f.close()
    diff = difflib.SequenceMatcher(isjunk=None, a=left, b=right)
    codes = diff.get_opcodes()
    out = []
    for code in codes:
        if code[0] == "replace":
            op = "c"
        elif code[0] == "insert":
            op = "a"
        elif code[0] == "delete":
            op = "d"
        else:
            op = None

        if op:
            out.append("%s%s%s\n" % (code[2] if code[1] + 1 >= code[2] else
                                     "{},{}".format(code[1] + 1, code[2]),
                                     op,
                                     code[4] if code[3] + 1 >= code[4] else
                                     "{},{}".format(code[3] + 1, code[4])))
            for x in range(code[1], code[2]):
                out.append("< %s" % (left[x]))
            for x in range(code[3], code[4]):
                out.append("> %s" % (right[x]))

    f = io.open(result_file.path, "wb")
    f.writelines(out)
    f.close()


GPS.VCS2._diff = _diff