Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_git_context(lint_config, msg_filename, refspec):
""" Builds a git context based on passed parameters and order of precedence """
# Determine which GitContext method to use if a custom message is passed
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(message, lint_config.target) # noqa
# Order of precedence:
# 1. Any data specified via --msg-filename
if msg_filename:
LOG.debug("Using --msg-filename.")
return from_commit_msg(ustr(msg_filename.read()))
# 2. Any data sent to stdin (unless stdin is being ignored)
if not lint_config.ignore_stdin:
stdin_input = get_stdin_data()
if stdin_input:
LOG.debug("Stdin data: '%s'", stdin_input)
LOG.debug("Stdin detected and not ignored. Using as input.")
return from_commit_msg(stdin_input)
if lint_config.staged:
raise GitLintUsageError(u"The 'staged' option (--staged) can only be used when using '--msg-filename' or "
u"when piping data to gitlint via stdin.")
# 3. Fallback to reading from local repository
LOG.debug("No --msg-filename flag, no or empty data passed to stdin. Using the local repo.")
return GitContext.from_local_repository(lint_config.target, refspec)
def current_branch(self):
current_branch = ustr(_git("rev-parse", "--abbrev-ref", "HEAD", _cwd=self.repository_path)).strip()
return current_branch
def author_email(self):
return ustr(_git("config", "--get", "user.email", _cwd=self.context.repository_path)).strip()
LOG.debug(u"Configuration\n%s", ustr(config))
ctx.obj = (config, config_builder, commits, msg_filename)
# If no subcommand is specified, then just lint
if ctx.invoked_subcommand is None:
ctx.invoke(lint)
except GitContextError as e:
click.echo(ustr(e))
ctx.exit(GIT_CONTEXT_ERROR_CODE)
except GitLintUsageError as e:
click.echo(u"Error: {0}".format(ustr(e)))
ctx.exit(USAGE_ERROR_CODE)
except LintConfigError as e:
click.echo(u"Config Error: {0}".format(ustr(e)))
ctx.exit(CONFIG_ERROR_CODE)
def git_hooks_dir(repository_path):
""" Determine hooks directory for a given target dir """
hooks_dir = _git("rev-parse", "--git-path", "hooks", _cwd=repository_path)
hooks_dir = ustr(hooks_dir).replace(u"\n", u"")
return os.path.realpath(os.path.join(repository_path, hooks_dir))
def set(self, value):
if isinstance(value, list):
the_list = value
else:
the_list = ustr(value).split(",")
self.value = [ustr(item.strip()) for item in the_list if item.strip() != ""]
def install_hook(ctx):
""" Install gitlint as a git commit-msg hook. """
try:
lint_config = ctx.obj[0]
hooks.GitHookInstaller.install_commit_msg_hook(lint_config)
hook_path = hooks.GitHookInstaller.commit_msg_hook_path(lint_config)
click.echo(u"Successfully installed gitlint commit-msg hook in {0}".format(hook_path))
ctx.exit(0)
except hooks.GitHookInstallerError as e:
click.echo(ustr(e), err=True)
ctx.exit(GIT_CONTEXT_ERROR_CODE)
def _get_option(self, rule_name_or_id, option_name):
rule_name_or_id = ustr(rule_name_or_id) # convert to unicode first
option_name = ustr(option_name)
rule = self.rules.find_rule(rule_name_or_id)
if not rule:
raise LintConfigError(u"No such rule '{0}'".format(rule_name_or_id))
option = rule.options.get(option_name)
if not option:
raise LintConfigError(u"Rule '{0}' has no option '{1}'".format(rule_name_or_id, option_name))
return option
def __unicode__(self):
format_str = (u"--- Commit Message ----\n%s\n"
u"--- Meta info ---------\n"
u"Author: %s <%s>\nDate: %s\n"
u"is-merge-commit: %s\nis-fixup-commit: %s\n"
u"is-squash-commit: %s\nis-revert-commit: %s\n"
u"Branches: %s\n"
u"Changed Files: %s\n"
u"-----------------------") # pragma: no cover
date_str = arrow.get(self.date).format(GIT_TIMEFORMAT) if self.date else None
return format_str % (ustr(self.message), self.author_name, self.author_email, date_str,
self.is_merge_commit, self.is_fixup_commit, self.is_squash_commit,
self.is_revert_commit, sstr(self.branches), sstr(self.changed_files)) # pragma: no cover
def validate(self, line, _commit):
violations = []
for commit_type in self.options["types"].value:
if line.startswith(ustr(commit_type)):
break
else:
msg = u"Title does not start with one of {0}".format(', '.join(self.options['types'].value))
violations.append(RuleViolation(self.id, msg, line))
if not RULE_REGEX.match(line):
msg = u"Title does not follow ConventionalCommits.org format 'type(optional-scope): description'"
violations.append(RuleViolation(self.id, msg, line))
return violations