kriti21

Untitled

Jul 4th, 2018
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. import abc
  2. import re
  3.  
  4. from aenum import Flag
  5.  
  6. from coalib.bears.GlobalBear import GlobalBear
  7. from coalib.results.HiddenResult import HiddenResult
  8. from coalib.misc.Shell import run_shell_command
  9. from coala_utils.decorators import (enforce_signature, generate_ordering,
  10.                                     generate_repr)
  11.  
  12.  
  13. class COMMIT_TYPE(Flag):
  14.     simple_commit = 0
  15.     ci_skip_commit = 1
  16.     merge_commit = 2
  17.     revert_commit = 3
  18.  
  19.  
  20. @generate_repr(('id', hex),
  21.                'origin',
  22.                'raw_commit_message',
  23.                'commit_sha',
  24.                'commit_type',
  25.                'modified_files',
  26.                'added_files',
  27.                'deleted_files')
  28. @generate_ordering('raw_commit_message',
  29.                    'commit_sha',
  30.                    'commit_type',
  31.                    'modified_files',
  32.                    'added_files',
  33.                    'deleted_files',
  34.                    'severity',
  35.                    'confidence',
  36.                    'origin',
  37.                    'message_base',
  38.                    'message_arguments',
  39.                    'aspect',
  40.                    'additional_info',
  41.                    'debug_msg')
  42. class CommitResult(HiddenResult):
  43.  
  44.     @enforce_signature
  45.     def __init__(self, origin,
  46.                  raw_commit_message: str,
  47.                  commit_sha: str,
  48.                  commit_type: COMMIT_TYPE,
  49.                  modified_files: list,
  50.                  added_files: list,
  51.                  deleted_files: list):
  52.  
  53.         super().__init__(origin, '')
  54.         self.raw_commit_message = raw_commit_message
  55.         self.commit_sha = commit_sha
  56.         self.commit_type = commit_type
  57.         self.modified_files = modified_files
  58.         self.added_files = added_files
  59.         self.deleted_files = deleted_files
  60.  
  61.  
  62. class VCSCommitBear(GlobalBear):
  63.     __metaclass__ = abc.ABCMeta
  64.     LANGUAGES = {'Git'}
  65.     AUTHORS = {'The coala developers'}
  66.     AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
  67.     LICENSE = 'AGPL-3.0'
  68.  
  69.     @abc.abstractmethod
  70.     def get_head_commit(self):
  71.         """
  72.        Return the commit message from the head commit
  73.        """
  74.  
  75.     def analyze_git_commit(self, head_commit):
  76.         """
  77.        Check the current commit at HEAD.
  78.  
  79.        Return the commit information such as commit message,
  80.        type of commit, commit SHA, modified, added and
  81.        delete files by the commit.
  82.  
  83.        :param head_commit: The commit message at HEAD
  84.        :return:            A tuple comprising of commit message,
  85.                            commit sha, type of commit and lists of
  86.                            modified, added and deleted files
  87.        """
  88.         raw_commit_message = head_commit
  89.  
  90.         commit_sha = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  91.  
  92.         commit_type = COMMIT_TYPE.simple_commit
  93.  
  94.         head_commit = head_commit.strip('\n')
  95.  
  96.         if re.search(r'\[ci skip\]|\[skip ci\]', head_commit):
  97.             commit_type |= COMMIT_TYPE.ci_skip_commit
  98.  
  99.         get_parent_commits = 'git log --pretty=%P -n 1 ' + commit_sha
  100.         parent_commits = run_shell_command(get_parent_commits)[0]
  101.         parent_commits_list = parent_commits.split(' ')
  102.  
  103.         if len(parent_commits_list) >= 2:
  104.             commit_type |= COMMIT_TYPE.merge_commit
  105.  
  106.         if re.search(
  107.           r'Revert\s\".+\"\n\nThis\sreverts\scommit\s[0-9a-f]{40}\.',
  108.           head_commit):
  109.             commit_type |= COMMIT_TYPE.revert_commit
  110.  
  111.         get_all_committed_files = ('git show --pretty="" --name-status ' +
  112.                                    commit_sha)
  113.         all_committed_files = run_shell_command(get_all_committed_files)[0]
  114.         all_committed_files = all_committed_files.split('\n')
  115.  
  116.         modified_files_list = []
  117.         added_files_list = []
  118.         deleted_files_list = []
  119.  
  120.         for line in all_committed_files:
  121.             pos = line.find('\t')
  122.             change = line[:pos]
  123.             if change == 'M':
  124.                 modified_files_list.append(line[pos+1:])
  125.             elif change == 'A':
  126.                 added_files_list.append(line[pos+1:])
  127.             elif change == 'D':
  128.                 deleted_files_list.append(line[pos+1:])
  129.  
  130.         yield (raw_commit_message, commit_sha, commit_type,
  131.                modified_files_list, added_files_list, deleted_files_list)
  132.  
  133.     def run(self, **kwargs):
  134.         """
  135.        This bear returns information about the HEAD commit
  136.        as HiddenResult which can be used for inspection by
  137.        other bears.
  138.        """
  139.         head_commit, error = self.get_head_commit()
  140.  
  141.         if error:
  142.             vcs_name = list(self.LANGUAGES)[0].lower()+':'
  143.             self.err(vcs_name, repr(error))
  144.             return
  145.  
  146.         for (raw_commit_message, commit_sha, commit_type, modified_files,
  147.              added_files, deleted_files) in self.analyze_git_commit(
  148.                 head_commit):
  149.  
  150.             yield CommitResult(self, raw_commit_message, commit_sha,
  151.                                commit_type, modified_files,
  152.                                added_files, deleted_files)
Add Comment
Please, Sign In to add comment