diff options
author | Joris Roovers <jroovers@cisco.com> | 2018-04-15 12:56:29 +0200 |
---|---|---|
committer | Joris Roovers <jroovers@cisco.com> | 2018-04-15 12:56:29 +0200 |
commit | c424ef45bbde216f87203d9bd8b6d73d8566e8a4 (patch) | |
tree | d2e3b6296f80cf5be1e36cf1ac5a4a8cd0aee99c | |
parent | 8eefab08c468e5ef4814df11512a441b5de6edcc (diff) |
Support for pre-push - alternative codeissues/61
We'll probably not use this (see comments on #61), but keeping this
around in case we change our minds!
-rw-r--r-- | gitlint/cli.py | 17 | ||||
-rw-r--r-- | gitlint/git.py | 22 |
2 files changed, 30 insertions, 9 deletions
diff --git a/gitlint/cli.py b/gitlint/cli.py index 1d22bf2..bd549aa 100644 --- a/gitlint/cli.py +++ b/gitlint/cli.py @@ -173,6 +173,18 @@ def cli( # pylint: disable=too-many-arguments ctx.exit(GIT_CONTEXT_ERROR_CODE) +@cli.command("lint-pre-push") +@click.pass_context +def lint_pre_push(ctx): + """ Lints a git pre-push """ + lint_config = ctx.obj[0] + + hashes = [line.split()[1] for line in sys.stdin] + + gitcontext = GitContext.from_local_repository(lint_config.target, sha_list=hashes) + lint_context(ctx, gitcontext) + + @cli.command("lint") @click.pass_context def lint(ctx): @@ -196,6 +208,11 @@ def lint(ctx): LOG.debug("No --msg-filename flag, no or empty data passed to stdin. Attempting to read from the local repo.") gitcontext = GitContext.from_local_repository(lint_config.target, ctx.obj[2]) + lint_context(ctx, gitcontext) + + +def lint_context(ctx, gitcontext): + lint_config = ctx.obj[0] number_of_commits = len(gitcontext.commits) # Exit if we don't have commits in the specified range. Use a 0 exit code, since a popular use-case is one # where users are using --commits in a check job to check the commit messages inside a CI job. By returning 0, we diff --git a/gitlint/git.py b/gitlint/git.py index 422bd9f..7f7dd99 100644 --- a/gitlint/git.py +++ b/gitlint/git.py @@ -166,7 +166,7 @@ class GitContext(object): return context @staticmethod - def from_local_repository(repository_path, refspec=None): + def from_local_repository(repository_path, refspec=None, sha_list=None): """ Retrieves the git context from a local git repository. :param repository_path: Path to the git repository to retrieve the context from :param refspec: The commit(s) to retrieve @@ -174,14 +174,18 @@ class GitContext(object): context = GitContext() - # If no refspec is defined, fallback to the last commit on the current branch - if refspec is None: - # We tried many things here e.g.: defaulting to e.g. HEAD or HEAD^... (incl. dealing with - # repos that only have a single commit - HEAD^... doesn't work there), but then we still get into - # problems with e.g. merge commits. Easiest solution is just taking the SHA from `git log -1`. - sha_list = [_git("log", "-1", "--pretty=%H", _cwd=repository_path).replace(u"\n", u"")] - else: - sha_list = _git("rev-list", refspec, _cwd=repository_path).split() + # If no explicit sha_list is provided, then let's see if a refspec is specified + if sha_list is None: + + # If no refspec is defined, fallback to the last commit on the current branch + if refspec is None: + # We tried many things here e.g.: defaulting to e.g. HEAD or HEAD^... (incl. dealing with + # repos that only have a single commit - HEAD^... doesn't work there), but then we still get into + # problems with e.g. merge commits. Easiest solution is just taking the SHA from `git log -1`. + sha_list = [_git("log", "-1", "--pretty=%H", _cwd=repository_path).replace(u"\n", u"")] + else: + sha_list = _git("rev-list", refspec, _cwd=repository_path).split() + for sha in sha_list: # Get info from the local git repository: https://git-scm.com/docs/pretty-formats |