Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions gitlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import os
import os.path
import sys
from fnmatch import fnmatch

import docopt
import termcolor
Expand Down Expand Up @@ -104,7 +105,7 @@ def get_config(repo_root):
else:
yaml_config = yaml.load(content)

return linters.parse_yaml_config(yaml_config, repo_root)
return linters.parse_yaml_config(yaml_config, repo_root), yaml_config.get("ignore", [])


def format_comment(comment_data):
Expand All @@ -115,7 +116,7 @@ def format_comment(comment_data):

'line {line}, col {column}: {severity}: [{message_id}]: {message}'

Any of the fields may nbe absent.
Any of the fields may be absent.

Args:
comment_data: dictionary with the linter data.
Expand Down Expand Up @@ -214,9 +215,14 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):

linter_not_found = False
files_with_problems = 0
gitlint_config = get_config(repository_root)
gitlint_config, ignore_patterns = get_config(repository_root)
json_result = {}

# Filter out ignored files
for pattern in ignore_patterns:
modified_files = {filename: mode for filename, mode in modified_files.items()
if not fnmatch(filename, pattern)}

for filename in sorted(modified_files.keys()):
rel_filename = os.path.relpath(filename)
if not json_output:
Expand Down
8 changes: 8 additions & 0 deletions gitlint/configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
# braces, as in '{{}}' or '{{foo}}'. See the pylint configuration for an
# example.

# Ignored paths
# You can use this system to ignore paths from the linting. This can be useful
# when a frame work automatically generates files that do not pass you linting.
# Uncomment to activate. The file paths use glob syntax.

#ignore:
# - */migrations/*.py

# CSS
# Sample output:
# /home/skreft/opensource/git-lint/test/e2etest/data/csslint/error.css: line 3, col 2, Warning - Duplicate property 'width' found.
Expand Down
4 changes: 2 additions & 2 deletions gitlint/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def modified_files(root, tracked_only=False, commit=None):
r'(?P<mode>%s) (?P<filename>.+)' % modes_str,
groups=('filename', 'mode'))

return dict((os.path.join(root, _remove_filename_quotes(filename)), mode)
for filename, mode in modified_file_status)
return {os.path.join(root, _remove_filename_quotes(filename)): mode
for filename, mode in modified_file_status}


def _modified_files_with_commit(root, commit):
Expand Down
5 changes: 5 additions & 0 deletions gitlint/linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ def parse_yaml_config(yaml_config, repo_home):
}

for name, data in yaml_config.items():

#Skip the ignore path settings
if name == "ignore":
continue

command = _replace_variables([data['command']], variables)[0]
requirements = _replace_variables(data.get('requirements', []),
variables)
Expand Down
2 changes: 1 addition & 1 deletion test/e2etest/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_linter(self):
@classmethod
def add_linter_checks(cls):
"""Add a test for each defined linter and extension."""
for extension, linter_list in gitlint.get_config(None).items():
for extension, linter_list in gitlint.get_config(None)[0].items():
for linter in linter_list:
cls.add_linter_check(linter.args[0], extension)

Expand Down
10 changes: 5 additions & 5 deletions test/unittest/test_gitlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GitLintTest(unittest.TestCase):
def setUpClass(cls):
cls._stderr = sys.stderr
sys.stderr = sys.stdout
cls.git_lint_config = gitlint.get_config(None)
cls.git_lint_config = gitlint.get_config(None)[0]

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -493,20 +493,20 @@ def test_get_config(self):
mock.patch('gitlint.open',
mock.mock_open(read_data=config),
create=True) as mock_open:
parsed_config = gitlint.get_config(self.root)
parsed_config = gitlint.get_config(self.root)[0]
mock_open.assert_called_once_with(git_config)
self.assertEqual(['.py'], list(parsed_config.keys()))
self.assertEqual(1, len(parsed_config['.py']))

def test_get_config_from_default(self):
with mock.patch('os.path.exists', return_value=False):
parsed_config = gitlint.get_config(self.root)
parsed_config = gitlint.get_config(self.root)[0]
self.assertEquals(self.git_lint_config, parsed_config)

def test_get_config_not_in_a_repo(self):
# When not in a repo should return the default config.
self.git_repository_root.return_value = None
parsed_config = gitlint.get_config(None)
parsed_config = gitlint.get_config(None)[0]
self.assertEquals(self.git_lint_config, parsed_config)

def test_get_config_empty(self):
Expand All @@ -515,7 +515,7 @@ def test_get_config_empty(self):
mock.patch('gitlint.open',
mock.mock_open(read_data=''),
create=True) as mock_open:
parsed_config = gitlint.get_config(self.root)
parsed_config = gitlint.get_config(self.root)[0]
self.assertEqual({}, parsed_config)

def test_format_comment(self):
Expand Down