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"])
commit_msg_lines = git_output.splitlines()
commit_msg_line_numbers = {}
for i in range(len(commit_msg_lines)):
commit_msg_line_numbers[commit_msg_lines[i]] = i + 1
_PopBlankLines(commit_msg_lines, True)
_PopBlankLines(commit_msg_lines, False)
if len(commit_msg_lines) > 0:
for whitelist_string in whitelist_strings:
if commit_msg_lines[0].startswith(whitelist_string):
return []
errors = [] errors = []
if git_output.find("\t") != -1:
errors.append(output_api.PresubmitError("Tabs are not allowed in commit message.")) for k in range(len(multiple_commits)):
commit = multiple_commits[k]
# get rid of the last paragraph, which we assume to always be the tags commit_number = len(multiple_commits) - k
last_paragraph_line_count = 0 commit_tag = "Commit " + str(commit_number) + ":"
while len(commit_msg_lines) > 0 and not _IsLineBlank(commit_msg_lines[-1]): commit_msg_lines = commit.splitlines()
last_paragraph_line_count += 1 commit_msg_line_numbers = {}
commit_msg_lines.pop() for i in range(len(commit_msg_lines)):
if last_paragraph_line_count == 0: commit_msg_line_numbers[commit_msg_lines[i]] = i + 1
errors.append(
output_api.PresubmitError(
"Please ensure that there are tags (e.g., Bug:, Test:) in your description."))
if len(commit_msg_lines) > 0:
# pop the blank line between tag paragraph and description body
commit_msg_lines.pop()
if len(commit_msg_lines) > 0 and _IsLineBlank(commit_msg_lines[-1]):
errors.append(
output_api.PresubmitError('Please ensure that there exists only 1 blank line '
'between tags 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: _PopBlankLines(commit_msg_lines, False)
errors.append( whitelisted = False
output_api.PresubmitError('Please ensure that your description summary' if len(commit_msg_lines) > 0:
' and description body are not blank.')) for whitelist_string in whitelist_strings:
return errors if commit_msg_lines[0].startswith(whitelist_string):
whitelisted = True
if summary_linelength_warning_lower_limit <= len(commit_msg_lines[0]) \ break
<= summary_linelength_warning_upper_limit: if whitelisted:
errors.append( continue
output_api.PresubmitPromptWarning(
"Your description summary should be on one line of " + if commit.find("\t") != -1:
str(summary_linelength_warning_lower_limit - 1) + " or less characters.")) errors.append(
elif len(commit_msg_lines[0]) > summary_linelength_warning_upper_limit: output_api.PresubmitError(commit_tag + "Tabs are not allowed in commit message."))
errors.append(
output_api.PresubmitError( # the tags paragraph is at the end of the message
"Please ensure that your description summary is on one line of " + # the break between the tags paragraph is the first line without ":"
str(summary_linelength_warning_lower_limit - 1) + " or less characters.")) # this is sufficient because if a line is blank, it will not have ":"
commit_msg_lines.pop(0) # get rid of description summary last_paragraph_line_count = 0
if len(commit_msg_lines) == 0: while len(commit_msg_lines) > 0 and _IsTagLine(commit_msg_lines[-1]):
return errors last_paragraph_line_count += 1
if not _IsLineBlank(commit_msg_lines[0]): commit_msg_lines.pop()
errors.append( if last_paragraph_line_count == 0:
output_api.PresubmitError('Please ensure the summary is only 1 line and ' errors.append(
' there is 1 blank line between the summary ' output_api.PresubmitError(
'and description body.')) commit_tag +
else: "Please ensure that there are tags (e.g., Bug:, Test:) in your description."))
commit_msg_lines.pop(0) # pop first blank line 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
commit_msg_lines.pop()
if len(commit_msg_lines) > 0 and _IsLineBlank(commit_msg_lines[-1]):
errors.append(
output_api.PresubmitError(
commit_tag + 'Please ensure that there exists only 1 blank line '
'between tags and description body.'))
# pop all the remaining blank lines between tag and description body
_PopBlankLines(commit_msg_lines, True)
if len(commit_msg_lines) == 0: if len(commit_msg_lines) == 0:
return errors
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 +
'between description summary and description body.')) 'Please ensure that your description summary'
# pop all the remaining blank lines between description summary and description body ' and description body are not blank.'))
_PopBlankLines(commit_msg_lines)
# loop through description body
while len(commit_msg_lines) > 0:
line = commit_msg_lines.pop(0)
# lines starting with 4 spaces or lines without space(urls) are exempt from length check
if line.startswith(" ") or " " not in line:
continue continue
if len(line) > description_linelength_limit:
if summary_linelength_warning_lower_limit <= len(commit_msg_lines[0]) \
<= summary_linelength_warning_upper_limit:
errors.append(
output_api.PresubmitPromptWarning(
commit_tag + "Your description summary should be on one line of " +
str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
elif len(commit_msg_lines[0]) > summary_linelength_warning_upper_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 + "Please ensure that your description summary is on one line of " +
'"\n' + 'Please wrap it to ' + str(description_linelength_limit) + str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
' characters. ' + commit_msg_lines.pop(0) # get rid of description summary
"Lines without spaces or lines starting with 4 spaces are exempt.")) if len(commit_msg_lines) == 0:
return errors continue
if not _IsLineBlank(commit_msg_lines[0]):
errors.append(
output_api.PresubmitError(commit_tag +
'Please ensure the summary is only 1 line and '
'there is 1 blank line between the summary '
'and description body.'))
else:
commit_msg_lines.pop(0) # pop first blank line
if len(commit_msg_lines) == 0:
continue
if _IsLineBlank(commit_msg_lines[0]):
errors.append(
output_api.PresubmitError(commit_tag +
'Please ensure that there exists only 1 blank line '
'between description summary and description body.'))
# pop all the remaining blank lines between
# description summary and description body
_PopBlankLines(commit_msg_lines)
# loop through description body
while len(commit_msg_lines) > 0:
line = commit_msg_lines.pop(0)
# lines starting with 4 spaces or lines without space(urls)
# are exempt from length check
if line.startswith(" ") or " " not in line:
continue
if len(line) > description_linelength_limit:
errors.append(
output_api.PresubmitError(
commit_tag + 'Line ' + str(commit_msg_line_numbers[line]) +
' is too long.\n' + '"' + line + '"\n' + 'Please wrap it to ' +
str(description_linelength_limit) + ' characters. ' +
"Lines without spaces or lines starting with 4 spaces are exempt."))
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