forked from tern-tools/tern
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure commit linter checks all commits
In a previous commit (0cba95a) test_commit_message.py was introduced under the assumption that circleci would run against all commits. We soon realized that this was not the case and in fact we were only checking the HEAD commit. This commit introduces the following changes: 1) Creates a new direrctory called 'ci' 2) Moves test_commit_message.py from 'tests' to 'ci' 3) Made moderate edits to test_commit_message.py in order to create a separate commit message linting function. Also updated the error messages to specify the offending commit. Resolves tern-tools#309 Signed-off-by: Rose Judge <[email protected]>
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2019 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
from git import Repo | ||
import os | ||
import re | ||
import sys | ||
|
||
# This script is written to be used with circleci and | ||
# is not meant to be run locally | ||
|
||
def lint_commit(commit_id): | ||
"""Given a commit ID, determine if the | ||
commit message complies with the tern guidelines. If | ||
it does not comply, output the offending commit and | ||
specify the reason for failure.""" | ||
|
||
r = Repo(os.getcwd()) | ||
check = True | ||
message = r.commit(commit_id).message | ||
sha_short = r.git.rev_parse(commit_id, short=7) | ||
|
||
# Check 1: Subject, body and DCO exist | ||
# Note that git does not allow for empty subjects | ||
if len(re.split('\n\n|\r', message)) <= 2: | ||
print("Commit {} does not have a body.".format(sha_short)) | ||
check = False | ||
|
||
# Check 2: Subject length is less than about 50 | ||
if len(re.split('\n\n', message)[0]) > 54: | ||
print("The subject of commit {} should be 50 characters or less.".format(sha_short)) | ||
check = False | ||
|
||
#Check 3: Each line of the body is less than 72 | ||
body = re.split('\n\n|\r', message, 1)[1] | ||
for i in range(len(body)): | ||
if len(body[i]) > 72: | ||
print("Line {0} of commit {1} exceeds 72 characters.".format(i+1, sha_short)) | ||
check = False | ||
|
||
if not check: | ||
sys.exit(1) | ||
|
||
if __name__ == '__main__': | ||
# Get the list of commits and loop through them, | ||
# inputting each one into the linting function | ||
|
||
# We are in the 'tern' directory | ||
repo = Repo(os.getcwd()) | ||
repo.git.remote('add', 'upstream', '[email protected]:vmware/tern.git') | ||
repo.git.fetch('upstream') | ||
# Will give list of commit IDs differentiating HEAD and master | ||
commits = repo.git.rev_list('HEAD', '^upstream/master').split('\n') | ||
|
||
for commit_id in commits: | ||
lint_commit(commit_id) | ||
|
||
|