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>
This commit is contained in:
Gavin Mak
2024-06-21 19:14:16 +00:00
committed by LUCI CQ
parent 6cc020f612
commit 47a841a204
2 changed files with 13 additions and 1 deletions

View File

@@ -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')