Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty commit list in --commits (#22) #25

Merged
merged 1 commit into from
Mar 16, 2017
Merged
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
7 changes: 6 additions & 1 deletion gitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def lint(ctx):
click.echo(ustr(e))
ctx.exit(GIT_CONTEXT_ERROR_CODE)

number_of_commits = len(gitcontext.commits)

if number_of_commits == 0:
click.echo(u'No commits in range "{0}".'.format(ctx.obj[2]))
ctx.exit(0)

config_builder = ctx.obj[1]
last_commit = gitcontext.commits[-1]
# Apply an additional config that is specified in the last commit message
Expand All @@ -129,7 +135,6 @@ def lint(ctx):

# Let's get linting!
linter = GitLinter(lint_config)
number_of_commits = len(gitcontext.commits)
first_violation = True

for commit in gitcontext.commits:
Expand Down
10 changes: 10 additions & 0 deletions gitlint/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ def test_git_error(self, sys, sh):
result = self.cli.invoke(cli.cli)
self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE)

@patch('gitlint.git.sh')
@patch('gitlint.cli.sys')
def test_no_commits_in_range(self, sys, sh):
sys.stdin.isatty.return_value = True
sh.git.side_effect = lambda *_args, **_kwargs: ""
result = self.cli.invoke(cli.cli, ["--commits", "master...HEAD"])
expected = u'No commits in range "master...HEAD".\n'
self.assertEqual(result.output, expected)
self.assertEqual(result.exit_code, 0)

@patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook')
def test_install_hook(self, install_hook):
result = self.cli.invoke(cli.cli, ["install-hook"])
Expand Down