diff options
author | Łukasz Rogalski <rogalski.91@gmail.com> | 2019-06-21 21:52:47 +0200 |
---|---|---|
committer | Joris Roovers <joris.roovers@gmail.com> | 2019-07-08 13:26:47 +0200 |
commit | 3ee281eec4ef4325ce90d27f6f368a6b95818cfe (patch) | |
tree | fee6ffd1ac165c2f79b725ebb8b4490d76f6986b | |
parent | e95fe6122fae5930534f46dfc15dcd3bb7cf8059 (diff) |
Initial implementation of '--ignore-stdin' flag
This feature is beneficial when running in automated environments
like CI servers. Current heuristic fails in some conditions.
With this flag enabled we can simply always ignore stdin
and fall back to using target repo.
Closes #56
-rw-r--r-- | docs/configuration.md | 1 | ||||
-rw-r--r-- | gitlint/cli.py | 19 | ||||
-rw-r--r-- | gitlint/config.py | 11 | ||||
-rw-r--r-- | gitlint/tests/test_cli.py | 16 |
4 files changed, 42 insertions, 5 deletions
diff --git a/docs/configuration.md b/docs/configuration.md index 6c830af..9bf9dc1 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -161,3 +161,4 @@ debug | false | >= 0.7.1 | ```--debug``` target | (empty) | >= 0.8.0 | ```--target=/home/joe/myrepo/ ``` | Target git repository gitlint should be linting against. extra-path | (empty) | >= 0.8.0 | ```--extra-path=/home/joe/rules/``` | Path where gitlint looks for [user-defined rules](user_defined_rules.md). contrib | (empty) | >= 0.12.0 | ```--contrib=contrib-title-conventional-commits,CC1``` | [Contrib rules](contrib_rules) to enable. +ignore-stdin | false | >= 0.12.0 | ```--ignore-stdin``` | Ignore any stdin data. Useful for running in CI server. diff --git a/gitlint/cli.py b/gitlint/cli.py index 7e186aa..7719e6a 100644 --- a/gitlint/cli.py +++ b/gitlint/cli.py @@ -54,7 +54,9 @@ def log_system_info(): LOG.debug("Gitlint version: %s", gitlint.__version__) -def build_config(ctx, target, config_path, c, extra_path, ignore, contrib, verbose, silent, debug): +def build_config( # pylint: disable=too-many-arguments + ctx, target, config_path, c, extra_path, ignore, contrib, ignore_stdin, verbose, silent, debug +): """ Creates a LintConfig object based on a set of commandline parameters. """ config_builder = LintConfigBuilder() try: @@ -75,6 +77,9 @@ def build_config(ctx, target, config_path, c, extra_path, ignore, contrib, verbo if contrib: config_builder.set_option('general', 'contrib', contrib) + if ignore_stdin: + config_builder.set_option('general', 'ignore-stdin', ignore_stdin) + if silent: config_builder.set_option('general', 'verbosity', 0) elif verbose > 0: @@ -144,6 +149,7 @@ def get_stdin_data(): @click.option('--ignore', default="", help="Ignore rules (comma-separated by id or name).") @click.option('--contrib', default="", help="Contrib rules to enable (comma-separated by id or name).") @click.option('--msg-filename', type=click.File(), help="Path to a file containing a commit-msg.") +@click.option('--ignore-stdin', is_flag=True, help="Ignore any stdin data. Useful for running in CI server.") @click.option('-v', '--verbose', count=True, default=0, help="Verbosity, more v's for more verbose output (e.g.: -v, -vv, -vvv). [default: -vvv]", ) @click.option('-s', '--silent', help="Silent mode (no output). Takes precedence over -v, -vv, -vvv.", is_flag=True) @@ -152,7 +158,7 @@ def get_stdin_data(): @click.pass_context def cli( # pylint: disable=too-many-arguments ctx, target, config, c, commits, extra_path, ignore, contrib, - msg_filename, verbose, silent, debug, + msg_filename, ignore_stdin, verbose, silent, debug, ): """ Git lint tool, checks your git commit messages for styling issues @@ -169,7 +175,7 @@ def cli( # pylint: disable=too-many-arguments # Get the lint config from the commandline parameters and # store it in the context (click allows storing an arbitrary object in ctx.obj). config, config_builder = build_config(ctx, target, config, c, extra_path, - ignore, contrib, verbose, silent, debug) + ignore, contrib, ignore_stdin, verbose, silent, debug) LOG.debug(u"Configuration\n%s", ustr(config)) @@ -197,11 +203,14 @@ def lint(ctx): # 2. Any data sent to stdin # 3. Fallback to reading from local repository stdin_input = get_stdin_data() + if stdin_input: + LOG.debug("Stdin data: %r", stdin_input) + if msg_filename: LOG.debug("Attempting to read from --msg-filename.") gitcontext = GitContext.from_commit_msg(ustr(msg_filename.read())) - elif stdin_input: - LOG.debug("No --msg-filename flag. Attempting to read from stdin.") + elif stdin_input and not lint_config.ignore_stdin: + LOG.debug("Stdin detected and not ignored. Will be used as input.") gitcontext = GitContext.from_commit_msg(stdin_input) else: LOG.debug("No --msg-filename flag, no or empty data passed to stdin. Attempting to read from the local repo.") diff --git a/gitlint/config.py b/gitlint/config.py index 5d5e133..1ac0849 100644 --- a/gitlint/config.py +++ b/gitlint/config.py @@ -74,6 +74,8 @@ class LintConfig(object): self._ignore = options.ListOption('ignore', [], 'List of rule-ids to ignore') self._contrib = options.ListOption('contrib', [], 'List of contrib-rules to enable') self._config_path = None + ignore_stdin_description = "Ignore any stdin data. Useful for running in CI server." + self._ignore_stdin = options.BoolOption('ignore-stdin', False, ignore_stdin_description) @property def target(self): @@ -209,6 +211,15 @@ class LintConfig(object): raise LintConfigError(ustr(e)) @property + def ignore_stdin(self): + return self._ignore_stdin.value + + @ignore_stdin.setter + @handle_option_error + def ignore_stdin(self, value): + return self._ignore_stdin.set(value) + + @property def rules(self): # Create a new list based on _rules.values() because in python 3, values() is a ValuesView as opposed to a list return [rule for rule in self._rules.values()] diff --git a/gitlint/tests/test_cli.py b/gitlint/tests/test_cli.py index aa2de1f..a72b1e9 100644 --- a/gitlint/tests/test_cli.py +++ b/gitlint/tests/test_cli.py @@ -198,6 +198,22 @@ class CLITests(BaseTestCase): self.assertEqual(result.exit_code, 3) self.assertEqual(result.output, "") + @patch('gitlint.cli.get_stdin_data', return_value="Should be ignored\n") + @patch('gitlint.git.sh') + def test_lint_ignore_stdin(self, sh, _): + """ Test for ignoring stdin when --ignore-stdin flag is enabled""" + sh.git.side_effect = self.GIT_CONFIG_SIDE_EFFECTS + [ + "6f29bf81a8322a04071bb794666e48c443a90360", + u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 01:00\x00åbc\n" + u"commït-title\n\ncommït-body", + u"file1.txt\npåth/to/file2.txt\n" + ] + + with patch('gitlint.display.stderr', new=StringIO()) as stderr: + result = self.cli.invoke(cli.cli, ["--ignore-stdin"]) + self.assertEqual(stderr.getvalue(), u'3: B5 Body message is too short (11<20): "commït-body"\n') + self.assertEqual(result.exit_code, 1) + @patch('gitlint.cli.get_stdin_data', return_value=False) def test_msg_filename(self, _): expected_output = u"3: B6 Body message is missing\n" |