BaseTools/PatchCheck.py: Add warning info for new binary files

The commit adds the detection of adding new binary files in a patch file
or in a commit.

The following warning messages will be appended at the end of the script
output:

WARNING - The following binary files will be added into the repository:
  <BinaryFile1>
  <BinaryFile2>
  ...

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Hao Wu 2017-06-08 14:57:48 +08:00
parent aab57eff5a
commit 6a69dd4937
1 changed files with 21 additions and 13 deletions

View File

@ -243,6 +243,7 @@ class GitDiffCheck:
self.count = len(self.lines) self.count = len(self.lines)
self.line_num = 0 self.line_num = 0
self.state = START self.state = START
self.new_bin = []
while self.line_num < self.count and self.format_ok: while self.line_num < self.count and self.format_ok:
line_num = self.line_num line_num = self.line_num
self.run() self.run()
@ -254,6 +255,11 @@ class GitDiffCheck:
return return
if self.ok: if self.ok:
print('The code passed all checks.') print('The code passed all checks.')
if self.new_bin:
print('\nWARNING - The following binary files will be added ' +
'into the repository:')
for binary in self.new_bin:
print(' ' + binary)
def run(self): def run(self):
line = self.lines[self.line_num] line = self.lines[self.line_num]
@ -276,21 +282,25 @@ class GitDiffCheck:
if self.state == START: if self.state == START:
if line.startswith('diff --git'): if line.startswith('diff --git'):
self.state = PRE_PATCH self.state = PRE_PATCH
self.set_filename(None) self.filename = line[13:].split(' ',1)[0]
self.is_newfile = False
self.force_crlf = not self.filename.endswith('.sh')
elif len(line.rstrip()) != 0: elif len(line.rstrip()) != 0:
self.format_error("didn't find diff command") self.format_error("didn't find diff command")
self.line_num += 1 self.line_num += 1
elif self.state == PRE_PATCH: elif self.state == PRE_PATCH:
if line.startswith('+++ b/'):
self.set_filename(line[6:].rstrip())
if line.startswith('@@ '): if line.startswith('@@ '):
self.state = PATCH self.state = PATCH
self.binary = False self.binary = False
elif line.startswith('GIT binary patch'): elif line.startswith('GIT binary patch') or \
line.startswith('Binary files'):
self.state = PATCH self.state = PATCH
self.binary = True self.binary = True
if self.is_newfile:
self.new_bin.append(self.filename)
else: else:
ok = False ok = False
self.is_newfile = self.newfile_prefix_re.match(line)
for pfx in self.pre_patch_prefixes: for pfx in self.pre_patch_prefixes:
if line.startswith(pfx): if line.startswith(pfx):
ok = True ok = True
@ -320,22 +330,20 @@ class GitDiffCheck:
'new mode ', 'new mode ',
'similarity index ', 'similarity index ',
'rename ', 'rename ',
'Binary files ',
) )
line_endings = ('\r\n', '\n\r', '\n', '\r') line_endings = ('\r\n', '\n\r', '\n', '\r')
def set_filename(self, filename): newfile_prefix_re = \
self.hunk_filename = filename re.compile(r'''^
if filename: index\ 0+\.\.
self.force_crlf = not filename.endswith('.sh') ''',
else: re.VERBOSE)
self.force_crlf = True
def added_line_error(self, msg, line): def added_line_error(self, msg, line):
lines = [ msg ] lines = [ msg ]
if self.hunk_filename is not None: if self.filename is not None:
lines.append('File: ' + self.hunk_filename) lines.append('File: ' + self.filename)
lines.append('Line: ' + line) lines.append('Line: ' + line)
self.error(*lines) self.error(*lines)