Support infra/specs/PRESUBMIT.py on cog
cog workspaces don't have a git directory and can't run "git diff". Replace it with python's difflib instead. Bug: b/333744051 Change-Id: I5bd8ccd873a0db55f0bbadf165180b3f2aa42903 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/357900 Reviewed-by: Jeremy Leconte <jleconte@google.com> Commit-Queue: Jeremy Leconte <jleconte@webrtc.org> Reviewed-by: Jeremy Leconte <jleconte@webrtc.org> Cr-Commit-Position: refs/heads/main@{#42674}
This commit is contained in:
parent
e9d066d3b7
commit
d7d21337d1
@ -8,6 +8,7 @@
|
|||||||
# in the file PATENTS. All contributing project authors may
|
# in the file PATENTS. All contributing project authors may
|
||||||
# be found in the AUTHORS file in the root of the source tree.
|
# be found in the AUTHORS file in the root of the source tree.
|
||||||
|
|
||||||
|
import difflib
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
@ -15,26 +16,37 @@ import shlex
|
|||||||
USE_PYTHON3 = True
|
USE_PYTHON3 = True
|
||||||
|
|
||||||
|
|
||||||
def _HasLocalChanges(input_api):
|
|
||||||
ret = input_api.subprocess.call(['git', 'diff', '--quiet'])
|
|
||||||
return ret != 0
|
|
||||||
|
|
||||||
|
|
||||||
def CheckPatchFormatted(input_api, output_api):
|
def CheckPatchFormatted(input_api, output_api):
|
||||||
results = []
|
results = []
|
||||||
file_filter = lambda x: x.LocalPath().endswith('.pyl')
|
file_filter = lambda x: x.LocalPath().endswith('.pyl')
|
||||||
affected_files = input_api.AffectedFiles(include_deletes=False,
|
affected_files = input_api.AffectedFiles(
|
||||||
file_filter=file_filter)
|
include_deletes=False, file_filter=file_filter
|
||||||
|
)
|
||||||
|
|
||||||
|
diffs = []
|
||||||
for f in affected_files:
|
for f in affected_files:
|
||||||
|
# NewContents just reads the file.
|
||||||
|
prev_content = f.NewContents()
|
||||||
|
|
||||||
cmd = ['yapf', '-i', f.AbsoluteLocalPath()]
|
cmd = ['yapf', '-i', f.AbsoluteLocalPath()]
|
||||||
if input_api.subprocess.call(cmd):
|
if input_api.subprocess.call(cmd):
|
||||||
results.append(
|
results.append(
|
||||||
output_api.PresubmitError('Error calling "' + shlex.join(cmd) + '"'))
|
output_api.PresubmitError('Error calling "' + shlex.join(cmd) + '"')
|
||||||
|
)
|
||||||
|
|
||||||
if _HasLocalChanges(input_api):
|
new_content = f.NewContents()
|
||||||
msg = ('Diff found after running "yapf -i" on modified .pyl files.\n'
|
if new_content != prev_content:
|
||||||
'Please commit or discard the new changes.')
|
path = f.LocalPath()
|
||||||
|
diff = difflib.unified_diff(prev_content, new_content, path, path)
|
||||||
|
diffs.append(''.join(diff))
|
||||||
|
|
||||||
|
if diffs:
|
||||||
|
combined_diffs = '\n'.join(diffs)
|
||||||
|
msg = (
|
||||||
|
'Diff found after running "yapf -i" on modified .pyl files:\n'
|
||||||
|
f'{combined_diffs}\n'
|
||||||
|
'Please commit or discard the new changes.'
|
||||||
|
)
|
||||||
results.append(output_api.PresubmitError(msg))
|
results.append(output_api.PresubmitError(msg))
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user