How to use the gitlint.utils function in gitlint

To help you get started, we’ve selected a few gitlint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sk- / git-lint / test / unittest / test_utils.py View on Github external
def test_filter_lines_no_groups(self):
        lines = ['a', 'b', 'c', 'ad']
        self.assertEqual(lines, list(utils.filter_lines(lines, '.')))
        self.assertEqual(['a', 'ad'], list(utils.filter_lines(lines, 'a')))
        self.assertEqual(['ad'], list(utils.filter_lines(lines, '.d')))
        self.assertEqual(['ad'], list(utils.filter_lines(lines, 'd')))
        self.assertEqual([], list(utils.filter_lines(lines, '^d')))
        self.assertEqual([], list(utils.filter_lines(lines, 'foo')))
github sk- / git-lint / test / unittest / test_utils.py View on Github external
def test_filter_lines_no_groups(self):
        lines = ['a', 'b', 'c', 'ad']
        self.assertEqual(lines, list(utils.filter_lines(lines, '.')))
        self.assertEqual(['a', 'ad'], list(utils.filter_lines(lines, 'a')))
        self.assertEqual(['ad'], list(utils.filter_lines(lines, '.d')))
        self.assertEqual(['ad'], list(utils.filter_lines(lines, 'd')))
        self.assertEqual([], list(utils.filter_lines(lines, '^d')))
        self.assertEqual([], list(utils.filter_lines(lines, 'foo')))
github aosp-mirror / platform_development / tools / checkstyle / gitlint / git.py View on Github external
def _modified_files_with_commit(root, commit):
    # Convert to unicode and split
    status_lines = subprocess.check_output(
        ['git', 'diff-tree', '-r', '--root', '--no-commit-id', '--name-status',
         commit]).decode('utf-8').split(os.linesep)

    modified_file_status = utils.filter_lines(
        status_lines,
        r'(?PA|M)\s(?P.+)',
        groups=('filename', 'mode'))

    # We need to add a space to the mode, so to be compatible with the output
    # generated by modified files.
    return dict((os.path.join(root, _remove_filename_quotes(filename)),
                 mode + ' ') for filename, mode in modified_file_status)
github sk- / git-lint / gitlint / git.py View on Github external
def _modified_files_with_commit(root, commit):
    # Convert to unicode and split
    status_lines = subprocess.check_output([
        'git', 'diff-tree', '-r', '--root', '--no-commit-id', '--name-status',
        commit
    ]).decode('utf-8').split(os.linesep)

    modified_file_status = utils.filter_lines(
        status_lines,
        r'(?PA|M)\s(?P.+)',
        groups=('filename', 'mode'))

    # We need to add a space to the mode, so to be compatible with the output
    # generated by modified files.
    return dict((os.path.join(root, _remove_filename_quotes(filename)),
                 mode + ' ') for filename, mode in modified_file_status)
github sk- / git-lint / gitlint / hg.py View on Github external
assert os.path.isabs(root), "Root has to be absolute, got: %s" % root

    command = ['hg', 'status']
    if commit:
        command.append('--change=%s' % commit)

    # Convert to unicode and split
    status_lines = subprocess.check_output(command).decode('utf-8').split(
        os.linesep)

    modes = ['M', 'A']
    if not tracked_only:
        modes.append(r'\?')
    modes_str = '|'.join(modes)

    modified_file_status = utils.filter_lines(
        status_lines,
        r'(?P%s) (?P.+)' % modes_str,
        groups=('filename', 'mode'))

    return dict((os.path.join(root, filename), mode)
                for filename, mode in modified_file_status)
github aosp-mirror / platform_development / tools / checkstyle / gitlint / git.py View on Github external
new.
    """
    if extra_data is None:
        return []
    if extra_data not in ('M ', ' M', 'MM'):
        return None

    if commit is None:
        commit = '0' * 40
    commit = commit.encode('utf-8')

    # Split as bytes, as the output may have some non unicode characters.
    blame_lines = subprocess.check_output(
        ['git', 'blame', commit, '--porcelain', '--', filename]).split(
            os.linesep.encode('utf-8'))
    modified_line_numbers = utils.filter_lines(
        blame_lines,
        commit + br' (?P
github sk- / git-lint / gitlint / hg.py View on Github external
new.
    """
    if extra_data is None:
        return []
    if extra_data != 'M':
        return None

    command = ['hg', 'diff', '-U', '0']
    if commit:
        command.append('--change=%s' % commit)
    command.append(filename)

    # Split as bytes, as the output may have some non unicode characters.
    diff_lines = subprocess.check_output(command).split(
        os.linesep.encode('utf-8'))
    diff_line_numbers = utils.filter_lines(
        diff_lines,
        br'@@ -\d+,\d+ \+(?P\d+),(?P\d+) @@',
        groups=('start_line', 'lines'))
    modified_line_numbers = []
    for start_line, lines in diff_line_numbers:
        start_line = int(start_line)
        lines = int(lines)
        modified_line_numbers.extend(range(start_line, start_line + lines))

    return modified_line_numbers
github sk- / git-lint / gitlint / git.py View on Github external
new.
    """
    if extra_data is None:
        return []
    if extra_data not in ('M ', ' M', 'MM'):
        return None

    if commit is None:
        commit = '0' * 40
    commit = commit.encode('utf-8')

    # Split as bytes, as the output may have some non unicode characters.
    blame_lines = subprocess.check_output(
        ['git', 'blame', '--porcelain', filename]).split(
            os.linesep.encode('utf-8'))
    modified_line_numbers = utils.filter_lines(
        blame_lines, commit + br' (?P
github jorisroovers / gitlint / examples / my_commit_rules.py View on Github external
def validate(self, commit):
        violations = []
        allowed_branch_prefixes = self.options['branch-prefixes'].value
        for branch in commit.branches:
            valid_branch_name = False

            for allowed_prefix in allowed_branch_prefixes:
                if branch.startswith(allowed_prefix):
                    valid_branch_name = True
                    break

            if not valid_branch_name:
                msg = "Branch name '{0}' does not start with one of {1}".format(branch,
                                                                                utils.sstr(allowed_branch_prefixes))
                violations.append(RuleViolation(self.id, msg, line_nr=1))

        return violations
github sk- / git-lint / gitlint / linters.py View on Github external
Executes the lint tool 'program' with arguments 'arguments' over the file
    'filename' returning only those lines matching the regular expression
    'filter_regex'.

    Args:
      name: string: the name of the linter.
      program: string: lint program.
      arguments: list[string]: extra arguments for the program.
      filter_regex: string: regular expression to filter lines.
      filename: string: filename to lint.
      lines: list[int]|None: list of lines that we want to capture. If None,
        then all lines will be captured.

    Returns: dict: a dict with the extracted info from the message.
    """
    output = utils.get_output_from_cache(name, filename)

    if output is None:
        call_arguments = [program] + arguments + [filename]
        try:
            output = subprocess.check_output(
                call_arguments, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as error:
            output = error.output
        except OSError:
            return {
                filename: {
                    'error': [('Could not execute "%s".%sMake sure all ' +
                               'required programs are installed') %
                              (' '.join(call_arguments), os.linesep)]
                }
            }