Commit 31bbe1ba by Manh Nguyen Committed by Commit Bot

Change how commit messages are taken + multiple commits handling

Multiple commits are separated and format-checked separately. Commit message is now taken solely from input_api. Tags paragraph now covers the very last paragraph until the first line that doesn't have a ":" Bug: angleproject:4662 Change-Id: I84fe3fd1ffc30f6892a5c9dbe545acf24b0fc595 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2241617 Commit-Queue: Manh Nguyen <nguyenmh@google.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org>
parent 12ed3b6e
...@@ -41,97 +41,124 @@ def _CheckCommitMessageFormatting(input_api, output_api): ...@@ -41,97 +41,124 @@ def _CheckCommitMessageFormatting(input_api, output_api):
while len(lines) > 0 and _IsLineBlank(lines[0]): while len(lines) > 0 and _IsLineBlank(lines[0]):
lines.pop(0) lines.pop(0)
def _IsTagLine(line):
return ":" in line
whitelist_strings = ['Revert "', 'Roll ', 'Reland '] whitelist_strings = ['Revert "', 'Roll ', 'Reland ']
summary_linelength_warning_lower_limit = 65 summary_linelength_warning_lower_limit = 65
summary_linelength_warning_upper_limit = 70 summary_linelength_warning_upper_limit = 70
description_linelength_limit = 72 description_linelength_limit = 72
if input_api.change.issue: git_output = input_api.change.DescriptionText()
git_output = input_api.gerrit.GetChangeDescription(input_api.change.issue)
else: multiple_commits = re.split(r"Change-Id: [a-zA-Z0-9]*\n", git_output)
git_output = subprocess.check_output(["git", "log", "-n", "1", "--pretty=format:%B"]) errors = []
commit_msg_lines = git_output.splitlines()
for k in range(len(multiple_commits)):
commit = multiple_commits[k]
commit_number = len(multiple_commits) - k
commit_tag = "Commit " + str(commit_number) + ":"
commit_msg_lines = commit.splitlines()
commit_msg_line_numbers = {} commit_msg_line_numbers = {}
for i in range(len(commit_msg_lines)): for i in range(len(commit_msg_lines)):
commit_msg_line_numbers[commit_msg_lines[i]] = i + 1 commit_msg_line_numbers[commit_msg_lines[i]] = i + 1
_PopBlankLines(commit_msg_lines, True) _PopBlankLines(commit_msg_lines, True)
_PopBlankLines(commit_msg_lines, False) _PopBlankLines(commit_msg_lines, False)
whitelisted = False
if len(commit_msg_lines) > 0: if len(commit_msg_lines) > 0:
for whitelist_string in whitelist_strings: for whitelist_string in whitelist_strings:
if commit_msg_lines[0].startswith(whitelist_string): if commit_msg_lines[0].startswith(whitelist_string):
return [] whitelisted = True
errors = [] break
if git_output.find("\t") != -1: if whitelisted:
errors.append(output_api.PresubmitError("Tabs are not allowed in commit message.")) continue
if commit.find("\t") != -1:
errors.append(
output_api.PresubmitError(commit_tag + "Tabs are not allowed in commit message."))
# get rid of the last paragraph, which we assume to always be the tags # the tags paragraph is at the end of the message
# the break between the tags paragraph is the first line without ":"
# this is sufficient because if a line is blank, it will not have ":"
last_paragraph_line_count = 0 last_paragraph_line_count = 0
while len(commit_msg_lines) > 0 and not _IsLineBlank(commit_msg_lines[-1]): while len(commit_msg_lines) > 0 and _IsTagLine(commit_msg_lines[-1]):
last_paragraph_line_count += 1 last_paragraph_line_count += 1
commit_msg_lines.pop() commit_msg_lines.pop()
if last_paragraph_line_count == 0: if last_paragraph_line_count == 0:
errors.append( errors.append(
output_api.PresubmitError( output_api.PresubmitError(
commit_tag +
"Please ensure that there are tags (e.g., Bug:, Test:) in your description.")) "Please ensure that there are tags (e.g., Bug:, Test:) in your description."))
if len(commit_msg_lines) > 0: if len(commit_msg_lines) > 0:
if not _IsLineBlank(commit_msg_lines[-1]):
output_api.PresubmitError(commit_tag +
"Please ensure that there exists 1 blank line " +
"between tags and description body.")
else:
# pop the blank line between tag paragraph and description body # pop the blank line between tag paragraph and description body
commit_msg_lines.pop() commit_msg_lines.pop()
if len(commit_msg_lines) > 0 and _IsLineBlank(commit_msg_lines[-1]): if len(commit_msg_lines) > 0 and _IsLineBlank(commit_msg_lines[-1]):
errors.append( errors.append(
output_api.PresubmitError('Please ensure that there exists only 1 blank line ' output_api.PresubmitError(
commit_tag + 'Please ensure that there exists only 1 blank line '
'between tags and description body.')) 'between tags and description body.'))
# pop all the remaining blank lines between tag and description body # pop all the remaining blank lines between tag and description body
_PopBlankLines(commit_msg_lines, True) _PopBlankLines(commit_msg_lines, True)
if len(commit_msg_lines) == 0: if len(commit_msg_lines) == 0:
errors.append( errors.append(
output_api.PresubmitError('Please ensure that your description summary' output_api.PresubmitError(commit_tag +
'Please ensure that your description summary'
' and description body are not blank.')) ' and description body are not blank.'))
return errors continue
if summary_linelength_warning_lower_limit <= len(commit_msg_lines[0]) \ if summary_linelength_warning_lower_limit <= len(commit_msg_lines[0]) \
<= summary_linelength_warning_upper_limit: <= summary_linelength_warning_upper_limit:
errors.append( errors.append(
output_api.PresubmitPromptWarning( output_api.PresubmitPromptWarning(
"Your description summary should be on one line of " + commit_tag + "Your description summary should be on one line of " +
str(summary_linelength_warning_lower_limit - 1) + " or less characters.")) str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
elif len(commit_msg_lines[0]) > summary_linelength_warning_upper_limit: elif len(commit_msg_lines[0]) > summary_linelength_warning_upper_limit:
errors.append( errors.append(
output_api.PresubmitError( output_api.PresubmitError(
"Please ensure that your description summary is on one line of " + commit_tag + "Please ensure that your description summary is on one line of " +
str(summary_linelength_warning_lower_limit - 1) + " or less characters.")) str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
commit_msg_lines.pop(0) # get rid of description summary commit_msg_lines.pop(0) # get rid of description summary
if len(commit_msg_lines) == 0: if len(commit_msg_lines) == 0:
return errors continue
if not _IsLineBlank(commit_msg_lines[0]): if not _IsLineBlank(commit_msg_lines[0]):
errors.append( errors.append(
output_api.PresubmitError('Please ensure the summary is only 1 line and ' output_api.PresubmitError(commit_tag +
' there is 1 blank line between the summary ' 'Please ensure the summary is only 1 line and '
'there is 1 blank line between the summary '
'and description body.')) 'and description body.'))
else: else:
commit_msg_lines.pop(0) # pop first blank line commit_msg_lines.pop(0) # pop first blank line
if len(commit_msg_lines) == 0: if len(commit_msg_lines) == 0:
return errors continue
if _IsLineBlank(commit_msg_lines[0]): if _IsLineBlank(commit_msg_lines[0]):
errors.append( errors.append(
output_api.PresubmitError('Please ensure that there exists only 1 blank line ' output_api.PresubmitError(commit_tag +
'Please ensure that there exists only 1 blank line '
'between description summary and description body.')) 'between description summary and description body.'))
# pop all the remaining blank lines between description summary and description body # pop all the remaining blank lines between
# description summary and description body
_PopBlankLines(commit_msg_lines) _PopBlankLines(commit_msg_lines)
# loop through description body # loop through description body
while len(commit_msg_lines) > 0: while len(commit_msg_lines) > 0:
line = commit_msg_lines.pop(0) line = commit_msg_lines.pop(0)
# lines starting with 4 spaces or lines without space(urls) are exempt from length check # lines starting with 4 spaces or lines without space(urls)
# are exempt from length check
if line.startswith(" ") or " " not in line: if line.startswith(" ") or " " not in line:
continue continue
if len(line) > description_linelength_limit: if len(line) > description_linelength_limit:
errors.append( errors.append(
output_api.PresubmitError( output_api.PresubmitError(
'Line ' + str(commit_msg_line_numbers[line]) + ' is too long.\n' + '"' + line + commit_tag + 'Line ' + str(commit_msg_line_numbers[line]) +
'"\n' + 'Please wrap it to ' + str(description_linelength_limit) + ' is too long.\n' + '"' + line + '"\n' + 'Please wrap it to ' +
' characters. ' + str(description_linelength_limit) + ' characters. ' +
"Lines without spaces or lines starting with 4 spaces are exempt.")) "Lines without spaces or lines starting with 4 spaces are exempt."))
return errors break
return errors return errors
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment