From c846efd888616bf85c796b1deddeb4f4dd8623de Mon Sep 17 00:00:00 2001 From: Roberto Alanis Date: Wed, 8 Dec 2021 18:57:09 +0000 Subject: [PATCH] patch-check: commit subject length check Change-Id: I7a1c7349f4f461c2384cbd1a62d1aa2e4e3be819 Bug: b:185520507 --- PRESUBMIT.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index df4c333d..994473bc 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -33,15 +33,45 @@ See https://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for details on the presubmit API built into depot_tools. """ +import re import subprocess2 USE_PYTHON3 = True _BASH_INDENTATION = "2" +_GIT_COMMIT_SUBJECT_LENGTH = 65 _INCLUDE_BASH_FILES_ONLY = [r".*\.sh$"] _INCLUDE_MAN_FILES_ONLY = [r"man/.+\.1$"] _LIBWEBP_MAX_LINE_LENGTH = 80 +def _CheckCommitSubjectLength(input_api, output_api): + """Ensures commit's subject length is no longer than 65 chars.""" + name = "git-commit subject" + cmd = ["git", "log", "-1", "--pretty=%s"] + start = input_api.time.time() + proc = subprocess2.Popen( + cmd, + stderr=subprocess2.PIPE, + stdout=subprocess2.PIPE, + universal_newlines=True) + + stdout, _ = proc.communicate() + duration = input_api.time.time() - start + + if not re.match(r"^Revert", + stdout) and (len(stdout) - 1) > _GIT_COMMIT_SUBJECT_LENGTH: + failure_msg = ( + "The commit subject: %s is too long (%d chars)\n" + "Try to keep this to 50 or less (up to 65 is permitted for " + "non-reverts).\n" + "https://www.git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-" + "Project#_commit_guidelines") % (stdout, len(stdout) - 1) + return output_api.PresubmitError("%s\n (%4.2fs) failed\n%s" % + (name, duration, failure_msg)) + + return output_api.PresubmitResult("%s\n (%4.2fs) success" % (name, duration)) + + def _GetFilesToSkip(input_api): return list(input_api.DEFAULT_FILES_TO_SKIP) + [ r"swig/.*\.py$", @@ -123,6 +153,7 @@ def _CommonChecks(input_api, output_api): results.extend( input_api.canned_checks.CheckChangeHasNoStrayWhitespace( input_api, output_api)) + results.append(_CheckCommitSubjectLength(input_api, output_api)) source_file_filter = lambda x: input_api.FilterSourceFile( x, files_to_skip=_GetFilesToSkip(input_api))