]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/PatchCheck.py
173d4517e0a80d5e62b12c1c9b9b798e567f7d58
2 # Check a patch for various format issues
4 # Copyright (c) 2015 - 2020, Intel Corporation. All rights reserved.<BR>
5 # Copyright (C) 2020, Red Hat, Inc.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 from __future__
import print_function
13 __copyright__
= "Copyright (c) 2015 - 2016, Intel Corporation All rights reserved."
23 SILENT
, ONELINE
, NORMAL
= range(3)
26 class EmailAddressCheck
:
27 """Checks an email address."""
29 def __init__(self
, email
, description
):
33 self
.error('Email address is missing!')
35 if description
is None:
36 self
.error('Email description is missing!')
39 self
.description
= "'" + description
+ "'"
40 self
.check_email_address(email
)
42 def error(self
, *err
):
43 if self
.ok
and Verbose
.level
> Verbose
.ONELINE
:
44 print('The ' + self
.description
+ ' email address is not valid:')
46 if Verbose
.level
< Verbose
.NORMAL
:
50 prefix
= (' *', ' ')[count
> 0]
54 email_re1
= re
.compile(r
'(?:\s*)(.*?)(\s*)<(.+)>\s*$',
55 re
.MULTILINE|re
.IGNORECASE
)
57 def check_email_address(self
, email
):
59 mo
= self
.email_re1
.match(email
)
61 self
.error("Email format is invalid: " + email
.strip())
64 name
= mo
.group(1).strip()
66 self
.error("Name is not provided with email address: " +
69 quoted
= len(name
) > 2 and name
[0] == '"' and name
[-1] == '"'
70 if name
.find(',') >= 0 and not quoted
:
71 self
.error('Add quotes (") around name with a comma: ' +
75 self
.error("There should be a space between the name and " +
76 "email address: " + email
)
78 if mo
.group(3).find(' ') >= 0:
79 self
.error("The email address cannot contain a space: " +
82 class CommitMessageCheck
:
83 """Checks the contents of a git commit message."""
85 def __init__(self
, subject
, message
):
88 if subject
is None and message
is None:
89 self
.error('Commit message is missing!')
92 self
.subject
= subject
97 self
.check_contributed_under()
98 self
.check_signed_off_by()
99 self
.check_misc_signatures()
100 self
.check_overall_format()
101 self
.report_message_result()
103 url
= 'https://github.com/tianocore/tianocore.github.io/wiki/Commit-Message-Format'
105 def report_message_result(self
):
106 if Verbose
.level
< Verbose
.NORMAL
:
111 print('The commit message format passed all checks.')
117 def error(self
, *err
):
118 if self
.ok
and Verbose
.level
> Verbose
.ONELINE
:
119 print('The commit message format is not valid:')
121 if Verbose
.level
< Verbose
.NORMAL
:
125 prefix
= (' *', ' ')[count
> 0]
129 # Find 'contributed-under:' at the start of a line ignoring case and
130 # requires ':' to be present. Matches if there is white space before
131 # the tag or between the tag and the ':'.
132 contributed_under_re
= \
133 re
.compile(r
'^\s*contributed-under\s*:', re
.MULTILINE|re
.IGNORECASE
)
135 def check_contributed_under(self
):
136 match
= self
.contributed_under_re
.search(self
.msg
)
137 if match
is not None:
138 self
.error('Contributed-under! (Note: this must be ' +
139 'removed by the code contributor!)')
142 def make_signature_re(sig
, re_input
=False):
146 sub_re
= sig
.replace('-', r
'[-\s]+')
147 re_str
= (r
'^(?P<tag>' + sub_re
+
148 r
')(\s*):(\s*)(?P<value>\S.*?)(?:\s*)$')
150 return re
.compile(re_str
, re
.MULTILINE|re
.IGNORECASE
)
152 print("Tried to compile re:", re_str
)
157 (?: (?P<tag>[^:]+) \s* : \s*
160 (?: \[ (?P<updater>[^:]+) \s* : \s*
161 (?P<note>.+?) \s* \] )
163 re
.VERBOSE | re
.MULTILINE
)
165 def find_signatures(self
, sig
):
166 if not sig
.endswith('-by') and sig
!= 'Cc':
168 regex
= self
.make_signature_re(sig
)
170 sigs
= regex
.findall(self
.msg
)
172 bad_case_sigs
= filter(lambda m
: m
[0] != sig
, sigs
)
173 for s
in bad_case_sigs
:
174 self
.error("'" +s
[0] + "' should be '" + sig
+ "'")
178 self
.error('There should be no spaces between ' + sig
+
181 self
.error("There should be a space after '" + sig
+ ":'")
183 EmailAddressCheck(s
[3], sig
)
187 def check_signed_off_by(self
):
189 if self
.msg
.find(sob
) < 0:
190 self
.error('Missing Signed-off-by! (Note: this must be ' +
191 'added by the code contributor!)')
194 sobs
= self
.find_signatures('Signed-off')
197 self
.error('Invalid Signed-off-by format!')
209 def check_misc_signatures(self
):
210 for sig
in self
.sig_types
:
211 self
.find_signatures(sig
)
213 cve_re
= re
.compile('CVE-[0-9]{4}-[0-9]{5}[^0-9]')
215 def check_overall_format(self
):
216 lines
= self
.msg
.splitlines()
218 if len(lines
) >= 1 and lines
[0].endswith('\r\n'):
223 lines
.insert(0, empty_line
)
224 lines
.insert(0, self
.subject
+ empty_line
)
229 self
.error('Empty commit message!')
232 if count
>= 1 and re
.search(self
.cve_re
, lines
[0]):
234 # If CVE-xxxx-xxxxx is present in subject line, then limit length of
235 # subject line to 92 characters
237 if len(lines
[0].rstrip()) >= 93:
239 'First line of commit message (subject line) is too long (%d >= 93).' %
240 (len(lines
[0].rstrip()))
244 # If CVE-xxxx-xxxxx is not present in subject line, then limit
245 # length of subject line to 75 characters
247 if len(lines
[0].rstrip()) >= 76:
249 'First line of commit message (subject line) is too long (%d >= 76).' %
250 (len(lines
[0].rstrip()))
253 if count
>= 1 and len(lines
[0].strip()) == 0:
254 self
.error('First line of commit message (subject line) ' +
257 if count
>= 2 and lines
[1].strip() != '':
258 self
.error('Second line of commit message should be ' +
261 for i
in range(2, count
):
262 if (len(lines
[i
]) >= 76 and
263 len(lines
[i
].split()) > 1 and
264 not lines
[i
].startswith('git-svn-id:')):
266 # Print a warning if body line is longer than 75 characters
269 'WARNING - Line %d of commit message is too long (%d >= 76).' %
270 (i
+ 1, len(lines
[i
]))
275 for i
in range(count
- 1, 0, -1):
277 mo
= self
.sig_block_re
.match(line
)
279 if line
.strip() == '':
281 elif last_sig_line
is not None:
282 err2
= 'Add empty line before "%s"?' % last_sig_line
283 self
.error('The line before the signature block ' +
284 'should be empty', err2
)
286 self
.error('The signature block was not found')
288 last_sig_line
= line
.strip()
290 (START
, PRE_PATCH
, PATCH
) = range(3)
293 """Checks the contents of a git diff."""
295 def __init__(self
, diff
):
297 self
.format_ok
= True
298 self
.lines
= diff
.splitlines(True)
299 self
.count
= len(self
.lines
)
303 while self
.line_num
< self
.count
and self
.format_ok
:
304 line_num
= self
.line_num
306 assert(self
.line_num
> line_num
)
307 self
.report_message_result()
309 def report_message_result(self
):
310 if Verbose
.level
< Verbose
.NORMAL
:
313 print('The code passed all checks.')
315 print('\nWARNING - The following binary files will be added ' +
316 'into the repository:')
317 for binary
in self
.new_bin
:
321 line
= self
.lines
[self
.line_num
]
323 if self
.state
in (PRE_PATCH
, PATCH
):
324 if line
.startswith('diff --git'):
326 if self
.state
== PATCH
:
327 if line
.startswith('@@ '):
328 self
.state
= PRE_PATCH
329 elif len(line
) >= 1 and line
[0] not in ' -+' and \
330 not line
.startswith('\r\n') and \
331 not line
.startswith(r
'\ No newline ') and not self
.binary
:
332 for line
in self
.lines
[self
.line_num
+ 1:]:
333 if line
.startswith('diff --git'):
334 self
.format_error('diff found after end of patch')
336 self
.line_num
= self
.count
339 if self
.state
== START
:
340 if line
.startswith('diff --git'):
341 self
.state
= PRE_PATCH
342 self
.filename
= line
[13:].split(' ', 1)[0]
343 self
.is_newfile
= False
344 self
.force_crlf
= not self
.filename
.endswith('.sh')
345 elif len(line
.rstrip()) != 0:
346 self
.format_error("didn't find diff command")
348 elif self
.state
== PRE_PATCH
:
349 if line
.startswith('@@ '):
352 elif line
.startswith('GIT binary patch') or \
353 line
.startswith('Binary files'):
357 self
.new_bin
.append(self
.filename
)
360 self
.is_newfile
= self
.newfile_prefix_re
.match(line
)
361 for pfx
in self
.pre_patch_prefixes
:
362 if line
.startswith(pfx
):
365 self
.format_error("didn't find diff hunk marker (@@)")
367 elif self
.state
== PATCH
:
370 elif line
.startswith('-'):
372 elif line
.startswith('+'):
373 self
.check_added_line(line
[1:])
374 elif line
.startswith('\r\n'):
376 elif line
.startswith(r
'\ No newline '):
378 elif not line
.startswith(' '):
379 self
.format_error("unexpected patch line")
382 pre_patch_prefixes
= (
396 line_endings
= ('\r\n', '\n\r', '\n', '\r')
398 newfile_prefix_re
= \
404 def added_line_error(self
, msg
, line
):
406 if self
.filename
is not None:
407 lines
.append('File: ' + self
.filename
)
408 lines
.append('Line: ' + line
)
414 DEBUG \s* \( \s* \( \s*
415 (?: DEBUG_[A-Z_]+ \s* \| \s*)*
420 def check_added_line(self
, line
):
422 for an_eol
in self
.line_endings
:
423 if line
.endswith(an_eol
):
425 line
= line
[:-len(eol
)]
427 stripped
= line
.rstrip()
429 if self
.force_crlf
and eol
!= '\r\n':
430 self
.added_line_error('Line ending (%s) is not CRLF' % repr(eol
),
433 self
.added_line_error('Tab character used', line
)
434 if len(stripped
) < len(line
):
435 self
.added_line_error('Trailing whitespace found', line
)
437 mo
= self
.old_debug_re
.search(line
)
439 self
.added_line_error('EFI_D_' + mo
.group(1) + ' was used, '
440 'but DEBUG_' + mo
.group(1) +
441 ' is now recommended', line
)
443 split_diff_re
= re
.compile(r
'''
445 ^ diff \s+ --git \s+ a/.+ \s+ b/.+ $
451 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
453 def format_error(self
, err
):
454 self
.format_ok
= False
455 err
= 'Patch format error: ' + err
456 err2
= 'Line: ' + self
.lines
[self
.line_num
].rstrip()
457 self
.error(err
, err2
)
459 def error(self
, *err
):
460 if self
.ok
and Verbose
.level
> Verbose
.ONELINE
:
461 print('Code format is not valid:')
463 if Verbose
.level
< Verbose
.NORMAL
:
467 prefix
= (' *', ' ')[count
> 0]
472 """Checks the contents of a git email formatted patch.
474 Various checks are performed on both the commit message and the
478 def __init__(self
, name
, patch
):
480 self
.find_patch_pieces()
482 email_check
= EmailAddressCheck(self
.author_email
, 'Author')
483 email_ok
= email_check
.ok
485 msg_check
= CommitMessageCheck(self
.commit_subject
, self
.commit_msg
)
486 msg_ok
= msg_check
.ok
489 if self
.diff
is not None:
490 diff_check
= GitDiffCheck(self
.diff
)
491 diff_ok
= diff_check
.ok
493 self
.ok
= email_ok
and msg_ok
and diff_ok
495 if Verbose
.level
== Verbose
.ONELINE
:
501 result
.append('commit message')
503 result
.append('diff content')
504 result
= 'bad ' + ' and '.join(result
)
508 git_diff_re
= re
.compile(r
'''
509 ^ diff \s+ --git \s+ a/.+ \s+ b/.+ $
511 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
515 (?P<commit_message> [\s\S\r\n]* )
518 (?: ^ \s+ .+ \s+ \| \s+ \d+ \s+ \+* \-*
523 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
525 subject_prefix_re
= \
528 [^\[\]]* # Allow all non-brackets
533 def find_patch_pieces(self
):
534 if sys
.version_info
< (3, 0):
535 patch
= self
.patch
.encode('ascii', 'ignore')
539 self
.commit_msg
= None
541 self
.commit_subject
= None
542 self
.commit_prefix
= None
545 if patch
.startswith('diff --git'):
549 pmail
= email
.message_from_string(patch
)
550 parts
= list(pmail
.walk())
551 assert(len(parts
) == 1)
552 assert(parts
[0].get_content_type() == 'text/plain')
553 content
= parts
[0].get_payload(decode
=True).decode('utf-8', 'ignore')
555 mo
= self
.git_diff_re
.search(content
)
557 self
.diff
= content
[mo
.start():]
558 content
= content
[:mo
.start()]
560 mo
= self
.stat_re
.search(content
)
562 self
.commit_msg
= content
564 self
.stat
= mo
.group('stat')
565 self
.commit_msg
= mo
.group('commit_message')
567 # Parse subject line from email header. The subject line may be
568 # composed of multiple parts with different encodings. Decode and
569 # combine all the parts to produce a single string with the contents of
570 # the decoded subject line.
572 parts
= email
.header
.decode_header(pmail
.get('subject'))
574 for (part
, encoding
) in parts
:
576 part
= part
.decode(encoding
)
582 subject
= subject
+ part
584 self
.commit_subject
= subject
.replace('\r\n', '')
585 self
.commit_subject
= self
.commit_subject
.replace('\n', '')
586 self
.commit_subject
= self
.subject_prefix_re
.sub('', self
.commit_subject
, 1)
588 self
.author_email
= pmail
['from']
590 class CheckGitCommits
:
591 """Reads patches from git based on the specified git revision range.
593 The patches are read from git, and then checked.
596 def __init__(self
, rev_spec
, max_count
):
597 commits
= self
.read_commit_list_from_git(rev_spec
, max_count
)
598 if len(commits
) == 1 and Verbose
.level
> Verbose
.ONELINE
:
599 commits
= [ rev_spec
]
602 for commit
in commits
:
603 if Verbose
.level
> Verbose
.ONELINE
:
608 print('Checking git commit:', commit
)
609 email
= self
.read_committer_email_address_from_git(commit
)
610 self
.ok
&= EmailAddressCheck(email
, 'Committer').ok
611 patch
= self
.read_patch_from_git(commit
)
612 self
.ok
&= CheckOnePatch(commit
, patch
).ok
614 print("Couldn't find commit matching: '{}'".format(rev_spec
))
616 def read_commit_list_from_git(self
, rev_spec
, max_count
):
617 # Run git to get the commit patch
618 cmd
= [ 'rev-list', '--abbrev-commit', '--no-walk' ]
619 if max_count
is not None:
620 cmd
.append('--max-count=' + str(max_count
))
622 out
= self
.run_git(*cmd
)
623 return out
.split() if out
else []
625 def read_patch_from_git(self
, commit
):
626 # Run git to get the commit patch
627 return self
.run_git('show', '--pretty=email', '--no-textconv', commit
)
629 def read_committer_email_address_from_git(self
, commit
):
630 # Run git to get the committer email
631 return self
.run_git('show', '--pretty=%cn <%ce>', '--no-patch', commit
)
633 def run_git(self
, *args
):
636 p
= subprocess
.Popen(cmd
,
637 stdout
=subprocess
.PIPE
,
638 stderr
=subprocess
.STDOUT
)
639 Result
= p
.communicate()
640 return Result
[0].decode('utf-8', 'ignore') if Result
[0] and Result
[0].find(b
"fatal")!=0 else None
642 class CheckOnePatchFile
:
643 """Performs a patch check for a single file.
645 stdin is used when the filename is '-'.
648 def __init__(self
, patch_filename
):
649 if patch_filename
== '-':
650 patch
= sys
.stdin
.read()
651 patch_filename
= 'stdin'
653 f
= open(patch_filename
, 'rb')
654 patch
= f
.read().decode('utf-8', 'ignore')
656 if Verbose
.level
> Verbose
.ONELINE
:
657 print('Checking patch file:', patch_filename
)
658 self
.ok
= CheckOnePatch(patch_filename
, patch
).ok
661 """Performs a patch check for a single command line argument.
663 The argument will be handed off to a file or git-commit based
667 def __init__(self
, param
, max_count
=None):
669 if param
== '-' or os
.path
.exists(param
):
670 checker
= CheckOnePatchFile(param
)
672 checker
= CheckGitCommits(param
, max_count
)
676 """Checks patches based on the command line arguments."""
680 patches
= self
.args
.patches
682 if len(patches
) == 0:
687 for patch
in patches
:
688 self
.process_one_arg(patch
)
690 if self
.count
is not None:
691 self
.process_one_arg('HEAD')
698 def process_one_arg(self
, arg
):
699 if len(arg
) >= 2 and arg
[0] == '-':
701 self
.count
= int(arg
[1:])
705 self
.ok
&= CheckOneArg(arg
, self
.count
).ok
708 def parse_options(self
):
709 parser
= argparse
.ArgumentParser(description
=__copyright__
)
710 parser
.add_argument('--version', action
='version',
711 version
='%(prog)s ' + VersionNumber
)
712 parser
.add_argument('patches', nargs
='*',
713 help='[patch file | git rev list]')
714 group
= parser
.add_mutually_exclusive_group()
715 group
.add_argument("--oneline",
717 help="Print one result per line")
718 group
.add_argument("--silent",
720 help="Print nothing")
721 self
.args
= parser
.parse_args()
722 if self
.args
.oneline
:
723 Verbose
.level
= Verbose
.ONELINE
725 Verbose
.level
= Verbose
.SILENT
727 if __name__
== "__main__":
728 sys
.exit(PatchCheckApp().retval
)