From 495e14b7b6fb55cfb481d9e4d4fa4ea10bd28603 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Fri, 26 Jul 2024 17:10:26 +0000 Subject: [PATCH] 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 Commit-Queue: Gavin Mak --- presubmit_support.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index dabcc5d300..ec44bdd886 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -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(