summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint-core/gitlint/cli.py')
-rw-r--r--gitlint-core/gitlint/cli.py70
1 files changed, 33 insertions, 37 deletions
diff --git a/gitlint-core/gitlint/cli.py b/gitlint-core/gitlint/cli.py
index 387072e..619f006 100644
--- a/gitlint-core/gitlint/cli.py
+++ b/gitlint-core/gitlint/cli.py
@@ -1,22 +1,22 @@
-# pylint: disable=bad-option-value,wrong-import-position
-# We need to disable the import position checks because of the windows check that we need to do below
import copy
import logging
import os
import platform
import stat
import sys
+
import click
import gitlint
-from gitlint.lint import GitLinter
+from gitlint import hooks
from gitlint.config import LintConfigBuilder, LintConfigError, LintConfigGenerator
-from gitlint.deprecation import LOG as DEPRECATED_LOG, DEPRECATED_LOG_FORMAT
+from gitlint.deprecation import DEPRECATED_LOG_FORMAT
+from gitlint.deprecation import LOG as DEPRECATED_LOG
+from gitlint.exception import GitlintError
from gitlint.git import GitContext, GitContextError, git_version
-from gitlint import hooks
+from gitlint.lint import GitLinter
from gitlint.shell import shell
from gitlint.utils import LOG_FORMAT
-from gitlint.exception import GitlintError
# Error codes
GITLINT_SUCCESS = 0
@@ -40,8 +40,6 @@ LOG = logging.getLogger("gitlint.cli")
class GitLintUsageError(GitlintError):
"""Exception indicating there is an issue with how gitlint is used."""
- pass
-
def setup_logging():
"""Setup gitlint logging"""
@@ -49,7 +47,7 @@ def setup_logging():
# Root log, mostly used for debug
root_log = logging.getLogger("gitlint")
root_log.propagate = False # Don't propagate to child loggers, the gitlint root logger handles everything
- root_log.setLevel(logging.ERROR)
+ root_log.setLevel(logging.WARN)
handler = logging.StreamHandler()
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)
@@ -69,10 +67,11 @@ def log_system_info():
LOG.debug("Git version: %s", git_version())
LOG.debug("Gitlint version: %s", gitlint.__version__)
LOG.debug("GITLINT_USE_SH_LIB: %s", os.environ.get("GITLINT_USE_SH_LIB", "[NOT SET]"))
- LOG.debug("DEFAULT_ENCODING: %s", gitlint.utils.DEFAULT_ENCODING)
+ LOG.debug("TERMINAL_ENCODING: %s", gitlint.utils.TERMINAL_ENCODING)
+ LOG.debug("FILE_ENCODING: %s", gitlint.utils.FILE_ENCODING)
-def build_config( # pylint: disable=too-many-arguments
+def build_config(
target,
config_path,
c,
@@ -172,11 +171,9 @@ def build_git_context(lint_config, msg_filename, commit_hash, refspec):
from_commit_msg = GitContext.from_commit_msg
if lint_config.staged:
LOG.debug("Fetching additional meta-data from staged commit")
- from_commit_msg = (
- lambda message: GitContext.from_staged_commit( # pylint: disable=unnecessary-lambda-assignment
- message, lint_config.target
- )
- )
+
+ def from_commit_msg(message):
+ return GitContext.from_staged_commit(message, lint_config.target)
# Order of precedence:
# 1. Any data specified via --msg-filename
@@ -208,7 +205,7 @@ def build_git_context(lint_config, msg_filename, commit_hash, refspec):
if refspec:
# 3.1.1 Not real refspec, but comma-separated list of commit hashes
if "," in refspec:
- commit_hashes = [hash.strip() for hash in refspec.split(",")]
+ commit_hashes = [hash.strip() for hash in refspec.split(",") if hash]
return GitContext.from_local_repository(lint_config.target, commit_hashes=commit_hashes)
# 3.1.2 Real refspec
return GitContext.from_local_repository(lint_config.target, refspec=refspec)
@@ -247,43 +244,43 @@ class ContextObj:
# fmt: off
-@click.group(invoke_without_command=True, context_settings={'max_content_width': 120},
+@click.group(invoke_without_command=True, context_settings={"max_content_width": 120},
epilog="When no COMMAND is specified, gitlint defaults to 'gitlint lint'.")
-@click.option('--target', envvar='GITLINT_TARGET',
+@click.option("--target", envvar="GITLINT_TARGET",
type=click.Path(exists=True, resolve_path=True, file_okay=False, readable=True),
help="Path of the target git repository. [default: current working directory]")
-@click.option('-C', '--config', envvar='GITLINT_CONFIG',
+@click.option("-C", "--config", envvar="GITLINT_CONFIG",
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
help=f"Config file location [default: {DEFAULT_CONFIG_FILE}]")
-@click.option('-c', multiple=True,
+@click.option("-c", multiple=True,
help="Config flags in format <rule>.<option>=<value> (e.g.: -c T1.line-length=80). " +
- "Flag can be used multiple times to set multiple config values.") # pylint: disable=bad-continuation
-@click.option('--commit', envvar='GITLINT_COMMIT', default=None, help="Hash (SHA) of specific commit to lint.")
-@click.option('--commits', envvar='GITLINT_COMMITS', default=None,
+ "Flag can be used multiple times to set multiple config values.")
+@click.option("--commit", envvar="GITLINT_COMMIT", default=None, help="Hash (SHA) of specific commit to lint.")
+@click.option("--commits", envvar="GITLINT_COMMITS", default=None,
help="The range of commits (refspec or comma-separated hashes) to lint. [default: HEAD]")
-@click.option('-e', '--extra-path', envvar='GITLINT_EXTRA_PATH',
+@click.option("-e", "--extra-path", envvar="GITLINT_EXTRA_PATH",
help="Path to a directory or python module with extra user-defined rules",
type=click.Path(exists=True, resolve_path=True, readable=True))
-@click.option('--ignore', envvar='GITLINT_IGNORE', default="", help="Ignore rules (comma-separated by id or name).")
-@click.option('--contrib', envvar='GITLINT_CONTRIB', default="",
+@click.option("--ignore", envvar="GITLINT_IGNORE", default="", help="Ignore rules (comma-separated by id or name).")
+@click.option("--contrib", envvar="GITLINT_CONTRIB", default="",
help="Contrib rules to enable (comma-separated by id or name).")
-@click.option('--msg-filename', type=click.File(encoding=gitlint.utils.DEFAULT_ENCODING),
+@click.option("--msg-filename", type=click.File(encoding=gitlint.utils.FILE_ENCODING),
help="Path to a file containing a commit-msg.")
-@click.option('--ignore-stdin', envvar='GITLINT_IGNORE_STDIN', is_flag=True,
+@click.option("--ignore-stdin", envvar="GITLINT_IGNORE_STDIN", is_flag=True,
help="Ignore any stdin data. Useful for running in CI server.")
-@click.option('--staged', envvar='GITLINT_STAGED', is_flag=True,
+@click.option("--staged", envvar="GITLINT_STAGED", is_flag=True,
help="Attempt smart guesses about meta info (like author name, email, branch, changed files, etc) " +
"for staged commits.")
-@click.option('--fail-without-commits', envvar='GITLINT_FAIL_WITHOUT_COMMITS', is_flag=True,
+@click.option("--fail-without-commits", envvar="GITLINT_FAIL_WITHOUT_COMMITS", is_flag=True,
help="Hard fail when the target commit range is empty.")
-@click.option('-v', '--verbose', envvar='GITLINT_VERBOSITY', count=True, default=0,
- help="Verbosity, more v's for more verbose output (e.g.: -v, -vv, -vvv). [default: -vvv]", )
-@click.option('-s', '--silent', envvar='GITLINT_SILENT', is_flag=True,
+@click.option("-v", "--verbose", envvar="GITLINT_VERBOSITY", count=True, default=0,
+ help="Verbosity, use multiple times for more verbose output (e.g.: -v, -vv, -vvv). [default: -vvv]", )
+@click.option("-s", "--silent", envvar="GITLINT_SILENT", is_flag=True,
help="Silent mode (no output). Takes precedence over -v, -vv, -vvv.")
-@click.option('-d', '--debug', envvar='GITLINT_DEBUG', help="Enable debugging output.", is_flag=True)
+@click.option("-d", "--debug", envvar="GITLINT_DEBUG", help="Enable debugging output.", is_flag=True)
@click.version_option(version=gitlint.__version__)
@click.pass_context
-def cli( # pylint: disable=too-many-arguments
+def cli(
ctx, target, config, c, commit, commits, extra_path, ignore, contrib,
msg_filename, ignore_stdin, staged, fail_without_commits, verbose,
silent, debug,
@@ -499,5 +496,4 @@ def generate_config(ctx):
# Let's Party!
setup_logging()
if __name__ == "__main__":
- # pylint: disable=no-value-for-parameter
cli() # pragma: no cover