]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/PatchCheck.py
2 # Check a patch for various format issues
4 # Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made
7 # available under the terms and conditions of the BSD License which
8 # accompanies this distribution. The full text of the license may be
9 # found at http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
12 # BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
16 from __future__
import print_function
19 __copyright__
= "Copyright (c) 2015 - 2016, Intel Corporation All rights reserved."
29 SILENT
, ONELINE
, NORMAL
= range(3)
32 class CommitMessageCheck
:
33 """Checks the contents of a git commit message."""
35 def __init__(self
, subject
, message
):
38 if subject
is None and message
is None:
39 self
.error('Commit message is missing!')
42 self
.subject
= subject
45 self
.check_contributed_under()
46 self
.check_signed_off_by()
47 self
.check_misc_signatures()
48 self
.check_overall_format()
49 self
.report_message_result()
51 url
= 'https://github.com/tianocore/tianocore.github.io/wiki/Commit-Message-Format'
53 def report_message_result(self
):
54 if Verbose
.level
< Verbose
.NORMAL
:
59 print('The commit message format passed all checks.')
65 def error(self
, *err
):
66 if self
.ok
and Verbose
.level
> Verbose
.ONELINE
:
67 print('The commit message format is not valid:')
69 if Verbose
.level
< Verbose
.NORMAL
:
73 prefix
= (' *', ' ')[count
> 0]
77 def check_contributed_under(self
):
78 cu_msg
='Contributed-under: TianoCore Contribution Agreement 1.0'
79 if self
.msg
.find(cu_msg
) < 0:
80 self
.error('Missing Contributed-under! (Note: this must be ' +
81 'added by the code contributor!)')
84 def make_signature_re(sig
, re_input
=False):
88 sub_re
= sig
.replace('-', r
'[-\s]+')
89 re_str
= (r
'^(?P<tag>' + sub_re
+
90 r
')(\s*):(\s*)(?P<value>\S.*?)(?:\s*)$')
92 return re
.compile(re_str
, re
.MULTILINE|re
.IGNORECASE
)
94 print("Tried to compile re:", re_str
)
99 (?: (?P<tag>[^:]+) \s* : \s*
102 (?: \[ (?P<updater>[^:]+) \s* : \s*
103 (?P<note>.+?) \s* \] )
105 re
.VERBOSE | re
.MULTILINE
)
107 def find_signatures(self
, sig
):
108 if not sig
.endswith('-by') and sig
!= 'Cc':
110 regex
= self
.make_signature_re(sig
)
112 sigs
= regex
.findall(self
.msg
)
114 bad_case_sigs
= filter(lambda m
: m
[0] != sig
, sigs
)
115 for s
in bad_case_sigs
:
116 self
.error("'" +s
[0] + "' should be '" + sig
+ "'")
120 self
.error('There should be no spaces between ' + sig
+
123 self
.error("There should be a space after '" + sig
+ ":'")
125 self
.check_email_address(s
[3])
129 email_re1
= re
.compile(r
'(?:\s*)(.*?)(\s*)<(.+)>\s*$',
130 re
.MULTILINE|re
.IGNORECASE
)
132 def check_email_address(self
, email
):
133 email
= email
.strip()
134 mo
= self
.email_re1
.match(email
)
136 self
.error("Email format is invalid: " + email
.strip())
139 name
= mo
.group(1).strip()
141 self
.error("Name is not provided with email address: " +
144 quoted
= len(name
) > 2 and name
[0] == '"' and name
[-1] == '"'
145 if name
.find(',') >= 0 and not quoted
:
146 self
.error('Add quotes (") around name with a comma: ' +
149 if mo
.group(2) == '':
150 self
.error("There should be a space between the name and " +
151 "email address: " + email
)
153 if mo
.group(3).find(' ') >= 0:
154 self
.error("The email address cannot contain a space: " +
157 def check_signed_off_by(self
):
159 if self
.msg
.find(sob
) < 0:
160 self
.error('Missing Signed-off-by! (Note: this must be ' +
161 'added by the code contributor!)')
164 sobs
= self
.find_signatures('Signed-off')
167 self
.error('Invalid Signed-off-by format!')
179 def check_misc_signatures(self
):
180 for sig
in self
.sig_types
:
181 self
.find_signatures(sig
)
183 def check_overall_format(self
):
184 lines
= self
.msg
.splitlines()
186 if len(lines
) >= 1 and lines
[0].endswith('\r\n'):
191 lines
.insert(0, empty_line
)
192 lines
.insert(0, self
.subject
+ empty_line
)
197 self
.error('Empty commit message!')
200 if count
>= 1 and len(lines
[0]) >= 72:
201 self
.error('First line of commit message (subject line) ' +
204 if count
>= 1 and len(lines
[0].strip()) == 0:
205 self
.error('First line of commit message (subject line) ' +
208 if count
>= 2 and lines
[1].strip() != '':
209 self
.error('Second line of commit message should be ' +
212 for i
in range(2, count
):
213 if (len(lines
[i
]) >= 76 and
214 len(lines
[i
].split()) > 1 and
215 not lines
[i
].startswith('git-svn-id:')):
216 self
.error('Line %d of commit message is too long.' % (i
+ 1))
219 for i
in range(count
- 1, 0, -1):
221 mo
= self
.sig_block_re
.match(line
)
223 if line
.strip() == '':
225 elif last_sig_line
is not None:
226 err2
= 'Add empty line before "%s"?' % last_sig_line
227 self
.error('The line before the signature block ' +
228 'should be empty', err2
)
230 self
.error('The signature block was not found')
232 last_sig_line
= line
.strip()
234 (START
, PRE_PATCH
, PATCH
) = range(3)
237 """Checks the contents of a git diff."""
239 def __init__(self
, diff
):
241 self
.format_ok
= True
242 self
.lines
= diff
.splitlines(True)
243 self
.count
= len(self
.lines
)
247 while self
.line_num
< self
.count
and self
.format_ok
:
248 line_num
= self
.line_num
250 assert(self
.line_num
> line_num
)
251 self
.report_message_result()
253 def report_message_result(self
):
254 if Verbose
.level
< Verbose
.NORMAL
:
257 print('The code passed all checks.')
259 print('\nWARNING - The following binary files will be added ' +
260 'into the repository:')
261 for binary
in self
.new_bin
:
265 line
= self
.lines
[self
.line_num
]
267 if self
.state
in (PRE_PATCH
, PATCH
):
268 if line
.startswith('diff --git'):
270 if self
.state
== PATCH
:
271 if line
.startswith('@@ '):
272 self
.state
= PRE_PATCH
273 elif len(line
) >= 1 and line
[0] not in ' -+' and \
274 not line
.startswith(r
'\ No newline ') and not self
.binary
:
275 for line
in self
.lines
[self
.line_num
+ 1:]:
276 if line
.startswith('diff --git'):
277 self
.format_error('diff found after end of patch')
279 self
.line_num
= self
.count
282 if self
.state
== START
:
283 if line
.startswith('diff --git'):
284 self
.state
= PRE_PATCH
285 self
.filename
= line
[13:].split(' ',1)[0]
286 self
.is_newfile
= False
287 self
.force_crlf
= not self
.filename
.endswith('.sh')
288 elif len(line
.rstrip()) != 0:
289 self
.format_error("didn't find diff command")
291 elif self
.state
== PRE_PATCH
:
292 if line
.startswith('@@ '):
295 elif line
.startswith('GIT binary patch') or \
296 line
.startswith('Binary files'):
300 self
.new_bin
.append(self
.filename
)
303 self
.is_newfile
= self
.newfile_prefix_re
.match(line
)
304 for pfx
in self
.pre_patch_prefixes
:
305 if line
.startswith(pfx
):
308 self
.format_error("didn't find diff hunk marker (@@)")
310 elif self
.state
== PATCH
:
313 elif line
.startswith('-'):
315 elif line
.startswith('+'):
316 self
.check_added_line(line
[1:])
317 elif line
.startswith(r
'\ No newline '):
319 elif not line
.startswith(' '):
320 self
.format_error("unexpected patch line")
323 pre_patch_prefixes
= (
335 line_endings
= ('\r\n', '\n\r', '\n', '\r')
337 newfile_prefix_re
= \
343 def added_line_error(self
, msg
, line
):
345 if self
.filename
is not None:
346 lines
.append('File: ' + self
.filename
)
347 lines
.append('Line: ' + line
)
353 DEBUG \s* \( \s* \( \s*
354 (?: DEBUG_[A-Z_]+ \s* \| \s*)*
359 def check_added_line(self
, line
):
361 for an_eol
in self
.line_endings
:
362 if line
.endswith(an_eol
):
364 line
= line
[:-len(eol
)]
366 stripped
= line
.rstrip()
368 if self
.force_crlf
and eol
!= '\r\n':
369 self
.added_line_error('Line ending (%s) is not CRLF' % repr(eol
),
372 self
.added_line_error('Tab character used', line
)
373 if len(stripped
) < len(line
):
374 self
.added_line_error('Trailing whitespace found', line
)
376 mo
= self
.old_debug_re
.search(line
)
378 self
.added_line_error('EFI_D_' + mo
.group(1) + ' was used, '
379 'but DEBUG_' + mo
.group(1) +
380 ' is now recommended', line
)
382 split_diff_re
= re
.compile(r
'''
384 ^ diff \s+ --git \s+ a/.+ \s+ b/.+ $
390 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
392 def format_error(self
, err
):
393 self
.format_ok
= False
394 err
= 'Patch format error: ' + err
395 err2
= 'Line: ' + self
.lines
[self
.line_num
].rstrip()
396 self
.error(err
, err2
)
398 def error(self
, *err
):
399 if self
.ok
and Verbose
.level
> Verbose
.ONELINE
:
400 print('Code format is not valid:')
402 if Verbose
.level
< Verbose
.NORMAL
:
406 prefix
= (' *', ' ')[count
> 0]
411 """Checks the contents of a git email formatted patch.
413 Various checks are performed on both the commit message and the
417 def __init__(self
, name
, patch
):
419 self
.find_patch_pieces()
421 msg_check
= CommitMessageCheck(self
.commit_subject
, self
.commit_msg
)
422 msg_ok
= msg_check
.ok
425 if self
.diff
is not None:
426 diff_check
= GitDiffCheck(self
.diff
)
427 diff_ok
= diff_check
.ok
429 self
.ok
= msg_ok
and diff_ok
431 if Verbose
.level
== Verbose
.ONELINE
:
437 result
.append('commit message')
439 result
.append('diff content')
440 result
= 'bad ' + ' and '.join(result
)
444 git_diff_re
= re
.compile(r
'''
445 ^ diff \s+ --git \s+ a/.+ \s+ b/.+ $
447 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
451 (?P<commit_message> [\s\S\r\n]* )
454 (?: ^ \s+ .+ \s+ \| \s+ \d+ \s+ \+* \-*
459 re
.IGNORECASE | re
.VERBOSE | re
.MULTILINE
)
461 subject_prefix_re
= \
464 [^\[\]]* # Allow all non-brackets
469 def find_patch_pieces(self
):
470 if sys
.version_info
< (3, 0):
471 patch
= self
.patch
.encode('ascii', 'ignore')
475 self
.commit_msg
= None
477 self
.commit_subject
= None
478 self
.commit_prefix
= None
481 if patch
.startswith('diff --git'):
485 pmail
= email
.message_from_string(patch
)
486 parts
= list(pmail
.walk())
487 assert(len(parts
) == 1)
488 assert(parts
[0].get_content_type() == 'text/plain')
489 content
= parts
[0].get_payload(decode
=True).decode('utf-8', 'ignore')
491 mo
= self
.git_diff_re
.search(content
)
493 self
.diff
= content
[mo
.start():]
494 content
= content
[:mo
.start()]
496 mo
= self
.stat_re
.search(content
)
498 self
.commit_msg
= content
500 self
.stat
= mo
.group('stat')
501 self
.commit_msg
= mo
.group('commit_message')
503 self
.commit_subject
= pmail
['subject'].replace('\r\n', '')
504 self
.commit_subject
= self
.commit_subject
.replace('\n', '')
505 self
.commit_subject
= self
.subject_prefix_re
.sub('', self
.commit_subject
, 1)
507 class CheckGitCommits
:
508 """Reads patches from git based on the specified git revision range.
510 The patches are read from git, and then checked.
513 def __init__(self
, rev_spec
, max_count
):
514 commits
= self
.read_commit_list_from_git(rev_spec
, max_count
)
515 if len(commits
) == 1 and Verbose
.level
> Verbose
.ONELINE
:
516 commits
= [ rev_spec
]
519 for commit
in commits
:
520 if Verbose
.level
> Verbose
.ONELINE
:
525 print('Checking git commit:', commit
)
526 patch
= self
.read_patch_from_git(commit
)
527 self
.ok
&= CheckOnePatch(commit
, patch
).ok
529 def read_commit_list_from_git(self
, rev_spec
, max_count
):
530 # Run git to get the commit patch
531 cmd
= [ 'rev-list', '--abbrev-commit', '--no-walk' ]
532 if max_count
is not None:
533 cmd
.append('--max-count=' + str(max_count
))
535 out
= self
.run_git(*cmd
)
538 def read_patch_from_git(self
, commit
):
539 # Run git to get the commit patch
540 return self
.run_git('show', '--pretty=email', commit
)
542 def run_git(self
, *args
):
545 p
= subprocess
.Popen(cmd
,
546 stdout
=subprocess
.PIPE
,
547 stderr
=subprocess
.STDOUT
)
548 return p
.communicate()[0].decode('utf-8', 'ignore')
550 class CheckOnePatchFile
:
551 """Performs a patch check for a single file.
553 stdin is used when the filename is '-'.
556 def __init__(self
, patch_filename
):
557 if patch_filename
== '-':
558 patch
= sys
.stdin
.read()
559 patch_filename
= 'stdin'
561 f
= open(patch_filename
, 'rb')
562 patch
= f
.read().decode('utf-8', 'ignore')
564 if Verbose
.level
> Verbose
.ONELINE
:
565 print('Checking patch file:', patch_filename
)
566 self
.ok
= CheckOnePatch(patch_filename
, patch
).ok
569 """Performs a patch check for a single command line argument.
571 The argument will be handed off to a file or git-commit based
575 def __init__(self
, param
, max_count
=None):
577 if param
== '-' or os
.path
.exists(param
):
578 checker
= CheckOnePatchFile(param
)
580 checker
= CheckGitCommits(param
, max_count
)
584 """Checks patches based on the command line arguments."""
588 patches
= self
.args
.patches
590 if len(patches
) == 0:
595 for patch
in patches
:
596 self
.process_one_arg(patch
)
598 if self
.count
is not None:
599 self
.process_one_arg('HEAD')
606 def process_one_arg(self
, arg
):
607 if len(arg
) >= 2 and arg
[0] == '-':
609 self
.count
= int(arg
[1:])
613 self
.ok
&= CheckOneArg(arg
, self
.count
).ok
616 def parse_options(self
):
617 parser
= argparse
.ArgumentParser(description
=__copyright__
)
618 parser
.add_argument('--version', action
='version',
619 version
='%(prog)s ' + VersionNumber
)
620 parser
.add_argument('patches', nargs
='*',
621 help='[patch file | git rev list]')
622 group
= parser
.add_mutually_exclusive_group()
623 group
.add_argument("--oneline",
625 help="Print one result per line")
626 group
.add_argument("--silent",
628 help="Print nothing")
629 self
.args
= parser
.parse_args()
630 if self
.args
.oneline
:
631 Verbose
.level
= Verbose
.ONELINE
633 Verbose
.level
= Verbose
.SILENT
635 if __name__
== "__main__":
636 sys
.exit(PatchCheckApp().retval
)