Add flush_cache flag to input_api.change.NewContents

NewContents always reads from cache, but it is sometimes useful flush
cache and use NewContents as a way to read the updatedcontents of a
file.

For example, some presubmit checks run formatters and check if any files
are modified. Many of these use `git diff` but that isn't available in
a non-git workspace, so checks will have to rely on reading contents
directly.

Bug: b/333744051
Change-Id: I3b3f4c88cc130607020ad599bbc15616d559725c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5740609
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
changes/09/5740609/4
Gavin Mak 1 year ago committed by LUCI CQ
parent 8dde9800ee
commit 495e14b7b6

@ -1076,16 +1076,18 @@ class AffectedFile(object):
return self._diff_cache.GetOldContents(self.LocalPath(),
self._local_root).splitlines()
def NewContents(self):
def NewContents(self, flush_cache=False):
"""Returns an iterator over the lines in the new version of file.
The new version is the file in the user's workspace, i.e. the 'right hand
side'.
If flush_cache is True, read from disk and replace any cached contents.
Contents will be empty if the file is a directory or does not exist.
Note: The carriage returns (LF or CR) are stripped off.
"""
if self._cached_new_contents is None:
if self._cached_new_contents is None or flush_cache:
self._cached_new_contents = []
try:
self._cached_new_contents = gclient_utils.FileRead(

Loading…
Cancel
Save