From ec8e47192adddf9fc0d76eb7f7189704d1bd1ded Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Wed, 22 Mar 2017 20:18:48 +0000 Subject: [PATCH] lint: Add checks for commit messages using gitlint. Fixes #1131. --- .gitlint | 11 +++++++++++ .travis.yml | 1 + requirements/dev.txt | 7 +++++++ tools/commit-msg | 19 +++++++++++++++++++ tools/lint-all | 6 ++++++ tools/lint-commits | 12 ++++++++++++ tools/pre-commit | 4 ++-- tools/setup-git-repo | 3 ++- version.py | 2 +- 9 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 .gitlint create mode 100755 tools/commit-msg create mode 100755 tools/lint-commits diff --git a/.gitlint b/.gitlint new file mode 100644 index 0000000000..4287c8d1aa --- /dev/null +++ b/.gitlint @@ -0,0 +1,11 @@ +[general] +ignore=title-trailing-punctuation, body-min-length, body-is-missing + +[title-match-regex] +regex=^.+\.$ + +[title-max-length] +line-length=76 + +[body-max-line-length] +line-length=76 diff --git a/.travis.yml b/.travis.yml index 171c1659ae..38f11d6cbb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ env: - COVERALLS_SERVICE_NAME=travis-pro - COVERALLS_REPO_TOKEN=hnXUEBKsORKHc8xIENGs9JjktlTb2HKlG - BOTO_CONFIG=/tmp/nowhere + - UPSTREAM_MASTER=$(git ls-remote git://github.com/zulip/zulip.git refs/heads/master | cut -f 1) language: python matrix: include: diff --git a/requirements/dev.txt b/requirements/dev.txt index b6c9b65313..06137ec05a 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -21,6 +21,13 @@ pyflakes==1.5.0 # Needed to run tests in parallel tblib==1.3.0 +# Needed to lint Git commit messages +gitlint==0.8.1 + +# Needed for gitlint +arrow==0.10.0 +sh==1.11 + # Needed to sync translations from transifex transifex-client==0.12.4 diff --git a/tools/commit-msg b/tools/commit-msg new file mode 100755 index 0000000000..5adf9accec --- /dev/null +++ b/tools/commit-msg @@ -0,0 +1,19 @@ +#!/bin/bash + +# This hook runs gitlint on your commit message. + +# If your machine contains a provisioned Zulip development environment, the +# linter will be invoked through `vagrant ssh`. + +lint_cmd="cd ~/zulip && cat \"$1\" | python -m gitlint.cli" +if [ -z "$VIRTUAL_ENV" ] && `which vagrant > /dev/null` && [ -e .vagrant ]; then + vagrant ssh -c "$lint_cmd" +else + $lint_cmd +fi + +if [ $? -ne 0 ]; then + echo "WARNING: Your commit message does not match Zulip's style guide." +fi + +exit 0 diff --git a/tools/lint-all b/tools/lint-all index 53a0f2472d..a431df8e2b 100755 --- a/tools/lint-all +++ b/tools/lint-all @@ -617,6 +617,9 @@ def run(): parser.add_option('--pep8', action='store_true', help='Run the pep8 checker') + parser.add_option('--no-gitlint', + action='store_true', + help='Disable gitlint') parser.add_option('--modified', '-m', action='store_true', help='Only check modified files') @@ -687,6 +690,9 @@ def run(): external_linter('templates', ['tools/check-templates'], ['handlebars', 'html']) external_linter('urls', ['tools/check-urls']) + if not options.no_gitlint: + external_linter('commit_messages', ['tools/lint-commits']) + @lint def custom_py(): # type: () -> int diff --git a/tools/lint-commits b/tools/lint-commits new file mode 100755 index 0000000000..1132966fa2 --- /dev/null +++ b/tools/lint-commits @@ -0,0 +1,12 @@ +#!/bin/bash + +# Lints all commit messages that are newer than upstream/master. +# The rules can be found in /.gitlint + +# If this script is running on TravisCI, $UPSTREAM_MASTER would be set to the +# latest HEAD in travis.yml +if [ ! $TRAVIS ]; then + UPSTREAM_MASTER=upstream/master +fi + +gitlint --commits $UPSTREAM_MASTER...HEAD diff --git a/tools/pre-commit b/tools/pre-commit index 14ea067e68..0120a4d1a4 100755 --- a/tools/pre-commit +++ b/tools/pre-commit @@ -16,10 +16,10 @@ if [ -z "$changed_files" ]; then fi if [ -z "$VIRTUAL_ENV" ] && `which vagrant > /dev/null` && [ -e .vagrant ]; then - vcmd="/srv/zulip/tools/lint-all --pep8 --force $changed_files || true" + vcmd="/srv/zulip/tools/lint-all --pep8 --no-gitlint --force $changed_files || true" echo "Running lint-all using vagrant..." vagrant ssh -c "$vcmd" else - ./tools/lint-all --pep8 --force $changed_files || true + ./tools/lint-all --pep8 --no-gitlint --force $changed_files || true fi exit 0 diff --git a/tools/setup-git-repo b/tools/setup-git-repo index 3c21c8677d..6035cee220 100755 --- a/tools/setup-git-repo +++ b/tools/setup-git-repo @@ -6,6 +6,7 @@ if ! [ -d ".git/hooks/" ]; then exit 1 fi -for hook in pre-commit; do +for hook in pre-commit commit-msg +do ln -snf ../../tools/"$hook" .git/hooks/ done diff --git a/version.py b/version.py index 4c3587a78c..b788bc5b3b 100644 --- a/version.py +++ b/version.py @@ -1,2 +1,2 @@ ZULIP_VERSION = "1.5.1+git" -PROVISION_VERSION = '4.17' +PROVISION_VERSION = '4.18'