Handle empty diff in _diffs_to_change_files

An valid empty diff can be passed into _diffs_to_change_files if the
modified file's contents is same as upstream. In this case,
_diffs_to_change_files gets an IndexError.

Bug: b/336555565
Change-Id: I848e6016a1e8089473ff8a72d2e0142fbabfda9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5635166
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
changes/66/5635166/3
Gavin Mak 1 year ago committed by LUCI CQ
parent 6cc020f612
commit 47a841a204

@ -2191,10 +2191,18 @@ def _diffs_to_change_files(diffs):
A list of change file tuples from the diffs.
Raises:
PresubmitFailure: If a diff is empty or otherwise invalid.
PresubmitFailure: If a diff is invalid.
"""
change_files = []
for file, file_diff in diffs.items():
if not file_diff:
# If a file is modified such that its contents are the same as the
# upstream commit, it may not have a diff. For example, if you added
# a newline to a file in PS1, then deleted it in PS2, the diff will
# be empty. Add this to change_files as modified anyway.
change_files.append(('M', file))
continue
header_line = file_diff.splitlines()[1]
if not header_line:
raise PresubmitFailure('diff header is empty')

@ -266,6 +266,10 @@ diff --git a/ffoo b/foo
with self.assertRaises(presubmit_support.PresubmitFailure):
presubmit_support._parse_unified_diff(diff)
def test_diffs_to_change_files_with_empty_diff(self):
res = presubmit_support._diffs_to_change_files({'file': ''})
self.assertEqual(res, [('M', 'file')])
if __name__ == "__main__":
unittest.main()

Loading…
Cancel
Save